main.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "common.h"
  2. #include "network.h"
  3. #include "connection.h"
  4. #include "misc.h"
  5. #include "log.h"
  6. #include "lib/md5.h"
  7. #include "encrypt.h"
  8. #include "fd_manager.h"
  9. void sigpipe_cb(struct ev_loop *l, ev_signal *w, int revents)
  10. {
  11. mylog(log_info, "got sigpipe, ignored");
  12. }
  13. void sigterm_cb(struct ev_loop *l, ev_signal *w, int revents)
  14. {
  15. mylog(log_info, "got sigterm, exit");
  16. myexit(0);
  17. }
  18. void sigint_cb(struct ev_loop *l, ev_signal *w, int revents)
  19. {
  20. mylog(log_info, "got sigint, exit");
  21. myexit(0);
  22. }
  23. int client_event_loop();
  24. int main(int argc, char *argv[])
  25. {
  26. assert(sizeof(unsigned short)==2);
  27. assert(sizeof(unsigned int)==4);
  28. assert(sizeof(unsigned long long)==8);
  29. init_ws();
  30. dup2(1, 2);//redirect stderr to stdout
  31. #if defined(__MINGW32__)
  32. enable_log_color=0;
  33. #endif
  34. pre_process_arg(argc,argv);
  35. if(program_mode==client_mode)
  36. {
  37. struct ev_loop* loop=ev_default_loop(0);
  38. #if !defined(__MINGW32__)
  39. ev_signal signal_watcher_sigpipe;
  40. ev_signal_init(&signal_watcher_sigpipe, sigpipe_cb, SIGPIPE);
  41. ev_signal_start(loop, &signal_watcher_sigpipe);
  42. #endif
  43. ev_signal signal_watcher_sigterm;
  44. ev_signal_init(&signal_watcher_sigterm, sigterm_cb, SIGTERM);
  45. ev_signal_start(loop, &signal_watcher_sigterm);
  46. ev_signal signal_watcher_sigint;
  47. ev_signal_init(&signal_watcher_sigint, sigint_cb, SIGINT);
  48. ev_signal_start(loop, &signal_watcher_sigint);
  49. }
  50. else
  51. {
  52. mylog(log_fatal,"server mode not supported in multi-platform version\n");
  53. myexit(-1);
  54. /*
  55. signal(SIGINT, signal_handler);
  56. signal(SIGHUP, signal_handler);
  57. signal(SIGKILL, signal_handler);
  58. signal(SIGTERM, signal_handler);
  59. signal(SIGQUIT, signal_handler);
  60. */
  61. }
  62. #if !defined(__MINGW32__)
  63. if(geteuid() != 0)
  64. {
  65. 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");
  66. }
  67. else
  68. {
  69. mylog(log_warn,"you can run udp2raw with non-root account for better security. check README.md in repo for more info.\n");
  70. }
  71. #endif
  72. mylog(log_info,"remote_ip=[%s], make sure this is a vaild IP address\n",remote_addr.get_ip());
  73. //init_random_number_fd();
  74. srand(get_true_random_number_nz());
  75. const_id=get_true_random_number_nz();
  76. mylog(log_info,"const_id:%x\n",const_id);
  77. my_init_keys(key_string,program_mode==client_mode?1:0);
  78. iptables_rule();
  79. //init_raw_socket();
  80. //init_raw_socket() has to be done after dev dectection in mp version
  81. if(program_mode==client_mode)
  82. {
  83. client_event_loop();
  84. }
  85. else
  86. {
  87. mylog(log_fatal,"server mode not supported in multi-platform version\n");
  88. myexit(-1);
  89. /*
  90. server_event_loop();
  91. */
  92. }
  93. return 0;
  94. }