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 | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 32 | std::unique_ptr<ConnectionManager::Config> |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 33 | connectionManagerConfig(dht::crypto::Identity identity, |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 34 | const std::string& bootstrap, |
| 35 | std::shared_ptr<Logger> logger, |
| 36 | std::shared_ptr<tls::CertificateStore> certStore, |
| 37 | std::shared_ptr<asio::io_context> ioContext, |
| 38 | std::shared_ptr<IceTransportFactory> iceFactory, |
| 39 | const std::string& turn_host, |
| 40 | const std::string& turn_user, |
| 41 | const std::string& turn_pass, |
| 42 | const std::string& turn_realm) |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 43 | { |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 44 | std::filesystem::create_directories(PATH/"certstore"); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 45 | // 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] | 46 | dht::DhtRunner::Config dhtConfig; |
| 47 | dhtConfig.dht_config.id = identity; |
| 48 | dhtConfig.threaded = true; |
| 49 | dhtConfig.peer_discovery = false; |
| 50 | dhtConfig.peer_publish = false; |
| 51 | dht::DhtRunner::Context dhtContext; |
| 52 | dhtContext.identityAnnouncedCb = [logger](bool ok) { |
| 53 | if (logger) |
| 54 | logger->debug("Identity announced {}\n", ok); |
| 55 | }; |
Amna | 7c973d5 | 2023-09-25 14:12:29 -0400 | [diff] [blame] | 56 | dhtContext.certificateStore = [certStore](const dht::InfoHash& pk_id) { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 57 | std::vector<std::shared_ptr<dht::crypto::Certificate>> ret; |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 58 | if (auto cert = certStore->getCertificate(pk_id.toString())) |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 59 | ret.emplace_back(std::move(cert)); |
| 60 | return ret; |
| 61 | }; |
| 62 | auto runner = std::make_shared<dht::DhtRunner>(); |
| 63 | runner->run(dhtConfig, std::move(dhtContext)); |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 64 | runner->bootstrap(bootstrap); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 65 | |
| 66 | // DHT node creation end: |
| 67 | // ConnectionManager creation: |
| 68 | auto config = std::make_unique<ConnectionManager::Config>(); |
| 69 | config->dht = runner; |
| 70 | config->id = identity; |
| 71 | config->ioContext = ioContext; |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 72 | config->certStore = certStore; |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 73 | config->cachePath = PATH; |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 74 | config->factory = iceFactory; |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 75 | config->logger = logger; |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 76 | if (!turn_host.empty()){ |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 77 | config->turnEnabled = true; |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 78 | config->turnServer = turn_host; |
| 79 | config->turnServerUserName = turn_user; |
| 80 | config->turnServerPwd = turn_pass; |
| 81 | config->turnServerRealm = turn_realm; |
| 82 | } |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 83 | return std::move(config); |
| 84 | } |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 85 | template<typename T> |
| 86 | void |
| 87 | readFromPipe(std::shared_ptr<ChannelSocket> socket, T input, Buffer buffer) |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 88 | { |
| 89 | asio::async_read(*input, |
| 90 | asio::buffer(*buffer), |
| 91 | asio::transfer_at_least(1), |
| 92 | [socket, input, buffer](const asio::error_code& error, size_t bytesRead) { |
| 93 | if (!error) { |
| 94 | // Process the data received in the buffer |
| 95 | std::error_code ec; |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 96 | // Write the data to the socket |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 97 | socket->write(buffer->data(), bytesRead, ec); |
| 98 | if (!ec) { |
| 99 | // Continue reading more data |
| 100 | readFromPipe(socket, input, buffer); |
| 101 | } else { |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 102 | fmt::print(stderr, "Error writing to socket: {}\n", ec.message()); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 103 | } |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 104 | } else if (error == asio::error::eof) { |
| 105 | // Connection closed cleanly by peer. |
| 106 | socket->shutdown(); |
| 107 | }else{ |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 108 | fmt::print(stderr, "Error reading from stdin: {}\n", error.message()); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 109 | } |
| 110 | }); |
| 111 | } |
| 112 | |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 113 | template void readFromPipe(std::shared_ptr<ChannelSocket> socket, |
| 114 | std::shared_ptr<asio::posix::stream_descriptor> input, |
| 115 | Buffer buffer); |
| 116 | template void readFromPipe(std::shared_ptr<ChannelSocket> socket, |
| 117 | std::shared_ptr<asio::ip::tcp::socket> input, |
| 118 | Buffer buffer); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 119 | |
| 120 | } // namespace dhtnet |