Bugfix on geogate

This commit is contained in:
Noah Petherbridge 2023-06-24 15:51:53 -07:00
parent c172565a56
commit 3d30102ade
3 changed files with 27 additions and 25 deletions

View File

@ -16,5 +16,5 @@ var BlockUSStates = map[string]interface{}{
// Countries to block. // Countries to block.
var BlockCountries = map[string]interface{}{ var BlockCountries = map[string]interface{}{
// "DE": nil, // "US": nil, // TEST
} }

View File

@ -1,14 +1,11 @@
package middleware package middleware
import ( import (
"fmt"
"net" "net"
"net/http" "net/http"
"strings"
"code.nonshy.com/nonshy/website/pkg/config" "code.nonshy.com/nonshy/website/pkg/config"
"code.nonshy.com/nonshy/website/pkg/controller/index" "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/session"
"code.nonshy.com/nonshy/website/pkg/utility" "code.nonshy.com/nonshy/website/pkg/utility"
"github.com/oschwald/geoip2-golang" "github.com/oschwald/geoip2-golang"
@ -33,9 +30,8 @@ func GeoGate(handler http.Handler) http.Handler {
defer db.Close() defer db.Close()
// If you are using strings that may be invalid, check that ip is not nil // If you are using strings that may be invalid, check that ip is not nil
addr := strings.SplitN(utility.IPAddress(r), ":", 2)[0] addr := utility.IPAddress(r)
ip := net.ParseIP(utility.IPAddress(r)) ip := net.ParseIP(addr)
log.Info("IP addr: %s (raw: %s)", ip, addr)
if ip != nil { if ip != nil {
record, err := db.City(ip) record, err := db.City(ip)
if err != nil { if err != nil {
@ -43,8 +39,6 @@ func GeoGate(handler http.Handler) http.Handler {
return return
} }
log.Info("Raw: %+v", record)
// Blocked by US states // Blocked by US states
if record.Country.IsoCode == "US" { if record.Country.IsoCode == "US" {
for _, sub := range record.Subdivisions { for _, sub := range record.Subdivisions {
@ -66,6 +60,7 @@ func GeoGate(handler http.Handler) http.Handler {
} }
// Debug info // Debug info
/*
fmt.Printf("Portuguese (BR) city name: %v\n", record.City.Names["pt-BR"]) fmt.Printf("Portuguese (BR) city name: %v\n", record.City.Names["pt-BR"])
if len(record.Subdivisions) > 0 { if len(record.Subdivisions) > 0 {
fmt.Printf("English subdivision name: %v\n", record.Subdivisions[0].Names["en"]) 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 // ISO country code: GB
// Time zone: Europe/London // Time zone: Europe/London
// Coordinates: 51.5142, -0.0931 // Coordinates: 51.5142, -0.0931
*/
} }
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)

View File

@ -1,6 +1,7 @@
package utility package utility
import ( import (
"net"
"net/http" "net/http"
"strings" "strings"
@ -19,5 +20,10 @@ func IPAddress(r *http.Request) string {
return strings.SplitN(xff, " ", 1)[0] return strings.SplitN(xff, " ", 1)[0]
} }
} }
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return r.RemoteAddr return r.RemoteAddr
}
return ip
} }