From edce3d337129c9c6e8a60df2122745984ba0f3e0 Mon Sep 17 00:00:00 2001 From: soup Date: Fri, 29 Nov 2024 23:17:08 +0100 Subject: [PATCH] feat(geolite): add Tailscale IP detection with CGNAT range check (#77) Co-authored-by: Elias Schneider --- backend/internal/service/geolite_service.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/backend/internal/service/geolite_service.go b/backend/internal/service/geolite_service.go index d176652..9a8398a 100644 --- a/backend/internal/service/geolite_service.go +++ b/backend/internal/service/geolite_service.go @@ -5,15 +5,18 @@ import ( "compress/gzip" "errors" "fmt" - "github.com/oschwald/maxminddb-golang/v2" - "github.com/stonith404/pocket-id/backend/internal/common" "io" "log" + "net" "net/http" "net/netip" "os" "path/filepath" "time" + + "github.com/oschwald/maxminddb-golang/v2" + + "github.com/stonith404/pocket-id/backend/internal/common" ) type GeoLiteService struct{} @@ -33,6 +36,13 @@ func NewGeoLiteService() *GeoLiteService { // GetLocationByIP returns the country and city of the given IP address. func (s *GeoLiteService) GetLocationByIP(ipAddress string) (country, city string, err error) { + // Check if IP is in Tailscale's CGNAT range (100.64.0.0/10) + if ip := net.ParseIP(ipAddress); ip != nil { + if ip.To4() != nil && ip.To4()[0] == 100 && ip.To4()[1] >= 64 && ip.To4()[1] <= 127 { + return "Internal Network", "Tailscale", nil + } + } + db, err := maxminddb.Open(common.EnvConfig.GeoLiteDBPath) if err != nil { return "", "", err