Skip to content

Commit

Permalink
feat(geolite): add Tailscale IP detection with CGNAT range check (#77)
Browse files Browse the repository at this point in the history
Co-authored-by: Elias Schneider <[email protected]>
  • Loading branch information
s0up4200 and stonith404 authored Nov 29, 2024
1 parent 9a8ec15 commit edce3d3
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions backend/internal/service/geolite_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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
Expand Down

0 comments on commit edce3d3

Please sign in to comment.