main.go 5.2 KB

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