main.go 4.6 KB

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