main.go 3.8 KB

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