Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 1 | /* |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 2 | * Copyright (C) 2023 Savoir-faire Linux Inc. |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | #include "dnc.h" |
| 18 | #include "certstore.h" |
| 19 | #include "connectionmanager.h" |
| 20 | #include "fileutils.h" |
| 21 | #include "../common.h" |
| 22 | |
| 23 | #include <opendht/log.h> |
| 24 | #include <opendht/crypto.h> |
| 25 | #include <asio.hpp> |
| 26 | |
| 27 | #include <fcntl.h> |
| 28 | #include <unistd.h> |
| 29 | |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 30 | #include <chrono> |
| 31 | #include <string> |
| 32 | #include <string_view> |
| 33 | #include <filesystem> |
| 34 | #include <memory> |
| 35 | |
| 36 | namespace dhtnet { |
| 37 | std::pair<std::string, std::string> |
| 38 | Dnc::parseName(const std::string_view name) |
| 39 | { |
| 40 | // Find the position of the first ':' character after "nc//" |
| 41 | size_t ip_add_start = name.find("nc//") + 6; // Adding 5 to skip "nc//" |
| 42 | size_t colonPos = name.find(':', ip_add_start); |
| 43 | |
| 44 | if (colonPos == std::string::npos) { |
| 45 | // Return an empty pair if ':' is not found |
| 46 | return std::make_pair("", ""); |
| 47 | } |
| 48 | |
| 49 | std::string ip_add(name.substr(ip_add_start, colonPos - ip_add_start)); |
| 50 | std::string port(name.substr(colonPos + 1)); |
| 51 | |
| 52 | return std::make_pair(ip_add, port); |
| 53 | } |
| 54 | |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 55 | // Build a server |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 56 | Dnc::Dnc(dht::crypto::Identity identity, |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 57 | const std::string& bootstrap, |
| 58 | const std::string& turn_host, |
| 59 | const std::string& turn_user, |
| 60 | const std::string& turn_pass, |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 61 | const std::string& turn_realm, |
Amna | 6999623 | 2024-07-23 14:04:44 -0400 | [diff] [blame] | 62 | const bool anonymous, |
Amna | 2ee14f0 | 2024-07-24 15:15:55 -0400 | [diff] [blame] | 63 | const bool verbose, |
Amna | 45db776 | 2024-07-24 18:33:48 -0400 | [diff] [blame] | 64 | const std::map<std::string, std::vector<int>> authorized_services, |
| 65 | const bool enable_upnp) |
Amna | 6999623 | 2024-07-23 14:04:44 -0400 | [diff] [blame] | 66 | :logger(verbose ? dht::log::getStdLogger() : nullptr), |
| 67 | ioContext(std::make_shared<asio::io_context>()), |
Amna | 0e5f076 | 2024-05-06 15:40:14 -0400 | [diff] [blame] | 68 | iceFactory(std::make_shared<IceTransportFactory>(logger)) |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 69 | { |
Amna | 5170f76 | 2024-08-16 11:49:56 -0400 | [diff] [blame] | 70 | |
Amna | 0e5f076 | 2024-05-06 15:40:14 -0400 | [diff] [blame] | 71 | certStore = std::make_shared<tls::CertificateStore>(cachePath()/"certStore", logger); |
| 72 | trustStore = std::make_shared<tls::TrustStore>(*certStore); |
| 73 | |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 74 | auto ca = identity.second->issuer; |
| 75 | trustStore->setCertificateStatus(ca->getId().toString(), tls::TrustStore::PermissionStatus::ALLOWED); |
| 76 | |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 77 | auto config = connectionManagerConfig(identity, |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 78 | bootstrap, |
| 79 | logger, |
| 80 | certStore, |
| 81 | ioContext, |
| 82 | iceFactory, |
| 83 | turn_host, |
| 84 | turn_user, |
| 85 | turn_pass, |
Amna | 45db776 | 2024-07-24 18:33:48 -0400 | [diff] [blame] | 86 | turn_realm, |
| 87 | enable_upnp); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 88 | // create a connection manager |
Adrien Béraud | c1cac45 | 2023-08-22 20:32:36 -0400 | [diff] [blame] | 89 | connectionManager = std::make_unique<ConnectionManager>(std::move(config)); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 90 | |
| 91 | connectionManager->onDhtConnected(identity.first->getPublicKey()); |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 92 | connectionManager->onICERequest([this, identity, anonymous](const DeviceId& deviceId) { |
| 93 | auto cert = certStore->getCertificate(deviceId.toString()); |
| 94 | return trustStore->isAllowed(*cert, anonymous); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 95 | }); |
| 96 | |
| 97 | std::mutex mtx; |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 98 | std::unique_lock lk {mtx}; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 99 | |
| 100 | connectionManager->onChannelRequest( |
Amna | 2ee14f0 | 2024-07-24 15:15:55 -0400 | [diff] [blame] | 101 | [authorized_services, this](const std::shared_ptr<dht::crypto::Certificate>&, const std::string& name) { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 102 | // handle channel request |
Amna | 2ee14f0 | 2024-07-24 15:15:55 -0400 | [diff] [blame] | 103 | if (authorized_services.empty()) { |
| 104 | // Accept all connections if no authorized services are provided |
| 105 | return true; |
| 106 | } |
| 107 | // parse channel name to get the ip address and port: nc://<ip>:<port> |
| 108 | auto parsedName = parseName(name); |
| 109 | const std::string &ip = parsedName.first; |
| 110 | int port = 0; |
| 111 | try { |
| 112 | port = std::stoi(parsedName.second); |
| 113 | } |
| 114 | catch (std::exception const &err) { |
| 115 | fmt::print(stderr, "Rejecting connection: port '{}' is not a valid number", parsedName.second); |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | // Check if the IP is authorized |
| 120 | auto it = authorized_services.find(ip); |
| 121 | if (it == authorized_services.end()) { |
| 122 | // Reject the connection if the ip is not authorized |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame] | 123 | Log("Rejecting connection to {}:{}", ip, port); |
Amna | 2ee14f0 | 2024-07-24 15:15:55 -0400 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | |
| 127 | // Check if the port is authorized |
| 128 | const auto &ports = it->second; |
| 129 | if (std::find(ports.begin(), ports.end(), port) == ports.end()) { |
| 130 | // Reject the connection if the port is not authorized |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame] | 131 | Log("Rejecting connection to {}:{}", ip, port); |
Amna | 2ee14f0 | 2024-07-24 15:15:55 -0400 | [diff] [blame] | 132 | return false; |
| 133 | } |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame] | 134 | Log("Accepting connection to {}:{}", ip, port); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 135 | return true; |
| 136 | }); |
| 137 | |
| 138 | connectionManager->onConnectionReady([&](const DeviceId&, |
| 139 | const std::string& name, |
| 140 | std::shared_ptr<ChannelSocket> mtlxSocket) { |
| 141 | if (name.empty()) { |
| 142 | // Handle the empty input case here |
| 143 | return; |
| 144 | } |
| 145 | try { |
| 146 | auto parsedName = parseName(name); |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame] | 147 | Log("Connecting to {}:{}", parsedName.first, parsedName.second); |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 148 | |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 149 | asio::ip::tcp::resolver resolver(*ioContext); |
| 150 | asio::ip::tcp::resolver::results_type endpoints = resolver.resolve(parsedName.first, |
| 151 | parsedName.second); |
| 152 | |
| 153 | // Create a TCP socket |
| 154 | auto socket = std::make_shared<asio::ip::tcp::socket>(*ioContext); |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 155 | socket->open(asio::ip::tcp::v4()); |
| 156 | socket->set_option(asio::socket_base::keep_alive(true)); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 157 | asio::async_connect( |
| 158 | *socket, |
| 159 | endpoints, |
| 160 | [this, socket, mtlxSocket](const std::error_code& error, |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 161 | const asio::ip::tcp::endpoint& ep) { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 162 | if (!error) { |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame] | 163 | Log("Connected!\n"); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 164 | mtlxSocket->setOnRecv([socket, this](const uint8_t* data, size_t size) { |
| 165 | auto data_copy = std::make_shared<std::vector<uint8_t>>(data, |
| 166 | data + size); |
| 167 | asio::async_write(*socket, |
| 168 | asio::buffer(*data_copy), |
| 169 | [data_copy, this](const std::error_code& error, |
| 170 | std::size_t bytesWritten) { |
| 171 | if (error) { |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame] | 172 | Log("Write error: {}\n", error.message()); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 173 | } |
Amna | 6999623 | 2024-07-23 14:04:44 -0400 | [diff] [blame] | 174 | |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 175 | }); |
| 176 | return size; |
| 177 | }); |
| 178 | // Create a buffer to read data into |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 179 | auto buffer = std::make_shared<std::vector<uint8_t>>(BUFFER_SIZE); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 180 | readFromPipe(mtlxSocket, socket, buffer); |
| 181 | } else { |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame] | 182 | Log("Connection error: {}\n", error.message()); |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 183 | mtlxSocket->shutdown(); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 184 | } |
| 185 | }); |
| 186 | |
| 187 | } catch (std::exception& e) { |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame] | 188 | Log("Exception: {}\n", e.what()); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 189 | } |
| 190 | }); |
| 191 | } |
| 192 | // Build a client |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 193 | Dnc::Dnc(dht::crypto::Identity identity, |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 194 | const std::string& bootstrap, |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 195 | dht::InfoHash peer_id, |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 196 | const std::string& remote_host, |
| 197 | int remote_port, |
| 198 | const std::string& turn_host, |
| 199 | const std::string& turn_user, |
| 200 | const std::string& turn_pass, |
Amna | 6999623 | 2024-07-23 14:04:44 -0400 | [diff] [blame] | 201 | const std::string& turn_realm, |
Amna | 45db776 | 2024-07-24 18:33:48 -0400 | [diff] [blame] | 202 | const bool verbose, |
| 203 | const bool enable_upnp) |
| 204 | : Dnc(identity, bootstrap,turn_host,turn_user,turn_pass, turn_realm, true, verbose, {}, enable_upnp) |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 205 | { |
| 206 | std::condition_variable cv; |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 207 | auto name = fmt::format("nc://{:s}:{:d}", remote_host, remote_port); |
Amna | c5ce269 | 2024-08-05 12:18:40 -0400 | [diff] [blame] | 208 | Log("Requesting socket: {}\n", name.c_str()); |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 209 | connectionManager->connectDevice( |
| 210 | peer_id, name, [&](std::shared_ptr<ChannelSocket> socket, const dht::InfoHash&) { |
| 211 | if (socket) { |
| 212 | socket->setOnRecv([this, socket](const uint8_t* data, size_t size) { |
| 213 | std::cout.write((const char*) data, size); |
| 214 | std::cout.flush(); |
| 215 | return size; |
| 216 | }); |
| 217 | // Create a buffer to read data into |
| 218 | auto buffer = std::make_shared<std::vector<uint8_t>>(BUFFER_SIZE); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 219 | |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 220 | // Create a shared_ptr to the stream_descriptor |
| 221 | auto stdinPipe = std::make_shared<asio::posix::stream_descriptor>(*ioContext, |
| 222 | ::dup( |
| 223 | STDIN_FILENO)); |
| 224 | readFromPipe(socket, stdinPipe, buffer); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 225 | |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 226 | socket->onShutdown([this]() { |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame] | 227 | Log("Exit program\n"); |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 228 | ioContext->stop(); |
| 229 | }); |
| 230 | } |
| 231 | }); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 232 | |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 233 | connectionManager->onConnectionReady( |
| 234 | [&](const DeviceId&, const std::string& name, std::shared_ptr<ChannelSocket> mtlxSocket) { |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame] | 235 | Log("Connected!\n"); |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 236 | }); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void |
| 240 | Dnc::run() |
| 241 | { |
Amna | 14d54f2 | 2024-08-29 16:17:38 -0400 | [diff] [blame] | 242 | auto work = asio::make_work_guard(*ioContext); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 243 | ioContext->run(); |
| 244 | } |
| 245 | |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 246 | Dnc::~Dnc() |
| 247 | { |
| 248 | ioContext->stop(); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 249 | } |
| 250 | } // namespace dhtnet |