Fix matching unknown AS Orgs

This commit is contained in:
Daniel 2022-01-22 12:33:57 +01:00
parent bb78f85494
commit aec32fb6a2

View file

@ -3,7 +3,9 @@ package geoip
import ( import (
"encoding/binary" "encoding/binary"
"net" "net"
"strings"
"github.com/safing/portbase/utils"
"github.com/umahmood/haversine" "github.com/umahmood/haversine"
) )
@ -94,11 +96,14 @@ func (l *Location) EstimateNetworkProximity(to *Location) (proximity float32) {
} }
switch { 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, // Rely more on the ASN data, as it is more accurate than the ASOrg data,
// especially when combining location data from multiple sources. // especially when combining location data from multiple sources.
proximity += weightASOrgMatch + weightASNMatch 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 proximity += weightASOrgMatch
} }
@ -183,3 +188,20 @@ func PrimitiveNetworkProximity(from net.IP, to net.IP, ipVersion uint8) int {
return 0 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),
)
}