main.go 5.5 KB

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