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, |
| 64 | const std::map<std::string, std::vector<int>> authorized_services) |
Amna | 6999623 | 2024-07-23 14:04:44 -0400 | [diff] [blame] | 65 | :logger(verbose ? dht::log::getStdLogger() : nullptr), |
| 66 | ioContext(std::make_shared<asio::io_context>()), |
Amna | 0e5f076 | 2024-05-06 15:40:14 -0400 | [diff] [blame] | 67 | iceFactory(std::make_shared<IceTransportFactory>(logger)) |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 68 | { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 69 | ioContextRunner = std::thread([context = ioContext, logger = logger] { |
| 70 | try { |
| 71 | auto work = asio::make_work_guard(*context); |
| 72 | context->run(); |
| 73 | } catch (const std::exception& ex) { |
| 74 | if (logger) |
| 75 | logger->error("Error in ioContextRunner: {}", ex.what()); |
| 76 | } |
| 77 | }); |
| 78 | |
Amna | 0e5f076 | 2024-05-06 15:40:14 -0400 | [diff] [blame] | 79 | certStore = std::make_shared<tls::CertificateStore>(cachePath()/"certStore", logger); |
| 80 | trustStore = std::make_shared<tls::TrustStore>(*certStore); |
| 81 | |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 82 | auto ca = identity.second->issuer; |
| 83 | trustStore->setCertificateStatus(ca->getId().toString(), tls::TrustStore::PermissionStatus::ALLOWED); |
| 84 | |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 85 | auto config = connectionManagerConfig(identity, |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 86 | bootstrap, |
| 87 | logger, |
| 88 | certStore, |
| 89 | ioContext, |
| 90 | iceFactory, |
| 91 | turn_host, |
| 92 | turn_user, |
| 93 | turn_pass, |
| 94 | turn_realm); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 95 | // create a connection manager |
Adrien Béraud | c1cac45 | 2023-08-22 20:32:36 -0400 | [diff] [blame] | 96 | connectionManager = std::make_unique<ConnectionManager>(std::move(config)); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 97 | |
| 98 | connectionManager->onDhtConnected(identity.first->getPublicKey()); |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 99 | connectionManager->onICERequest([this, identity, anonymous](const DeviceId& deviceId) { |
| 100 | auto cert = certStore->getCertificate(deviceId.toString()); |
| 101 | return trustStore->isAllowed(*cert, anonymous); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 102 | }); |
| 103 | |
| 104 | std::mutex mtx; |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 105 | std::unique_lock lk {mtx}; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 106 | |
| 107 | connectionManager->onChannelRequest( |
Amna | 2ee14f0 | 2024-07-24 15:15:55 -0400 | [diff] [blame] | 108 | [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] | 109 | // handle channel request |
Amna | 2ee14f0 | 2024-07-24 15:15:55 -0400 | [diff] [blame] | 110 | if (authorized_services.empty()) { |
| 111 | // Accept all connections if no authorized services are provided |
| 112 | return true; |
| 113 | } |
| 114 | // parse channel name to get the ip address and port: nc://<ip>:<port> |
| 115 | auto parsedName = parseName(name); |
| 116 | const std::string &ip = parsedName.first; |
| 117 | int port = 0; |
| 118 | try { |
| 119 | port = std::stoi(parsedName.second); |
| 120 | } |
| 121 | catch (std::exception const &err) { |
| 122 | fmt::print(stderr, "Rejecting connection: port '{}' is not a valid number", parsedName.second); |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | // Check if the IP is authorized |
| 127 | auto it = authorized_services.find(ip); |
| 128 | if (it == authorized_services.end()) { |
| 129 | // Reject the connection if the ip is not authorized |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame^] | 130 | Log("Rejecting connection to {}:{}", ip, port); |
Amna | 2ee14f0 | 2024-07-24 15:15:55 -0400 | [diff] [blame] | 131 | return false; |
| 132 | } |
| 133 | |
| 134 | // Check if the port is authorized |
| 135 | const auto &ports = it->second; |
| 136 | if (std::find(ports.begin(), ports.end(), port) == ports.end()) { |
| 137 | // Reject the connection if the port is not authorized |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame^] | 138 | Log("Rejecting connection to {}:{}", ip, port); |
Amna | 2ee14f0 | 2024-07-24 15:15:55 -0400 | [diff] [blame] | 139 | return false; |
| 140 | } |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame^] | 141 | Log("Accepting connection to {}:{}", ip, port); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 142 | return true; |
| 143 | }); |
| 144 | |
| 145 | connectionManager->onConnectionReady([&](const DeviceId&, |
| 146 | const std::string& name, |
| 147 | std::shared_ptr<ChannelSocket> mtlxSocket) { |
| 148 | if (name.empty()) { |
| 149 | // Handle the empty input case here |
| 150 | return; |
| 151 | } |
| 152 | try { |
| 153 | auto parsedName = parseName(name); |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame^] | 154 | Log("Connecting to {}:{}", parsedName.first, parsedName.second); |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 155 | |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 156 | asio::ip::tcp::resolver resolver(*ioContext); |
| 157 | asio::ip::tcp::resolver::results_type endpoints = resolver.resolve(parsedName.first, |
| 158 | parsedName.second); |
| 159 | |
| 160 | // Create a TCP socket |
| 161 | auto socket = std::make_shared<asio::ip::tcp::socket>(*ioContext); |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 162 | socket->open(asio::ip::tcp::v4()); |
| 163 | socket->set_option(asio::socket_base::keep_alive(true)); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 164 | asio::async_connect( |
| 165 | *socket, |
| 166 | endpoints, |
| 167 | [this, socket, mtlxSocket](const std::error_code& error, |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 168 | const asio::ip::tcp::endpoint& ep) { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 169 | if (!error) { |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame^] | 170 | Log("Connected!\n"); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 171 | mtlxSocket->setOnRecv([socket, this](const uint8_t* data, size_t size) { |
| 172 | auto data_copy = std::make_shared<std::vector<uint8_t>>(data, |
| 173 | data + size); |
| 174 | asio::async_write(*socket, |
| 175 | asio::buffer(*data_copy), |
| 176 | [data_copy, this](const std::error_code& error, |
| 177 | std::size_t bytesWritten) { |
| 178 | if (error) { |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame^] | 179 | Log("Write error: {}\n", error.message()); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 180 | } |
Amna | 6999623 | 2024-07-23 14:04:44 -0400 | [diff] [blame] | 181 | |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 182 | }); |
| 183 | return size; |
| 184 | }); |
| 185 | // Create a buffer to read data into |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 186 | auto buffer = std::make_shared<std::vector<uint8_t>>(BUFFER_SIZE); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 187 | readFromPipe(mtlxSocket, socket, buffer); |
| 188 | } else { |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame^] | 189 | Log("Connection error: {}\n", error.message()); |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 190 | mtlxSocket->shutdown(); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 191 | } |
| 192 | }); |
| 193 | |
| 194 | } catch (std::exception& e) { |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame^] | 195 | Log("Exception: {}\n", e.what()); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 196 | } |
| 197 | }); |
| 198 | } |
| 199 | // Build a client |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 200 | Dnc::Dnc(dht::crypto::Identity identity, |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 201 | const std::string& bootstrap, |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 202 | dht::InfoHash peer_id, |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 203 | const std::string& remote_host, |
| 204 | int remote_port, |
| 205 | const std::string& turn_host, |
| 206 | const std::string& turn_user, |
| 207 | const std::string& turn_pass, |
Amna | 6999623 | 2024-07-23 14:04:44 -0400 | [diff] [blame] | 208 | const std::string& turn_realm, |
| 209 | const bool verbose) |
Amna | 2ee14f0 | 2024-07-24 15:15:55 -0400 | [diff] [blame] | 210 | : Dnc(identity, bootstrap,turn_host,turn_user,turn_pass, turn_realm, true, verbose, {}) |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 211 | { |
| 212 | std::condition_variable cv; |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 213 | auto name = fmt::format("nc://{:s}:{:d}", remote_host, remote_port); |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame^] | 214 | Log("Requesting socket: %s\n", name.c_str()); |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 215 | connectionManager->connectDevice( |
| 216 | peer_id, name, [&](std::shared_ptr<ChannelSocket> socket, const dht::InfoHash&) { |
| 217 | if (socket) { |
| 218 | socket->setOnRecv([this, socket](const uint8_t* data, size_t size) { |
| 219 | std::cout.write((const char*) data, size); |
| 220 | std::cout.flush(); |
| 221 | return size; |
| 222 | }); |
| 223 | // Create a buffer to read data into |
| 224 | auto buffer = std::make_shared<std::vector<uint8_t>>(BUFFER_SIZE); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 225 | |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 226 | // Create a shared_ptr to the stream_descriptor |
| 227 | auto stdinPipe = std::make_shared<asio::posix::stream_descriptor>(*ioContext, |
| 228 | ::dup( |
| 229 | STDIN_FILENO)); |
| 230 | readFromPipe(socket, stdinPipe, buffer); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 231 | |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 232 | socket->onShutdown([this]() { |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame^] | 233 | Log("Exit program\n"); |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 234 | ioContext->stop(); |
| 235 | }); |
| 236 | } |
| 237 | }); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 238 | |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 239 | connectionManager->onConnectionReady( |
| 240 | [&](const DeviceId&, const std::string& name, std::shared_ptr<ChannelSocket> mtlxSocket) { |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame^] | 241 | Log("Connected!\n"); |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 242 | }); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | void |
| 246 | Dnc::run() |
| 247 | { |
| 248 | ioContext->run(); |
| 249 | } |
| 250 | |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 251 | Dnc::~Dnc() |
| 252 | { |
| 253 | ioContext->stop(); |
| 254 | ioContextRunner.join(); |
| 255 | } |
| 256 | } // namespace dhtnet |