如果 IP 位于 IP 范围之间,则限制该 IP
好的,今天是星期五下午,我度过了漫长的一周,所以希望得到一些帮助! 目前,我有一个 IP 范围列表,如下所示:
List<IPRange> ipRanges = new List<IPRange>();
ipRanges.Add(new IPRange { From = "145.36.0.0", To = "145.36.255.255" });
ipRanges.Add(new IPRange { From = "194.183.227.184", To = "194.183.227.191" });
ipRanges.Add(new IPRange { From = "193.131.192.0", To = "193.131.223.255" });
获取客户端的 IP 后,如果它落在这些范围集之间的任何位置,则需要将它们重定向到其他地方。
例如,
如果有人使用 IP 192.168.0.1
访问该网站,他们将被允许访问。 如果他们使用 145.36.1.0
进行访问,则不会允许他们访问,因为它位于该列表中的第一个范围之间。
我可以按周期分割每个 IP,并计算出范围开始变化的位置,然后进行比较,但这会对服务器造成沉重负担。
我知道 IP 基本上只是十进制数字,但我不太确定它是如何工作的。
以前有人遇到过这个吗?
干杯,肖恩。
Ok, it's friday afternoon, and i've had a long week so would appreciate some help!
Currently, i have a list of IP ranges, as follows:
List<IPRange> ipRanges = new List<IPRange>();
ipRanges.Add(new IPRange { From = "145.36.0.0", To = "145.36.255.255" });
ipRanges.Add(new IPRange { From = "194.183.227.184", To = "194.183.227.191" });
ipRanges.Add(new IPRange { From = "193.131.192.0", To = "193.131.223.255" });
After getting the IP of the client, if it falls anywhere between these sets of ranges, they need to be redirected elsewhere.
For example,
If someone visited the site with the IP 192.168.0.1
, they would be allowed access.
If they visited with 145.36.1.0
, they would not be allowed access because it falls between the first range in that list.
I could split each IP by the period, and work out where the range starts to change, then do a comparison, but that would be heavy on the server.
I know IP's are basically just decimal numbers, but am not really sure how that works.
Has anyone come across this before?
Cheers, Sean.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将每个 IP 地址转换为数字,然后检查用户 ip 地址是否在这些数字之间。
Convert Each IP-address to number, and then check if the user ip address is between those numbers.
我会将 IP 地址转换为 32 位数字,然后执行简单的 >= From 和 <= 来检查它是否在范围内。
例如,192.168.1.1→ 192 * 256^3 + 168 * 256^2 + 1 * 256 + 1。
按照您的价值观工作,145.36.0.0 -> 2435055616 和 145.36.0.0 -> 2435121151。所以 145.36.200.30 -> 2435106846,并且落在该范围内,因此它是有效的。但是 145.35.255.255 -> 2435055615 不在范围内(只是勉强),因此失败。
I would convert the IP addresses to 32-bit numbers and then do a simple >= From and <= To check to see if it's in range.
For example, 192.168.1.1 -> 192 * 256^3 + 168 * 256^2 + 1 * 256 + 1.
Working with your values, 145.36.0.0 -> 2435055616 and 145.36.0.0 -> 2435121151. So 145.36.200.30 -> 2435106846, and falls in that range, so it's valid. But 145.35.255.255 -> 2435055615 is not in the range (just barely), so it fails.
我会编写我的 IPRange 类,以便 getter/setter 在内部将 IP 字符串转换为数字:
在内部设置:
然后添加一个 .IsInRange(string ip) 方法,将传入的 IP 转换为 name int 形式并进行简单的比较。
这样,您就不必在每次检查时按句点分割范围内的 IP。
I would write my IPRange class so that the getter/setters internally convert the IP string to a number:
Would internally set:
Then add a .IsInRange(string ip) method that converts the incoming IP to the name int form and does a plain comparison.
That way you dont have to split the IPs in the range by periods each time its checked.
只是为了好玩(以及某种程度的完整性) - 另一种明显的方法是确保在存储为字符串时,IP 地址的每个段始终使用 3 位数字,即 145.36.0.0 应该是 145.036.000.000 - 即这样字符串就可以直接比较。
不太明显的是拥有一个显式的 IP 地址类和一套您自己的比较逻辑(我鲁莽地假设 .NET 框架的深处还没有这样的东西......)
Just for fun (and some semblance of completeness) - the other way obvious to do this is to ensure that you always use 3 digits for each segment of the IP address when stored as a string i.e. 145.36.0.0 should be 145.036.000.000 - that way the strings would be directly comparable.
And the Less obvious would be to have an explicit IP Address class and a roll your own set of comparison logic (I'm rashly assuming that there isn't already something like this buried in the depths of the .NET framework...)
几天前我读到了这篇文章。
您可以转换并比较您的 IP 范围。
这是它需要的 IPStringToNumber 函数:
参考资料:
http://www.sqlservercentral.com/脚本/Miscellaneous/31036/
http://www.sqlservercentral.com /作者/脚本/kgayda/17134/
I read about this a few days ago.
You can get your IP ranges converted and compare.
Here is the IPStringToNumber function that it needs:
References:
http://www.sqlservercentral.com/scripts/Miscellaneous/31036/
http://www.sqlservercentral.com/Authors/Scripts/kgayda/17134/