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" |
| 19 | |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | #include <iostream> |
| 23 | #include <unistd.h> |
| 24 | #include <getopt.h> |
| 25 | #if __has_include(<fmt/std.h>) |
| 26 | #include <fmt/std.h> |
| 27 | #else |
| 28 | #include <fmt/ostream.h> |
| 29 | #endif |
| 30 | #include <netinet/in.h> |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 31 | #include <yaml-cpp/yaml.h> |
| 32 | #include <fstream> |
Amna | 4e52b16 | 2024-01-14 21:16:57 -0500 | [diff] [blame] | 33 | |
| 34 | struct dhtvpn_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 turn_host {}; |
| 43 | std::string turn_user {}; |
| 44 | std::string turn_pass {}; |
| 45 | std::string turn_realm {}; |
| 46 | std::string configuration_file {}; |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 47 | std::string ca {}; |
| 48 | std::string dvpn_configuration_file {}; |
Amna | 4e52b16 | 2024-01-14 21:16:57 -0500 | [diff] [blame] | 49 | }; |
| 50 | |
| 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 | {"id_path", required_argument, nullptr, 'I'}, |
| 57 | {"turn_host", required_argument, nullptr, 't'}, |
| 58 | {"turn_user", required_argument, nullptr, 'u'}, |
| 59 | {"turn_pass", required_argument, nullptr, 'w'}, |
| 60 | {"turn_realm", required_argument, nullptr, 'r'}, |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 61 | {"vpn_configuration_file", required_argument, nullptr, 'c'}, |
| 62 | {"CA", required_argument, nullptr, 'C'}, |
| 63 | {"dvpn_configuration_file", required_argument, nullptr, 'd'}, |
Amna | 4e52b16 | 2024-01-14 21:16:57 -0500 | [diff] [blame] | 64 | {nullptr, 0, nullptr, 0}}; |
| 65 | |
| 66 | dhtvpn_params |
| 67 | parse_args(int argc, char** argv) |
| 68 | { |
| 69 | dhtvpn_params params; |
| 70 | int opt; |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 71 | while ((opt = getopt_long(argc, argv, "hvlw:r:u:t:I:b:c:C:d:", long_options, nullptr)) != -1) { |
Amna | 4e52b16 | 2024-01-14 21:16:57 -0500 | [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 'I': |
| 86 | params.path = optarg; |
| 87 | break; |
| 88 | case 't': |
| 89 | params.turn_host = optarg; |
| 90 | break; |
| 91 | case 'u': |
| 92 | params.turn_user = optarg; |
| 93 | break; |
| 94 | case 'w': |
| 95 | params.turn_pass = optarg; |
| 96 | break; |
| 97 | case 'r': |
| 98 | params.turn_realm = optarg; |
| 99 | break; |
| 100 | case 'c': |
| 101 | params.configuration_file = optarg; |
| 102 | break; |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 103 | case 'C': |
| 104 | params.ca = optarg; |
| 105 | break; |
| 106 | case 'd': |
| 107 | params.dvpn_configuration_file = optarg; |
| 108 | break; |
Amna | 4e52b16 | 2024-01-14 21:16:57 -0500 | [diff] [blame] | 109 | default: |
| 110 | std::cerr << "Invalid option" << std::endl; |
| 111 | exit(EXIT_FAILURE); |
| 112 | } |
| 113 | } |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 114 | // extract values from dvpn yaml file |
| 115 | if (!params.dvpn_configuration_file.empty()) { |
| 116 | printf("read configuration file: %s\n", params.dvpn_configuration_file.c_str()); |
| 117 | std::ifstream config_file(params.dvpn_configuration_file); |
| 118 | if (!config_file.is_open()) { |
| 119 | std::cerr << "Error: Could not open configuration file.\n"; |
| 120 | } else { |
| 121 | YAML::Node config = YAML::Load(config_file); |
| 122 | if (config["bootstrap"] && params.bootstrap.empty()) { |
| 123 | params.bootstrap = config["bootstrap"].as<std::string>(); |
| 124 | } |
| 125 | if (config["id_path"] && params.path.empty()) { |
| 126 | params.path = config["id_path"].as<std::string>(); |
| 127 | } |
| 128 | if (config["turn_host"] && params.turn_host.empty()) { |
| 129 | params.turn_host = config["turn_host"].as<std::string>(); |
| 130 | } |
| 131 | if (config["turn_user"] && params.turn_user.empty()) { |
| 132 | params.turn_user = config["turn_user"].as<std::string>(); |
| 133 | } |
| 134 | if (config["turn_pass"] && params.turn_pass.empty()) { |
| 135 | params.turn_pass = config["turn_pass"].as<std::string>(); |
| 136 | } |
| 137 | if (config["turn_realm"] && params.turn_realm.empty()) { |
| 138 | params.turn_realm = config["turn_realm"].as<std::string>(); |
| 139 | } |
| 140 | if (config["CA"] && params.ca.empty()) { |
| 141 | params.ca = config["CA"].as<std::string>(); |
| 142 | } |
| 143 | if (config["configuration_file"] && params.configuration_file.empty()) { |
| 144 | params.configuration_file = config["configuration_file"].as<std::string>(); |
| 145 | } |
| 146 | } |
| 147 | } |
Amna | 4e52b16 | 2024-01-14 21:16:57 -0500 | [diff] [blame] | 148 | |
| 149 | // If not listening, the peer_id argument is required |
| 150 | if (!params.listen && !params.help && !params.version) { |
| 151 | if (optind < argc) { |
| 152 | params.peer_id = dht::InfoHash(argv[optind]); |
| 153 | optind++; // Move to the next argument |
| 154 | } else { |
| 155 | std::cerr << "Error: Missing peer_id argument.\n"; |
| 156 | exit(EXIT_FAILURE); |
| 157 | } |
| 158 | } |
Amna | 4e52b16 | 2024-01-14 21:16:57 -0500 | [diff] [blame] | 159 | return params; |
| 160 | } |
| 161 | |
| 162 | static void |
| 163 | setSipLogLevel() |
| 164 | { |
| 165 | int level = 0; |
| 166 | if (char* envvar = getenv("SIPLOGLEVEL")) { |
| 167 | // From 0 (min) to 6 (max) |
| 168 | level = std::clamp(std::stoi(envvar), 0, 6); |
| 169 | } |
| 170 | |
| 171 | pj_log_set_level(level); |
| 172 | pj_log_set_log_func([](int level, const char* data, int len) { |
| 173 | fmt::print("{}", std::string_view(data, len)); |
| 174 | }); |
| 175 | } |
| 176 | |
| 177 | int |
| 178 | main(int argc, char** argv) |
| 179 | { |
| 180 | setSipLogLevel(); |
| 181 | auto params = parse_args(argc, argv); |
| 182 | |
| 183 | if (params.help) { |
| 184 | fmt::print( |
| 185 | "Usage: dvpn [options] [PEER_ID]\n" |
| 186 | "\nOptions:\n" |
| 187 | " -h, --help Show this help message and exit.\n" |
| 188 | " -v, --version Display the program version.\n" |
| 189 | " -l, --listen Start the program in listen mode.\n" |
| 190 | " -b, --bootstrap Specify the bootstrap option with an argument.\n" |
| 191 | " -I, --id_path Specify the id_path option with an argument.\n" |
| 192 | " -t, --turn_host Specify the turn_host option with an argument.\n" |
| 193 | " -u, --turn_user Specify the turn_user option with an argument.\n" |
| 194 | " -w, --turn_pass Specify the turn_pass option with an argument.\n" |
| 195 | " -r, --turn_realm Specify the turn_realm option with an argument.\n" |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 196 | " -c, --vpn_configuration_file Specify the vpn_configuration_file path option with an argument.\n" |
| 197 | " -C, --CA Specify the CA path option with an argument.\n" |
| 198 | " -d, --dvpn_configuration_file Specify the dvpn_configuration_file path option with an argument.\n" |
Amna | 4e52b16 | 2024-01-14 21:16:57 -0500 | [diff] [blame] | 199 | "\n"); |
| 200 | return EXIT_SUCCESS; |
| 201 | } |
| 202 | if (params.version) { |
| 203 | fmt::print("dvpn v1.0\n"); |
| 204 | return EXIT_SUCCESS; |
| 205 | } |
| 206 | |
| 207 | fmt::print("dvpn 1.0\n"); |
Amna | 41848a2 | 2024-01-22 16:22:57 -0500 | [diff] [blame] | 208 | |
| 209 | auto identity = dhtnet::loadIdentity(params.path, params.ca); |
Amna | 4e52b16 | 2024-01-14 21:16:57 -0500 | [diff] [blame] | 210 | fmt::print("Loaded identity: {} from {}\n", identity.second->getId(), params.path); |
| 211 | |
| 212 | std::unique_ptr<dhtnet::Dvpn> dvpn; |
| 213 | if (params.listen) { |
| 214 | // create dvpn instance |
| 215 | dvpn = std::make_unique<dhtnet::DvpnServer>(params.path, |
| 216 | identity, |
| 217 | params.bootstrap, |
| 218 | params.turn_host, |
| 219 | params.turn_user, |
| 220 | params.turn_pass, |
| 221 | params.turn_realm, |
| 222 | params.configuration_file); |
| 223 | } else { |
| 224 | dvpn = std::make_unique<dhtnet::DvpnClient>(params.peer_id, |
| 225 | params.path, |
| 226 | identity, |
| 227 | params.bootstrap, |
| 228 | params.turn_host, |
| 229 | params.turn_user, |
| 230 | params.turn_pass, |
| 231 | params.turn_realm, |
| 232 | params.configuration_file); |
| 233 | } |
| 234 | dvpn->run(); |
| 235 | return EXIT_SUCCESS; |
| 236 | } |