main.go 3.7 KB

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