机架攻击:IP 地址数组 [英] Rack-Attack: Array of IP addresses
问题描述
我正在尝试创建一组 IP 地址,以便在应用程序运行时 Rack-Attack 可以从允许访问应用程序的 IP 地址集中进行识别.所以我所做的如下:
I'm trying to create an array of IP addresses so that when the application is ran Rack-Attack can identify from the set of IP addresses that are allowed to access the application. So what I have done is as followed:
a = "127.0.0.1"
Rack::Attack.blacklist('allow from localhost') do |req|
p "#{'127.0.0.1' == req.ip} "
a != req.ip
end
以上有效,因此本地主机可以访问该应用程序,但我尝试了以下似乎不起作用的以下方法:
The above works, so localhost can access the application but I have tried the following below which seems to not work what so ever:
a = "127.0.0.1", "1.2.3.4"
Rack::Attack.blacklist('allow from localhost') do |req|
a.select{|x| x != req.ip}.join("")
end
有人可以解释一下这样做的正确方法是什么.你可以看到我创建了一个数组.我想让 Rack::Attack
检测数组中的 IP 地址是否可以访问.
Can someone explain what the correct way would be to do this. You can see that I create an array. I want Rack::Attack
to detect whether the IP address in the array has access or not.
推荐答案
一种有效的方法是使用 Set,一个类似于数组的容器,但提供对单个、唯一元素的快速查找.
An efficient way to do this would be to use a Set, a container that's like an array but provides fast lookup on individual, unique elements.
因此,请记住这一点:
allowed = %w[ 127.0.0.1 1.2.3.4 ].to_set
Rack::Attack.blacklist('allow from localhost') do |req|
!allowed.include?(req.ip)
end
在您的原始声明中:
a = "x", "y"
在这种情况下,a
被分配给列表中的第一件事,"x"
,其余的被忽略.
In this case a
is assigned to the first thing in that list, "x"
, and the rest is ignored.
这篇关于机架攻击:IP 地址数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!