main.go 5.3 KB

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