1
0

main.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. fmt.Println("- Shutting down " + name)
  80. fmt.Println("- Closing GeoDB ")
  81. geodbStore.Close()
  82. fmt.Println("- Closing Netstats Listener")
  83. netstatBuffers.Close()
  84. fmt.Println("- Closing Statistic Collector")
  85. statisticCollector.Close()
  86. fmt.Println("- Stopping mDNS Discoverer")
  87. //Stop the mdns service
  88. mdnsTickerStop <- true
  89. mdnsScanner.Close()
  90. //Remove the tmp folder
  91. fmt.Println("- Cleaning up tmp files")
  92. os.RemoveAll("./tmp")
  93. //Close database, final
  94. fmt.Println("- Stopping system database")
  95. sysdb.Close()
  96. os.Exit(0)
  97. }()
  98. }
  99. func main() {
  100. //Start the aoModule pipeline (which will parse the flags as well). Pass in the module launch information
  101. handler = aroz.HandleFlagParse(aroz.ServiceInfo{
  102. Name: name,
  103. Desc: "Dynamic Reverse Proxy Server",
  104. Group: "Network",
  105. IconPath: "zoraxy/img/small_icon.png",
  106. Version: version,
  107. StartDir: "zoraxy/index.html",
  108. SupportFW: true,
  109. LaunchFWDir: "zoraxy/index.html",
  110. SupportEmb: false,
  111. InitFWSize: []int{1080, 580},
  112. })
  113. if *showver {
  114. fmt.Println(name + " - Version " + version)
  115. os.Exit(0)
  116. }
  117. SetupCloseHandler()
  118. //Read or create the system uuid
  119. uuidRecord := "./sys.uuid"
  120. if !utils.FileExists(uuidRecord) {
  121. newSystemUUID := uuid.New().String()
  122. os.WriteFile(uuidRecord, []byte(newSystemUUID), 0775)
  123. }
  124. uuidBytes, err := os.ReadFile(uuidRecord)
  125. if err != nil {
  126. log.Println("Unable to read system uuid from file system")
  127. panic(err)
  128. }
  129. nodeUUID = string(uuidBytes)
  130. //Startup all modules
  131. startupSequence()
  132. //Initiate management interface APIs
  133. requireAuth = !(*noauth || handler.IsUsingExternalPermissionManager())
  134. initAPIs()
  135. //Start the reverse proxy server in go routine
  136. go func() {
  137. ReverseProxtInit()
  138. }()
  139. time.Sleep(500 * time.Millisecond)
  140. //Start the finalize sequences
  141. finalSequence()
  142. log.Println("Zoraxy started. Visit control panel at http://localhost" + handler.Port)
  143. err = http.ListenAndServe(handler.Port, nil)
  144. if err != nil {
  145. log.Fatal(err)
  146. }
  147. }