main.cpp 3.0 KB

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