1
0

helpers.go 2.2 KB

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