main.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.2"
  37. nodeUUID = "generic"
  38. development = true //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. log.Println("\r- Shutting down " + name)
  68. geodbStore.Close()
  69. netstatBuffers.Close()
  70. statisticCollector.Close()
  71. //Stop the mdns service
  72. mdnsTickerStop <- true
  73. mdnsScanner.Close()
  74. //Remove the tmp folder
  75. os.RemoveAll("./tmp")
  76. //Close database, final
  77. sysdb.Close()
  78. os.Exit(0)
  79. }()
  80. }
  81. func main() {
  82. //Start the aoModule pipeline (which will parse the flags as well). Pass in the module launch information
  83. handler = aroz.HandleFlagParse(aroz.ServiceInfo{
  84. Name: name,
  85. Desc: "Dynamic Reverse Proxy Server",
  86. Group: "Network",
  87. IconPath: "zoraxy/img/small_icon.png",
  88. Version: version,
  89. StartDir: "zoraxy/index.html",
  90. SupportFW: true,
  91. LaunchFWDir: "zoraxy/index.html",
  92. SupportEmb: false,
  93. InitFWSize: []int{1080, 580},
  94. })
  95. if *showver {
  96. fmt.Println(name + " - Version " + version)
  97. os.Exit(0)
  98. }
  99. SetupCloseHandler()
  100. //Read or create the system uuid
  101. uuidRecord := "./sys.uuid"
  102. if !utils.FileExists(uuidRecord) {
  103. newSystemUUID := uuid.New().String()
  104. os.WriteFile(uuidRecord, []byte(newSystemUUID), 0775)
  105. }
  106. uuidBytes, err := os.ReadFile(uuidRecord)
  107. if err != nil {
  108. log.Println("Unable to read system uuid from file system")
  109. panic(err)
  110. }
  111. nodeUUID = string(uuidBytes)
  112. //Startup all modules
  113. startupSequence()
  114. //Initiate management interface APIs
  115. requireAuth = !(*noauth || handler.IsUsingExternalPermissionManager())
  116. initAPIs()
  117. //Start the reverse proxy server in go routine
  118. go func() {
  119. ReverseProxtInit()
  120. }()
  121. time.Sleep(500 * time.Millisecond)
  122. log.Println("Zoraxy started. Visit control panel at http://localhost" + handler.Port)
  123. err = http.ListenAndServe(handler.Port, nil)
  124. if err != nil {
  125. log.Fatal(err)
  126. }
  127. }