main.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package main
  2. /*
  3. ______
  4. |___ /
  5. / / ___ _ __ __ ___ ___ _
  6. / / / _ \| '__/ _` \ \/ / | | |
  7. / /_| (_) | | | (_| |> <| |_| |
  8. /_____\___/|_| \__,_/_/\_\\__, |
  9. __/ |
  10. |___/
  11. Zoraxy - A general purpose HTTP reverse proxy and forwarding tool
  12. Author: tobychui
  13. License: AGPLv3
  14. --------------------------------------------
  15. This program is free software: you can redistribute it and/or modify
  16. it under the terms of the GNU Affero General Public License as published by
  17. the Free Software Foundation, version 3 of the License or any later version.
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU Affero General Public License for more details.
  22. You should have received a copy of the GNU Affero General Public License
  23. along with this program. If not, see <https://www.gnu.org/licenses/>.
  24. */
  25. import (
  26. "flag"
  27. "fmt"
  28. "log"
  29. "net/http"
  30. "os"
  31. "os/signal"
  32. "syscall"
  33. "time"
  34. "github.com/google/uuid"
  35. "github.com/gorilla/csrf"
  36. "imuslab.com/zoraxy/mod/update"
  37. "imuslab.com/zoraxy/mod/utils"
  38. )
  39. /* SIGTERM handler, do shutdown sequences before closing */
  40. func SetupCloseHandler() {
  41. c := make(chan os.Signal, 2)
  42. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  43. go func() {
  44. <-c
  45. ShutdownSeq()
  46. os.Exit(0)
  47. }()
  48. }
  49. func ShutdownSeq() {
  50. SystemWideLogger.Println("Shutting down " + SYSTEM_NAME)
  51. SystemWideLogger.Println("Closing Netstats Listener")
  52. if netstatBuffers != nil {
  53. netstatBuffers.Close()
  54. }
  55. SystemWideLogger.Println("Closing Statistic Collector")
  56. if statisticCollector != nil {
  57. statisticCollector.Close()
  58. }
  59. if mdnsTickerStop != nil {
  60. SystemWideLogger.Println("Stopping mDNS Discoverer (might take a few minutes)")
  61. // Stop the mdns service
  62. mdnsTickerStop <- true
  63. }
  64. if mdnsScanner != nil {
  65. mdnsScanner.Close()
  66. }
  67. SystemWideLogger.Println("Shutting down load balancer")
  68. if loadBalancer != nil {
  69. loadBalancer.Close()
  70. }
  71. SystemWideLogger.Println("Closing Certificates Auto Renewer")
  72. if acmeAutoRenewer != nil {
  73. acmeAutoRenewer.Close()
  74. }
  75. //Remove the tmp folder
  76. SystemWideLogger.Println("Cleaning up tmp files")
  77. os.RemoveAll("./tmp")
  78. //Close database
  79. SystemWideLogger.Println("Stopping system database")
  80. sysdb.Close()
  81. //Close logger
  82. SystemWideLogger.Println("Closing system wide logger")
  83. SystemWideLogger.Close()
  84. }
  85. func main() {
  86. //Parse startup flags
  87. flag.Parse()
  88. if *showver {
  89. fmt.Println(SYSTEM_NAME + " - Version " + SYSTEM_VERSION)
  90. os.Exit(0)
  91. }
  92. if !utils.ValidateListeningAddress(*webUIPort) {
  93. fmt.Println("Malformed -port (listening address) paramter. Do you mean -port=:" + *webUIPort + "?")
  94. os.Exit(0)
  95. }
  96. if *enableAutoUpdate {
  97. fmt.Println("Checking required config update")
  98. update.RunConfigUpdate(0, update.GetVersionIntFromVersionNumber(SYSTEM_VERSION))
  99. }
  100. SetupCloseHandler()
  101. //Read or create the system uuid
  102. uuidRecord := "./sys.uuid"
  103. if !utils.FileExists(uuidRecord) {
  104. newSystemUUID := uuid.New().String()
  105. os.WriteFile(uuidRecord, []byte(newSystemUUID), 0775)
  106. }
  107. uuidBytes, err := os.ReadFile(uuidRecord)
  108. if err != nil {
  109. SystemWideLogger.PrintAndLog("ZeroTier", "Unable to read system uuid from file system", nil)
  110. panic(err)
  111. }
  112. nodeUUID = string(uuidBytes)
  113. //Create a new webmin mux and csrf middleware layer
  114. webminPanelMux = http.NewServeMux()
  115. csrfMiddleware = csrf.Protect(
  116. []byte(nodeUUID),
  117. csrf.CookieName(CSRF_COOKIENAME),
  118. csrf.Secure(false),
  119. csrf.Path("/"),
  120. csrf.SameSite(csrf.SameSiteLaxMode),
  121. )
  122. //Startup all modules
  123. startupSequence()
  124. //Initiate management interface APIs
  125. requireAuth = !(*noauth)
  126. initAPIs(webminPanelMux)
  127. //Start the reverse proxy server in go routine
  128. go func() {
  129. ReverseProxtInit()
  130. }()
  131. time.Sleep(500 * time.Millisecond)
  132. //Start the finalize sequences
  133. finalSequence()
  134. SystemWideLogger.Println(SYSTEM_NAME + " started. Visit control panel at http://localhost" + *webUIPort)
  135. err = http.ListenAndServe(*webUIPort, csrfMiddleware(webminPanelMux))
  136. if err != nil {
  137. log.Fatal(err)
  138. }
  139. }