wangyu-udp2raw/main.cpp

103 lines
2.5 KiB
C++
Raw Normal View History

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