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"
2017-08-11 12:25:04 +08:00
# 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
2023-02-07 07:11:49 -05:00
void sigpipe_cb ( struct ev_loop * l , ev_signal * w , int revents ) {
mylog ( log_info , " got sigpipe, ignored " ) ;
2018-08-29 04:01:37 -05:00
}
2023-02-07 07:11:49 -05:00
void sigterm_cb ( struct ev_loop * l , ev_signal * w , int revents ) {
mylog ( log_info , " got sigterm, exit " ) ;
myexit ( 0 ) ;
2018-08-29 04:01:37 -05:00
}
2023-02-07 07:11:49 -05:00
void sigint_cb ( struct ev_loop * l , ev_signal * w , int revents ) {
mylog ( log_info , " got sigint, exit " ) ;
myexit ( 0 ) ;
2018-08-29 04:01:37 -05:00
}
2018-08-29 01:45:57 -05:00
int client_event_loop ( ) ;
int server_event_loop ( ) ;
2017-08-21 20:26:55 +08:00
2023-02-07 07:11:49 -05:00
int main ( int argc , char * argv [ ] ) {
assert ( sizeof ( unsigned short ) = = 2 ) ;
assert ( sizeof ( unsigned int ) = = 4 ) ;
assert ( sizeof ( unsigned long long ) = = 8 ) ;
2018-08-29 04:01:37 -05:00
2020-07-15 15:29:32 -04:00
# ifdef UDP2RAW_MP
2023-02-07 07:11:49 -05:00
init_ws ( ) ;
2020-07-15 15:29:32 -04:00
# endif
2023-02-07 07:11:49 -05:00
dup2 ( 1 , 2 ) ; // redirect stderr to stdout
2018-08-29 05:01:30 -05:00
# if defined(__MINGW32__)
2023-02-07 07:11:49 -05:00
enable_log_color = 0 ;
2018-08-29 05:01:30 -05:00
# endif
2017-08-04 18:35:51 +08:00
2023-02-07 07:11:49 -05:00
pre_process_arg ( argc , argv ) ;
2018-08-29 14:26:34 -05:00
2023-02-07 07:11:49 -05:00
if ( program_mode = = client_mode ) {
struct ev_loop * loop = ev_default_loop ( 0 ) ;
2018-08-29 04:01:37 -05:00
# if !defined(__MINGW32__)
2023-02-07 07:11:49 -05:00
ev_signal signal_watcher_sigpipe ;
ev_signal_init ( & signal_watcher_sigpipe , sigpipe_cb , SIGPIPE ) ;
ev_signal_start ( loop , & signal_watcher_sigpipe ) ;
2018-08-29 04:01:37 -05:00
# endif
2023-02-07 07:11:49 -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
2023-02-07 07:11:49 -05:00
ev_signal signal_watcher_sigint ;
ev_signal_init ( & signal_watcher_sigint , sigint_cb , SIGINT ) ;
ev_signal_start ( loop , & signal_watcher_sigint ) ;
} else {
2020-07-15 15:29:32 -04:00
# ifdef UDP2RAW_LINUX
2023-02-07 07:11:49 -05:00
signal ( SIGINT , signal_handler ) ;
signal ( SIGHUP , signal_handler ) ;
signal ( SIGKILL , signal_handler ) ;
signal ( SIGTERM , signal_handler ) ;
signal ( SIGQUIT , signal_handler ) ;
2020-07-15 15:29:32 -04:00
# else
2023-02-07 07:11:49 -05:00
mylog ( log_fatal , " server mode not supported in multi-platform version \n " ) ;
myexit ( - 1 ) ;
2020-07-15 15:29:32 -04:00
# endif
2023-02-07 07:11:49 -05:00
}
2018-08-29 04:01:37 -05:00
# if !defined(__MINGW32__)
2023-02-07 07:11:49 -05: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 " ) ;
}
2018-06-05 11:26:55 -05:00
# endif
2017-07-26 19:20:15 +08:00
2023-02-07 07:11:49 -05: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
2023-02-07 07:11:49 -05:00
// init_random_number_fd();
srand ( get_true_random_number_nz ( ) ) ;
const_id = get_true_random_number_nz ( ) ;
2017-07-19 06:05:08 +08:00
2023-02-07 07:11:49 -05:00
mylog ( log_info , " const_id:%x \n " , const_id ) ;
2017-07-11 18:01:11 +08:00
2023-02-07 07:11:49 -05:00
my_init_keys ( key_string , program_mode = = client_mode ? 1 : 0 ) ;
iptables_rule ( ) ;
2017-07-24 11:31:21 +08:00
2020-07-15 15:29:32 -04:00
# ifdef UDP2RAW_LINUX
2023-02-07 07:11:49 -05:00
init_raw_socket ( ) ;
2020-07-15 15:29:32 -04:00
# endif
2017-08-21 20:26:55 +08:00
2023-02-07 07:11:49 -05:00
if ( program_mode = = client_mode ) {
client_event_loop ( ) ;
} else {
2020-07-15 15:29:32 -04:00
# ifdef UDP2RAW_LINUX
2023-02-07 07:11:49 -05:00
server_event_loop ( ) ;
2020-07-15 15:29:32 -04:00
# else
2023-02-07 07:11:49 -05:00
mylog ( log_fatal , " server mode not supported in multi-platform version \n " ) ;
myexit ( - 1 ) ;
2020-07-15 15:29:32 -04:00
# endif
2023-02-07 07:11:49 -05:00
}
2017-07-11 18:01:11 +08:00
2023-02-07 07:11:49 -05:00
return 0 ;
2017-07-11 18:01:11 +08:00
}