blob: 1cf3ec9467e99cfecb862104ba32fb60aa904440 [file] [log] [blame]
Amna4e52b162024-01-14 21:16:57 -05001/*
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"
Amna423b25b2024-02-06 13:21:19 -050019#include "dhtnet_crtmgr/dhtnet_crtmgr.h"
Amna4e52b162024-01-14 21:16:57 -050020
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>
Amna41848a22024-01-22 16:22:57 -050032#include <yaml-cpp/yaml.h>
33#include <fstream>
Amna4e52b162024-01-14 21:16:57 -050034
35struct dhtvpn_params
36{
37 bool help {false};
38 bool version {false};
39 bool listen {false};
Amnac75ffe92024-02-08 17:23:29 -050040 std::filesystem::path privateKey {};
Amna4e52b162024-01-14 21:16:57 -050041 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 {};
Amnac75ffe92024-02-08 17:23:29 -050048 std::filesystem::path cert {};
49 std::string configuration {};
Amna4325f0f2024-01-22 16:11:00 -050050 bool anonymous_cnx {false};
Amna4e52b162024-01-14 21:16:57 -050051};
52
53static 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'},
Amnac75ffe92024-02-08 17:23:29 -050058 {"privateKey", required_argument, nullptr, 'p'},
Amna4e52b162024-01-14 21:16:57 -050059 {"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'},
Amna77e27dc2024-07-24 13:09:40 -040063 {"vpn_configuration", required_argument, nullptr, 'C'},
Amnac75ffe92024-02-08 17:23:29 -050064 {"certificate", required_argument, nullptr, 'c'},
65 {"configuration", required_argument, nullptr, 'd'},
Amna4325f0f2024-01-22 16:11:00 -050066 {"anonymous", no_argument, nullptr, 'a'},
Amna4e52b162024-01-14 21:16:57 -050067 {nullptr, 0, nullptr, 0}};
68
69dhtvpn_params
70parse_args(int argc, char** argv)
71{
72 dhtvpn_params params;
73 int opt;
Amna77e27dc2024-07-24 13:09:40 -040074 while ((opt = getopt_long(argc, argv, "hvlab:t:u:w:r:p:c:C:d:", long_options, nullptr)) != -1) {
Amna4e52b162024-01-14 21:16:57 -050075 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;
Amna77e27dc2024-07-24 13:09:40 -040085 case 'a':
86 params.anonymous_cnx = true;
87 break;
Amna4e52b162024-01-14 21:16:57 -050088 case 'b':
89 params.bootstrap = optarg;
90 break;
Amna4e52b162024-01-14 21:16:57 -050091 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;
Amnac75ffe92024-02-08 17:23:29 -0500103 case 'c':
104 params.cert = optarg;
Amna41848a22024-01-22 16:22:57 -0500105 break;
Amna77e27dc2024-07-24 13:09:40 -0400106 case 'p':
107 params.privateKey = optarg;
108 break;
109 case 'C':
110 params.configuration_file = optarg;
111 break;
Amna41848a22024-01-22 16:22:57 -0500112 case 'd':
Amnac75ffe92024-02-08 17:23:29 -0500113 params.configuration = optarg;
Amna41848a22024-01-22 16:22:57 -0500114 break;
Amna4e52b162024-01-14 21:16:57 -0500115 default:
116 std::cerr << "Invalid option" << std::endl;
117 exit(EXIT_FAILURE);
118 }
119 }
Amna41848a22024-01-22 16:22:57 -0500120 // extract values from dvpn yaml file
Amnac75ffe92024-02-08 17:23:29 -0500121 if (!params.configuration.empty()) {
122 printf("read configuration file: %s\n", params.configuration.c_str());
123 std::ifstream config_file(params.configuration);
Amna41848a22024-01-22 16:22:57 -0500124 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 }
Amnac75ffe92024-02-08 17:23:29 -0500131 if (config["privateKey"] && params.privateKey.empty()) {
132 params.privateKey = config["privateKey"].as<std::string>();
Amna41848a22024-01-22 16:22:57 -0500133 }
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 }
Amnac75ffe92024-02-08 17:23:29 -0500146 if (config["certificate"] && params.cert.empty()) {
147 params.cert = config["certificate"].as<std::string>();
Amna41848a22024-01-22 16:22:57 -0500148 }
149 if (config["configuration_file"] && params.configuration_file.empty()) {
150 params.configuration_file = config["configuration_file"].as<std::string>();
151 }
Amna4325f0f2024-01-22 16:11:00 -0500152 if (config["anonymous"] && !params.anonymous_cnx) {
153 params.anonymous_cnx = config["anonymous"].as<bool>();
154 }
Amna41848a22024-01-22 16:22:57 -0500155 }
156 }
Amna4e52b162024-01-14 21:16:57 -0500157
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 }
Amna4e52b162024-01-14 21:16:57 -0500168 return params;
169}
170
171static void
172setSipLogLevel()
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) {
Amnac03c4b52024-07-24 17:57:25 -0400182 Log("{}", std::string_view(data, len));
Amna4e52b162024-01-14 21:16:57 -0500183 });
184}
185
186int
187main(int argc, char** argv)
188{
189 setSipLogLevel();
190 auto params = parse_args(argc, argv);
191
192 if (params.help) {
Amnac03c4b52024-07-24 17:57:25 -0400193 Log(
Amna4e52b162024-01-14 21:16:57 -0500194 "Usage: dvpn [options] [PEER_ID]\n"
195 "\nOptions:\n"
Amna77e27dc2024-07-24 13:09:40 -0400196 " -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"
Amna4e52b162024-01-14 21:16:57 -0500209 "\n");
210 return EXIT_SUCCESS;
211 }
212 if (params.version) {
Amnac03c4b52024-07-24 17:57:25 -0400213 Log("dvpn v1.0\n");
Amna4e52b162024-01-14 21:16:57 -0500214 return EXIT_SUCCESS;
215 }
216
Amnac03c4b52024-07-24 17:57:25 -0400217 Log("dvpn 1.0\n");
Amna41848a22024-01-22 16:22:57 -0500218
Amnac75ffe92024-02-08 17:23:29 -0500219 auto identity = dhtnet::loadIdentity(params.privateKey, params.cert);
Louis Maillard2da67252024-07-23 14:17:16 -0400220 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 }
Amnac03c4b52024-07-24 17:57:25 -0400224 Log("Loaded identity: {}\n", identity.second->getId());
Amna4e52b162024-01-14 21:16:57 -0500225
226 std::unique_ptr<dhtnet::Dvpn> dvpn;
227 if (params.listen) {
228 // create dvpn instance
Amnac75ffe92024-02-08 17:23:29 -0500229 dvpn = std::make_unique<dhtnet::DvpnServer>(identity,
Amna4e52b162024-01-14 21:16:57 -0500230 params.bootstrap,
231 params.turn_host,
232 params.turn_user,
233 params.turn_pass,
234 params.turn_realm,
Amna4325f0f2024-01-22 16:11:00 -0500235 params.configuration_file,
236 params.anonymous_cnx);
Amna4e52b162024-01-14 21:16:57 -0500237 } else {
238 dvpn = std::make_unique<dhtnet::DvpnClient>(params.peer_id,
Amna4e52b162024-01-14 21:16:57 -0500239 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}