1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- package geoip
- import (
- "errors"
- "log"
- "net"
- "net/http"
- "strings"
- "github.com/kodnaplakal/caddy/caddyhttp/httpserver"
- )
- func (gip GeoIP) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
- gip.addPlaceholders(w, r)
- return gip.Next.ServeHTTP(w, r)
- }
- var record struct {
- Country struct {
- ISOCode string `maxminddb:"iso_code"`
- IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
- Names map[string]string `maxminddb:"names"`
- } `maxminddb:"country"`
- City struct {
- Names map[string]string `maxminddb:"names"`
- } `maxminddb:"city"`
- Location struct {
- Latitude float64 `maxminddb:"latitude"`
- Longitude float64 `maxminddb:"longitude"`
- TimeZone string `maxminddb:"time_zone"`
- } `maxminddb:"location"`
- }
- func (gip GeoIP) addPlaceholders(w http.ResponseWriter, r *http.Request) {
- clientIP, _ := getClientIP(r, true)
- err := gip.DBHandler.Lookup(clientIP, &record)
- if err != nil {
- log.Println(err)
- }
- if rr, ok := w.(*httpserver.ResponseRecorder); ok && rr.Replacer != nil {
- rr.Replacer.Set("geoip_country_code", record.Country.ISOCode)
- }
- // repl.Set("sd", record.Country.ISOCode)
- r.Header.Set("ssdsd", record.Country.ISOCode)
- // r.Header.Set(gip.Config.HeaderNameCountryIsEU, strconv.FormatBool(record.Country.IsInEuropeanUnion))
- // r.Header.Set(gip.Config.HeaderNameCountryName, record.Country.Names["en"])
- // r.Header.Set(gip.Config.HeaderNameCityName, record.City.Names["en"])
- // r.Header.Set(gip.Config.HeaderNameLocationLat, strconv.FormatFloat(record.Location.Latitude, 'f', 6, 64))
- // r.Header.Set(gip.Config.HeaderNameLocationLon, strconv.FormatFloat(record.Location.Longitude, 'f', 6, 64))
- // r.Header.Set(gip.Config.HeaderNameLocationTimeZone, record.Location.TimeZone)
- }
- func getClientIP(r *http.Request, strict bool) (net.IP, error) {
- var ip string
- // Use the client ip from the 'X-Forwarded-For' header, if available.
- if fwdFor := r.Header.Get("X-Forwarded-For"); fwdFor != "" && !strict {
- ips := strings.Split(fwdFor, ", ")
- ip = ips[0]
- } else {
- // Otherwise, get the client ip from the request remote address.
- var err error
- ip, _, err = net.SplitHostPort(r.RemoteAddr)
- if err != nil {
- if serr, ok := err.(*net.AddrError); ok && serr.Err == "missing port in address" { // It's not critical try parse
- ip = r.RemoteAddr
- } else {
- log.Printf("Error when SplitHostPort: %v", serr.Err)
- return nil, err
- }
- }
- }
- // Parse the ip address string into a net.IP.
- parsedIP := net.ParseIP(ip)
- if parsedIP == nil {
- return nil, errors.New("unable to parse address")
- }
- return parsedIP, nil
- }
|