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)
- }
-
- r.Header.Set("ssdsd", record.Country.ISOCode)
-
-
-
-
-
-
- }
- func getClientIP(r *http.Request, strict bool) (net.IP, error) {
- var ip string
-
- if fwdFor := r.Header.Get("X-Forwarded-For"); fwdFor != "" && !strict {
- ips := strings.Split(fwdFor, ", ")
- ip = ips[0]
- } else {
-
- 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" {
- ip = r.RemoteAddr
- } else {
- log.Printf("Error when SplitHostPort: %v", serr.Err)
- return nil, err
- }
- }
- }
-
- parsedIP := net.ParseIP(ip)
- if parsedIP == nil {
- return nil, errors.New("unable to parse address")
- }
- return parsedIP, nil
- }
|