blob: b6550abff7fc0618fce070a8af35ef05f1b61cf8 [file] [log] [blame]
Amna38768302023-08-21 11:51:56 -04001/*
Amna2f3539b2023-09-18 13:59:22 -04002 * Copyright (C) 2023 Savoir-faire Linux Inc.
Amna38768302023-08-21 11:51:56 -04003 *
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"
Amna38768302023-08-21 11:51:56 -040018#include "connectionmanager.h"
19#include "common.h"
20#include "fileutils.h"
21#include "ice_transport.h"
22
Adrien Béraudc1cac452023-08-22 20:32:36 -040023#include <opendht/crypto.h>
Amna38768302023-08-21 11:51:56 -040024#include <string>
25#include <filesystem>
26#include <unistd.h>
27#include <fcntl.h>
28#include <asio.hpp>
29
30namespace dhtnet {
31
Amna38768302023-08-21 11:51:56 -040032dht::crypto::Identity
Adrien Béraudecde63f2023-08-26 18:11:21 -040033loadIdentity(const std::filesystem::path& path)
Amna38768302023-08-21 11:51:56 -040034{
Adrien Béraudecde63f2023-08-26 18:11:21 -040035 if (!std::filesystem::exists(path)) {
36 std::filesystem::create_directory(path);
Amna38768302023-08-21 11:51:56 -040037 }
Adrien Béraudecde63f2023-08-26 18:11:21 -040038 try {
Amna2f3539b2023-09-18 13:59:22 -040039 for (const auto& path : std::filesystem::directory_iterator(path)) {
Adrien Béraudecde63f2023-08-26 18:11:21 -040040 auto p = path.path();
41 if (p.extension() == ".pem") {
Amna2f3539b2023-09-18 13:59:22 -040042 auto privateKey = std::make_unique<dht::crypto::PrivateKey>(fileutils::loadFile(p));
Adrien Béraudecde63f2023-08-26 18:11:21 -040043 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
Amna38768302023-08-21 11:51:56 -040052 auto ca = dht::crypto::generateIdentity("ca");
53 auto id = dht::crypto::generateIdentity("dhtnc", ca);
Amna2f3539b2023-09-18 13:59:22 -040054 fmt::print("Generated new identity: {}\n", id.first->getPublicKey().getId());
Adrien Béraudecde63f2023-08-26 18:11:21 -040055 dht::crypto::saveIdentity(id, path / "id");
Amna38768302023-08-21 11:51:56 -040056 return id;
57}
58
59std::unique_ptr<ConnectionManager::Config>
Adrien Béraudecde63f2023-08-26 18:11:21 -040060connectionManagerConfig(const std::filesystem::path& path,
Amna2f3539b2023-09-18 13:59:22 -040061 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)
Amna38768302023-08-21 11:51:56 -040071{
Adrien Béraudecde63f2023-08-26 18:11:21 -040072 std::filesystem::create_directories(path / "certstore");
Amna38768302023-08-21 11:51:56 -040073
74 // DHT node creation: To make a connection manager at first a DHT node should be created
Amna38768302023-08-21 11:51:56 -040075 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;
Amna2f3539b2023-09-18 13:59:22 -040087 if (auto cert = certStore->getCertificate(pk_id.toString()))
Amna38768302023-08-21 11:51:56 -040088 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éraudecde63f2023-08-26 18:11:21 -040093 runner->bootstrap(bootstrap);
Amna38768302023-08-21 11:51:56 -040094
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;
Amna2f3539b2023-09-18 13:59:22 -0400101 config->certStore = certStore;
102 config->factory = iceFactory;
Adrien Béraudecde63f2023-08-26 18:11:21 -0400103 config->cachePath = path;
104 config->logger = logger;
Amna2f3539b2023-09-18 13:59:22 -0400105 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
Amna38768302023-08-21 11:51:56 -0400112
113 return std::move(config);
114}
Amna2f3539b2023-09-18 13:59:22 -0400115template<typename T>
116void
117readFromPipe(std::shared_ptr<ChannelSocket> socket, T input, Buffer buffer)
Amna38768302023-08-21 11:51:56 -0400118{
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;
Amna2f3539b2023-09-18 13:59:22 -0400126 // Write the data to the socket
Amna38768302023-08-21 11:51:56 -0400127 socket->write(buffer->data(), bytesRead, ec);
128 if (!ec) {
129 // Continue reading more data
130 readFromPipe(socket, input, buffer);
131 } else {
Amna2f3539b2023-09-18 13:59:22 -0400132 fmt::print(stderr, "Error writing to socket: {}\n", ec.message());
Amna38768302023-08-21 11:51:56 -0400133 }
Amna2f3539b2023-09-18 13:59:22 -0400134 } else if (error == asio::error::eof) {
135 // Connection closed cleanly by peer.
136 socket->shutdown();
137 }else{
Amna38768302023-08-21 11:51:56 -0400138 fmt::print(stderr, "Error reading from stdin: {}\n", error.message());
Amna38768302023-08-21 11:51:56 -0400139 }
140 });
141}
142
Amna2f3539b2023-09-18 13:59:22 -0400143template void readFromPipe(std::shared_ptr<ChannelSocket> socket,
144 std::shared_ptr<asio::posix::stream_descriptor> input,
145 Buffer buffer);
146template void readFromPipe(std::shared_ptr<ChannelSocket> socket,
147 std::shared_ptr<asio::ip::tcp::socket> input,
148 Buffer buffer);
Amna38768302023-08-21 11:51:56 -0400149
150} // namespace dhtnet