github.com/geph-official/geph2@v0.22.6-0.20210211030601-f527cb59b0df/cmd/geph-binder/ipinfo.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "net/http" 6 "strings" 7 8 "github.com/abh/geoip" 9 ) 10 11 var db *geoip.GeoIP 12 13 func init() { 14 var e error 15 db, e = geoip.Open("/usr/share/GeoIP/GeoIP.dat") 16 if e != nil { 17 panic(e) 18 } 19 } 20 21 func handleClientInfo(w http.ResponseWriter, r *http.Request) { 22 countUserAgent(r) 23 var cinfo struct { 24 Address string 25 Country string 26 } 27 addrs := strings.Split(r.Header.Get("X-Forwarded-For"), ",") 28 addr := addrs[0] 29 cinfo.Address = addr 30 w.Header().Add("content-type", "application/json") 31 32 country, _ := db.GetCountry(addr) 33 cinfo.Country = country 34 // if cinfo.Country == "IR" { 35 // cinfo.Country = "CN" // HACK to put iranian users on bridges 36 // } 37 if len(cinfo.Country) != 2 { 38 cinfo.Country = "CN" 39 } 40 json.NewEncoder(w).Encode(cinfo) 41 }