main.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 server_event_loop();
  25. int main(int argc, char *argv[])
  26. {
  27. assert(sizeof(unsigned short)==2);
  28. assert(sizeof(unsigned int)==4);
  29. assert(sizeof(unsigned long long)==8);
  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. signal(SIGINT, signal_handler);
  53. signal(SIGHUP, signal_handler);
  54. signal(SIGKILL, signal_handler);
  55. signal(SIGTERM, signal_handler);
  56. signal(SIGQUIT, signal_handler);
  57. }
  58. #if !defined(__MINGW32__)
  59. if(geteuid() != 0)
  60. {
  61. 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");
  62. }
  63. else
  64. {
  65. mylog(log_warn,"you can run udp2raw with non-root account for better security. check README.md in repo for more info.\n");
  66. }
  67. #endif
  68. mylog(log_info,"remote_ip=[%s], make sure this is a vaild IP address\n",remote_addr.get_ip());
  69. //init_random_number_fd();
  70. srand(get_true_random_number_nz());
  71. const_id=get_true_random_number_nz();
  72. mylog(log_info,"const_id:%x\n",const_id);
  73. my_init_keys(key_string,program_mode==client_mode?1:0);
  74. iptables_rule();
  75. init_raw_socket();
  76. if(program_mode==client_mode)
  77. {
  78. client_event_loop();
  79. }
  80. else
  81. {
  82. server_event_loop();
  83. }
  84. return 0;
  85. }