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 | dht::crypto::Identity |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 33 | loadIdentity(const std::filesystem::path& path) |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 34 | { |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 35 | if (!std::filesystem::exists(path)) { |
| 36 | std::filesystem::create_directory(path); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 37 | } |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 38 | try { |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 39 | for (const auto& path : std::filesystem::directory_iterator(path)) { |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 40 | auto p = path.path(); |
| 41 | if (p.extension() == ".pem") { |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 42 | auto privateKey = std::make_unique<dht::crypto::PrivateKey>(fileutils::loadFile(p)); |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 43 | auto certificate = std::make_unique<dht::crypto::Certificate>( |
| 44 | fileutils::loadFile(p.replace_extension(".crt"))); |
| 45 | return dht::crypto::Identity(std::move(privateKey), std::move(certificate)); |
| 46 | } |
| 47 | } |
| 48 | } catch (const std::exception& e) { |
| 49 | fmt::print(stderr, "Error loadind key from .dhtnetTools: {}\n", e.what()); |
| 50 | } |
| 51 | |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 52 | auto ca = dht::crypto::generateIdentity("ca"); |
| 53 | auto id = dht::crypto::generateIdentity("dhtnc", ca); |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 54 | fmt::print("Generated new identity: {}\n", id.first->getPublicKey().getId()); |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 55 | dht::crypto::saveIdentity(id, path / "id"); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 56 | return id; |
| 57 | } |
| 58 | |
| 59 | std::unique_ptr<ConnectionManager::Config> |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 60 | connectionManagerConfig(const std::filesystem::path& path, |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 61 | dht::crypto::Identity identity, |
| 62 | const std::string& bootstrap, |
| 63 | std::shared_ptr<Logger> logger, |
| 64 | std::shared_ptr<tls::CertificateStore> certStore, |
| 65 | std::shared_ptr<asio::io_context> ioContext, |
| 66 | std::shared_ptr<IceTransportFactory> iceFactory, |
| 67 | const std::string& turn_host, |
| 68 | const std::string& turn_user, |
| 69 | const std::string& turn_pass, |
| 70 | const std::string& turn_realm) |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 71 | { |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 72 | std::filesystem::create_directories(path / "certstore"); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 73 | |
| 74 | // 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] | 75 | dht::DhtRunner::Config dhtConfig; |
| 76 | dhtConfig.dht_config.id = identity; |
| 77 | dhtConfig.threaded = true; |
| 78 | dhtConfig.peer_discovery = false; |
| 79 | dhtConfig.peer_publish = false; |
| 80 | dht::DhtRunner::Context dhtContext; |
| 81 | dhtContext.identityAnnouncedCb = [logger](bool ok) { |
| 82 | if (logger) |
| 83 | logger->debug("Identity announced {}\n", ok); |
| 84 | }; |
| 85 | dhtContext.certificateStore = [&](const dht::InfoHash& pk_id) { |
| 86 | std::vector<std::shared_ptr<dht::crypto::Certificate>> ret; |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 87 | if (auto cert = certStore->getCertificate(pk_id.toString())) |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 88 | ret.emplace_back(std::move(cert)); |
| 89 | return ret; |
| 90 | }; |
| 91 | auto runner = std::make_shared<dht::DhtRunner>(); |
| 92 | runner->run(dhtConfig, std::move(dhtContext)); |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 93 | runner->bootstrap(bootstrap); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 94 | |
| 95 | // DHT node creation end: |
| 96 | // ConnectionManager creation: |
| 97 | auto config = std::make_unique<ConnectionManager::Config>(); |
| 98 | config->dht = runner; |
| 99 | config->id = identity; |
| 100 | config->ioContext = ioContext; |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 101 | config->certStore = certStore; |
| 102 | config->factory = iceFactory; |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 103 | config->cachePath = path; |
| 104 | config->logger = logger; |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 105 | if (!turn_host.empty()) |
| 106 | config->turnEnabled = true; |
| 107 | config->turnServer = turn_host; |
| 108 | config->turnServerUserName = turn_user; |
| 109 | config->turnServerPwd = turn_pass; |
| 110 | config->turnServerRealm = turn_realm; |
| 111 | |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 112 | |
| 113 | return std::move(config); |
| 114 | } |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 115 | template<typename T> |
| 116 | void |
| 117 | readFromPipe(std::shared_ptr<ChannelSocket> socket, T input, Buffer buffer) |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 118 | { |
| 119 | asio::async_read(*input, |
| 120 | asio::buffer(*buffer), |
| 121 | asio::transfer_at_least(1), |
| 122 | [socket, input, buffer](const asio::error_code& error, size_t bytesRead) { |
| 123 | if (!error) { |
| 124 | // Process the data received in the buffer |
| 125 | std::error_code ec; |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 126 | // Write the data to the socket |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 127 | socket->write(buffer->data(), bytesRead, ec); |
| 128 | if (!ec) { |
| 129 | // Continue reading more data |
| 130 | readFromPipe(socket, input, buffer); |
| 131 | } else { |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 132 | fmt::print(stderr, "Error writing to socket: {}\n", ec.message()); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 133 | } |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 134 | } else if (error == asio::error::eof) { |
| 135 | // Connection closed cleanly by peer. |
| 136 | socket->shutdown(); |
| 137 | }else{ |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 138 | fmt::print(stderr, "Error reading from stdin: {}\n", error.message()); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 139 | } |
| 140 | }); |
| 141 | } |
| 142 | |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 143 | template void readFromPipe(std::shared_ptr<ChannelSocket> socket, |
| 144 | std::shared_ptr<asio::posix::stream_descriptor> input, |
| 145 | Buffer buffer); |
| 146 | template void readFromPipe(std::shared_ptr<ChannelSocket> socket, |
| 147 | std::shared_ptr<asio::ip::tcp::socket> input, |
| 148 | Buffer buffer); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 149 | |
| 150 | } // namespace dhtnet |