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 "certstore.h" |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 18 | #include "connectionmanager.h" |
| 19 | #include "common.h" |
| 20 | #include "fileutils.h" |
| 21 | #include "ice_transport.h" |
| 22 | |
Adrien Béraud | c1cac45 | 2023-08-22 20:32:36 -0400 | [diff] [blame] | 23 | #include <opendht/crypto.h> |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <filesystem> |
| 26 | #include <unistd.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <asio.hpp> |
| 29 | |
| 30 | namespace dhtnet { |
| 31 | |
Amna | 0e5f076 | 2024-05-06 15:40:14 -0400 | [diff] [blame] | 32 | std::filesystem::path cachePath() |
| 33 | { |
| 34 | auto* cache_path = getenv("DHTNET_CACHE_DIR"); |
| 35 | if (cache_path) { |
| 36 | return std::filesystem::path(cache_path); |
| 37 | } |
| 38 | auto* home = getenv("HOME"); |
| 39 | if (home) { |
| 40 | return std::filesystem::path(home) / ".cache" / "dhtnet"; |
| 41 | } |
| 42 | // If user got no HOME and no DHTNET_CACHE_DIR set, use /tmp |
| 43 | return std::filesystem::path("/tmp"); |
| 44 | } |
| 45 | |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 46 | std::unique_ptr<ConnectionManager::Config> |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 47 | connectionManagerConfig(dht::crypto::Identity identity, |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 48 | const std::string& bootstrap, |
| 49 | std::shared_ptr<Logger> logger, |
| 50 | std::shared_ptr<tls::CertificateStore> certStore, |
| 51 | std::shared_ptr<asio::io_context> ioContext, |
| 52 | std::shared_ptr<IceTransportFactory> iceFactory, |
| 53 | const std::string& turn_host, |
| 54 | const std::string& turn_user, |
| 55 | const std::string& turn_pass, |
Amna | 45db776 | 2024-07-24 18:33:48 -0400 | [diff] [blame] | 56 | const std::string& turn_realm, |
| 57 | const bool enable_upnp) |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 58 | { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 59 | // DHT node creation: To make a connection manager at first a DHT node should be created |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 60 | dht::DhtRunner::Config dhtConfig; |
| 61 | dhtConfig.dht_config.id = identity; |
| 62 | dhtConfig.threaded = true; |
| 63 | dhtConfig.peer_discovery = false; |
| 64 | dhtConfig.peer_publish = false; |
| 65 | dht::DhtRunner::Context dhtContext; |
| 66 | dhtContext.identityAnnouncedCb = [logger](bool ok) { |
| 67 | if (logger) |
| 68 | logger->debug("Identity announced {}\n", ok); |
| 69 | }; |
Amna | 7c973d5 | 2023-09-25 14:12:29 -0400 | [diff] [blame] | 70 | dhtContext.certificateStore = [certStore](const dht::InfoHash& pk_id) { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 71 | std::vector<std::shared_ptr<dht::crypto::Certificate>> ret; |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 72 | if (auto cert = certStore->getCertificate(pk_id.toString())) |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 73 | ret.emplace_back(std::move(cert)); |
| 74 | return ret; |
| 75 | }; |
| 76 | auto runner = std::make_shared<dht::DhtRunner>(); |
| 77 | runner->run(dhtConfig, std::move(dhtContext)); |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 78 | runner->bootstrap(bootstrap); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 79 | |
| 80 | // DHT node creation end: |
| 81 | // ConnectionManager creation: |
| 82 | auto config = std::make_unique<ConnectionManager::Config>(); |
| 83 | config->dht = runner; |
| 84 | config->id = identity; |
| 85 | config->ioContext = ioContext; |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 86 | config->certStore = certStore; |
Amna | 0e5f076 | 2024-05-06 15:40:14 -0400 | [diff] [blame] | 87 | config->cachePath = cachePath(); |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 88 | config->factory = iceFactory; |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 89 | config->logger = logger; |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 90 | if (!turn_host.empty()){ |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 91 | config->turnEnabled = true; |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 92 | config->turnServer = turn_host; |
| 93 | config->turnServerUserName = turn_user; |
| 94 | config->turnServerPwd = turn_pass; |
| 95 | config->turnServerRealm = turn_realm; |
| 96 | } |
Amna | 45db776 | 2024-07-24 18:33:48 -0400 | [diff] [blame] | 97 | |
| 98 | if (enable_upnp) { |
| 99 | // UPnP configuration |
| 100 | auto upnpContext = std::make_shared<dhtnet::upnp::UPnPContext>(ioContext, logger); |
| 101 | auto controller = std::make_shared<dhtnet::upnp::Controller>(upnpContext); |
| 102 | config->upnpEnabled = true; |
| 103 | config->upnpCtrl = controller; |
| 104 | } |
| 105 | |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 106 | return std::move(config); |
| 107 | } |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 108 | template<typename T> |
| 109 | void |
| 110 | readFromPipe(std::shared_ptr<ChannelSocket> socket, T input, Buffer buffer) |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 111 | { |
| 112 | asio::async_read(*input, |
| 113 | asio::buffer(*buffer), |
| 114 | asio::transfer_at_least(1), |
| 115 | [socket, input, buffer](const asio::error_code& error, size_t bytesRead) { |
| 116 | if (!error) { |
| 117 | // Process the data received in the buffer |
| 118 | std::error_code ec; |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 119 | // Write the data to the socket |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 120 | socket->write(buffer->data(), bytesRead, ec); |
| 121 | if (!ec) { |
| 122 | // Continue reading more data |
| 123 | readFromPipe(socket, input, buffer); |
| 124 | } else { |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 125 | fmt::print(stderr, "Error writing to socket: {}\n", ec.message()); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 126 | } |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 127 | } else if (error == asio::error::eof) { |
| 128 | // Connection closed cleanly by peer. |
| 129 | socket->shutdown(); |
| 130 | }else{ |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 131 | fmt::print(stderr, "Error reading from stdin: {}\n", error.message()); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 132 | } |
| 133 | }); |
| 134 | } |
| 135 | |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 136 | template void readFromPipe(std::shared_ptr<ChannelSocket> socket, |
| 137 | std::shared_ptr<asio::posix::stream_descriptor> input, |
| 138 | Buffer buffer); |
| 139 | template void readFromPipe(std::shared_ptr<ChannelSocket> socket, |
| 140 | std::shared_ptr<asio::ip::tcp::socket> input, |
| 141 | Buffer buffer); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 142 | |
| 143 | } // namespace dhtnet |