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