wangyu-udp2raw/main.cpp

112 lines
2.8 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
2018-06-12 10:08:41 -05: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();
2017-07-26 19:47:52 +08:00
int main(int argc, char *argv[])
{
2018-06-19 00:15:58 +08:00
assert(sizeof(unsigned short)==2);
assert(sizeof(unsigned int)==4);
2018-06-19 00:36:53 +08:00
assert(sizeof(unsigned long long)==8);
2018-06-18 21:50:57 +08:00
init_ws();
2018-06-17 00:35:31 +08:00
int i=0;
2018-06-12 10:08:41 -05:00
//libnet_t *l; /* the libnet context */
//char errbuf[LIBNET_ERRBUF_SIZE];
2018-06-09 09:04:00 -05:00
2018-06-12 10:08:41 -05:00
//l = libnet_init(LIBNET_RAW4, NULL, errbuf);
2018-06-09 09:04:00 -05:00
2017-07-29 20:32:26 +08:00
dup2(1, 2);//redirect stderr to stdout
2018-06-12 10:08:41 -05:00
//signal(SIGINT, signal_handler);
//signal(SIGHUP, signal_handler);
//signal(SIGKILL, signal_handler);
//signal(SIGTERM, signal_handler);
//signal(SIGQUIT, signal_handler);
struct ev_loop* loop=ev_default_loop(0);
2018-06-19 17:19:56 +08:00
//printf("%x %x\n",ev_supported_backends(),ev_backend(loop));
2018-06-18 21:50:57 +08:00
#if !defined(__MINGW32__)
2018-06-12 10:08:41 -05:00
ev_signal signal_watcher_sigpipe;
ev_signal_init(&signal_watcher_sigpipe, sigpipe_cb, SIGPIPE);
ev_signal_start(loop, &signal_watcher_sigpipe);
2018-06-20 00:41:13 +08:00
#else
enable_log_color=0;
2018-06-18 21:50:57 +08:00
#endif
2018-06-12 10:08:41 -05:00
ev_signal signal_watcher_sigterm;
ev_signal_init(&signal_watcher_sigterm, sigterm_cb, SIGTERM);
ev_signal_start(loop, &signal_watcher_sigterm);
ev_signal signal_watcher_sigint;
ev_signal_init(&signal_watcher_sigint, sigint_cb, SIGINT);
ev_signal_start(loop, &signal_watcher_sigint);
2017-08-04 18:35:51 +08:00
2017-08-23 07:01:21 -05:00
pre_process_arg(argc,argv);
2018-06-18 21:50:57 +08:00
#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");
}
2018-06-18 21:50:57 +08:00
#endif
local_ip_uint32=inet_addr(local_ip);
source_ip_uint32=inet_addr(source_ip);
2018-06-14 08:01:03 -05:00
2018-06-06 19:50:42 -05:00
strcpy(remote_ip,remote_address);
2018-06-14 08:01:03 -05:00
mylog(log_info,"remote_ip=[%s], make sure this is a vaild IP address\n",remote_ip);
2018-06-06 19:50:42 -05:00
remote_ip_uint32=inet_addr(remote_ip);
2018-06-05 12:57:25 -05:00
2018-06-18 21:50:57 +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
2018-06-23 17:12:04 -05: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();
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
{
2018-06-23 12:08:28 +08:00
mylog(log_fatal,"server mode not supported in multi-platform version\n");
2018-06-06 19:50:42 -05:00
myexit(-1);
//server_event_loop();
2017-07-11 18:01:11 +08:00
}
return 0;
}