main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/geodb"
  37. "imuslab.com/zoraxy/mod/update"
  38. "imuslab.com/zoraxy/mod/utils"
  39. )
  40. /* SIGTERM handler, do shutdown sequences before closing */
  41. func SetupCloseHandler() {
  42. c := make(chan os.Signal, 2)
  43. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  44. go func() {
  45. <-c
  46. ShutdownSeq()
  47. os.Exit(0)
  48. }()
  49. }
  50. func main() {
  51. //Parse startup flags
  52. flag.Parse()
  53. /* Maintaince Function Modes */
  54. if *showver {
  55. fmt.Println(SYSTEM_NAME + " - Version " + SYSTEM_VERSION)
  56. os.Exit(0)
  57. }
  58. if *geoDbUpdate {
  59. geodb.DownloadGeoDBUpdate("./conf/geodb")
  60. os.Exit(0)
  61. }
  62. /* Main Zoraxy Routines */
  63. if !utils.ValidateListeningAddress(*webUIPort) {
  64. fmt.Println("Malformed -port (listening address) paramter. Do you mean -port=:" + *webUIPort + "?")
  65. os.Exit(0)
  66. }
  67. if *enableAutoUpdate {
  68. fmt.Println("Checking required config update")
  69. update.RunConfigUpdate(0, update.GetVersionIntFromVersionNumber(SYSTEM_VERSION))
  70. }
  71. SetupCloseHandler()
  72. //Read or create the system uuid
  73. uuidRecord := *path_uuid
  74. if !utils.FileExists(uuidRecord) {
  75. newSystemUUID := uuid.New().String()
  76. os.WriteFile(uuidRecord, []byte(newSystemUUID), 0775)
  77. }
  78. uuidBytes, err := os.ReadFile(uuidRecord)
  79. if err != nil {
  80. SystemWideLogger.PrintAndLog("ZeroTier", "Unable to read system uuid from file system", nil)
  81. panic(err)
  82. }
  83. nodeUUID = string(uuidBytes)
  84. //Create a new webmin mux and csrf middleware layer
  85. webminPanelMux = http.NewServeMux()
  86. csrfMiddleware = csrf.Protect(
  87. []byte(nodeUUID),
  88. csrf.CookieName(CSRF_COOKIENAME),
  89. csrf.Secure(false),
  90. csrf.Path("/"),
  91. csrf.SameSite(csrf.SameSiteLaxMode),
  92. )
  93. //Startup all modules, see start.go
  94. startupSequence()
  95. //Initiate management interface APIs
  96. requireAuth = !(*noauth)
  97. initAPIs(webminPanelMux)
  98. //Start the reverse proxy server in go routine
  99. go func() {
  100. ReverseProxtInit()
  101. }()
  102. time.Sleep(500 * time.Millisecond)
  103. //Start the finalize sequences
  104. finalSequence()
  105. SystemWideLogger.Println(SYSTEM_NAME + " started. Visit control panel at http://localhost" + *webUIPort)
  106. err = http.ListenAndServe(*webUIPort, csrfMiddleware(webminPanelMux))
  107. if err != nil {
  108. log.Fatal(err)
  109. }
  110. }