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" |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | #include <iostream> |
| 22 | #include <unistd.h> |
| 23 | #include <getopt.h> |
| 24 | |
| 25 | #include <netinet/in.h> |
Adrien BĂ©raud | 277edaf | 2023-09-20 10:57:43 -0400 | [diff] [blame] | 26 | #if __has_include(<fmt/std.h>) |
| 27 | #include <fmt/std.h> |
| 28 | #else |
| 29 | #include <fmt/ostream.h> |
| 30 | #endif |
Amna | 2b5b07f | 2024-01-22 17:04:36 -0500 | [diff] [blame] | 31 | #include <yaml-cpp/yaml.h> |
| 32 | #include <fstream> |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 33 | |
| 34 | struct dhtsh_params |
| 35 | { |
| 36 | bool help {false}; |
| 37 | bool version {false}; |
| 38 | bool listen {false}; |
| 39 | std::filesystem::path path {}; |
| 40 | std::string bootstrap {}; |
| 41 | dht::InfoHash peer_id {}; |
| 42 | std::string binary {}; |
Amna | 2b5b07f | 2024-01-22 17:04:36 -0500 | [diff] [blame] | 43 | std::string ca {}; |
| 44 | std::string turn_host {}; |
| 45 | std::string turn_user {}; |
| 46 | std::string turn_pass {}; |
| 47 | std::string turn_realm {}; |
| 48 | std::string dsh_configuration {}; |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 49 | }; |
| 50 | |
Amna | 2b5b07f | 2024-01-22 17:04:36 -0500 | [diff] [blame] | 51 | static const constexpr struct option long_options[] |
| 52 | = {{"help", no_argument, nullptr, 'h'}, |
| 53 | {"version", no_argument, nullptr, 'v'}, |
| 54 | {"listen", no_argument, nullptr, 'l'}, |
| 55 | {"bootstrap", required_argument, nullptr, 'b'}, |
| 56 | {"binary", required_argument, nullptr, 's'}, |
| 57 | {"id_path", required_argument, nullptr, 'I'}, |
| 58 | {"CA", required_argument, nullptr, 'C'}, |
| 59 | {"turn_host", required_argument, nullptr, 't'}, |
| 60 | {"turn_user", required_argument, nullptr, 'u'}, |
| 61 | {"turn_pass", required_argument, nullptr, 'w'}, |
| 62 | {"turn_realm", required_argument, nullptr, 'r'}, |
| 63 | {"dsh_configuration", required_argument, nullptr, 'd'}, |
| 64 | {nullptr, 0, nullptr, 0}}; |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 65 | |
| 66 | dhtsh_params |
| 67 | parse_args(int argc, char** argv) |
| 68 | { |
| 69 | dhtsh_params params; |
| 70 | int opt; |
Amna | 2b5b07f | 2024-01-22 17:04:36 -0500 | [diff] [blame] | 71 | while ((opt = getopt_long(argc, argv, "hvls:I:p:i:C:r:w:u:t:d:", long_options, nullptr)) != -1) { |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 72 | switch (opt) { |
| 73 | case 'h': |
| 74 | params.help = true; |
| 75 | break; |
| 76 | case 'v': |
| 77 | params.version = true; |
| 78 | break; |
| 79 | case 'l': |
| 80 | params.listen = true; |
| 81 | break; |
| 82 | case 'b': |
| 83 | params.bootstrap = optarg; |
| 84 | break; |
| 85 | case 's': |
| 86 | params.binary = optarg; |
| 87 | break; |
| 88 | case 'I': |
| 89 | params.path = optarg; |
| 90 | break; |
Amna | 2b5b07f | 2024-01-22 17:04:36 -0500 | [diff] [blame] | 91 | case 't': |
| 92 | params.turn_host = optarg; |
| 93 | break; |
| 94 | case 'u': |
| 95 | params.turn_user = optarg; |
| 96 | break; |
| 97 | case 'w': |
| 98 | params.turn_pass = optarg; |
| 99 | break; |
| 100 | case 'r': |
| 101 | params.turn_realm = optarg; |
| 102 | break; |
| 103 | case 'C': |
| 104 | params.ca = optarg; |
| 105 | break; |
| 106 | case 'd': |
| 107 | params.dsh_configuration = optarg; |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 108 | default: |
| 109 | std::cerr << "Invalid option" << std::endl; |
| 110 | exit(EXIT_FAILURE); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // If not listening, the peer_id argument is required |
Amna | 77bb585 | 2023-09-28 10:31:11 -0400 | [diff] [blame] | 115 | if (!params.listen && !params.help && !params.version) { |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 116 | if (optind < argc) { |
| 117 | params.peer_id = dht::InfoHash(argv[optind]); |
| 118 | optind++; // Move to the next argument |
| 119 | } else { |
| 120 | std::cerr << "Error: Missing peer_id argument.\n"; |
| 121 | exit(EXIT_FAILURE); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // default values |
| 126 | if (params.bootstrap.empty()) |
| 127 | params.bootstrap = "bootstrap.jami.net"; |
| 128 | if (params.binary.empty()) |
| 129 | params.binary = "bash"; |
| 130 | if (params.path.empty()) |
| 131 | params.path = std::filesystem::path(getenv("HOME")) / ".dhtnet"; |
| 132 | return params; |
| 133 | } |
| 134 | |
| 135 | static void |
| 136 | setSipLogLevel() |
| 137 | { |
| 138 | char* envvar = getenv("SIPLOGLEVEL"); |
| 139 | |
| 140 | int level = 0; |
| 141 | |
| 142 | if (envvar != nullptr) { |
| 143 | level = std::stoi(envvar); |
| 144 | |
| 145 | // From 0 (min) to 6 (max) |
| 146 | level = std::max(0, std::min(level, 6)); |
| 147 | } |
| 148 | |
| 149 | pj_log_set_level(level); |
| 150 | pj_log_set_log_func([](int level, const char* data, int /*len*/) {}); |
| 151 | } |
| 152 | |
| 153 | int |
| 154 | main(int argc, char** argv) |
| 155 | { |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 156 | setSipLogLevel(); |
| 157 | auto params = parse_args(argc, argv); |
Amna | 77bb585 | 2023-09-28 10:31:11 -0400 | [diff] [blame] | 158 | |
| 159 | if (params.help){ |
| 160 | fmt::print("Usage: dsh [OPTIONS] [PEER_ID]\n" |
Amna | 2b5b07f | 2024-01-22 17:04:36 -0500 | [diff] [blame] | 161 | "\nOptions:\n" |
| 162 | " -h, --help Show this help message and exit.\n" |
| 163 | " -v, --version Display the program version.\n" |
| 164 | " -l, --listen Start the program in listen mode.\n" |
| 165 | " -b, --bootstrap Specify the bootstrap option with an argument.\n" |
| 166 | " -s, --binary Specify the binary option with an argument.\n" |
| 167 | " -I, --id_path Specify the id_path option with an argument.\n" |
| 168 | " -C, --CA Specify the CA option with an argument.\n" |
| 169 | " -t, --turn_host Specify the turn_host option with an argument.\n" |
| 170 | " -u, --turn_user Specify the turn_user option with an argument.\n" |
| 171 | " -w, --turn_pass Specify the turn_pass option with an argument.\n" |
| 172 | " -r, --turn_realm Specify the turn_realm option with an argument.\n"); |
Amna | 77bb585 | 2023-09-28 10:31:11 -0400 | [diff] [blame] | 173 | return EXIT_SUCCESS; |
| 174 | } |
| 175 | if (params.version){ |
| 176 | fmt::print("dsh v1.0\n"); |
| 177 | return EXIT_SUCCESS; |
| 178 | } |
| 179 | |
| 180 | fmt::print("dsh 1.0\n"); |
| 181 | |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 182 | auto identity = dhtnet::loadIdentity(params.path); |
| 183 | fmt::print("Loaded identity: {} from {}\n", identity.second->getId(), params.path); |
| 184 | |
| 185 | std::unique_ptr<dhtnet::Dsh> dhtsh; |
| 186 | if (params.listen) { |
| 187 | // create dnc instance |
Amna | 2b5b07f | 2024-01-22 17:04:36 -0500 | [diff] [blame] | 188 | dhtsh = std::make_unique<dhtnet::Dsh>(params.path, |
| 189 | identity, |
| 190 | params.bootstrap, |
| 191 | params.turn_host, |
| 192 | params.turn_user, |
| 193 | params.turn_pass, |
| 194 | params.turn_realm); |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 195 | } else { |
| 196 | dhtsh = std::make_unique<dhtnet::Dsh>(params.path, |
| 197 | identity, |
| 198 | params.bootstrap, |
| 199 | params.peer_id, |
Amna | 2b5b07f | 2024-01-22 17:04:36 -0500 | [diff] [blame] | 200 | params.binary, |
| 201 | params.turn_host, |
| 202 | params.turn_user, |
| 203 | params.turn_pass, |
| 204 | params.turn_realm); |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | dhtsh->run(); |
| 208 | return EXIT_SUCCESS; |
Amna | 4a70f5c | 2023-09-14 17:32:05 -0400 | [diff] [blame] | 209 | } |