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,6 +60,7 @@ 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"])
|
||||
|
@ -81,6 +76,7 @@ func GeoGate(handler http.Handler) http.Handler {
|
|||
// 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]
|
||||
}
|
||||
}
|
||||
|
||||
ip, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||
if err != nil {
|
||||
return r.RemoteAddr
|
||||
}
|
||||
return ip
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user