How to check if a given ip falls between a given ip range using node js
Solution 1
This is quite easy if we convert the ips to simple numbers:
function IPtoNum(ip){
return Number(
ip.split(".")
.map(d => ("000"+d).substr(-3) )
.join("")
);
}
Then we can check a certain range as:
if( IPtoNum(min) < IPtoNum(val) && IPtoNum(max) > IPtoNum(val) ) alert("in range");
That can also be applied to a table:
const ranges = [
["..41", "192.168.45"],
["123.124.125"," 126.124.123"]
];
const ip = "125.12.125";
const inRange = ranges.some(
([min,max]) => IPtoNum(min) < IPtoNum(ip) && IPtoNum(max) > IPtoNum(ip)
);
Solution 2
//Use getCIDR from rangecalc
getCIDR("5.9.0.0", "5.9.255.255")
//This return 5.9.0.0/16
//You can then use ipRangeCheck from ip_range_check
ipRangeCheck("IP TO BE CHECKED", "5.9.0.0/16")
//returns true or false
Pretty sure there's other way to do this.
INFOSYS
Updated on June 07, 2022Comments
-
INFOSYS almost 2 years
Please pardon for this trival question
Given a set of Ip the set is quite large and might increase https://github.com/client9/ipcat/blob/master/datacenters.csv#L4
Small example set - first column start ip second - end ip range
I will get the user ip from the request . I need to check if the ip falls in these set of ranges . How do i accomplish this.
I have looked into ip_range_check and range_check.
But they donot check for a ip given given range . How is thhis possible in node js with utmost performance. I dont want to go for a exhaustive search as performance is a hight priority.
Please help something new and quite challenging so far to me.
-
INFOSYS over 6 yearsThis would work but this will be exhaustive right like brute force. Something more performance wise will be better as we have huge ip range dnt you think. Still a solution none the less
-
Deja over 6 yearsThis might be helpful iptrie
-
INFOSYS over 6 yearsThanks for this answer but taking into account the large ip set will it be fast or give a good performance of the ip we need to check resides at the bottom of the table
-
Jonas Wilms over 6 years@infosys one could optimize it through converting the table from ips to numbers once. ( and i think further optimizations require big memory consumtion ) And have a look at bloom filters, they might do what youre looking for.
-
INFOSYS over 6 yearsThanks i will convert ip to numbers not an issue that function wont be required but what is bloom filter how to add it to that and search ?. I will up vote the answet and accept it too
-
Jonas Wilms over 6 years@infosys youre welcome ;) , as i said, if you use big tables for lookup (e.g. every ip address is stored as a boolean), then you have huge memory consumtion, but its very fast. Bloom filters do that with smaller memory but with loosing precision.
-
INFOSYS over 6 yearsI looked into bloom filters it like a hashtable of java. But how to insert the ip range and get the ip range this might be another question that hits my mind . Or how to search the bloom folter with min max?
-
Jonas Wilms over 6 years@infosys you cant search . the table would be huge. Thats a simple memory vs. performance discussion. And yeah, that would be a few other questions...
-
INFOSYS over 6 yearsmay be you can check this stackoverflow.com/questions/46203516/…