handler.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package geoip
  2. import (
  3. "errors"
  4. "log"
  5. "net"
  6. "net/http"
  7. "strings"
  8. "github.com/kodnaplakal/caddy/caddyhttp/httpserver"
  9. )
  10. func (gip GeoIP) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
  11. gip.addPlaceholders(w, r)
  12. return gip.Next.ServeHTTP(w, r)
  13. }
  14. var record struct {
  15. Country struct {
  16. ISOCode string `maxminddb:"iso_code"`
  17. IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
  18. Names map[string]string `maxminddb:"names"`
  19. } `maxminddb:"country"`
  20. City struct {
  21. Names map[string]string `maxminddb:"names"`
  22. } `maxminddb:"city"`
  23. Location struct {
  24. Latitude float64 `maxminddb:"latitude"`
  25. Longitude float64 `maxminddb:"longitude"`
  26. TimeZone string `maxminddb:"time_zone"`
  27. } `maxminddb:"location"`
  28. }
  29. func (gip GeoIP) addPlaceholders(w http.ResponseWriter, r *http.Request) {
  30. clientIP, _ := getClientIP(r, true)
  31. err := gip.DBHandler.Lookup(clientIP, &record)
  32. if err != nil {
  33. log.Println(err)
  34. }
  35. if rr, ok := w.(*httpserver.ResponseRecorder); ok && rr.Replacer != nil {
  36. rr.Replacer.Set("geoip_country_code", record.Country.ISOCode)
  37. }
  38. // repl.Set("sd", record.Country.ISOCode)
  39. r.Header.Set("ssdsd", record.Country.ISOCode)
  40. // r.Header.Set(gip.Config.HeaderNameCountryIsEU, strconv.FormatBool(record.Country.IsInEuropeanUnion))
  41. // r.Header.Set(gip.Config.HeaderNameCountryName, record.Country.Names["en"])
  42. // r.Header.Set(gip.Config.HeaderNameCityName, record.City.Names["en"])
  43. // r.Header.Set(gip.Config.HeaderNameLocationLat, strconv.FormatFloat(record.Location.Latitude, 'f', 6, 64))
  44. // r.Header.Set(gip.Config.HeaderNameLocationLon, strconv.FormatFloat(record.Location.Longitude, 'f', 6, 64))
  45. // r.Header.Set(gip.Config.HeaderNameLocationTimeZone, record.Location.TimeZone)
  46. }
  47. func getClientIP(r *http.Request, strict bool) (net.IP, error) {
  48. var ip string
  49. // Use the client ip from the 'X-Forwarded-For' header, if available.
  50. if fwdFor := r.Header.Get("X-Forwarded-For"); fwdFor != "" && !strict {
  51. ips := strings.Split(fwdFor, ", ")
  52. ip = ips[0]
  53. } else {
  54. // Otherwise, get the client ip from the request remote address.
  55. var err error
  56. ip, _, err = net.SplitHostPort(r.RemoteAddr)
  57. if err != nil {
  58. if serr, ok := err.(*net.AddrError); ok && serr.Err == "missing port in address" { // It's not critical try parse
  59. ip = r.RemoteAddr
  60. } else {
  61. log.Printf("Error when SplitHostPort: %v", serr.Err)
  62. return nil, err
  63. }
  64. }
  65. }
  66. // Parse the ip address string into a net.IP.
  67. parsedIP := net.ParseIP(ip)
  68. if parsedIP == nil {
  69. return nil, errors.New("unable to parse address")
  70. }
  71. return parsedIP, nil
  72. }