From aec32fb6a2f6ff101ffa6b4306ef98d7d2a40116 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 22 Jan 2022 12:33:57 +0100 Subject: [PATCH] Fix matching unknown AS Orgs --- intel/geoip/location.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/intel/geoip/location.go b/intel/geoip/location.go index 964d7ea0..ef89c208 100644 --- a/intel/geoip/location.go +++ b/intel/geoip/location.go @@ -3,7 +3,9 @@ package geoip import ( "encoding/binary" "net" + "strings" + "github.com/safing/portbase/utils" "github.com/umahmood/haversine" ) @@ -94,11 +96,14 @@ func (l *Location) EstimateNetworkProximity(to *Location) (proximity float32) { } switch { - case l.AutonomousSystemNumber != 0 && l.AutonomousSystemNumber == to.AutonomousSystemNumber: + case l.AutonomousSystemNumber == to.AutonomousSystemNumber && + l.AutonomousSystemNumber != 0: // Rely more on the ASN data, as it is more accurate than the ASOrg data, // especially when combining location data from multiple sources. proximity += weightASOrgMatch + weightASNMatch - case l.AutonomousSystemOrganization != "" && l.AutonomousSystemOrganization == to.AutonomousSystemOrganization: + case l.AutonomousSystemOrganization == to.AutonomousSystemOrganization && + l.AutonomousSystemNumber != 0 && // Check if an ASN is set. If the ASOrg is known, the ASN must be too. + !ASOrgUnknown(l.AutonomousSystemOrganization): // Check if the ASOrg name is valid. proximity += weightASOrgMatch } @@ -183,3 +188,20 @@ func PrimitiveNetworkProximity(from net.IP, to net.IP, ipVersion uint8) int { return 0 } } + +var unknownASOrgNames = []string{ + "", // Expected default for unknown. + "not routed", // Observed as "Not routed" in data set. + "unknown", // Observed as "UNKNOWN" in online data set. + "nil", // Programmatic unknown value. + "null", // Programmatic unknown value. + "undef", // Programmatic unknown value. + "undefined", // Programmatic unknown value. +} + +func ASOrgUnknown(asOrg string) bool { + return utils.StringInSlice( + unknownASOrgNames, + strings.ToLower(asOrg), + ) +}