Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 "dsh.h" |
| 18 | #include "../common.h" |
Amna | 423b25b | 2024-02-06 13:21:19 -0500 | [diff] [blame] | 19 | #include "dhtnet_crtmgr/dhtnet_crtmgr.h" |
| 20 | |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 21 | #include <string> |
| 22 | #include <vector> |
| 23 | #include <iostream> |
| 24 | #include <unistd.h> |
| 25 | #include <getopt.h> |
| 26 | |
| 27 | #include <netinet/in.h> |
Adrien BĂ©raud | 277edaf | 2023-09-20 10:57:43 -0400 | [diff] [blame] | 28 | #if __has_include(<fmt/std.h>) |
| 29 | #include <fmt/std.h> |
| 30 | #else |
| 31 | #include <fmt/ostream.h> |
| 32 | #endif |
Amna | 2b5b07f | 2024-01-22 17:04:36 -0500 | [diff] [blame] | 33 | #include <yaml-cpp/yaml.h> |
| 34 | #include <fstream> |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 35 | |
| 36 | struct dhtsh_params |
| 37 | { |
| 38 | bool help {false}; |
| 39 | bool version {false}; |
| 40 | bool listen {false}; |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 41 | std::filesystem::path privateKey {}; |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 42 | std::string bootstrap {}; |
| 43 | dht::InfoHash peer_id {}; |
| 44 | std::string binary {}; |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 45 | std::filesystem::path cert {}; |
Amna | 2b5b07f | 2024-01-22 17:04:36 -0500 | [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 | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 52 | }; |
| 53 | |
Amna | 2b5b07f | 2024-01-22 17:04:36 -0500 | [diff] [blame] | 54 | static const constexpr struct option long_options[] |
| 55 | = {{"help", no_argument, nullptr, 'h'}, |
| 56 | {"version", no_argument, nullptr, 'v'}, |
| 57 | {"listen", no_argument, nullptr, 'l'}, |
| 58 | {"bootstrap", required_argument, nullptr, 'b'}, |
| 59 | {"binary", required_argument, nullptr, 's'}, |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 60 | {"privateKey", required_argument, nullptr, 'p'}, |
| 61 | {"certificate", required_argument, nullptr, 'c'}, |
Amna | 2b5b07f | 2024-01-22 17:04:36 -0500 | [diff] [blame] | 62 | {"turn_host", required_argument, nullptr, 't'}, |
| 63 | {"turn_user", required_argument, nullptr, 'u'}, |
| 64 | {"turn_pass", required_argument, nullptr, 'w'}, |
| 65 | {"turn_realm", required_argument, nullptr, 'r'}, |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 66 | {"configuration", required_argument, nullptr, 'd'}, |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 67 | {"anonymous", no_argument, nullptr, 'a'}, |
Amna | 2b5b07f | 2024-01-22 17:04:36 -0500 | [diff] [blame] | 68 | {nullptr, 0, nullptr, 0}}; |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 69 | |
| 70 | dhtsh_params |
| 71 | parse_args(int argc, char** argv) |
| 72 | { |
| 73 | dhtsh_params params; |
| 74 | int opt; |
Amna | 77e27dc | 2024-07-24 13:09:40 -0400 | [diff] [blame] | 75 | while ((opt = getopt_long(argc, argv, "hvlab:s:p:c:r:w:u:t:d:", long_options, nullptr)) != -1) { |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 76 | switch (opt) { |
| 77 | case 'h': |
| 78 | params.help = true; |
| 79 | break; |
| 80 | case 'v': |
| 81 | params.version = true; |
| 82 | break; |
| 83 | case 'l': |
| 84 | params.listen = true; |
| 85 | break; |
| 86 | case 'b': |
| 87 | params.bootstrap = optarg; |
| 88 | break; |
| 89 | case 's': |
| 90 | params.binary = optarg; |
| 91 | break; |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 92 | case 'p': |
| 93 | params.privateKey = optarg; |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 94 | break; |
Amna | 2b5b07f | 2024-01-22 17:04:36 -0500 | [diff] [blame] | 95 | case 't': |
| 96 | params.turn_host = optarg; |
| 97 | break; |
| 98 | case 'u': |
| 99 | params.turn_user = optarg; |
| 100 | break; |
| 101 | case 'w': |
| 102 | params.turn_pass = optarg; |
| 103 | break; |
| 104 | case 'r': |
| 105 | params.turn_realm = optarg; |
| 106 | break; |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 107 | case 'c': |
| 108 | params.cert = optarg; |
Amna | 2b5b07f | 2024-01-22 17:04:36 -0500 | [diff] [blame] | 109 | break; |
| 110 | case 'd': |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 111 | params.configuration = optarg; |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 112 | break; |
| 113 | case 'a': |
| 114 | params.anonymous_cnx = true; |
| 115 | break; |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 116 | default: |
| 117 | std::cerr << "Invalid option" << std::endl; |
| 118 | exit(EXIT_FAILURE); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // If not listening, the peer_id argument is required |
Amna | 77bb585 | 2023-09-28 10:31:11 -0400 | [diff] [blame] | 123 | if (!params.listen && !params.help && !params.version) { |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 124 | if (optind < argc) { |
| 125 | params.peer_id = dht::InfoHash(argv[optind]); |
| 126 | optind++; // Move to the next argument |
| 127 | } else { |
| 128 | std::cerr << "Error: Missing peer_id argument.\n"; |
| 129 | exit(EXIT_FAILURE); |
| 130 | } |
| 131 | } |
| 132 | |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 133 | // extract values from dsh yaml file |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 134 | if (!params.configuration.empty()) { |
| 135 | printf("read configuration file: %s\n", params.configuration.c_str()); |
| 136 | std::ifstream config_file(params.configuration); |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 137 | if (!config_file.is_open()) { |
ovari123 | a15c688 | 2024-09-17 18:34:20 -0400 | [diff] [blame] | 138 | std::cerr << "Error: Unable to open configuration file.\n"; |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 139 | } else { |
| 140 | YAML::Node config = YAML::Load(config_file); |
| 141 | if (config["bootstrap"] && params.bootstrap.empty()) { |
| 142 | params.bootstrap = config["bootstrap"].as<std::string>(); |
| 143 | } |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 144 | if (config["privateKey"] && params.privateKey.empty()) { |
| 145 | params.privateKey = config["privateKey"].as<std::string>(); |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 146 | } |
| 147 | if (config["turn_host"] && params.turn_host.empty()) { |
| 148 | params.turn_host = config["turn_host"].as<std::string>(); |
| 149 | } |
| 150 | if (config["turn_user"] && params.turn_user.empty()) { |
| 151 | params.turn_user = config["turn_user"].as<std::string>(); |
| 152 | } |
| 153 | if (config["turn_pass"] && params.turn_pass.empty()) { |
| 154 | params.turn_pass = config["turn_pass"].as<std::string>(); |
| 155 | } |
| 156 | if (config["turn_realm"] && params.turn_realm.empty()) { |
| 157 | params.turn_realm = config["turn_realm"].as<std::string>(); |
| 158 | } |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 159 | if (config["certificate"] && params.cert.empty()) { |
| 160 | params.cert = config["certificate"].as<std::string>(); |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 161 | } |
| 162 | if (config["binary"] && params.binary.empty()) { |
| 163 | params.binary = config["binary"].as<std::string>(); |
| 164 | } |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 165 | if (config["anonymous"] && !params.anonymous_cnx) { |
| 166 | params.anonymous_cnx = config["anonymous"].as<bool>(); |
| 167 | } |
| 168 | |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 169 | } |
| 170 | } |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 171 | return params; |
| 172 | } |
| 173 | |
| 174 | static void |
| 175 | setSipLogLevel() |
| 176 | { |
| 177 | char* envvar = getenv("SIPLOGLEVEL"); |
| 178 | |
| 179 | int level = 0; |
| 180 | |
| 181 | if (envvar != nullptr) { |
| 182 | level = std::stoi(envvar); |
| 183 | |
| 184 | // From 0 (min) to 6 (max) |
| 185 | level = std::max(0, std::min(level, 6)); |
| 186 | } |
| 187 | |
| 188 | pj_log_set_level(level); |
| 189 | pj_log_set_log_func([](int level, const char* data, int /*len*/) {}); |
| 190 | } |
| 191 | |
| 192 | int |
| 193 | main(int argc, char** argv) |
| 194 | { |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 195 | setSipLogLevel(); |
| 196 | auto params = parse_args(argc, argv); |
Amna | 77bb585 | 2023-09-28 10:31:11 -0400 | [diff] [blame] | 197 | |
| 198 | if (params.help){ |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame] | 199 | Log("Usage: dsh [OPTIONS] [PEER_ID]\n" |
Amna | 2b5b07f | 2024-01-22 17:04:36 -0500 | [diff] [blame] | 200 | "\nOptions:\n" |
Amna | 77e27dc | 2024-07-24 13:09:40 -0400 | [diff] [blame] | 201 | " -h, --help Show this help message and exit.\n" |
| 202 | " -v, --version Display the program version.\n" |
| 203 | " -l, --listen Start the program in listen mode.\n" |
| 204 | " -b, --bootstrap [ADDRESS] Specify the bootstrap option with an argument.\n" |
| 205 | " -s, --binary [COMMAND] Specify the binary option with an argument.\n" |
| 206 | " -p, --privateKey [FILE] Specify the privateKey option with an argument.\n" |
| 207 | " -c, --certificate [FILE] Specify the certificate option with an argument.\n" |
| 208 | " -d, --configuration [FILE] Specify the configuration file option with an argument.\n" |
| 209 | " -t, --turn_host [ADDRESS] Specify the turn_host option with an argument.\n" |
| 210 | " -u, --turn_user [USER] Specify the turn_user option with an argument.\n" |
| 211 | " -w, --turn_pass [SECRET] Specify the turn_pass option with an argument.\n" |
| 212 | " -r, --turn_realm [REALM] Specify the turn_realm option with an argument.\n" |
| 213 | " -a, --anonymous Enable anonymous connection.\n"); |
Amna | 77bb585 | 2023-09-28 10:31:11 -0400 | [diff] [blame] | 214 | return EXIT_SUCCESS; |
| 215 | } |
| 216 | if (params.version){ |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame] | 217 | Log("dsh v1.0\n"); |
Amna | 77bb585 | 2023-09-28 10:31:11 -0400 | [diff] [blame] | 218 | return EXIT_SUCCESS; |
| 219 | } |
| 220 | |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame] | 221 | Log("dsh 1.0\n"); |
Amna | 77bb585 | 2023-09-28 10:31:11 -0400 | [diff] [blame] | 222 | |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 223 | auto identity = dhtnet::loadIdentity(params.privateKey, params.cert); |
Louis Maillard | 2da6725 | 2024-07-23 14:17:16 -0400 | [diff] [blame] | 224 | if (!identity.first || !identity.second) { |
| 225 | fmt::print(stderr, "Hint: To generate new identity files, run: dhtnet-crtmgr --interactive\n"); |
| 226 | return EXIT_FAILURE; |
| 227 | } |
Amna | c03c4b5 | 2024-07-24 17:57:25 -0400 | [diff] [blame] | 228 | Log("Loaded identity: {} \n", identity.second->getId()); |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 229 | |
| 230 | std::unique_ptr<dhtnet::Dsh> dhtsh; |
| 231 | if (params.listen) { |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 232 | // create dsh instance |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 233 | dhtsh = std::make_unique<dhtnet::Dsh>(identity, |
Amna | 2b5b07f | 2024-01-22 17:04:36 -0500 | [diff] [blame] | 234 | params.bootstrap, |
| 235 | params.turn_host, |
| 236 | params.turn_user, |
| 237 | params.turn_pass, |
Amna | 4325f0f | 2024-01-22 16:11:00 -0500 | [diff] [blame] | 238 | params.turn_realm, |
| 239 | params.anonymous_cnx); |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 240 | } else { |
Amna | c75ffe9 | 2024-02-08 17:23:29 -0500 | [diff] [blame] | 241 | dhtsh = std::make_unique<dhtnet::Dsh>(identity, |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 242 | params.bootstrap, |
| 243 | params.peer_id, |
Amna | 2b5b07f | 2024-01-22 17:04:36 -0500 | [diff] [blame] | 244 | params.binary, |
| 245 | params.turn_host, |
| 246 | params.turn_user, |
| 247 | params.turn_pass, |
| 248 | params.turn_realm); |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | dhtsh->run(); |
| 252 | return EXIT_SUCCESS; |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 253 | } |