satellite/geoip: exclude nodes with represented_contry from geofencing
Change-Id: I80b8e5d98f46559b158a26c47fff0586b97aff79
This commit is contained in:
parent
0fd7f2958f
commit
ceb7b7375c
@ -22,9 +22,12 @@ func OpenMaxmindDB(filepath string) (*MaxmindDB, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ipInfo struct {
|
type ipInfo struct {
|
||||||
Country struct {
|
Country country `maxminddb:"country"`
|
||||||
|
RepresentedCountry country `maxminddb:"represented_country"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type country struct {
|
||||||
IsoCode string `maxminddb:"iso_code"`
|
IsoCode string `maxminddb:"iso_code"`
|
||||||
} `maxminddb:"country"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MaxmindDB provides access to GeoIP data via the maxmind geoip databases.
|
// MaxmindDB provides access to GeoIP data via the maxmind geoip databases.
|
||||||
@ -52,5 +55,14 @@ func (m *MaxmindDB) LookupISOCountryCode(address string) (location.CountryCode,
|
|||||||
return location.CountryCode(0), err
|
return location.CountryCode(0), err
|
||||||
}
|
}
|
||||||
|
|
||||||
return location.ToCountryCode(info.Country.IsoCode), nil
|
return toCountryCode(info), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func toCountryCode(info *ipInfo) location.CountryCode {
|
||||||
|
// it's a tricky situation when represented_country is returned (like an embassy or military base).
|
||||||
|
// we have only 1-2 such nodes. it's more safe to exclude them from geofencing.
|
||||||
|
if info.RepresentedCountry.IsoCode != "" && info.RepresentedCountry.IsoCode != info.Country.IsoCode {
|
||||||
|
return location.None
|
||||||
|
}
|
||||||
|
return location.ToCountryCode(info.Country.IsoCode)
|
||||||
}
|
}
|
||||||
|
59
satellite/geoip/maxmind_test.go
Normal file
59
satellite/geoip/maxmind_test.go
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
// Copyright (C) 2023 Storj Labs, Inc.
|
||||||
|
// See LICENSE for copying information
|
||||||
|
|
||||||
|
package geoip
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"storj.io/common/storj/location"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestToCountryCode(t *testing.T) {
|
||||||
|
require.Equal(t, location.Germany, toCountryCode(&ipInfo{
|
||||||
|
Country: country{
|
||||||
|
IsoCode: "DE",
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|
||||||
|
require.Equal(t, location.Germany, toCountryCode(&ipInfo{
|
||||||
|
Country: country{
|
||||||
|
IsoCode: "DE",
|
||||||
|
},
|
||||||
|
RepresentedCountry: country{
|
||||||
|
IsoCode: "DE",
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|
||||||
|
require.Equal(t, location.None, toCountryCode(&ipInfo{
|
||||||
|
Country: country{
|
||||||
|
IsoCode: "DE",
|
||||||
|
},
|
||||||
|
RepresentedCountry: country{
|
||||||
|
IsoCode: "US",
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMaxmind(t *testing.T) {
|
||||||
|
maxmindDB := os.Getenv("TEST_MAXMIND_DB")
|
||||||
|
if maxmindDB == "" {
|
||||||
|
t.Skip("Optional test")
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := OpenMaxmindDB(maxmindDB)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// these assertions are based on the db from 2023-08-04. Can be different with different DB.
|
||||||
|
|
||||||
|
code, err := db.LookupISOCountryCode("62.112.192.4:80")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, location.Hungary, code)
|
||||||
|
|
||||||
|
code, err = db.LookupISOCountryCode("178.76.189.106:28967")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, location.None, code)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user