main.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package main
  2. import (
  3. "embed"
  4. "flag"
  5. "fmt"
  6. "log"
  7. "net/http"
  8. "os"
  9. "os/signal"
  10. "syscall"
  11. "time"
  12. "github.com/google/uuid"
  13. "imuslab.com/zoraxy/mod/aroz"
  14. "imuslab.com/zoraxy/mod/auth"
  15. "imuslab.com/zoraxy/mod/database"
  16. "imuslab.com/zoraxy/mod/dynamicproxy/redirection"
  17. "imuslab.com/zoraxy/mod/email"
  18. "imuslab.com/zoraxy/mod/ganserv"
  19. "imuslab.com/zoraxy/mod/geodb"
  20. "imuslab.com/zoraxy/mod/mdns"
  21. "imuslab.com/zoraxy/mod/sshprox"
  22. "imuslab.com/zoraxy/mod/statistic"
  23. "imuslab.com/zoraxy/mod/tlscert"
  24. "imuslab.com/zoraxy/mod/uptime"
  25. "imuslab.com/zoraxy/mod/utils"
  26. )
  27. // General flags
  28. var noauth = flag.Bool("noauth", false, "Disable authentication for management interface")
  29. var showver = flag.Bool("version", false, "Show version of this server")
  30. var allowSshLoopback = flag.Bool("sshlb", false, "Allow loopback web ssh connection (DANGER)")
  31. var ztAuthToken = flag.String("ztauth", "", "ZeroTier authtoken for the local node")
  32. var ztAPIPort = flag.Int("ztport", 9993, "ZeroTier controller API port")
  33. var (
  34. name = "Zoraxy"
  35. version = "2.2"
  36. nodeUUID = "generic"
  37. development = false //Set this to false to use embedded web fs
  38. /*
  39. Binary Embedding File System
  40. */
  41. //go:embed web/*
  42. webres embed.FS
  43. /*
  44. Handler Modules
  45. */
  46. handler *aroz.ArozHandler //Handle arozos managed permission system
  47. sysdb *database.Database //System database
  48. authAgent *auth.AuthAgent //Authentication agent
  49. tlsCertManager *tlscert.Manager //TLS / SSL management
  50. redirectTable *redirection.RuleTable //Handle special redirection rule sets
  51. geodbStore *geodb.Store //GeoIP database
  52. statisticCollector *statistic.Collector //Collecting statistic from visitors
  53. uptimeMonitor *uptime.Monitor //Uptime monitor service worker
  54. mdnsScanner *mdns.MDNSHost //mDNS discovery services
  55. ganManager *ganserv.NetworkManager //Global Area Network Manager
  56. webSshManager *sshprox.Manager //Web SSH connection service
  57. EmailSender *email.Sender //Email sender that handle email sending
  58. )
  59. // Kill signal handler. Do something before the system the core terminate.
  60. func SetupCloseHandler() {
  61. c := make(chan os.Signal, 2)
  62. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  63. go func() {
  64. <-c
  65. log.Println("\r- Shutting down " + name)
  66. geodbStore.Close()
  67. statisticCollector.Close()
  68. //Stop the mdns service
  69. mdnsTickerStop <- true
  70. mdnsScanner.Close()
  71. //Remove the tmp folder
  72. os.RemoveAll("./tmp")
  73. //Close database, final
  74. sysdb.Close()
  75. os.Exit(0)
  76. }()
  77. }
  78. func main() {
  79. //Start the aoModule pipeline (which will parse the flags as well). Pass in the module launch information
  80. handler = aroz.HandleFlagParse(aroz.ServiceInfo{
  81. Name: name,
  82. Desc: "Dynamic Reverse Proxy Server",
  83. Group: "Network",
  84. IconPath: "zoraxy/img/small_icon.png",
  85. Version: version,
  86. StartDir: "zoraxy/index.html",
  87. SupportFW: true,
  88. LaunchFWDir: "zoraxy/index.html",
  89. SupportEmb: false,
  90. InitFWSize: []int{1080, 580},
  91. })
  92. if *showver {
  93. fmt.Println(name + " - Version " + version)
  94. os.Exit(0)
  95. }
  96. SetupCloseHandler()
  97. //Read or create the system uuid
  98. uuidRecord := "./sys.uuid"
  99. if !utils.FileExists(uuidRecord) {
  100. newSystemUUID := uuid.New().String()
  101. os.WriteFile(uuidRecord, []byte(newSystemUUID), 0775)
  102. }
  103. uuidBytes, err := os.ReadFile(uuidRecord)
  104. if err != nil {
  105. log.Println("Unable to read system uuid from file system")
  106. panic(err)
  107. }
  108. nodeUUID = string(uuidBytes)
  109. //Startup all modules
  110. startupSequence()
  111. //Initiate management interface APIs
  112. requireAuth = !(*noauth || handler.IsUsingExternalPermissionManager())
  113. initAPIs()
  114. //Start the reverse proxy server in go routine
  115. go func() {
  116. ReverseProxtInit()
  117. }()
  118. time.Sleep(500 * time.Millisecond)
  119. log.Println("Zoraxy started. Visit control panel at http://localhost" + handler.Port)
  120. err = http.ListenAndServe(handler.Port, nil)
  121. if err != nil {
  122. log.Fatal(err)
  123. }
  124. }