Improved IP address handling (includes IPv6)

This commit is contained in:
JonnyWong16 2017-02-05 18:55:10 -08:00
parent d875f21647
commit ca472ff597
14 changed files with 178 additions and 220 deletions

View file

@ -266,20 +266,45 @@ function getPlatformImagePath(platformName) {
}
}
$.cachedScript = function (url) {
return $.ajax({
dataType: "script",
cache: true,
url: url
});
};
function isPrivateIP(ip_address) {
if (ip_address.indexOf(".") > -1) {
// get IPv4 mapped address (xxx.xxx.xxx.xxx) from IPv6 addresss (::ffff:xxx.xxx.xxx.xxx)
var parts = ip_address.split(":");
var parts = parts[parts.length - 1].split('.');
if ((parts[0] === '127' && parts[1] === '0' && parts[2] === '0' && parts[3] === '1') || (parts[0] === '10') ||
(parts[0] === '172' && (parseInt(parts[1], 10) >= 16 && parseInt(parts[1], 10) <= 31)) || (parts[0] ===
'192' && parts[1] === '168')) {
return true;
var defer = $.Deferred();
$.cachedScript('js/ipaddr.min.js').done(function () {
if (ipaddr.isValid(ip_address)) {
var addr = ipaddr.process(ip_address)
if (addr.kind() === 'ipv4') {
var rangeList = [
ipaddr.parseCIDR('127.0.0.0/8'),
ipaddr.parseCIDR('10.0.0.0/8'),
ipaddr.parseCIDR('172.16.0.0/12'),
ipaddr.parseCIDR('192.168.0.0/16')
]
} else {
var rangeList = [
ipaddr.parseCIDR('fd00::/8')
]
}
if (ipaddr.subnetMatch(addr, rangeList, -1) >= 0) {
defer.resolve();
} else {
defer.reject();
}
} else {
defer.resolve('n/a');
}
return false;
} else {
return true;
}
})
return defer.promise();
}
function humanTime(seconds) {