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