main.go 3.6 KB

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