blob: aeb21ed83fec1d87d70f2387f2babed6bc4df796 [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>
Amnac75ffe92024-02-08 17:23:29 -050033connectionManagerConfig(dht::crypto::Identity identity,
Amna2f3539b2023-09-18 13:59:22 -040034 const std::string& bootstrap,
35 std::shared_ptr<Logger> logger,
36 std::shared_ptr<tls::CertificateStore> certStore,
37 std::shared_ptr<asio::io_context> ioContext,
38 std::shared_ptr<IceTransportFactory> iceFactory,
39 const std::string& turn_host,
40 const std::string& turn_user,
41 const std::string& turn_pass,
42 const std::string& turn_realm)
Amna38768302023-08-21 11:51:56 -040043{
Amnac75ffe92024-02-08 17:23:29 -050044 std::filesystem::create_directories(PATH/"certstore");
Amna38768302023-08-21 11:51:56 -040045 // DHT node creation: To make a connection manager at first a DHT node should be created
Amna38768302023-08-21 11:51:56 -040046 dht::DhtRunner::Config dhtConfig;
47 dhtConfig.dht_config.id = identity;
48 dhtConfig.threaded = true;
49 dhtConfig.peer_discovery = false;
50 dhtConfig.peer_publish = false;
51 dht::DhtRunner::Context dhtContext;
52 dhtContext.identityAnnouncedCb = [logger](bool ok) {
53 if (logger)
54 logger->debug("Identity announced {}\n", ok);
55 };
Amna7c973d52023-09-25 14:12:29 -040056 dhtContext.certificateStore = [certStore](const dht::InfoHash& pk_id) {
Amna38768302023-08-21 11:51:56 -040057 std::vector<std::shared_ptr<dht::crypto::Certificate>> ret;
Amna2f3539b2023-09-18 13:59:22 -040058 if (auto cert = certStore->getCertificate(pk_id.toString()))
Amna38768302023-08-21 11:51:56 -040059 ret.emplace_back(std::move(cert));
60 return ret;
61 };
62 auto runner = std::make_shared<dht::DhtRunner>();
63 runner->run(dhtConfig, std::move(dhtContext));
Adrien Béraudecde63f2023-08-26 18:11:21 -040064 runner->bootstrap(bootstrap);
Amna38768302023-08-21 11:51:56 -040065
66 // DHT node creation end:
67 // ConnectionManager creation:
68 auto config = std::make_unique<ConnectionManager::Config>();
69 config->dht = runner;
70 config->id = identity;
71 config->ioContext = ioContext;
Amna2f3539b2023-09-18 13:59:22 -040072 config->certStore = certStore;
Amnac75ffe92024-02-08 17:23:29 -050073 config->cachePath = PATH;
Amna2f3539b2023-09-18 13:59:22 -040074 config->factory = iceFactory;
Adrien Béraudecde63f2023-08-26 18:11:21 -040075 config->logger = logger;
Amna41848a22024-01-22 16:22:57 -050076 if (!turn_host.empty()){
Amna2f3539b2023-09-18 13:59:22 -040077 config->turnEnabled = true;
Amna41848a22024-01-22 16:22:57 -050078 config->turnServer = turn_host;
79 config->turnServerUserName = turn_user;
80 config->turnServerPwd = turn_pass;
81 config->turnServerRealm = turn_realm;
82 }
Amna38768302023-08-21 11:51:56 -040083 return std::move(config);
84}
Amna2f3539b2023-09-18 13:59:22 -040085template<typename T>
86void
87readFromPipe(std::shared_ptr<ChannelSocket> socket, T input, Buffer buffer)
Amna38768302023-08-21 11:51:56 -040088{
89 asio::async_read(*input,
90 asio::buffer(*buffer),
91 asio::transfer_at_least(1),
92 [socket, input, buffer](const asio::error_code& error, size_t bytesRead) {
93 if (!error) {
94 // Process the data received in the buffer
95 std::error_code ec;
Amna2f3539b2023-09-18 13:59:22 -040096 // Write the data to the socket
Amna38768302023-08-21 11:51:56 -040097 socket->write(buffer->data(), bytesRead, ec);
98 if (!ec) {
99 // Continue reading more data
100 readFromPipe(socket, input, buffer);
101 } else {
Amna2f3539b2023-09-18 13:59:22 -0400102 fmt::print(stderr, "Error writing to socket: {}\n", ec.message());
Amna38768302023-08-21 11:51:56 -0400103 }
Amna2f3539b2023-09-18 13:59:22 -0400104 } else if (error == asio::error::eof) {
105 // Connection closed cleanly by peer.
106 socket->shutdown();
107 }else{
Amna38768302023-08-21 11:51:56 -0400108 fmt::print(stderr, "Error reading from stdin: {}\n", error.message());
Amna38768302023-08-21 11:51:56 -0400109 }
110 });
111}
112
Amna2f3539b2023-09-18 13:59:22 -0400113template void readFromPipe(std::shared_ptr<ChannelSocket> socket,
114 std::shared_ptr<asio::posix::stream_descriptor> input,
115 Buffer buffer);
116template void readFromPipe(std::shared_ptr<ChannelSocket> socket,
117 std::shared_ptr<asio::ip::tcp::socket> input,
118 Buffer buffer);
Amna38768302023-08-21 11:51:56 -0400119
120} // namespace dhtnet