-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
198 lines (166 loc) · 6.81 KB
/
main.cpp
File metadata and controls
198 lines (166 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <filesystem>
#include <iostream>
#include <thread>
#include <boost/asio.hpp>
#include "Logger.h"
#include "yaml-cpp/yaml.h"
#include "MeshNode.h"
std::shared_ptr<boost::asio::ssl::context> make_ssl_context(
const std::string &cert_file,
const std::string &key_file,
const std::string &ca_file
) {
try {
OPENSSL_init_ssl(OPENSSL_INIT_NO_LOAD_CONFIG, NULL);
std::shared_ptr<boost::asio::ssl::context> ssl_ctx;
// initialize context for TLS v1.2 or v1.3
ssl_ctx = std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tls_server);
ssl_ctx->set_options(
boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::no_sslv2 |
boost::asio::ssl::context::single_dh_use
);
// Load certificates from the file paths
ssl_ctx->use_certificate_chain_file(cert_file);
ssl_ctx->use_private_key_file(key_file, boost::asio::ssl::context::pem);
// If you need to verify peers (mutual TLS), load the CA
if (!ca_file.empty()) {
ssl_ctx->load_verify_file(ca_file);
ssl_ctx->set_verify_mode(boost::asio::ssl::verify_peer | boost::asio::ssl::verify_fail_if_no_peer_cert);
}
Log::debug("tls", {}, "TLS Context initialized successfully");
return ssl_ctx;
} catch (const std::exception &e) {
throw std::runtime_error("TLS Setup Failed: " + std::string(e.what()));
}
}
int main(int argc, char **argv) {
if (argc < 2) {
std::cerr << "Usage: meshnode config_path.yml\n";
return 1;
}
YAML::Node config = YAML::LoadFile(argv[1]);
int tcp_port = config["tcp-port"].as<int>();
int udp_port = config["udp-port"].as<int>();
std::string peer_id = config["peer-id"].as<std::string>();
std::filesystem::path output_dir;
if (config["output-dir"]) {
output_dir = config["output-dir"].as<std::string>();
} else {
output_dir = std::filesystem::current_path() / "out";
}
try {
if (std::filesystem::create_directories(output_dir)) {
std::cout << "Successfully created directory path: " << output_dir << std::endl;
} else {
std::cout << "Directory path already exists or a non-error condition occurred." << std::endl;
}
} catch (const std::filesystem::filesystem_error &e) {
std::cerr << "Error creating directory path: " << e.what() << std::endl;
}
bool is_debug_mode = false;
if (config["debug"]) {
is_debug_mode = config["debug"].as<bool>();
}
Log::init(peer_id, output_dir / "node_log.txt", is_debug_mode);
Log::info("main", {}, "Welcome to MeshNetworking!");
bool use_tls = false;
std::string cert, key, ca;
bool encrypt_messages = false;
if (config["tls"] && config["tls"]["cert-path"] && config["tls"]["key-path"] && config["tls"]["ca-path"]) {
use_tls = true;
cert = config["tls"]["cert-path"].as<std::string>();
key = config["tls"]["key-path"].as<std::string>();
ca = config["tls"]["ca-path"].as<std::string>();
// encryption relies on the three pems above to be present
if (config["tls"] && config["tls"]["encrypt-messages"]) {
encrypt_messages = true;
}
}
std::shared_ptr<boost::asio::ssl::context> ssl_ctx = nullptr;
std::shared_ptr<IdentityManager> identity_manager = std::make_shared<IdentityManager>();
if (use_tls) {
try {
ssl_ctx = make_ssl_context(cert, key, ca);
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
return 1;
}
}
if (encrypt_messages) {
identity_manager->load_identities(cert, key, ca);
}
boost::asio::io_context ioc;
MeshNode node(ioc, tcp_port, udp_port, peer_id, ssl_ctx, identity_manager, encrypt_messages);
node.set_output_directory(output_dir);
node.start();
if (config["auto_connect"]) {
const YAML::Node &auto_connect = config["auto_connect"];
for (const auto &elem: auto_connect) {
if (!elem["ip"] || !elem["port"]) {
Log::warn("yaml_parse", {}, "malformed auto_conect config, should have an ip and port present");
continue;
}
std::string ip = elem["ip"].as<std::string>();
int tcp_port = elem["port"].as<int>();
Log::debug("yaml_parser", {{"ip", ip}, {"port", tcp_port}}, "detected auto connect configuration");
node.add_auto_connection(ip, tcp_port);
}
}
std::thread t([&ioc]() { ioc.run(); });
// TODO: Add automatic connections to config yaml
std::string line;
while (std::getline(std::cin, line)) {
if (line.rfind("connect ", 0) == 0) {
// Command: connect <host> <port>
std::istringstream iss(line.substr(8));
std::string host;
int port;
std::string new_peer_id;
if (iss >> host >> port >> new_peer_id) {
node.connect(host, port);
} else {
std::cout << "Usage: connect <host> <port>\n";
}
} else if (line.rfind("dm ", 0) == 0) {
auto pos = line.find(' ', 3);
if (pos != std::string::npos) {
std::string target = line.substr(3, pos - 3);
std::string msg = line.substr(pos + 1);
node.send_text(target, msg);
}
} else if (line.rfind("broadcast ", 0) == 0) {
// node.broadcast_text(line.substr(10));
} else if (line.starts_with("topology")) {
std::string arg = line.substr(std::string("topology").size());
std::istringstream iss(arg);
std::string dest;
iss >> dest;
if (dest.empty()) {
std::cout << "Usage: topology <output_path>\n";
}
node.generate_topology_graph(dest);
} else if (line.starts_with("ping")) {
std::string arg = line.substr(std::string("ping").size());
std::istringstream iss(arg);
std::string peer;
iss >> peer;
node.ping(peer);
} else if (line == "get_nodes") {
std::vector<std::string> nodes = node.get_nodes_in_network();
std::cout << nodes.size() << " node(s)" << std::endl;
for (size_t i = 0; i < nodes.size(); ++i) {
std::cout << nodes[i] << std::endl;
}
} else if (line == "block") {
node.set_block_all_messages(true);
} else if (line == "unblock") {
node.set_block_all_messages(false);
} else if (line == "quit" || line == "exit") break;
else std::cout << "Commands: broadcast <text>, dm <peer> <text>, exit\n";
}
node.stop();
ioc.stop();
t.join();
return 0;
}