blob: af6207ce1aece00acfab17e7a0542306a930f287 [file] [log] [blame]
Amna38768302023-08-21 11:51:56 -04001/*
2 * Copyright (C) 2004-2023 Savoir-faire Linux Inc.
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"
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 <iostream>
25#include <string>
26#include <filesystem>
27#include <unistd.h>
28#include <fcntl.h>
29#include <asio.hpp>
30
31namespace dhtnet {
32
Amna38768302023-08-21 11:51:56 -040033dht::crypto::Identity
Adrien Béraudecde63f2023-08-26 18:11:21 -040034loadIdentity(const std::filesystem::path& path)
Amna38768302023-08-21 11:51:56 -040035{
Adrien Béraudecde63f2023-08-26 18:11:21 -040036 if (!std::filesystem::exists(path)) {
37 std::filesystem::create_directory(path);
Amna38768302023-08-21 11:51:56 -040038 }
Adrien Béraudecde63f2023-08-26 18:11:21 -040039 try {
40 for (const auto& path: std::filesystem::directory_iterator(path)) {
41 auto p = path.path();
42 if (p.extension() == ".pem") {
43 auto privateKey = std::make_unique<dht::crypto::PrivateKey>(
44 fileutils::loadFile(p));
45 auto certificate = std::make_unique<dht::crypto::Certificate>(
46 fileutils::loadFile(p.replace_extension(".crt")));
47 return dht::crypto::Identity(std::move(privateKey), std::move(certificate));
48 }
49 }
50 } catch (const std::exception& e) {
51 fmt::print(stderr, "Error loadind key from .dhtnetTools: {}\n", e.what());
52 }
53
Amna38768302023-08-21 11:51:56 -040054 auto ca = dht::crypto::generateIdentity("ca");
55 auto id = dht::crypto::generateIdentity("dhtnc", ca);
Adrien Béraudecde63f2023-08-26 18:11:21 -040056 dht::crypto::saveIdentity(id, path / "id");
Amna38768302023-08-21 11:51:56 -040057 return id;
58}
59
60std::unique_ptr<ConnectionManager::Config>
Adrien Béraudecde63f2023-08-26 18:11:21 -040061connectionManagerConfig(const std::filesystem::path& path,
62 dht::crypto::Identity identity,
63 const std::string& bootstrap,
Amna38768302023-08-21 11:51:56 -040064 std::shared_ptr<Logger> logger,
65 tls::CertificateStore& certStore,
66 std::shared_ptr<asio::io_context> ioContext,
67 IceTransportFactory& iceFactory)
68{
Adrien Béraudecde63f2023-08-26 18:11:21 -040069 std::filesystem::create_directories(path / "certstore");
Amna38768302023-08-21 11:51:56 -040070
71 // DHT node creation: To make a connection manager at first a DHT node should be created
Amna38768302023-08-21 11:51:56 -040072 dht::DhtRunner::Config dhtConfig;
73 dhtConfig.dht_config.id = identity;
74 dhtConfig.threaded = true;
75 dhtConfig.peer_discovery = false;
76 dhtConfig.peer_publish = false;
77 dht::DhtRunner::Context dhtContext;
78 dhtContext.identityAnnouncedCb = [logger](bool ok) {
79 if (logger)
80 logger->debug("Identity announced {}\n", ok);
81 };
82 dhtContext.certificateStore = [&](const dht::InfoHash& pk_id) {
83 std::vector<std::shared_ptr<dht::crypto::Certificate>> ret;
84 if (auto cert = certStore.getCertificate(pk_id.toString()))
85 ret.emplace_back(std::move(cert));
86 return ret;
87 };
88 auto runner = std::make_shared<dht::DhtRunner>();
89 runner->run(dhtConfig, std::move(dhtContext));
Adrien Béraudecde63f2023-08-26 18:11:21 -040090 runner->bootstrap(bootstrap);
Amna38768302023-08-21 11:51:56 -040091
92 // DHT node creation end:
93 // ConnectionManager creation:
94 auto config = std::make_unique<ConnectionManager::Config>();
95 config->dht = runner;
96 config->id = identity;
97 config->ioContext = ioContext;
98 config->certStore = &certStore;
99 config->factory = &iceFactory;
Adrien Béraudecde63f2023-08-26 18:11:21 -0400100 config->cachePath = path;
101 config->logger = logger;
Amna38768302023-08-21 11:51:56 -0400102
103 return std::move(config);
104}
105template <typename T>
106void readFromPipe(std::shared_ptr<ChannelSocket> socket, T input, Buffer buffer)
107{
108 asio::async_read(*input,
109 asio::buffer(*buffer),
110 asio::transfer_at_least(1),
111 [socket, input, buffer](const asio::error_code& error, size_t bytesRead) {
112 if (!error) {
113 // Process the data received in the buffer
114 std::error_code ec;
115 // print the data to stdout
116 socket->write(buffer->data(), bytesRead, ec);
117 if (!ec) {
118 // Continue reading more data
119 readFromPipe(socket, input, buffer);
120 } else {
121 fmt::print(stderr, "Error writing to socket: {}\n", ec.message());
122 // logger->error("Error writing to socket: {}", ec.message());
123 }
124 } else if(error != asio::error::eof) {
125 fmt::print(stderr, "Error reading from stdin: {}\n", error.message());
126 // logger->error("Error reading from stdin: {}", error.message());
127 }
128 });
129}
130
131template void readFromPipe(std::shared_ptr<ChannelSocket> socket, std::shared_ptr<asio::posix::stream_descriptor> input, Buffer buffer);
132template void readFromPipe(std::shared_ptr<ChannelSocket> socket, std::shared_ptr<asio::ip::tcp::socket> input, Buffer buffer);
133
134
135
136
137} // namespace dhtnet