forked from gitter-badger/toxvpn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
625 lines (512 loc) · 18.5 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define _POSIX_C_SOURCE 200900
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <limits.h>
#include <string>
#include <cstdarg>
#include "toxvpn.h"
#include "util.h"
#define DEFAULT_BOOTSTRAP_NODE "144.76.60.215:33445:04119E835DF3E78BACF0F84235B300546AF8B936F035185E2A8E9E0A67C8924F"
#define DEFAULT_SUBNET "10.0.100.0/24"
using std::string;
inline void trace(const char* formator, ...) {
FILE *file = stderr;
va_list al;
va_start(al, formator);
vfprintf(file, formator, al);
fprintf(file, "\n");
}
#define min(x, y) (x < y ? x : y)
#define SECRET_SIZE 32
static const char *help_message = \
"-h\t\tshow help\n" \
"-s <secret>\t use passed hex secret\n" \
"-p <proxy>\t use socks|http://hostname:port> proxy server\n" \
"-n <subnetwork/prefix length>\n" \
"-b <IP:port:tox_address>\tuse bootstap node\n" \
"-c <address>:<secret>\t connect to server using tox address\n" \
"-f <file>\tload/save settings from/to file\n";
enum OptionFlags {
SUBNET_SET = 1,
BOOTSTRAP_NODE_SET = 2,
SECRET_SET = 4,
NAME_SET = 8,
ADDRESS_SET = 16,
SERVER_ADDRESS_SET = 32,
CLIENT_MODE_SET = 64,
PROXY_SET = 128,
SETTINGS_FILE_SET = 256,
};
struct ApplicationContext{
ApplicationContext()
{
parse_bootstrap_node(DEFAULT_BOOTSTRAP_NODE);
parse_subnet(DEFAULT_SUBNET);
}
char *subnet;
uint16_t prefixlen;
uint8_t secret[SECRET_SIZE];
const uint8_t *name;
size_t name_size;
uint8_t self_address[TOX_ADDRESS_SIZE];
uint8_t server_address[TOX_ADDRESS_SIZE];
uint32_t toxvpn_id;
char *settings_path_pattern;
struct {
char *host;
uint16_t port;
TOX_PROXY_TYPE type;
} proxy;
struct {
char *host;
uint16_t port;
uint8_t *pk;
} dht_bootstrap_node;
int options_mask;
ToxVPNContext *vpn_context = nullptr;
bool running = true;
string get_secret_representation() const
{
size_t first_zero_byte_pos = (size_t) -1;
for (size_t i = 0; i < sizeof(secret); i++) {
if (secret[i] == 0) {
if (first_zero_byte_pos == (size_t) -1) {
first_zero_byte_pos = i;
}
}
else {
first_zero_byte_pos = (size_t) -1;
}
}
if (first_zero_byte_pos != (size_t) -1) {
char* secret_str = bin_to_hex_str(secret, first_zero_byte_pos);
string result(secret_str);
free(secret_str);
return result;
}
return string();
}
void print_usage(int argc, char **argv) const
{
fprintf(stderr, "usage: %s options", argv[0]);
fprintf(stderr, help_message);
}
int check_arguments() const
{
if (options_mask & CLIENT_MODE_SET)
{
const int client_mask = CLIENT_MODE_SET | SERVER_ADDRESS_SET;
return ((options_mask & client_mask) == client_mask);
}
else
{
const int server_mask = 0;
return ((options_mask & server_mask) == server_mask);
}
}
int parse_bootstrap_node(const char *arg)
{
char *arg_dup = strdup(arg);
static const char *delim= ":";
char *token = strtok(arg_dup, delim);
if (!token) {
goto getout;
}
dht_bootstrap_node.host = strdup(token);
token = strtok(NULL, delim);
if (!token) {
goto getout;
}
dht_bootstrap_node.port = atoi(token);
token = strtok(NULL, delim);
if (!token) {
goto getout;
}
if (strlen(token)/2 != TOX_PUBLIC_KEY_SIZE) {
fprintf(stderr, "invlalid public key size: %lu\n", strlen(token)/2);
goto getout;
}
dht_bootstrap_node.pk = hex_string_to_bin(token);
getout:
free(arg_dup);
return token == NULL ? -1 : 0;
}
int parse_subnet(const char *addr)
{
char *stringp = strdup(addr);
char *token = strtok(stringp, "/");
if (token != NULL) {
subnet = strdup(token);
}
else {
free(stringp);
return 1;
}
token = strtok(NULL, "/");
if (token != NULL) {
prefixlen = atoi(token);
}
else {
prefixlen = 24;
}
free(stringp);
return 0;
}
int parse_proxy(const char *arg)
{
char *arg_dup = strdup(arg);
static const char *delim= ":/";
char *token = strtok(arg_dup, delim);
if (!token) {
goto getout;
}
this->proxy.type = strcmp(token, "socks") == 0 ? TOX_PROXY_TYPE_SOCKS5 : TOX_PROXY_TYPE_HTTP;
if (!token) {
goto getout;
}
this->proxy.host = strdup(token);
token = strtok(NULL, delim);
if (!token) {
goto getout;
}
this->proxy.port = atoi(token);
getout:
free(arg_dup);
return token == NULL ? -1 : 0;
}
int parse_arguments(int argc, char **argv)
{
ApplicationContext *app_context = this;
uint8_t *converted;
int c;
while ((c = getopt (argc, argv, "hn:p:s:c:b:f:")) != -1) {
switch (c)
{
case 'p':
if (!parse_proxy(optarg)) {
app_context->options_mask |= PROXY_SET;
}
else {
fprintf(stderr, "invalid proxy server\n");
return -2;
}
break;
case 'n':
if (!parse_subnet(optarg)) {
app_context->options_mask |= SUBNET_SET;
}
else {
fprintf(stderr, "can't parse subnet address/prefix %s %hu\n", app_context->subnet, app_context->prefixlen);
return -2;
}
break;
case 'b':
if (parse_bootstrap_node(optarg)) {
fprintf(stderr, "can't parse bootstrap node address\n");
return -2;
}
else {
app_context->options_mask |= BOOTSTRAP_NODE_SET;
}
case 's':
bzero(app_context->secret, sizeof app_context->secret);
converted = hex_string_to_bin(optarg);
memcpy(app_context->secret, converted, min(sizeof(app_context->secret), strlen(optarg)/2));
free(converted);
app_context->options_mask |= SECRET_SET;
break;
case 'f':
app_context->settings_path_pattern = optarg;
app_context->options_mask |= SETTINGS_FILE_SET;
break;
case 'c':
{
if (strlen(optarg)/2 < TOX_ADDRESS_SIZE)
{
fprintf(stderr, "Invalid server node address size: %lu\n", strlen(optarg)/2);
return -2;
}
static const char* delim = ":";
char* arg_dup = strdup(optarg);
char* token = strtok(arg_dup, delim);
assert(token);
converted = hex_string_to_bin(token);
memcpy(app_context->server_address, converted, sizeof(app_context->server_address));
app_context->options_mask |= (SERVER_ADDRESS_SET | CLIENT_MODE_SET);
token = strtok(NULL, delim);
if (token) {
options_mask |= SECRET_SET;
uint8_t *secret_data = hex_string_to_bin(token);
memcpy(secret, secret_data, strlen(token) / 2);
free(secret_data);
}
free(arg_dup);
free(converted);
break;
}
case 'h':
return 1;
case '?':
return 1;
default:
return -1;
}
}
if (!check_arguments())
{
fprintf(stderr, "Not all arguments supplied correctly: %X\n", app_context->options_mask);
return 2;
}
else
{
return 0;
}
}
} app_context;
void singnal_handler(int signal_code)
{
switch (signal_code) {
case SIGINT:
case SIGTERM:
app_context.running = false;
break;
default:
trace("received unexpected signal %d", signal_code);
break;
}
}
static void on_fiend_connection_status_cnaged(Tox *tox, uint32_t friend_id, TOX_CONNECTION connection_status, void *user_data)
{
assert(user_data);
struct ApplicationContext *options = (struct ApplicationContext *) user_data;
if (!(options->options_mask & CLIENT_MODE_SET)) {
if (connection_status != TOX_CONNECTION_NONE) {
if (toxvpn_request_membership(options->vpn_context, options->toxvpn_id, friend_id, TOXVPN_MEMBERSHIP_ACCEPT) == TOX_ERR_FRIEND_CUSTOM_PACKET_OK) {
tox_trace(tox, "toxvpn invite has been sent to friend %u", friend_id);
}
else {
tox_trace(tox, "can't send membership request to friend %d", friend_id);
}
}
}
}
static void on_accept_friend_request(Tox *tox, const uint8_t *pk, const uint8_t *data, size_t length, void *userdata)
{
char *secret_str = bin_to_hex_str(data, length);
tox_trace(tox, "received Tox friend request from %lX with attached secret \"%s\"", *((uint64_t*) pk), secret_str);
if (length == sizeof(app_context.secret) && memcmp(app_context.secret, data, sizeof app_context.secret) == 0) {
TOX_ERR_FRIEND_ADD error;
uint32_t friendnumber = tox_friend_add_norequest(tox, pk, &error);
char *pk_str = bin_to_hex_str(pk, TOX_PUBLIC_KEY_SIZE);
tox_trace(tox, "Approved friend %u with PK %s", friendnumber, pk_str);
free(pk_str);
}
else
{
tox_trace(tox, "Secrets doesn't match");
}
free(secret_str);
}
static void on_membership_request(ToxVPNContext *context, int32_t toxvpn_id, int32_t friendnumber, uint8_t flags, void *userdata)
{
// ApplicationContext *options = static_cast<ApplicationContext *>(userdata);
tox_trace(context->tox, "received request - toxvpn_id: %X, friendnumber: %d, flags: %X", toxvpn_id, friendnumber, flags);
toxvpn_response_membership(context, toxvpn_id, friendnumber, TOXVPN_MEMBERSHIP_ACCEPT);
}
static void on_membership_response(ToxVPNContext *context, int32_t toxvpn_id, int32_t friendnumber, uint8_t flags, void *userdata)
{
tox_trace(context->tox, "Received membership response - toxvpn_id: %d, friendnumber: %d, flags: %X", toxvpn_id, friendnumber, flags);
}
size_t file_get_size(FILE *file)
{
const size_t pos = ftell(file);
fseek(file, 0, SEEK_END);
const size_t size = ftell(file);
fseek(file, pos, SEEK_SET);
return size;
}
static const char* settings_get_toxcore_config_path(const char *path)
{
static char settings_path[256];
sprintf(settings_path, "%s.toxcore", path);
return settings_path;
}
static const char* settings_get_toxvpn_config_path(const char *path)
{
static char settings_path[256];
sprintf(settings_path, "%s.toxvpn", path);
return settings_path;
}
Tox* create_tox_context(struct ApplicationContext *options)
{
size_t settings_size = 0;
uint8_t *settings_data = NULL;
struct Tox_Options *tox_options = tox_options_new(NULL);
tox_options_default(tox_options);
if (options->options_mask & SETTINGS_FILE_SET) {
assert(strlen(options->settings_path_pattern) > 0);
FILE *file = fopen(settings_get_toxcore_config_path(options->settings_path_pattern), "rb+");
if (file != NULL) {
settings_size = file_get_size(file);
settings_data = new uint8_t[settings_size];
if (fread(settings_data, 1, settings_size, file) != settings_size) {
free(settings_data);
settings_data = NULL;
settings_size = 0;
}
fclose(file);
}
}
if (options->options_mask & PROXY_SET) {
tox_trace(NULL, "proxy settings - host: %s, port: %hu, type: %d", options->proxy.host, (uint16_t) options->proxy.port, options->proxy.type);
tox_options->proxy_host = options->proxy.host;
tox_options->proxy_port = options->proxy.port;
tox_options->proxy_type = options->proxy.type;
}
tox_options->savedata_type = (settings_data == 0 && settings_size == 0) ? TOX_SAVEDATA_TYPE_NONE : TOX_SAVEDATA_TYPE_TOX_SAVE;
tox_options->savedata_data = settings_data;
tox_options->savedata_length = settings_size;
TOX_ERR_NEW error;
Tox *tox = tox_new(tox_options, &error);
if (tox == nullptr) {
trace("can't create toxcore context: %d", int(error));
return nullptr;
}
free(settings_data);
tox_callback_friend_request(tox, on_accept_friend_request, options);
tox_callback_friend_connection_status(tox, on_fiend_connection_status_cnaged, options);
tox_self_get_address(tox, options->self_address);
return tox;
}
ToxVPNContext* create_vpn_context(Tox *tox, ApplicationContext *context)
{
ToxVPNContext *vpn_context = toxvpn_create_context(tox);
assert(vpn_context);
context->vpn_context = vpn_context;
toxvpn_callback_membership_request(context->vpn_context, on_membership_request, context);
toxvpn_callback_membership_response(context->vpn_context, on_membership_response, context);
if (context->options_mask & SETTINGS_FILE_SET) {
assert(strlen(context->settings_path_pattern) > 0);
FILE *file = fopen(settings_get_toxvpn_config_path(context->settings_path_pattern), "rb+");
if (file != NULL) {
size_t settings_size = file_get_size(file);
uint8_t *settings_data = new uint8_t[settings_size];
toxvpn_settings_load(vpn_context, settings_data, settings_size);
fclose(file);
free(settings_data);
}
}
return vpn_context;
}
bool settings_save(const ApplicationContext *context, const char *filename)
{
FILE *file = fopen(settings_get_toxcore_config_path(filename), "wb");
if (!file) {
return false;
}
size_t size = tox_get_savedata_size(context->vpn_context->tox);
uint8_t *savedata = new uint8_t[size];
tox_get_savedata(context->vpn_context->tox, savedata);
const bool status = fwrite(savedata, size, 1, file) != size;
free(savedata);
fclose(file);
file = fopen(settings_get_toxvpn_config_path(filename), "w");
if (!file) {
return false;
}
char *toxpvn_json_settings = toxvpn_settings_dump(context->vpn_context);
fprintf(file, "%s", toxpvn_json_settings);
free(toxpvn_json_settings);
return status;
}
int main(int argc, char *argv[])
{
signal(SIGINT, singnal_handler);
signal(SIGTERM, singnal_handler);
if (app_context.parse_arguments(argc, argv) != 0)
{
app_context.print_usage(argc, argv);
return 0;
}
Tox *tox = create_tox_context(&app_context);
assert(tox);
if (app_context.options_mask & NAME_SET) {
tox_self_set_name(tox, app_context.name, app_context.name_size, NULL);
}
ToxVPNContext *vpn_context = create_vpn_context(tox, &app_context);
{
char *address_str = bin_to_hex_str(app_context.self_address, sizeof(app_context.self_address));
trace("connect address %s:%s", address_str, app_context.get_secret_representation().c_str());
free(address_str);
}
app_context.options_mask |= ADDRESS_SET;
if (app_context.options_mask & BOOTSTRAP_NODE_SET) {
assert(app_context.dht_bootstrap_node.pk);
TOX_ERR_BOOTSTRAP error;
char *bs_node_address_str = bin_to_hex_str(app_context.dht_bootstrap_node.pk, TOX_PUBLIC_KEY_SIZE);
tox_trace(tox, "dht bootstrap node is %s:%hu, tox address: %s", app_context.dht_bootstrap_node.host, app_context.dht_bootstrap_node.port, bs_node_address_str);
if (!tox_bootstrap(tox, app_context.dht_bootstrap_node.host, app_context.dht_bootstrap_node.port, app_context.dht_bootstrap_node.pk, &error)) {
tox_trace(tox, "can't use bootstap node: %d", error);
}
free(bs_node_address_str);
}
if (!(app_context.options_mask & CLIENT_MODE_SET)) { //server node logic here
app_context.toxvpn_id = toxvpn_new(vpn_context, app_context.subnet, app_context.prefixlen);
if (app_context.toxvpn_id == UINT32_MAX)
{
tox_trace(tox, "can't create toxvpn interface");
return -20;
}
} else {
TOX_ERR_FRIEND_ADD error;
if (tox_friend_add(tox, app_context.server_address, app_context.secret, sizeof(app_context.secret), &error) == UINT32_MAX) {
if (error != TOX_ERR_FRIEND_ADD_ALREADY_SENT) {
tox_trace(tox, "Can't add a server node: %d", error);
return -30;
}
}
char *server_address_str = bin_to_hex_str(app_context.server_address, sizeof(app_context.server_address));
tox_trace(tox, "added node %s", server_address_str);
free(server_address_str);
}
bool connected_to_dht = 0;
int approved = 0;
while (app_context.running) {
tox_iterate(tox);
toxvpn_events_loop(vpn_context);
if (!connected_to_dht) {
connected_to_dht = tox_self_get_connection_status(tox) != TOX_CONNECTION_NONE;
if (connected_to_dht) {
tox_trace(tox, "connected to dht node");
}
}
if (app_context.options_mask & CLIENT_MODE_SET) { //client node logic here
if (!approved && tox_friend_get_connection_status(tox, 0, NULL) != TOX_CONNECTION_NONE) {
approved = 1;
tox_trace(tox, "Friend %d connected", 0);
}
}
usleep(300);
}
if (app_context.options_mask & SETTINGS_FILE_SET) {
if (settings_save(&app_context, app_context.settings_path_pattern)) {
tox_trace(tox, "Settings has been saved to \"%s\"", app_context.settings_path_pattern);
}
else {
tox_trace(tox, "Can't save settings to \"%s\": %s", app_context.settings_path_pattern, strerror(errno));
}
}
tox_kill(tox);
return 0;
}