Created
September 27, 2021 04:39
-
-
Save guitarrapc/86a626b896a2f30c10cd32eafdb3304f to your computer and use it in GitHub Desktop.
Simply detect IPAddress is in specified CIDRs. no dependencies to any packages. ES5.1 compatible.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var range = getIpRange("192.168.0.0/24"); | |
console.log(`range: min ${range.min} max ${range.max}`); // {min: "192.168.0.0", max: "192.168.0.255"} | |
var ip1 = getIpRange("192.168.0.5"); | |
console.log(`ip1: min ${ip1.min} max ${ip1.max}`); // {min: "192.168.0.0", max: "192.168.0.255"} | |
var ip2 = getIpRange("192.168.0.100"); | |
console.log(`ip2: min ${ip2.min} max ${ip2.max}`); // {min: "192.168.0.0", max: "192.168.0.255"} | |
var ip3 = getIpRange("192.168.1.1"); | |
console.log(`ip3: min ${ip3.min} max ${ip3.max}`); // {min: "192.168.0.0", max: "192.168.0.255"} | |
if (ip1.min > range.min && ip1.max < range.max) { | |
console.log("in!") | |
} | |
if (ip3.min > range.min && ip3.max < range.max) { | |
console.log("in!") | |
} | |
isIpInCidr("192.168.0.1", ["192.168.1.0/24", "192.168.10.0/24", "192.168.0.0/24"]); | |
/** | |
* Detect IpAddress is in cidr or not | |
* @param {String} ipAddress The IP string to check. 192.168.0.1 | |
* @param {Array} cidrs The array of CIDR. [192.168.0.0/24] | |
* @return {Boolean} If IP string is in CIDR true, elase false. | |
**/ | |
function isIpInCidr(ipAddress, cidrs) { | |
var ip = getIpRange(ipAddress); | |
for (var i = 0; i < cidrs.length; i++) { | |
var range = getIpRange(cidrs[i]); | |
if (ip.min >= range.min && ip.max <= range.max) { | |
console.log(`${ipAddress} is in ${cidrs[i]}`); | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* Convert IP string to Bit string | |
* @param {Array} array The array of IP String without subnet. ['127', '0', '0', '0'] | |
* @return {String} The bit string converted. 01111111000000000000000000000000 | |
**/ | |
function convertToBitString(array) { | |
var ret = ''; | |
for (var i = 0; i < 4; i++) { | |
var bit = '00000000' + parseInt(array[i], 10).toString(2); | |
ret += bit.slice(-8); | |
} | |
return ret; | |
} | |
/** | |
* Convert Bit string to IP string | |
* @param {String} input The bit string input to convert. 01111111000000000000000000000000 | |
* @return {String} The ip string converted. 127.0.0.1 | |
**/ | |
function convertToIpString(input) { | |
var ret = '' | |
ret = parseInt(input.slice(0, 8), 2) + '.'; | |
ret += parseInt(input.slice(8, 16), 2) + '.'; | |
ret += parseInt(input.slice(16, 24), 2) + '.'; | |
ret += parseInt(input.slice(24, 32), 2); | |
return ret; | |
} | |
/** | |
* Get min,max of IP Range | |
* @param {String} ipAddress The IP String to detect range. You can use both CIDR and non-CIDR. 192.168.0.0/24 or 127.0.0.1 | |
* @return {Object} The range of the Bit string represent min, max. {min:11000000101010000000000000000000, max:11000000101010000000000011111111} | |
**/ | |
function getIpRange(ipAddress) { | |
var ip = ipAddress.split('/'), | |
group = ip[0].split('.'), | |
ipBit = '', | |
minIpBit = '', | |
maxIpBit = ''; | |
// validation | |
if (ip === '' || | |
group.length !== 4 || | |
(ip.length === 2 && String(ip[1]).match(/^([1-9]|[1-2][0-9]|3[0-2])$/) === null)) { | |
return { min: '', max: '' }; // empty | |
} | |
minIpBit = convertToBitString(group); | |
// no cidr input | |
if (ip.length === 1) { | |
return { min: minIpBit, max: minIpBit }; | |
} | |
// with cidr input | |
for (var i = 0; i < 4; i++) { | |
var bit = parseInt(group[i], 10).toString(2); | |
if (Number(ip[1]) >= ((i + 1) * 8)) { | |
ipBit += ('00000000' + bit).slice(-8); | |
} | |
else { | |
var tmpIpBit = ('00000000' + bit).slice(-8); | |
ipBit += (tmpIpBit.slice(0, Number(ip[1]) - (i * 8)) + '11111111').slice(0, 8); | |
break; | |
} | |
} | |
maxIpBit = (ipBit + '11111111111111111111111111111111').slice(0, 32); | |
return { min: minIpBit, max: maxIpBit }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment