handlers.go 872 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "imuslab.com/zoraxy/mod/utils"
  6. )
  7. func HandleCountryDistrSummary(w http.ResponseWriter, r *http.Request) {
  8. requestClientCountry := map[string]int{}
  9. statisticCollector.DailySummary.RequestClientIp.Range(func(key, value interface{}) bool {
  10. //Get this client country of original
  11. clientIp := key.(string)
  12. //requestCount := value.(int)
  13. ci, err := geodbStore.ResolveCountryCodeFromIP(clientIp)
  14. if err != nil {
  15. return true
  16. }
  17. isoCode := ci.CountryIsoCode
  18. if isoCode == "" {
  19. //local or reserved addr
  20. isoCode = "local"
  21. }
  22. uc, ok := requestClientCountry[isoCode]
  23. if !ok {
  24. //Create the counter
  25. requestClientCountry[isoCode] = 1
  26. } else {
  27. requestClientCountry[isoCode] = uc + 1
  28. }
  29. return true
  30. })
  31. js, _ := json.Marshal(requestClientCountry)
  32. utils.SendJSONResponse(w, string(js))
  33. }