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> |
| 31 | |
| 32 | struct dhtvpn_params |
| 33 | { |
| 34 | bool help {false}; |
| 35 | bool version {false}; |
| 36 | bool listen {false}; |
| 37 | std::filesystem::path path {}; |
| 38 | std::string bootstrap {}; |
| 39 | dht::InfoHash peer_id {}; |
| 40 | std::string turn_host {}; |
| 41 | std::string turn_user {}; |
| 42 | std::string turn_pass {}; |
| 43 | std::string turn_realm {}; |
| 44 | std::string configuration_file {}; |
| 45 | }; |
| 46 | |
| 47 | static const constexpr struct option long_options[] |
| 48 | = {{"help", no_argument, nullptr, 'h'}, |
| 49 | {"version", no_argument, nullptr, 'v'}, |
| 50 | {"listen", no_argument, nullptr, 'l'}, |
| 51 | {"bootstrap", required_argument, nullptr, 'b'}, |
| 52 | {"id_path", required_argument, nullptr, 'I'}, |
| 53 | {"turn_host", required_argument, nullptr, 't'}, |
| 54 | {"turn_user", required_argument, nullptr, 'u'}, |
| 55 | {"turn_pass", required_argument, nullptr, 'w'}, |
| 56 | {"turn_realm", required_argument, nullptr, 'r'}, |
| 57 | {"configuration_file", required_argument, nullptr, 'c'}, |
| 58 | {nullptr, 0, nullptr, 0}}; |
| 59 | |
| 60 | dhtvpn_params |
| 61 | parse_args(int argc, char** argv) |
| 62 | { |
| 63 | dhtvpn_params params; |
| 64 | int opt; |
| 65 | while ((opt = getopt_long(argc, argv, "hvlw:r:u:t:I:b:c:", long_options, nullptr)) != -1) { |
| 66 | switch (opt) { |
| 67 | case 'h': |
| 68 | params.help = true; |
| 69 | break; |
| 70 | case 'v': |
| 71 | params.version = true; |
| 72 | break; |
| 73 | case 'l': |
| 74 | params.listen = true; |
| 75 | break; |
| 76 | case 'b': |
| 77 | params.bootstrap = optarg; |
| 78 | break; |
| 79 | case 'I': |
| 80 | params.path = optarg; |
| 81 | break; |
| 82 | case 't': |
| 83 | params.turn_host = optarg; |
| 84 | break; |
| 85 | case 'u': |
| 86 | params.turn_user = optarg; |
| 87 | break; |
| 88 | case 'w': |
| 89 | params.turn_pass = optarg; |
| 90 | break; |
| 91 | case 'r': |
| 92 | params.turn_realm = optarg; |
| 93 | break; |
| 94 | case 'c': |
| 95 | params.configuration_file = optarg; |
| 96 | break; |
| 97 | default: |
| 98 | std::cerr << "Invalid option" << std::endl; |
| 99 | exit(EXIT_FAILURE); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // If not listening, the peer_id argument is required |
| 104 | if (!params.listen && !params.help && !params.version) { |
| 105 | if (optind < argc) { |
| 106 | params.peer_id = dht::InfoHash(argv[optind]); |
| 107 | optind++; // Move to the next argument |
| 108 | } else { |
| 109 | std::cerr << "Error: Missing peer_id argument.\n"; |
| 110 | exit(EXIT_FAILURE); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // default values |
| 115 | |
| 116 | if (params.bootstrap.empty()) |
| 117 | params.bootstrap = "bootstrap.jami.net"; |
| 118 | if (params.path.empty()) |
| 119 | params.path = std::filesystem::path(getenv("HOME")) / ".dhtnet"; |
| 120 | if (params.turn_host.empty()) |
| 121 | params.turn_host = "turn.jami.net"; |
| 122 | if (params.turn_user.empty()) |
| 123 | params.turn_user = "ring"; |
| 124 | if (params.turn_pass.empty()) |
| 125 | params.turn_pass = "ring"; |
| 126 | if (params.turn_realm.empty()) |
| 127 | params.turn_realm = "ring"; |
| 128 | if (params.configuration_file.empty()) |
| 129 | params.configuration_file = std::filesystem::path(__FILE__).parent_path()/"test_config.yaml"; |
| 130 | |
| 131 | return params; |
| 132 | } |
| 133 | |
| 134 | static void |
| 135 | setSipLogLevel() |
| 136 | { |
| 137 | int level = 0; |
| 138 | if (char* envvar = getenv("SIPLOGLEVEL")) { |
| 139 | // From 0 (min) to 6 (max) |
| 140 | level = std::clamp(std::stoi(envvar), 0, 6); |
| 141 | } |
| 142 | |
| 143 | pj_log_set_level(level); |
| 144 | pj_log_set_log_func([](int level, const char* data, int len) { |
| 145 | fmt::print("{}", std::string_view(data, len)); |
| 146 | }); |
| 147 | } |
| 148 | |
| 149 | int |
| 150 | main(int argc, char** argv) |
| 151 | { |
| 152 | setSipLogLevel(); |
| 153 | auto params = parse_args(argc, argv); |
| 154 | |
| 155 | if (params.help) { |
| 156 | fmt::print( |
| 157 | "Usage: dvpn [options] [PEER_ID]\n" |
| 158 | "\nOptions:\n" |
| 159 | " -h, --help Show this help message and exit.\n" |
| 160 | " -v, --version Display the program version.\n" |
| 161 | " -l, --listen Start the program in listen mode.\n" |
| 162 | " -b, --bootstrap Specify the bootstrap option with an argument.\n" |
| 163 | " -I, --id_path Specify the id_path option with an argument.\n" |
| 164 | " -t, --turn_host Specify the turn_host option with an argument.\n" |
| 165 | " -u, --turn_user Specify the turn_user option with an argument.\n" |
| 166 | " -w, --turn_pass Specify the turn_pass option with an argument.\n" |
| 167 | " -r, --turn_realm Specify the turn_realm option with an argument.\n" |
| 168 | " -c, --configuration_file Specify the configuration_file path option with an argument.\n" |
| 169 | "\n"); |
| 170 | return EXIT_SUCCESS; |
| 171 | } |
| 172 | if (params.version) { |
| 173 | fmt::print("dvpn v1.0\n"); |
| 174 | return EXIT_SUCCESS; |
| 175 | } |
| 176 | |
| 177 | fmt::print("dvpn 1.0\n"); |
| 178 | auto identity = dhtnet::loadIdentity(params.path); |
| 179 | fmt::print("Loaded identity: {} from {}\n", identity.second->getId(), params.path); |
| 180 | |
| 181 | std::unique_ptr<dhtnet::Dvpn> dvpn; |
| 182 | if (params.listen) { |
| 183 | // create dvpn instance |
| 184 | dvpn = std::make_unique<dhtnet::DvpnServer>(params.path, |
| 185 | identity, |
| 186 | params.bootstrap, |
| 187 | params.turn_host, |
| 188 | params.turn_user, |
| 189 | params.turn_pass, |
| 190 | params.turn_realm, |
| 191 | params.configuration_file); |
| 192 | } else { |
| 193 | dvpn = std::make_unique<dhtnet::DvpnClient>(params.peer_id, |
| 194 | params.path, |
| 195 | identity, |
| 196 | params.bootstrap, |
| 197 | params.turn_host, |
| 198 | params.turn_user, |
| 199 | params.turn_pass, |
| 200 | params.turn_realm, |
| 201 | params.configuration_file); |
| 202 | } |
| 203 | dvpn->run(); |
| 204 | return EXIT_SUCCESS; |
| 205 | } |