blob: 76597a824d0655043b761eac81c9dd66ccb42130 [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 -040032std::unique_ptr<ConnectionManager::Config>
Adrien Béraudecde63f2023-08-26 18:11:21 -040033connectionManagerConfig(const std::filesystem::path& path,
Amna2f3539b2023-09-18 13:59:22 -040034 dht::crypto::Identity identity,
35 const std::string& bootstrap,
36 std::shared_ptr<Logger> logger,
37 std::shared_ptr<tls::CertificateStore> certStore,
38 std::shared_ptr<asio::io_context> ioContext,
39 std::shared_ptr<IceTransportFactory> iceFactory,
40 const std::string& turn_host,
41 const std::string& turn_user,
42 const std::string& turn_pass,
43 const std::string& turn_realm)
Amna38768302023-08-21 11:51:56 -040044{
Adrien Béraudecde63f2023-08-26 18:11:21 -040045 std::filesystem::create_directories(path / "certstore");
Amna38768302023-08-21 11:51:56 -040046
47 // DHT node creation: To make a connection manager at first a DHT node should be created
Amna38768302023-08-21 11:51:56 -040048 dht::DhtRunner::Config dhtConfig;
49 dhtConfig.dht_config.id = identity;
50 dhtConfig.threaded = true;
51 dhtConfig.peer_discovery = false;
52 dhtConfig.peer_publish = false;
53 dht::DhtRunner::Context dhtContext;
54 dhtContext.identityAnnouncedCb = [logger](bool ok) {
55 if (logger)
56 logger->debug("Identity announced {}\n", ok);
57 };
Amna7c973d52023-09-25 14:12:29 -040058 dhtContext.certificateStore = [certStore](const dht::InfoHash& pk_id) {
Amna38768302023-08-21 11:51:56 -040059 std::vector<std::shared_ptr<dht::crypto::Certificate>> ret;
Amna2f3539b2023-09-18 13:59:22 -040060 if (auto cert = certStore->getCertificate(pk_id.toString()))
Amna38768302023-08-21 11:51:56 -040061 ret.emplace_back(std::move(cert));
62 return ret;
63 };
64 auto runner = std::make_shared<dht::DhtRunner>();
65 runner->run(dhtConfig, std::move(dhtContext));
Adrien Béraudecde63f2023-08-26 18:11:21 -040066 runner->bootstrap(bootstrap);
Amna38768302023-08-21 11:51:56 -040067
68 // DHT node creation end:
69 // ConnectionManager creation:
70 auto config = std::make_unique<ConnectionManager::Config>();
71 config->dht = runner;
72 config->id = identity;
73 config->ioContext = ioContext;
Amna2f3539b2023-09-18 13:59:22 -040074 config->certStore = certStore;
75 config->factory = iceFactory;
Adrien Béraudecde63f2023-08-26 18:11:21 -040076 config->cachePath = path;
77 config->logger = logger;
Amna41848a22024-01-22 16:22:57 -050078 if (!turn_host.empty()){
Amna2f3539b2023-09-18 13:59:22 -040079 config->turnEnabled = true;
Amna41848a22024-01-22 16:22:57 -050080 config->turnServer = turn_host;
81 config->turnServerUserName = turn_user;
82 config->turnServerPwd = turn_pass;
83 config->turnServerRealm = turn_realm;
84 }
Amna38768302023-08-21 11:51:56 -040085 return std::move(config);
86}
Amna2f3539b2023-09-18 13:59:22 -040087template<typename T>
88void
89readFromPipe(std::shared_ptr<ChannelSocket> socket, T input, Buffer buffer)
Amna38768302023-08-21 11:51:56 -040090{
91 asio::async_read(*input,
92 asio::buffer(*buffer),
93 asio::transfer_at_least(1),
94 [socket, input, buffer](const asio::error_code& error, size_t bytesRead) {
95 if (!error) {
96 // Process the data received in the buffer
97 std::error_code ec;
Amna2f3539b2023-09-18 13:59:22 -040098 // Write the data to the socket
Amna38768302023-08-21 11:51:56 -040099 socket->write(buffer->data(), bytesRead, ec);
100 if (!ec) {
101 // Continue reading more data
102 readFromPipe(socket, input, buffer);
103 } else {
Amna2f3539b2023-09-18 13:59:22 -0400104 fmt::print(stderr, "Error writing to socket: {}\n", ec.message());
Amna38768302023-08-21 11:51:56 -0400105 }
Amna2f3539b2023-09-18 13:59:22 -0400106 } else if (error == asio::error::eof) {
107 // Connection closed cleanly by peer.
108 socket->shutdown();
109 }else{
Amna38768302023-08-21 11:51:56 -0400110 fmt::print(stderr, "Error reading from stdin: {}\n", error.message());
Amna38768302023-08-21 11:51:56 -0400111 }
112 });
113}
114
Amna2f3539b2023-09-18 13:59:22 -0400115template void readFromPipe(std::shared_ptr<ChannelSocket> socket,
116 std::shared_ptr<asio::posix::stream_descriptor> input,
117 Buffer buffer);
118template void readFromPipe(std::shared_ptr<ChannelSocket> socket,
119 std::shared_ptr<asio::ip::tcp::socket> input,
120 Buffer buffer);
Amna38768302023-08-21 11:51:56 -0400121
122} // namespace dhtnet