main.go 4.3 KB

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