-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
main.cpp
106 lines (87 loc) · 3.01 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
#include "common.h"
#include "network.h"
#include "connection.h"
#include "misc.h"
#include "log.h"
#include "lib/md5.h"
#include "encrypt.h"
#include "fd_manager.h"
void sigpipe_cb(struct ev_loop *l, ev_signal *w, int revents) {
mylog(log_info, "got sigpipe, ignored");
}
void sigterm_cb(struct ev_loop *l, ev_signal *w, int revents) {
mylog(log_info, "got sigterm, exit");
myexit(0);
}
void sigint_cb(struct ev_loop *l, ev_signal *w, int revents) {
mylog(log_info, "got sigint, exit");
myexit(0);
}
int client_event_loop();
int server_event_loop();
int main(int argc, char *argv[]) {
assert(sizeof(unsigned short) == 2);
assert(sizeof(unsigned int) == 4);
assert(sizeof(unsigned long long) == 8);
#ifdef UDP2RAW_MP
init_ws();
#endif
dup2(1, 2); // redirect stderr to stdout
#if defined(__MINGW32__)
enable_log_color = 0;
#endif
pre_process_arg(argc, argv);
ev_signal signal_watcher_sigpipe;
ev_signal signal_watcher_sigterm;
ev_signal signal_watcher_sigint;
if (program_mode == client_mode) {
struct ev_loop *loop = ev_default_loop(0);
#if !defined(__MINGW32__)
ev_signal_init(&signal_watcher_sigpipe, sigpipe_cb, SIGPIPE);
ev_signal_start(loop, &signal_watcher_sigpipe);
#endif
ev_signal_init(&signal_watcher_sigterm, sigterm_cb, SIGTERM);
ev_signal_start(loop, &signal_watcher_sigterm);
ev_signal_init(&signal_watcher_sigint, sigint_cb, SIGINT);
ev_signal_start(loop, &signal_watcher_sigint);
} else {
#ifdef UDP2RAW_LINUX
signal(SIGINT, signal_handler);
signal(SIGHUP, signal_handler);
signal(SIGKILL, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGQUIT, signal_handler);
#else
mylog(log_fatal, "server mode not supported in multi-platform version\n");
myexit(-1);
#endif
}
#if !defined(__MINGW32__)
if (geteuid() != 0) {
mylog(log_warn, "root check failed, it seems like you are using a non-root account. we can try to continue, but it may fail. If you want to run udp2raw as non-root, you have to add iptables rule manually, and grant udp2raw CAP_NET_RAW capability, check README.md in repo for more info.\n");
} else {
mylog(log_warn, "you can run udp2raw with non-root account for better security. check README.md in repo for more info.\n");
}
#endif
mylog(log_info, "remote_ip=[%s], make sure this is a vaild IP address\n", remote_addr.get_ip());
// init_random_number_fd();
srand(get_true_random_number_nz());
const_id = get_true_random_number_nz();
mylog(log_info, "const_id:%x\n", const_id);
my_init_keys(key_string, program_mode == client_mode ? 1 : 0);
iptables_rule();
#ifdef UDP2RAW_LINUX
init_raw_socket();
#endif
if (program_mode == client_mode) {
client_event_loop();
} else {
#ifdef UDP2RAW_LINUX
server_event_loop();
#else
mylog(log_fatal, "server mode not supported in multi-platform version\n");
myexit(-1);
#endif
}
return 0;
}