Bugfix on geogate
This commit is contained in:
parent
c172565a56
commit
3d30102ade
|
@ -16,5 +16,5 @@ var BlockUSStates = map[string]interface{}{
|
|||
|
||||
// Countries to block.
|
||||
var BlockCountries = map[string]interface{}{
|
||||
// "DE": nil,
|
||||
// "US": nil, // TEST
|
||||
}
|
||||
|
|
|
@ -1,14 +1,11 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"code.nonshy.com/nonshy/website/pkg/config"
|
||||
"code.nonshy.com/nonshy/website/pkg/controller/index"
|
||||
"code.nonshy.com/nonshy/website/pkg/log"
|
||||
"code.nonshy.com/nonshy/website/pkg/session"
|
||||
"code.nonshy.com/nonshy/website/pkg/utility"
|
||||
"github.com/oschwald/geoip2-golang"
|
||||
|
@ -33,9 +30,8 @@ func GeoGate(handler http.Handler) http.Handler {
|
|||
defer db.Close()
|
||||
|
||||
// If you are using strings that may be invalid, check that ip is not nil
|
||||
addr := strings.SplitN(utility.IPAddress(r), ":", 2)[0]
|
||||
ip := net.ParseIP(utility.IPAddress(r))
|
||||
log.Info("IP addr: %s (raw: %s)", ip, addr)
|
||||
addr := utility.IPAddress(r)
|
||||
ip := net.ParseIP(addr)
|
||||
if ip != nil {
|
||||
record, err := db.City(ip)
|
||||
if err != nil {
|
||||
|
@ -43,8 +39,6 @@ func GeoGate(handler http.Handler) http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
log.Info("Raw: %+v", record)
|
||||
|
||||
// Blocked by US states
|
||||
if record.Country.IsoCode == "US" {
|
||||
for _, sub := range record.Subdivisions {
|
||||
|
@ -66,21 +60,23 @@ func GeoGate(handler http.Handler) http.Handler {
|
|||
}
|
||||
|
||||
// Debug info
|
||||
fmt.Printf("Portuguese (BR) city name: %v\n", record.City.Names["pt-BR"])
|
||||
if len(record.Subdivisions) > 0 {
|
||||
fmt.Printf("English subdivision name: %v\n", record.Subdivisions[0].Names["en"])
|
||||
}
|
||||
fmt.Printf("Russian country name: %v\n", record.Country.Names["ru"])
|
||||
fmt.Printf("ISO country code: %v\n", record.Country.IsoCode)
|
||||
fmt.Printf("Time zone: %v\n", record.Location.TimeZone)
|
||||
fmt.Printf("Coordinates: %v, %v\n", record.Location.Latitude, record.Location.Longitude)
|
||||
// Output:
|
||||
// Portuguese (BR) city name: Londres
|
||||
// English subdivision name: England
|
||||
// Russian country name: Великобритания
|
||||
// ISO country code: GB
|
||||
// Time zone: Europe/London
|
||||
// Coordinates: 51.5142, -0.0931
|
||||
/*
|
||||
fmt.Printf("Portuguese (BR) city name: %v\n", record.City.Names["pt-BR"])
|
||||
if len(record.Subdivisions) > 0 {
|
||||
fmt.Printf("English subdivision name: %v\n", record.Subdivisions[0].Names["en"])
|
||||
}
|
||||
fmt.Printf("Russian country name: %v\n", record.Country.Names["ru"])
|
||||
fmt.Printf("ISO country code: %v\n", record.Country.IsoCode)
|
||||
fmt.Printf("Time zone: %v\n", record.Location.TimeZone)
|
||||
fmt.Printf("Coordinates: %v, %v\n", record.Location.Latitude, record.Location.Longitude)
|
||||
// Output:
|
||||
// Portuguese (BR) city name: Londres
|
||||
// English subdivision name: England
|
||||
// Russian country name: Великобритания
|
||||
// ISO country code: GB
|
||||
// Time zone: Europe/London
|
||||
// Coordinates: 51.5142, -0.0931
|
||||
*/
|
||||
}
|
||||
|
||||
handler.ServeHTTP(w, r)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package utility
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
|
@ -19,5 +20,10 @@ func IPAddress(r *http.Request) string {
|
|||
return strings.SplitN(xff, " ", 1)[0]
|
||||
}
|
||||
}
|
||||
return r.RemoteAddr
|
||||
|
||||
ip, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||
if err != nil {
|
||||
return r.RemoteAddr
|
||||
}
|
||||
return ip
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user