blob: 8cbeebc208b74d1e7cae2c4ec3e24375a7a1e970 [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};
40 std::filesystem::path path {};
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 {};
Amna41848a22024-01-22 16:22:57 -050048 std::string ca {};
49 std::string dvpn_configuration_file {};
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'},
58 {"id_path", required_argument, nullptr, 'I'},
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'},
Amna41848a22024-01-22 16:22:57 -050063 {"vpn_configuration_file", required_argument, nullptr, 'c'},
64 {"CA", required_argument, nullptr, 'C'},
65 {"dvpn_configuration_file", 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;
Amna41848a22024-01-22 16:22:57 -050074 while ((opt = getopt_long(argc, argv, "hvlw:r:u:t:I:b: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;
85 case 'b':
86 params.bootstrap = optarg;
87 break;
88 case 'I':
89 params.path = optarg;
90 break;
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.configuration_file = optarg;
105 break;
Amna41848a22024-01-22 16:22:57 -0500106 case 'C':
107 params.ca = optarg;
108 break;
109 case 'd':
110 params.dvpn_configuration_file = optarg;
111 break;
Amna4325f0f2024-01-22 16:11:00 -0500112 case 'a':
113 params.anonymous_cnx = true;
114 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
121 if (!params.dvpn_configuration_file.empty()) {
122 printf("read configuration file: %s\n", params.dvpn_configuration_file.c_str());
123 std::ifstream config_file(params.dvpn_configuration_file);
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 }
131 if (config["id_path"] && params.path.empty()) {
132 params.path = config["id_path"].as<std::string>();
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 }
146 if (config["CA"] && params.ca.empty()) {
147 params.ca = config["CA"].as<std::string>();
148 }
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) {
182 fmt::print("{}", std::string_view(data, len));
183 });
184}
185
186int
187main(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"
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 Specify the bootstrap option with an argument.\n"
200 " -I, --id_path Specify the id_path option with an argument.\n"
201 " -t, --turn_host Specify the turn_host option with an argument.\n"
202 " -u, --turn_user Specify the turn_user option with an argument.\n"
203 " -w, --turn_pass Specify the turn_pass option with an argument.\n"
204 " -r, --turn_realm Specify the turn_realm option with an argument.\n"
Amna41848a22024-01-22 16:22:57 -0500205 " -c, --vpn_configuration_file Specify the vpn_configuration_file path option with an argument.\n"
206 " -C, --CA Specify the CA path option with an argument.\n"
207 " -d, --dvpn_configuration_file Specify the dvpn_configuration_file path option with an argument.\n"
Amna4325f0f2024-01-22 16:11:00 -0500208 " -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) {
213 fmt::print("dvpn v1.0\n");
214 return EXIT_SUCCESS;
215 }
216
217 fmt::print("dvpn 1.0\n");
Amna41848a22024-01-22 16:22:57 -0500218
219 auto identity = dhtnet::loadIdentity(params.path, params.ca);
Amna4e52b162024-01-14 21:16:57 -0500220 fmt::print("Loaded identity: {} from {}\n", identity.second->getId(), params.path);
221
222 std::unique_ptr<dhtnet::Dvpn> dvpn;
223 if (params.listen) {
224 // create dvpn instance
225 dvpn = std::make_unique<dhtnet::DvpnServer>(params.path,
226 identity,
227 params.bootstrap,
228 params.turn_host,
229 params.turn_user,
230 params.turn_pass,
231 params.turn_realm,
Amna4325f0f2024-01-22 16:11:00 -0500232 params.configuration_file,
233 params.anonymous_cnx);
Amna4e52b162024-01-14 21:16:57 -0500234 } else {
235 dvpn = std::make_unique<dhtnet::DvpnClient>(params.peer_id,
236 params.path,
237 identity,
238 params.bootstrap,
239 params.turn_host,
240 params.turn_user,
241 params.turn_pass,
242 params.turn_realm,
243 params.configuration_file);
244 }
245 dvpn->run();
246 return EXIT_SUCCESS;
247}