mirror of
https://github.com/safing/portmaster
synced 2025-09-02 10:39:22 +00:00
Shift focus away from coordinates for calculating IP distance
This commit is contained in:
parent
86fed20f71
commit
222249678e
1 changed files with 37 additions and 30 deletions
|
@ -49,50 +49,57 @@ func (l *Location) EstimateNetworkProximity(to *Location) (proximity int) {
|
||||||
// 100: same network/datacenter
|
// 100: same network/datacenter
|
||||||
|
|
||||||
// Weighting:
|
// Weighting:
|
||||||
// coordinate distance: 0-50
|
// continent match: 25
|
||||||
// continent match: 15
|
// country match: 20
|
||||||
// country match: 10
|
// AS owner match: 25
|
||||||
// AS owner match: 15
|
// AS network match: 20
|
||||||
// AS network match: 10
|
// coordinate distance: 0-10
|
||||||
|
|
||||||
// coordinate distance: 0-50
|
// continent match: 25
|
||||||
|
if l.Continent.Code == to.Continent.Code {
|
||||||
|
proximity += 25
|
||||||
|
// country match: 20
|
||||||
|
if l.Country.ISOCode == to.Country.ISOCode {
|
||||||
|
proximity += 20
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AS owner match: 25
|
||||||
|
if l.AutonomousSystemOrganization == to.AutonomousSystemOrganization {
|
||||||
|
proximity += 25
|
||||||
|
// AS network match: 20
|
||||||
|
if l.AutonomousSystemNumber == to.AutonomousSystemNumber {
|
||||||
|
proximity += 20
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// coordinate distance: 0-10
|
||||||
fromCoords := haversine.Coord{Lat: l.Coordinates.Latitude, Lon: l.Coordinates.Longitude}
|
fromCoords := haversine.Coord{Lat: l.Coordinates.Latitude, Lon: l.Coordinates.Longitude}
|
||||||
toCoords := haversine.Coord{Lat: to.Coordinates.Latitude, Lon: to.Coordinates.Longitude}
|
toCoords := haversine.Coord{Lat: to.Coordinates.Latitude, Lon: to.Coordinates.Longitude}
|
||||||
_, km := haversine.Distance(fromCoords, toCoords)
|
_, km := haversine.Distance(fromCoords, toCoords)
|
||||||
|
|
||||||
// proximity distance by accuracy
|
// adjust accuracy value
|
||||||
// get worst accuracy rating
|
|
||||||
accuracy := l.Coordinates.AccuracyRadius
|
accuracy := l.Coordinates.AccuracyRadius
|
||||||
if to.Coordinates.AccuracyRadius > accuracy {
|
switch {
|
||||||
|
case l.Coordinates.Latitude == 0 && l.Coordinates.Longitude == 0:
|
||||||
|
fallthrough
|
||||||
|
case to.Coordinates.Latitude == 0 && to.Coordinates.Longitude == 0:
|
||||||
|
// If we don't have any on any side coordinates, set accuracy to worst
|
||||||
|
// effective value.
|
||||||
|
accuracy = 1000
|
||||||
|
case to.Coordinates.AccuracyRadius > accuracy:
|
||||||
|
// If the destination accuracy is worse, use that one.
|
||||||
accuracy = to.Coordinates.AccuracyRadius
|
accuracy = to.Coordinates.AccuracyRadius
|
||||||
}
|
}
|
||||||
|
|
||||||
if km <= 10 && accuracy <= 100 {
|
if km <= 10 && accuracy <= 100 {
|
||||||
proximity += 50
|
proximity += 10
|
||||||
} else {
|
} else {
|
||||||
distanceIn50Percent := ((earthCircumferenceInKm - km) / earthCircumferenceInKm) * 50
|
distanceInPercent := (earthCircumferenceInKm - km) * 100 / earthCircumferenceInKm
|
||||||
|
|
||||||
// apply penalty for locations with low accuracy (targeting accuracy radius >100)
|
// apply penalty for locations with low accuracy (targeting accuracy radius >100)
|
||||||
accuracyModifier := 1 - float64(accuracy)/1000
|
accuracyModifier := 1 - float64(accuracy)/1000
|
||||||
proximity += int(distanceIn50Percent * accuracyModifier)
|
proximity += int(distanceInPercent * 0.10 * accuracyModifier)
|
||||||
}
|
|
||||||
|
|
||||||
// continent match: 15
|
|
||||||
if l.Continent.Code == to.Continent.Code {
|
|
||||||
proximity += 15
|
|
||||||
// country match: 10
|
|
||||||
if l.Country.ISOCode == to.Country.ISOCode {
|
|
||||||
proximity += 10
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// AS owner match: 15
|
|
||||||
if l.AutonomousSystemOrganization == to.AutonomousSystemOrganization {
|
|
||||||
proximity += 15
|
|
||||||
// AS network match: 10
|
|
||||||
if l.AutonomousSystemNumber == to.AutonomousSystemNumber {
|
|
||||||
proximity += 10
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return //nolint:nakedret
|
return //nolint:nakedret
|
||||||
|
|
Loading…
Add table
Reference in a new issue