Given a single public IP address (peer A) and a list of many other public IP addresses (a mix of IPv4 and IPv6 addresses), what is the easiest way to match up peer A the IP addr
Install https://github.com/runk/node-maxmind
Download 'GeoLite2-City.mmdb' from: http://dev.maxmind.com/geoip/geoip2/geolite2/
var maxmind = require('maxmind');
var lookup = maxmind.open('./GeoLite2-City.mmdb');
/**/
var peers = [
'31.193.128.0', // UK
'23.112.0.0', // USA
'5.24.0.0', // Turkey
'196.203.0.0', // Tunisia
'77.243.64.0' // Malta
];
var peerLocations = {};
peers.forEach(function(peer) {
var tmp = lookup.get(peer);
if (!tmp || !tmp.location) {
throw new Error('Unable to get initial peer location: ' + peer);
}
peerLocations[peer] = tmp.location;
});
/**/
var testIp = '84.17.64.0'; // Turkey
// 84.17.64.0 // Turkey
// 37.219.0.0 // Finland
// 5.39.0.0 // France
// 37.75.32.0 // Malta
// 5.2.96.0 // UK
// 15.0.0.0 // USA
// 41.224.0.0 // Tunisia
console.log( findClosestPeer(testIp, 3) );
function findClosestPeer(ip, len) {
var ipData = lookup.get(ip);
var distances = [];
if (ipData && ipData.location) {
Object.keys(peerLocations).forEach(function(key) {
var peer = peerLocations[key];
var distance = getDistanceFromLatLonInKM(ipData.location.latitude, ipData.location.longitude,
peer.latitude, peer.longitude);
distances.push({ip: key, distance: distance});
});
}
// 0 ... 9
distances.sort(function(a, b) {
return a.distance - b.distance;
});
return len > 1 ? distances.slice(0, len)
: distances.shift();
}
/* http://stackoverflow.com/a/21279990/605399 */
function getDistanceFromLatLonInKM(lat1, lon1, lat2, lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2 - lat1); // deg2rad below
var dLon = deg2rad(lon2 - lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1 - a) );
var d = R * c; // Distance in km
return d;
}
function deg2rad(deg) {
return deg * ( Math.PI / 180 );
}