helpers.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "imuslab.com/zoraxy/mod/dynamicproxy"
  6. "imuslab.com/zoraxy/mod/mdns"
  7. "imuslab.com/zoraxy/mod/uptime"
  8. "imuslab.com/zoraxy/mod/utils"
  9. )
  10. /*
  11. Statistic Summary
  12. */
  13. //Handle conversion of statistic daily summary to country summary
  14. func HandleCountryDistrSummary(w http.ResponseWriter, r *http.Request) {
  15. requestClientCountry := map[string]int{}
  16. statisticCollector.DailySummary.RequestClientIp.Range(func(key, value interface{}) bool {
  17. //Get this client country of original
  18. clientIp := key.(string)
  19. //requestCount := value.(int)
  20. ci, err := geodbStore.ResolveCountryCodeFromIP(clientIp)
  21. if err != nil {
  22. return true
  23. }
  24. isoCode := ci.CountryIsoCode
  25. if isoCode == "" {
  26. //local or reserved addr
  27. isoCode = "local"
  28. }
  29. uc, ok := requestClientCountry[isoCode]
  30. if !ok {
  31. //Create the counter
  32. requestClientCountry[isoCode] = 1
  33. } else {
  34. requestClientCountry[isoCode] = uc + 1
  35. }
  36. return true
  37. })
  38. js, _ := json.Marshal(requestClientCountry)
  39. utils.SendJSONResponse(w, string(js))
  40. }
  41. /*
  42. Up Time Monitor
  43. */
  44. //Generate uptime monitor targets from reverse proxy rules
  45. func GetUptimeTargetsFromReverseProxyRules(dp *dynamicproxy.Router) []*uptime.Target {
  46. subds := dp.GetSDProxyEndpointsAsMap()
  47. vdirs := dp.GetVDProxyEndpointsAsMap()
  48. UptimeTargets := []*uptime.Target{}
  49. for subd, target := range subds {
  50. url := "http://" + target.Domain
  51. protocol := "http"
  52. if target.RequireTLS {
  53. url = "https://" + target.Domain
  54. protocol = "https"
  55. }
  56. UptimeTargets = append(UptimeTargets, &uptime.Target{
  57. ID: subd,
  58. Name: subd,
  59. URL: url,
  60. Protocol: protocol,
  61. })
  62. }
  63. for vdir, target := range vdirs {
  64. url := "http://" + target.Domain
  65. protocol := "http"
  66. if target.RequireTLS {
  67. url = "https://" + target.Domain
  68. protocol = "https"
  69. }
  70. UptimeTargets = append(UptimeTargets, &uptime.Target{
  71. ID: vdir,
  72. Name: "*" + vdir,
  73. URL: url,
  74. Protocol: protocol,
  75. })
  76. }
  77. return UptimeTargets
  78. }
  79. //Handle rendering up time monitor data
  80. func HandleUptimeMonitorListing(w http.ResponseWriter, r *http.Request) {
  81. if uptimeMonitor != nil {
  82. uptimeMonitor.HandleUptimeLogRead(w, r)
  83. } else {
  84. http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
  85. return
  86. }
  87. }
  88. //Handle listing current registered mdns nodes
  89. func HandleMdnsListing(w http.ResponseWriter, r *http.Request) {
  90. js, _ := json.Marshal(previousmdnsScanResults)
  91. utils.SendJSONResponse(w, string(js))
  92. }
  93. func HandleMdnsScanning(w http.ResponseWriter, r *http.Request) {
  94. domain, err := utils.PostPara(r, "domain")
  95. var hosts []*mdns.NetworkHost
  96. if err != nil {
  97. //Search for arozos node
  98. hosts = mdnsScanner.Scan(30, "")
  99. previousmdnsScanResults = hosts
  100. } else {
  101. //Search for other nodes
  102. hosts = mdnsScanner.Scan(30, domain)
  103. }
  104. js, _ := json.Marshal(hosts)
  105. utils.SendJSONResponse(w, string(js))
  106. }