1
0

main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 main() {
  50. //Parse startup flags
  51. flag.Parse()
  52. if *showver {
  53. fmt.Println(SYSTEM_NAME + " - Version " + SYSTEM_VERSION)
  54. os.Exit(0)
  55. }
  56. if !utils.ValidateListeningAddress(*webUIPort) {
  57. fmt.Println("Malformed -port (listening address) paramter. Do you mean -port=:" + *webUIPort + "?")
  58. os.Exit(0)
  59. }
  60. if *enableAutoUpdate {
  61. fmt.Println("Checking required config update")
  62. update.RunConfigUpdate(0, update.GetVersionIntFromVersionNumber(SYSTEM_VERSION))
  63. }
  64. SetupCloseHandler()
  65. //Read or create the system uuid
  66. uuidRecord := "./sys.uuid"
  67. if !utils.FileExists(uuidRecord) {
  68. newSystemUUID := uuid.New().String()
  69. os.WriteFile(uuidRecord, []byte(newSystemUUID), 0775)
  70. }
  71. uuidBytes, err := os.ReadFile(uuidRecord)
  72. if err != nil {
  73. SystemWideLogger.PrintAndLog("ZeroTier", "Unable to read system uuid from file system", nil)
  74. panic(err)
  75. }
  76. nodeUUID = string(uuidBytes)
  77. //Create a new webmin mux and csrf middleware layer
  78. webminPanelMux = http.NewServeMux()
  79. csrfMiddleware = csrf.Protect(
  80. []byte(nodeUUID),
  81. csrf.CookieName(CSRF_COOKIENAME),
  82. csrf.Secure(false),
  83. csrf.Path("/"),
  84. csrf.SameSite(csrf.SameSiteLaxMode),
  85. )
  86. //Startup all modules, see start.go
  87. startupSequence()
  88. //Initiate management interface APIs
  89. requireAuth = !(*noauth)
  90. initAPIs(webminPanelMux)
  91. //Start the reverse proxy server in go routine
  92. go func() {
  93. ReverseProxtInit()
  94. }()
  95. time.Sleep(500 * time.Millisecond)
  96. //Start the finalize sequences
  97. finalSequence()
  98. SystemWideLogger.Println(SYSTEM_NAME + " started. Visit control panel at http://localhost" + *webUIPort)
  99. err = http.ListenAndServe(*webUIPort, csrfMiddleware(webminPanelMux))
  100. if err != nil {
  101. log.Fatal(err)
  102. }
  103. }