如何使用节点js检查给定的ip是否落在给定的ip范围之间 [英] How to check if a given ip falls between a given ip range using node js
问题描述
请原谅这个问题
给定一组Ip,这个集非常大,可能会增加
我将从请求中获取用户ip。我需要检查ip是否属于这些范围。我如何做到这一点。
我已经研究过 ip_range_check 和 range_check 。
但他们没有给出给定范围的ip检查。如何在具有最佳性能的节点j中实现这一点。我不想进行详尽的搜索,因为性能是一个优先考虑。
请帮助我做一些新的,对我来说非常具有挑战性。
如果我们将ips转换为简单数字,这很容易:
function IPtoNum(ip){
return Number(
ip.split(。)
.map(d =>(000+ d).substr (-3))
.join()
);
}
然后我们可以检查某个范围:
if(IPtoNum(min)< IPtoNum(val)&& IPtoNum(max)> IPtoNum(val))alert(in range) );
这也可以应用于一个表:
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)
);
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.
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)
);
这篇关于如何使用节点js检查给定的ip是否落在给定的ip范围之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!