blob: 35f971cc9302154f3a09e874a32973ae41a0f614 [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
34loadIdentity(bool isServer)
35{
36 std::string idDir = std::string(getenv("HOME")) + "/.dhtnetTools";
37 if (isServer){
38
39 if (!std::filesystem::exists(idDir)) {
40 std::filesystem::create_directory(idDir);
41 }
42
43 try {
44 std::filesystem::directory_iterator endIter;
45 for (std::filesystem::directory_iterator iter(idDir); iter != endIter; ++iter) {
46 if (iter->path().extension() == ".pem") {
47 auto privateKey = std::make_unique<dht::crypto::PrivateKey>(
48 fileutils::loadFile(std::filesystem::path(iter->path())));
49 // Generate certificate
50 auto certificate = std::make_unique<dht::crypto::Certificate>(
51 dht::crypto::Certificate::generate(*privateKey, "dhtnc"));
52 // return
53 return dht::crypto::Identity(std::move(privateKey), std::move(certificate));
54 }
55 }
56 } catch (const std::exception& e) {
57 fmt::print(stderr, "Error loadind key from .dhtnetTools: {}\n", e.what());
58 }
59 }
60 auto ca = dht::crypto::generateIdentity("ca");
61 auto id = dht::crypto::generateIdentity("dhtnc", ca);
62 idDir += "/id";
63 if (isServer)
64 dht::crypto::saveIdentity(id, idDir);
65 return id;
66}
67
68std::unique_ptr<ConnectionManager::Config>
69connectionManagerConfig(dht::crypto::Identity identity,
70 const std::string& bootstrap_ip_add,
71 const std::string& bootstrap_port,
72 std::shared_ptr<Logger> logger,
73 tls::CertificateStore& certStore,
74 std::shared_ptr<asio::io_context> ioContext,
75 IceTransportFactory& iceFactory)
76{
77 std::filesystem::create_directories(std::string(getenv("HOME")) + "/.dhtnetTools/certstore");
78
79 // DHT node creation: To make a connection manager at first a DHT node should be created
80
81 dht::DhtRunner::Config dhtConfig;
82 dhtConfig.dht_config.id = identity;
83 dhtConfig.threaded = true;
84 dhtConfig.peer_discovery = false;
85 dhtConfig.peer_publish = false;
86 dht::DhtRunner::Context dhtContext;
87 dhtContext.identityAnnouncedCb = [logger](bool ok) {
88 if (logger)
89 logger->debug("Identity announced {}\n", ok);
90 };
91 dhtContext.certificateStore = [&](const dht::InfoHash& pk_id) {
92 std::vector<std::shared_ptr<dht::crypto::Certificate>> ret;
93 if (auto cert = certStore.getCertificate(pk_id.toString()))
94 ret.emplace_back(std::move(cert));
95 return ret;
96 };
97 auto runner = std::make_shared<dht::DhtRunner>();
98 runner->run(dhtConfig, std::move(dhtContext));
99 runner->bootstrap(bootstrap_ip_add, bootstrap_port);
100
101 // DHT node creation end:
102 // ConnectionManager creation:
103 auto config = std::make_unique<ConnectionManager::Config>();
104 config->dht = runner;
105 config->id = identity;
106 config->ioContext = ioContext;
107 config->certStore = &certStore;
108 config->factory = &iceFactory;
109 config->cachePath = std::string(getenv("HOME")) + "/.dhtnetTools";
110
111 return std::move(config);
112}
113template <typename T>
114void readFromPipe(std::shared_ptr<ChannelSocket> socket, T input, Buffer buffer)
115{
116 asio::async_read(*input,
117 asio::buffer(*buffer),
118 asio::transfer_at_least(1),
119 [socket, input, buffer](const asio::error_code& error, size_t bytesRead) {
120 if (!error) {
121 // Process the data received in the buffer
122 std::error_code ec;
123 // print the data to stdout
124 socket->write(buffer->data(), bytesRead, ec);
125 if (!ec) {
126 // Continue reading more data
127 readFromPipe(socket, input, buffer);
128 } else {
129 fmt::print(stderr, "Error writing to socket: {}\n", ec.message());
130 // logger->error("Error writing to socket: {}", ec.message());
131 }
132 } else if(error != asio::error::eof) {
133 fmt::print(stderr, "Error reading from stdin: {}\n", error.message());
134 // logger->error("Error reading from stdin: {}", error.message());
135 }
136 });
137}
138
139template void readFromPipe(std::shared_ptr<ChannelSocket> socket, std::shared_ptr<asio::posix::stream_descriptor> input, Buffer buffer);
140template void readFromPipe(std::shared_ptr<ChannelSocket> socket, std::shared_ptr<asio::ip::tcp::socket> input, Buffer buffer);
141
142
143
144
145} // namespace dhtnet