main.go 3.1 KB

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