feat(geolite): add Tailscale IP detection with CGNAT range check (#77)

Co-authored-by: Elias Schneider <login@eliasschneider.com>
This commit is contained in:
soup
2024-11-29 23:17:08 +01:00
committed by GitHub
parent 9a8ec15678
commit edce3d3371

View File

@@ -5,15 +5,18 @@ import (
"compress/gzip" "compress/gzip"
"errors" "errors"
"fmt" "fmt"
"github.com/oschwald/maxminddb-golang/v2"
"github.com/stonith404/pocket-id/backend/internal/common"
"io" "io"
"log" "log"
"net"
"net/http" "net/http"
"net/netip" "net/netip"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
"github.com/oschwald/maxminddb-golang/v2"
"github.com/stonith404/pocket-id/backend/internal/common"
) )
type GeoLiteService struct{} type GeoLiteService struct{}
@@ -33,6 +36,13 @@ func NewGeoLiteService() *GeoLiteService {
// GetLocationByIP returns the country and city of the given IP address. // GetLocationByIP returns the country and city of the given IP address.
func (s *GeoLiteService) GetLocationByIP(ipAddress string) (country, city string, err error) { 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) db, err := maxminddb.Open(common.EnvConfig.GeoLiteDBPath)
if err != nil { if err != nil {
return "", "", err return "", "", err