1
0

main.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. netstatBuffers.Close()
  53. SystemWideLogger.Println("Closing Statistic Collector")
  54. statisticCollector.Close()
  55. if mdnsTickerStop != nil {
  56. SystemWideLogger.Println("Stopping mDNS Discoverer (might take a few minutes)")
  57. // Stop the mdns service
  58. mdnsTickerStop <- true
  59. }
  60. mdnsScanner.Close()
  61. SystemWideLogger.Println("Shutting down load balancer")
  62. loadBalancer.Close()
  63. SystemWideLogger.Println("Closing Certificates Auto Renewer")
  64. acmeAutoRenewer.Close()
  65. //Remove the tmp folder
  66. SystemWideLogger.Println("Cleaning up tmp files")
  67. os.RemoveAll("./tmp")
  68. //Close database
  69. SystemWideLogger.Println("Stopping system database")
  70. sysdb.Close()
  71. //Close logger
  72. SystemWideLogger.Println("Closing system wide logger")
  73. SystemWideLogger.Close()
  74. }
  75. func main() {
  76. //Parse startup flags
  77. flag.Parse()
  78. if *showver {
  79. fmt.Println(SYSTEM_NAME + " - Version " + SYSTEM_VERSION)
  80. os.Exit(0)
  81. }
  82. if !utils.ValidateListeningAddress(*webUIPort) {
  83. fmt.Println("Malformed -port (listening address) paramter. Do you mean -port=:" + *webUIPort + "?")
  84. os.Exit(0)
  85. }
  86. if *enableAutoUpdate {
  87. fmt.Println("Checking required config update")
  88. update.RunConfigUpdate(0, update.GetVersionIntFromVersionNumber(SYSTEM_VERSION))
  89. }
  90. SetupCloseHandler()
  91. //Read or create the system uuid
  92. uuidRecord := "./sys.uuid"
  93. if !utils.FileExists(uuidRecord) {
  94. newSystemUUID := uuid.New().String()
  95. os.WriteFile(uuidRecord, []byte(newSystemUUID), 0775)
  96. }
  97. uuidBytes, err := os.ReadFile(uuidRecord)
  98. if err != nil {
  99. SystemWideLogger.PrintAndLog("ZeroTier", "Unable to read system uuid from file system", nil)
  100. panic(err)
  101. }
  102. nodeUUID = string(uuidBytes)
  103. //Create a new webmin mux and csrf middleware layer
  104. webminPanelMux = http.NewServeMux()
  105. csrfMiddleware = csrf.Protect(
  106. []byte(nodeUUID),
  107. csrf.CookieName(CSRF_COOKIENAME),
  108. csrf.Secure(false),
  109. csrf.Path("/"),
  110. csrf.SameSite(csrf.SameSiteLaxMode),
  111. )
  112. //Startup all modules
  113. startupSequence()
  114. //Initiate management interface APIs
  115. requireAuth = !(*noauth)
  116. initAPIs(webminPanelMux)
  117. //Start the reverse proxy server in go routine
  118. go func() {
  119. ReverseProxtInit()
  120. }()
  121. time.Sleep(500 * time.Millisecond)
  122. //Start the finalize sequences
  123. finalSequence()
  124. SystemWideLogger.Println(SYSTEM_NAME + " started. Visit control panel at http://localhost" + *webUIPort)
  125. err = http.ListenAndServe(*webUIPort, csrfMiddleware(webminPanelMux))
  126. if err != nil {
  127. log.Fatal(err)
  128. }
  129. }