Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 1 | /* |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 2 | * Copyright (C) 2023 Savoir-faire Linux Inc. |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 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 "dnc.h" |
| 18 | #include "common.h" |
Amna | 423b25b | 2024-02-06 13:21:19 -0500 | [diff] [blame] | 19 | #include "dhtnet_crtmgr/dhtnet_crtmgr.h" |
Adrien Béraud | c1cac45 | 2023-08-22 20:32:36 -0400 | [diff] [blame] | 20 | |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 21 | #include <string> |
| 22 | #include <vector> |
| 23 | #include <iostream> |
| 24 | #include <unistd.h> |
| 25 | #include <getopt.h> |
Amna | b31c374 | 2023-08-28 13:58:31 -0400 | [diff] [blame] | 26 | #if __has_include(<fmt/std.h>) |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 27 | #include <fmt/std.h> |
Amna | b31c374 | 2023-08-28 13:58:31 -0400 | [diff] [blame] | 28 | #else |
| 29 | #include <fmt/ostream.h> |
| 30 | #endif |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 31 | #include <netinet/in.h> |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 32 | #include <yaml-cpp/yaml.h> |
| 33 | #include <fstream> |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 34 | |
| 35 | struct dhtnc_params |
| 36 | { |
| 37 | bool help {false}; |
| 38 | bool version {false}; |
| 39 | bool listen {false}; |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 40 | std::filesystem::path privateKey {}; |
| 41 | std::filesystem::path cert {}; |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 42 | std::string bootstrap {}; |
| 43 | std::string remote_host {}; |
| 44 | in_port_t remote_port {}; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 45 | dht::InfoHash peer_id {}; |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 46 | std::string turn_host {}; |
| 47 | std::string turn_user {}; |
| 48 | std::string turn_pass {}; |
| 49 | std::string turn_realm {}; |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 50 | std::string configuration {}; |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 51 | bool anonymous_cnx {false}; |
Amna | 6999623 | 2024-07-23 14:04:44 -0400 | [diff] [blame] | 52 | bool verbose {false}; |
Amna | 2ee14f0 | 2024-07-24 15:15:55 -0400 | [diff] [blame] | 53 | std::map<std::string, std::vector<int>> authorizedServices {}; |
Amna | 45db776 | 2024-07-24 18:33:48 -0400 | [diff] [blame] | 54 | bool enable_upnp {true}; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 55 | }; |
| 56 | |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 57 | static const constexpr struct option long_options[] |
| 58 | = {{"help", no_argument, nullptr, 'h'}, |
| 59 | {"version", no_argument, nullptr, 'v'}, |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 60 | {"port", required_argument, nullptr, 'P'}, |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 61 | {"ip", required_argument, nullptr, 'i'}, |
| 62 | {"listen", no_argument, nullptr, 'l'}, |
| 63 | {"bootstrap", required_argument, nullptr, 'b'}, |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 64 | {"privateKey", required_argument, nullptr, 'p'}, |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 65 | {"turn_host", required_argument, nullptr, 't'}, |
| 66 | {"turn_user", required_argument, nullptr, 'u'}, |
| 67 | {"turn_pass", required_argument, nullptr, 'w'}, |
| 68 | {"turn_realm", required_argument, nullptr, 'r'}, |
Amna | 77e27dc | 2024-07-24 13:09:40 -0400 | [diff] [blame] | 69 | {"certificate", required_argument, nullptr, 'c'}, |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 70 | {"configuration", required_argument, nullptr, 'd'}, |
Amna | 77e27dc | 2024-07-24 13:09:40 -0400 | [diff] [blame] | 71 | {"anonymous", no_argument, nullptr, 'a'}, |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 72 | {nullptr, 0, nullptr, 0}}; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 73 | |
| 74 | dhtnc_params |
| 75 | parse_args(int argc, char** argv) |
| 76 | { |
| 77 | dhtnc_params params; |
| 78 | int opt; |
Amna | 6999623 | 2024-07-23 14:04:44 -0400 | [diff] [blame] | 79 | int v_count = 0; |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 80 | while ((opt = getopt_long(argc, argv, "ahvlw:r:u:t:P:b:p:i:c:d:", long_options, nullptr)) != -1) { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 81 | switch (opt) { |
| 82 | case 'h': |
| 83 | params.help = true; |
| 84 | break; |
Amna | 65a6ea8 | 2023-09-26 12:29:47 -0400 | [diff] [blame] | 85 | case 'v': |
Amna | 6999623 | 2024-07-23 14:04:44 -0400 | [diff] [blame] | 86 | v_count++; |
| 87 | if (v_count == 1) { |
| 88 | params.version = true; |
| 89 | }else if (v_count == 2) { |
| 90 | params.version = false; |
| 91 | params.verbose = true; |
| 92 | } |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 93 | break; |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 94 | case 'P': |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 95 | params.remote_port = std::stoi(optarg); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 96 | break; |
| 97 | case 'i': |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 98 | params.remote_host = optarg; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 99 | break; |
| 100 | case 'l': |
| 101 | params.listen = true; |
| 102 | break; |
| 103 | case 'b': |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 104 | params.bootstrap = optarg; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 105 | break; |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 106 | case 'p': |
| 107 | params.privateKey = optarg; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 108 | break; |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 109 | case 't': |
| 110 | params.turn_host = optarg; |
| 111 | break; |
| 112 | case 'u': |
| 113 | params.turn_user = optarg; |
| 114 | break; |
| 115 | case 'w': |
| 116 | params.turn_pass = optarg; |
| 117 | break; |
| 118 | case 'r': |
| 119 | params.turn_realm = optarg; |
| 120 | break; |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 121 | case 'c': |
| 122 | params.cert = optarg; |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 123 | break; |
| 124 | case 'd': |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 125 | params.configuration = optarg; |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 126 | break; |
| 127 | case 'a': |
| 128 | params.anonymous_cnx = true; |
| 129 | break; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 130 | default: |
| 131 | std::cerr << "Invalid option" << std::endl; |
| 132 | exit(EXIT_FAILURE); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // If not listening, the peer_id argument is required |
Amna | 65a6ea8 | 2023-09-26 12:29:47 -0400 | [diff] [blame] | 137 | if (!params.listen && !params.help && !params.version) { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 138 | if (optind < argc) { |
| 139 | params.peer_id = dht::InfoHash(argv[optind]); |
| 140 | optind++; // Move to the next argument |
| 141 | } else { |
| 142 | std::cerr << "Error: Missing peer_id argument.\n"; |
| 143 | exit(EXIT_FAILURE); |
| 144 | } |
| 145 | } |
| 146 | |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 147 | // extract values from dnc yaml file |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 148 | if (!params.configuration.empty()) { |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame] | 149 | Log("Read configuration file: {}\n", params.configuration.c_str()); |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 150 | std::ifstream config_file(params.configuration); |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 151 | if (!config_file.is_open()) { |
| 152 | std::cerr << "Error: Could not open configuration file.\n"; |
| 153 | } else { |
| 154 | YAML::Node config = YAML::Load(config_file); |
| 155 | if (config["bootstrap"] && params.bootstrap.empty()) { |
| 156 | params.bootstrap = config["bootstrap"].as<std::string>(); |
| 157 | } |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 158 | if (config["privateKey"] && params.privateKey.empty()) { |
| 159 | params.privateKey = config["privateKey"].as<std::string>(); |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 160 | } |
| 161 | if (config["turn_host"] && params.turn_host.empty()) { |
| 162 | params.turn_host = config["turn_host"].as<std::string>(); |
| 163 | } |
| 164 | if (config["turn_user"] && params.turn_user.empty()) { |
| 165 | params.turn_user = config["turn_user"].as<std::string>(); |
| 166 | } |
| 167 | if (config["turn_pass"] && params.turn_pass.empty()) { |
| 168 | params.turn_pass = config["turn_pass"].as<std::string>(); |
| 169 | } |
| 170 | if (config["turn_realm"] && params.turn_realm.empty()) { |
| 171 | params.turn_realm = config["turn_realm"].as<std::string>(); |
| 172 | } |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 173 | if (config["certificate"] && params.cert.empty()) { |
| 174 | params.cert = config["certificate"].as<std::string>(); |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 175 | } |
| 176 | if (config["ip"] && params.remote_host.empty()) { |
Amna | 2ee14f0 | 2024-07-24 15:15:55 -0400 | [diff] [blame] | 177 | params.remote_host = config["ip"].as<std::string>(); |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 178 | } |
| 179 | if (config["port"] && params.remote_port == 0) { |
| 180 | params.remote_port = config["port"].as<int>(); |
| 181 | } |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 182 | if (config["anonymous"] && !params.anonymous_cnx) { |
| 183 | params.anonymous_cnx = config["anonymous"].as<bool>(); |
| 184 | } |
Amna | 6999623 | 2024-07-23 14:04:44 -0400 | [diff] [blame] | 185 | if (config["verbose"] && !params.verbose) { |
| 186 | params.verbose = config["verbose"].as<bool>(); |
| 187 | } |
Amna | 2ee14f0 | 2024-07-24 15:15:55 -0400 | [diff] [blame] | 188 | if (config["authorized_services"]) { |
| 189 | for (auto service : config["authorized_services"]) { |
| 190 | std::string ip = service["ip"].as<std::string>(); |
| 191 | int port = 0; |
| 192 | try { |
| 193 | port = service["port"].as<int>(); |
| 194 | } catch (YAML::TypedBadConversion<int> e) { |
| 195 | std::cerr << "Error: Invalid port number in configuration file.\n"; |
| 196 | exit(EXIT_FAILURE); |
| 197 | } |
| 198 | if (port < 1 || port > 65535 || ip.empty()) { |
| 199 | std::cerr << "Error: Invalid ip or port number in configuration file.\n"; |
| 200 | exit(EXIT_FAILURE); |
| 201 | } |
| 202 | params.authorizedServices[ip].push_back(port); |
| 203 | } |
| 204 | } |
Amna | 45db776 | 2024-07-24 18:33:48 -0400 | [diff] [blame] | 205 | if (config["enable_upnp"]) { |
| 206 | params.enable_upnp = config["enable_upnp"].as<bool>(); |
| 207 | } |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 208 | } |
| 209 | } |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 210 | return params; |
| 211 | } |
| 212 | |
| 213 | static void |
| 214 | setSipLogLevel() |
| 215 | { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 216 | int level = 0; |
Adrien Béraud | 6c6a962 | 2023-08-27 12:49:31 -0400 | [diff] [blame] | 217 | if (char* envvar = getenv("SIPLOGLEVEL")) { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 218 | // From 0 (min) to 6 (max) |
Adrien Béraud | 6c6a962 | 2023-08-27 12:49:31 -0400 | [diff] [blame] | 219 | level = std::clamp(std::stoi(envvar), 0, 6); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | pj_log_set_level(level); |
Amna | be36392 | 2023-11-20 15:24:54 -0500 | [diff] [blame] | 223 | pj_log_set_log_func([](int level, const char* data, int len) { |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame] | 224 | Log("{}", std::string_view(data, len)); |
Amna | be36392 | 2023-11-20 15:24:54 -0500 | [diff] [blame] | 225 | }); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | int |
| 229 | main(int argc, char** argv) |
| 230 | { |
| 231 | setSipLogLevel(); |
| 232 | auto params = parse_args(argc, argv); |
Amna | 65a6ea8 | 2023-09-26 12:29:47 -0400 | [diff] [blame] | 233 | |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 234 | if (params.help) { |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame] | 235 | Log("Usage: dnc [options] [PEER_ID]\n" |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 236 | "\nOptions:\n" |
Amna | 77e27dc | 2024-07-24 13:09:40 -0400 | [diff] [blame] | 237 | " -h, --help Show this help message and exit.\n" |
| 238 | " -v, --version Display the program version.\n" |
| 239 | " -P, --port [PORT] Specify the port option with an argument.\n" |
| 240 | " -i, --ip [ADDRESS] Specify the ip option with an argument.\n" |
| 241 | " -l, --listen Start the program in listen mode.\n" |
| 242 | " -b, --bootstrap [ADDRESS] Specify the bootstrap option with an argument.\n" |
| 243 | " -t, --turn_host [ADDRESS] Specify the turn_host option with an argument.\n" |
| 244 | " -u, --turn_user [USER] Specify the turn_user option with an argument.\n" |
| 245 | " -w, --turn_pass [SECRET] Specify the turn_pass option with an argument.\n" |
| 246 | " -r, --turn_realm [REALM] Specify the turn_realm option with an argument.\n" |
| 247 | " -c, --certificate [FILE] Specify the certificate option with an argument.\n" |
| 248 | " -d, --configuration [FILE] Specify the configuration option with an argument.\n" |
| 249 | " -p, --privateKey [FILE] Specify the privateKey option with an argument.\n" |
| 250 | " -a, --anonymous Enable the anonymous mode.\n" |
| 251 | " -vv, --verbose Enable verbose mode.\n"); |
Amna | 65a6ea8 | 2023-09-26 12:29:47 -0400 | [diff] [blame] | 252 | return EXIT_SUCCESS; |
| 253 | } |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 254 | |
Amna | 65a6ea8 | 2023-09-26 12:29:47 -0400 | [diff] [blame] | 255 | if (params.version) { |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame] | 256 | Log("dnc v1.0\n"); |
Amna | 65a6ea8 | 2023-09-26 12:29:47 -0400 | [diff] [blame] | 257 | return EXIT_SUCCESS; |
| 258 | } |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 259 | |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 260 | auto identity = dhtnet::loadIdentity(params.privateKey, params.cert); |
Louis Maillard | 2da6725 | 2024-07-23 14:17:16 -0400 | [diff] [blame] | 261 | if (!identity.first || !identity.second) { |
| 262 | fmt::print(stderr, "Hint: To generate new identity files, run: dhtnet-crtmgr --interactive\n"); |
| 263 | return EXIT_FAILURE; |
| 264 | } |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame] | 265 | Log("Loaded identity: {}\n", identity.second->getId()); |
Amna | 65a6ea8 | 2023-09-26 12:29:47 -0400 | [diff] [blame] | 266 | |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame] | 267 | Log("dnc 1.0\n"); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 268 | std::unique_ptr<dhtnet::Dnc> dhtnc; |
| 269 | if (params.listen) { |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 270 | // create dnc instance |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 271 | dhtnc = std::make_unique<dhtnet::Dnc>(identity, |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 272 | params.bootstrap, |
| 273 | params.turn_host, |
| 274 | params.turn_user, |
| 275 | params.turn_pass, |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 276 | params.turn_realm, |
Amna | 6999623 | 2024-07-23 14:04:44 -0400 | [diff] [blame] | 277 | params.anonymous_cnx, |
Amna | 2ee14f0 | 2024-07-24 15:15:55 -0400 | [diff] [blame] | 278 | params.verbose, |
Amna | 45db776 | 2024-07-24 18:33:48 -0400 | [diff] [blame] | 279 | params.authorizedServices, |
| 280 | params.enable_upnp); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 281 | } else { |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 282 | dhtnc = std::make_unique<dhtnet::Dnc>(identity, |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 283 | params.bootstrap, |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 284 | params.peer_id, |
Adrien Béraud | ecde63f | 2023-08-26 18:11:21 -0400 | [diff] [blame] | 285 | params.remote_host, |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 286 | params.remote_port, |
| 287 | params.turn_host, |
| 288 | params.turn_user, |
| 289 | params.turn_pass, |
Amna | 6999623 | 2024-07-23 14:04:44 -0400 | [diff] [blame] | 290 | params.turn_realm, |
Amna | 45db776 | 2024-07-24 18:33:48 -0400 | [diff] [blame] | 291 | params.verbose, |
| 292 | params.enable_upnp); |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 293 | } |
| 294 | dhtnc->run(); |
Amna | 2f3539b | 2023-09-18 13:59:22 -0400 | [diff] [blame] | 295 | return EXIT_SUCCESS; |
Amna | 3876830 | 2023-08-21 11:51:56 -0400 | [diff] [blame] | 296 | } |