main.go 3.8 KB

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