1
0

main.go 3.3 KB

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