main.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "os"
  8. "os/signal"
  9. "syscall"
  10. "time"
  11. "imuslab.com/zoraxy/mod/aroz"
  12. "imuslab.com/zoraxy/mod/auth"
  13. "imuslab.com/zoraxy/mod/database"
  14. "imuslab.com/zoraxy/mod/dynamicproxy/redirection"
  15. "imuslab.com/zoraxy/mod/geodb"
  16. "imuslab.com/zoraxy/mod/statistic"
  17. "imuslab.com/zoraxy/mod/tlscert"
  18. "imuslab.com/zoraxy/mod/uptime"
  19. )
  20. //General flags
  21. var noauth = flag.Bool("noauth", false, "Disable authentication for management interface")
  22. var showver = flag.Bool("version", false, "Show version of this server")
  23. var (
  24. name = "Zoraxy"
  25. version = "2.11"
  26. handler *aroz.ArozHandler //Handle arozos managed permission system
  27. sysdb *database.Database //System database
  28. authAgent *auth.AuthAgent //Authentication agent
  29. tlsCertManager *tlscert.Manager //TLS / SSL management
  30. redirectTable *redirection.RuleTable //Handle special redirection rule sets
  31. geodbStore *geodb.Store //GeoIP database
  32. statisticCollector *statistic.Collector //Collecting statistic from visitors
  33. uptimeMonitor *uptime.Monitor //Uptime monitor service worker
  34. )
  35. // Kill signal handler. Do something before the system the core terminate.
  36. func SetupCloseHandler() {
  37. c := make(chan os.Signal, 2)
  38. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  39. go func() {
  40. <-c
  41. log.Println("\r- Shutting down " + name)
  42. geodbStore.Close()
  43. statisticCollector.Close()
  44. //Close database, final
  45. sysdb.Close()
  46. os.Exit(0)
  47. }()
  48. }
  49. func main() {
  50. //Start the aoModule pipeline (which will parse the flags as well). Pass in the module launch information
  51. handler = aroz.HandleFlagParse(aroz.ServiceInfo{
  52. Name: name,
  53. Desc: "Dynamic Reverse Proxy Server",
  54. Group: "Network",
  55. IconPath: "reverseproxy/img/small_icon.png",
  56. Version: version,
  57. StartDir: "reverseproxy/index.html",
  58. SupportFW: true,
  59. LaunchFWDir: "reverseproxy/index.html",
  60. SupportEmb: false,
  61. InitFWSize: []int{1080, 580},
  62. })
  63. if *showver {
  64. fmt.Println(name + " - Version " + version)
  65. os.Exit(0)
  66. }
  67. SetupCloseHandler()
  68. //Create database
  69. db, err := database.NewDatabase("sys.db", false)
  70. if err != nil {
  71. log.Fatal(err)
  72. }
  73. sysdb = db
  74. //Create tables for the database
  75. sysdb.NewTable("settings")
  76. //Create an auth agent
  77. sessionKey, err := auth.GetSessionKey(sysdb)
  78. if err != nil {
  79. log.Fatal(err)
  80. }
  81. authAgent = auth.NewAuthenticationAgent(name, []byte(sessionKey), sysdb, true, func(w http.ResponseWriter, r *http.Request) {
  82. //Not logged in. Redirecting to login page
  83. http.Redirect(w, r, "/login.html", http.StatusTemporaryRedirect)
  84. })
  85. //Create a TLS certificate manager
  86. tlsCertManager, err = tlscert.NewManager("./certs")
  87. if err != nil {
  88. panic(err)
  89. }
  90. //Create a redirection rule table
  91. redirectTable, err = redirection.NewRuleTable("./rules")
  92. if err != nil {
  93. panic(err)
  94. }
  95. //Create a geodb store
  96. geodbStore, err = geodb.NewGeoDb(sysdb, "./system/GeoLite2-Country.mmdb")
  97. if err != nil {
  98. panic(err)
  99. }
  100. //Create a statistic collector
  101. statisticCollector, err = statistic.NewStatisticCollector(statistic.CollectorOption{
  102. Database: sysdb,
  103. })
  104. if err != nil {
  105. panic(err)
  106. }
  107. if err != nil {
  108. panic(err)
  109. }
  110. //Initiate management interface APIs
  111. initAPIs()
  112. //Start the reverse proxy server in go routine
  113. go func() {
  114. ReverseProxtInit()
  115. }()
  116. time.Sleep(500 * time.Millisecond)
  117. //Any log println will be shown in the core system via STDOUT redirection. But not STDIN.
  118. log.Println("ReverseProxy 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. }