udp2raw/main.cpp

107 lines
3.0 KiB
C++
Raw Normal View History

2017-07-29 12:32:26 +00:00
#include "common.h"
#include "network.h"
2017-09-23 07:40:23 +00:00
#include "connection.h"
2017-09-23 08:35:28 +00:00
#include "misc.h"
2017-07-24 13:18:58 +00:00
#include "log.h"
#include "lib/md5.h"
2017-08-22 16:00:44 +00:00
#include "encrypt.h"
2017-10-30 12:21:27 +00:00
#include "fd_manager.h"
2017-07-26 10:00:45 +00:00
2023-02-07 12:11:49 +00:00
void sigpipe_cb(struct ev_loop *l, ev_signal *w, int revents) {
mylog(log_info, "got sigpipe, ignored");
}
2023-02-07 12:11:49 +00:00
void sigterm_cb(struct ev_loop *l, ev_signal *w, int revents) {
mylog(log_info, "got sigterm, exit");
myexit(0);
}
2023-02-07 12:11:49 +00:00
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();
2023-02-07 12:11:49 +00:00
int main(int argc, char *argv[]) {
assert(sizeof(unsigned short) == 2);
assert(sizeof(unsigned int) == 4);
assert(sizeof(unsigned long long) == 8);
2020-07-15 19:29:32 +00:00
#ifdef UDP2RAW_MP
2023-02-07 12:11:49 +00:00
init_ws();
2020-07-15 19:29:32 +00:00
#endif
2023-02-07 12:11:49 +00:00
dup2(1, 2); // redirect stderr to stdout
2018-08-29 10:01:30 +00:00
#if defined(__MINGW32__)
2023-02-07 12:11:49 +00:00
enable_log_color = 0;
2018-08-29 10:01:30 +00:00
#endif
2017-08-04 10:35:51 +00:00
2023-02-07 12:11:49 +00:00
pre_process_arg(argc, argv);
ev_signal signal_watcher_sigpipe;
ev_signal signal_watcher_sigterm;
ev_signal signal_watcher_sigint;
2023-02-07 12:11:49 +00:00
if (program_mode == client_mode) {
struct ev_loop *loop = ev_default_loop(0);
#if !defined(__MINGW32__)
2023-02-07 12:11:49 +00:00
ev_signal_init(&signal_watcher_sigpipe, sigpipe_cb, SIGPIPE);
ev_signal_start(loop, &signal_watcher_sigpipe);
#endif
2023-02-07 12:11:49 +00:00
ev_signal_init(&signal_watcher_sigterm, sigterm_cb, SIGTERM);
ev_signal_start(loop, &signal_watcher_sigterm);
2017-07-26 11:20:15 +00:00
2023-02-07 12:11:49 +00:00
ev_signal_init(&signal_watcher_sigint, sigint_cb, SIGINT);
ev_signal_start(loop, &signal_watcher_sigint);
} else {
2020-07-15 19:29:32 +00:00
#ifdef UDP2RAW_LINUX
2023-02-07 12:11:49 +00:00
signal(SIGINT, signal_handler);
signal(SIGHUP, signal_handler);
signal(SIGKILL, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGQUIT, signal_handler);
2020-07-15 19:29:32 +00:00
#else
2023-02-07 12:11:49 +00:00
mylog(log_fatal, "server mode not supported in multi-platform version\n");
myexit(-1);
2020-07-15 19:29:32 +00:00
#endif
2023-02-07 12:11:49 +00:00
}
#if !defined(__MINGW32__)
2023-02-07 12:11:49 +00:00
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
2017-07-26 11:20:15 +00:00
2023-02-07 12:11:49 +00:00
mylog(log_info, "remote_ip=[%s], make sure this is a vaild IP address\n", remote_addr.get_ip());
2017-07-28 16:22:26 +00:00
2023-02-07 12:11:49 +00:00
// init_random_number_fd();
srand(get_true_random_number_nz());
const_id = get_true_random_number_nz();
2023-02-07 12:11:49 +00:00
mylog(log_info, "const_id:%x\n", const_id);
2017-07-11 10:01:11 +00:00
2023-02-07 12:11:49 +00:00
my_init_keys(key_string, program_mode == client_mode ? 1 : 0);
iptables_rule();
2017-07-24 03:31:21 +00:00
2020-07-15 19:29:32 +00:00
#ifdef UDP2RAW_LINUX
2023-02-07 12:11:49 +00:00
init_raw_socket();
2020-07-15 19:29:32 +00:00
#endif
2023-02-07 12:11:49 +00:00
if (program_mode == client_mode) {
client_event_loop();
} else {
2020-07-15 19:29:32 +00:00
#ifdef UDP2RAW_LINUX
2023-02-07 12:11:49 +00:00
server_event_loop();
2020-07-15 19:29:32 +00:00
#else
2023-02-07 12:11:49 +00:00
mylog(log_fatal, "server mode not supported in multi-platform version\n");
myexit(-1);
2020-07-15 19:29:32 +00:00
#endif
2023-02-07 12:11:49 +00:00
}
2017-07-11 10:01:11 +00:00
2023-02-07 12:11:49 +00:00
return 0;
2017-07-11 10:01:11 +00:00
}