main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "os"
  8. "os/signal"
  9. "syscall"
  10. "time"
  11. "github.com/google/uuid"
  12. "github.com/gorilla/csrf"
  13. "imuslab.com/zoraxy/mod/update"
  14. "imuslab.com/zoraxy/mod/utils"
  15. )
  16. // Kill signal handler. Do something before the system the core terminate.
  17. func SetupCloseHandler() {
  18. c := make(chan os.Signal, 2)
  19. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  20. go func() {
  21. <-c
  22. ShutdownSeq()
  23. os.Exit(0)
  24. }()
  25. }
  26. func ShutdownSeq() {
  27. SystemWideLogger.Println("Shutting down " + SYSTEM_NAME)
  28. //SystemWideLogger.Println("Closing GeoDB")
  29. //geodbStore.Close()
  30. SystemWideLogger.Println("Closing Netstats Listener")
  31. netstatBuffers.Close()
  32. SystemWideLogger.Println("Closing Statistic Collector")
  33. statisticCollector.Close()
  34. if mdnsTickerStop != nil {
  35. SystemWideLogger.Println("Stopping mDNS Discoverer (might take a few minutes)")
  36. // Stop the mdns service
  37. mdnsTickerStop <- true
  38. }
  39. mdnsScanner.Close()
  40. SystemWideLogger.Println("Shutting down load balancer")
  41. loadBalancer.Close()
  42. SystemWideLogger.Println("Closing Certificates Auto Renewer")
  43. acmeAutoRenewer.Close()
  44. //Remove the tmp folder
  45. SystemWideLogger.Println("Cleaning up tmp files")
  46. os.RemoveAll("./tmp")
  47. //Close database
  48. SystemWideLogger.Println("Stopping system database")
  49. sysdb.Close()
  50. //Close logger
  51. SystemWideLogger.Println("Closing system wide logger")
  52. SystemWideLogger.Close()
  53. }
  54. func main() {
  55. //Parse startup flags
  56. flag.Parse()
  57. if *showver {
  58. fmt.Println(SYSTEM_NAME + " - Version " + SYSTEM_VERSION)
  59. os.Exit(0)
  60. }
  61. if !utils.ValidateListeningAddress(*webUIPort) {
  62. fmt.Println("Malformed -port (listening address) paramter. Do you mean -port=:" + *webUIPort + "?")
  63. os.Exit(0)
  64. }
  65. if *enableAutoUpdate {
  66. fmt.Println("Checking required config update")
  67. update.RunConfigUpdate(0, update.GetVersionIntFromVersionNumber(SYSTEM_VERSION))
  68. }
  69. SetupCloseHandler()
  70. //Read or create the system uuid
  71. uuidRecord := "./sys.uuid"
  72. if !utils.FileExists(uuidRecord) {
  73. newSystemUUID := uuid.New().String()
  74. os.WriteFile(uuidRecord, []byte(newSystemUUID), 0775)
  75. }
  76. uuidBytes, err := os.ReadFile(uuidRecord)
  77. if err != nil {
  78. SystemWideLogger.PrintAndLog("ZeroTier", "Unable to read system uuid from file system", nil)
  79. panic(err)
  80. }
  81. nodeUUID = string(uuidBytes)
  82. //Create a new webmin mux and csrf middleware layer
  83. webminPanelMux = http.NewServeMux()
  84. csrfMiddleware = csrf.Protect(
  85. []byte(nodeUUID),
  86. csrf.CookieName("zoraxy-csrf"),
  87. csrf.Secure(false),
  88. csrf.Path("/"),
  89. csrf.SameSite(csrf.SameSiteLaxMode),
  90. )
  91. //Startup all modules
  92. startupSequence()
  93. //Initiate management interface APIs
  94. requireAuth = !(*noauth)
  95. initAPIs(webminPanelMux)
  96. //Start the reverse proxy server in go routine
  97. go func() {
  98. ReverseProxtInit()
  99. }()
  100. time.Sleep(500 * time.Millisecond)
  101. //Start the finalize sequences
  102. finalSequence()
  103. SystemWideLogger.Println("Zoraxy started. Visit control panel at http://localhost" + *webUIPort)
  104. err = http.ListenAndServe(*webUIPort, csrfMiddleware(webminPanelMux))
  105. if err != nil {
  106. log.Fatal(err)
  107. }
  108. }