main.go 3.9 KB

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