main.go 4.8 KB

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