subnet-check
Version:
Check if an IPv4 or IPv6 address is contained in the given CIDR subnet
87 lines • 3.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var net = require("./net");
/**
* Given an IPv4 address, convert it to a 32-bit long integer.
* @param ip the IPv4 address to expand
* @throws if the string is not a valid IPv4 address
*/
function ipv4ToLong(ip) {
if (!net.isIPv4(ip)) {
throw new Error("not a valid IPv4 address: " + ip);
}
var octets = ip.split('.');
return (((parseInt(octets[0], 10) << 24) +
(parseInt(octets[1], 10) << 16) +
(parseInt(octets[2], 10) << 8) +
parseInt(octets[3], 10)) >>>
0);
}
/**
* Test if the given IPv4 address is contained in the specified subnet.
* @param address the IPv4 address to check
* @param subnet the IPv4 CIDR to test (or an array of them)
* @throws if the address or subnet are not valid IP addresses, or the CIDR prefix length
* is not valid
*/
function isInSubnet(address, subnetOrSubnets) {
if (Array.isArray(subnetOrSubnets)) {
return subnetOrSubnets.some(function (subnet) { return isInSubnet(address, subnet); });
}
var subnet = subnetOrSubnets;
var _a = subnet.split('/'), subnetAddress = _a[0], prefixLengthString = _a[1];
var prefixLength = parseInt(prefixLengthString, 10);
if (!subnetAddress || !Number.isInteger(prefixLength)) {
throw new Error("not a valid IPv4 subnet: " + subnet);
}
if (prefixLength < 0 || prefixLength > 32) {
throw new Error("not a valid IPv4 prefix length: " + prefixLength + " (from " + subnet + ")");
}
// the next two lines throw if the addresses are not valid IPv4 addresses
var subnetLong = ipv4ToLong(subnetAddress);
var addressLong = ipv4ToLong(address);
if (prefixLength === 0) {
return true;
}
var subnetPrefix = subnetLong >> (32 - prefixLength);
var addressPrefix = addressLong >> (32 - prefixLength);
return subnetPrefix === addressPrefix;
}
exports.isInSubnet = isInSubnet;
/** Test if the given IP address is a private/internal IP address. */
function isPrivate(address) {
return isInSubnet(address, ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16']);
}
exports.isPrivate = isPrivate;
/** Test if the given IP address is a localhost address. */
function isLocalhost(address) {
return isInSubnet(address, '127.0.0.0/8');
}
exports.isLocalhost = isLocalhost;
/** Test if the given IP address is in a known reserved range and not a normal host IP */
function isReserved(address) {
return isInSubnet(address, [
'0.0.0.0/8',
'100.64.0.0/10',
'169.254.0.0/16',
'192.0.0.0/24',
'192.0.2.0/24',
'192.88.99.0/24',
'198.18.0.0/15',
'198.51.100.0/24',
'203.0.113.0/24',
'224.0.0.0/4',
'240.0.0.0/4',
'255.255.255.255/32' // limited broadcast address
]);
}
exports.isReserved = isReserved;
/**
* Test if the given IP address is a special address of any kind (private, reserved,
* localhost)
*/
function isSpecial(address) {
return isPrivate(address) || isLocalhost(address) || isReserved(address);
}
exports.isSpecial = isSpecial;
//# sourceMappingURL=ipv4.js.map