main.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/acme"
  14. "imuslab.com/zoraxy/mod/aroz"
  15. "imuslab.com/zoraxy/mod/auth"
  16. "imuslab.com/zoraxy/mod/database"
  17. "imuslab.com/zoraxy/mod/dynamicproxy/redirection"
  18. "imuslab.com/zoraxy/mod/email"
  19. "imuslab.com/zoraxy/mod/ganserv"
  20. "imuslab.com/zoraxy/mod/geodb"
  21. "imuslab.com/zoraxy/mod/mdns"
  22. "imuslab.com/zoraxy/mod/netstat"
  23. "imuslab.com/zoraxy/mod/pathrule"
  24. "imuslab.com/zoraxy/mod/sshprox"
  25. "imuslab.com/zoraxy/mod/statistic"
  26. "imuslab.com/zoraxy/mod/statistic/analytic"
  27. "imuslab.com/zoraxy/mod/tcpprox"
  28. "imuslab.com/zoraxy/mod/tlscert"
  29. "imuslab.com/zoraxy/mod/uptime"
  30. "imuslab.com/zoraxy/mod/utils"
  31. )
  32. // General flags
  33. var noauth = flag.Bool("noauth", false, "Disable authentication for management interface")
  34. var showver = flag.Bool("version", false, "Show version of this server")
  35. var allowSshLoopback = flag.Bool("sshlb", false, "Allow loopback web ssh connection (DANGER)")
  36. var ztAuthToken = flag.String("ztauth", "", "ZeroTier authtoken for the local node")
  37. var ztAPIPort = flag.Int("ztport", 9993, "ZeroTier controller API port")
  38. var (
  39. name = "Zoraxy"
  40. version = "2.6.5"
  41. nodeUUID = "generic"
  42. development = true //Set this to false to use embedded web fs
  43. bootTime = time.Now().Unix()
  44. /*
  45. Binary Embedding File System
  46. */
  47. //go:embed web/*
  48. webres embed.FS
  49. /*
  50. Handler Modules
  51. */
  52. handler *aroz.ArozHandler //Handle arozos managed permission system
  53. sysdb *database.Database //System database
  54. authAgent *auth.AuthAgent //Authentication agent
  55. tlsCertManager *tlscert.Manager //TLS / SSL management
  56. redirectTable *redirection.RuleTable //Handle special redirection rule sets
  57. pathRuleHandler *pathrule.Handler //Handle specific path blocking or custom headers
  58. geodbStore *geodb.Store //GeoIP database, also handle black list and whitelist features
  59. netstatBuffers *netstat.NetStatBuffers //Realtime graph buffers
  60. statisticCollector *statistic.Collector //Collecting statistic from visitors
  61. uptimeMonitor *uptime.Monitor //Uptime monitor service worker
  62. mdnsScanner *mdns.MDNSHost //mDNS discovery services
  63. ganManager *ganserv.NetworkManager //Global Area Network Manager
  64. webSshManager *sshprox.Manager //Web SSH connection service
  65. tcpProxyManager *tcpprox.Manager //TCP Proxy Manager
  66. acmeHandler *acme.ACMEHandler //Handler for ACME Certificate renew
  67. //Helper modules
  68. EmailSender *email.Sender //Email sender that handle email sending
  69. AnalyticLoader *analytic.DataLoader //Data loader for Zoraxy Analytic
  70. )
  71. // Kill signal handler. Do something before the system the core terminate.
  72. func SetupCloseHandler() {
  73. c := make(chan os.Signal, 2)
  74. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  75. go func() {
  76. <-c
  77. fmt.Println("- Shutting down " + name)
  78. fmt.Println("- Closing GeoDB ")
  79. geodbStore.Close()
  80. fmt.Println("- Closing Netstats Listener")
  81. netstatBuffers.Close()
  82. fmt.Println("- Closing Statistic Collector")
  83. statisticCollector.Close()
  84. fmt.Println("- Stopping mDNS Discoverer")
  85. //Stop the mdns service
  86. mdnsTickerStop <- true
  87. mdnsScanner.Close()
  88. //Remove the tmp folder
  89. fmt.Println("- Cleaning up tmp files")
  90. os.RemoveAll("./tmp")
  91. //Close database, final
  92. fmt.Println("- Stopping system database")
  93. sysdb.Close()
  94. os.Exit(0)
  95. }()
  96. }
  97. func main() {
  98. //Start the aoModule pipeline (which will parse the flags as well). Pass in the module launch information
  99. handler = aroz.HandleFlagParse(aroz.ServiceInfo{
  100. Name: name,
  101. Desc: "Dynamic Reverse Proxy Server",
  102. Group: "Network",
  103. IconPath: "zoraxy/img/small_icon.png",
  104. Version: version,
  105. StartDir: "zoraxy/index.html",
  106. SupportFW: true,
  107. LaunchFWDir: "zoraxy/index.html",
  108. SupportEmb: false,
  109. InitFWSize: []int{1080, 580},
  110. })
  111. if *showver {
  112. fmt.Println(name + " - Version " + version)
  113. os.Exit(0)
  114. }
  115. SetupCloseHandler()
  116. //Read or create the system uuid
  117. uuidRecord := "./sys.uuid"
  118. if !utils.FileExists(uuidRecord) {
  119. newSystemUUID := uuid.New().String()
  120. os.WriteFile(uuidRecord, []byte(newSystemUUID), 0775)
  121. }
  122. uuidBytes, err := os.ReadFile(uuidRecord)
  123. if err != nil {
  124. log.Println("Unable to read system uuid from file system")
  125. panic(err)
  126. }
  127. nodeUUID = string(uuidBytes)
  128. //Startup all modules
  129. startupSequence()
  130. //Initiate management interface APIs
  131. requireAuth = !(*noauth || handler.IsUsingExternalPermissionManager())
  132. initAPIs()
  133. //Start the reverse proxy server in go routine
  134. go func() {
  135. ReverseProxtInit()
  136. }()
  137. time.Sleep(500 * time.Millisecond)
  138. //Start the finalize sequences
  139. finalSequence()
  140. log.Println("Zoraxy started. Visit control panel at http://localhost" + handler.Port)
  141. err = http.ListenAndServe(handler.Port, nil)
  142. if err != nil {
  143. log.Fatal(err)
  144. }
  145. }