0

我已经安装并配置了 Rack::Attack,但列入黑名单的 ip 地址仍在不断地访问我的网站。

在 config/application.rb 中:

require_relative 'boot'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module MyApp
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.1
    config.middleware.use Rack::Attack


    ActionController::Base.config.relative_url_root = ''
  end
end

并在初始化程序/rack_attack.rb

class Rack::Attack

  Rack::Attack.blocklist_ip("46.229.168.154")
  Rack::Attack.blocklist_ip("23.101.169.3")

  RANGE = (IPAddr.new('54.36.0.0').to_i..IPAddr.new('54.38.255.255').to_i)
  Rack::Attack.blocklist('block_local_network')  do|req|
    RANGE.include?(IPAddr.new(req.ip).to_i)
  end


end

安装配置后,相同的 ip_addresses 仍在访问我的站点。我很兴奋,因为我的流量比平时增加了 5 倍,结果却发现这些垃圾邮件机器人过得很好。

4

1 回答 1

1

检查您的安全列表,看看是否有您要阻止的 IP 地址的范围或占位符。如果某个 IP 地址被安全列表覆盖,则即使它在阻止列表中也不会被阻止。顺便说一句,您的代码可以通过 IP 范围简化一点。另外,IPAddr.new(req.ip).to_i 不是必需的,只需使用 req.ip 代替。

RANGE = IPAddr.new '54.36.0.0/14'
puts RANGE.to_range                  # 54.36.0.0..54.39.255.255
puts RANGE.include? '54.36.0.0'      # true
puts RANGE.include? '54.39.255.255'  # true
于 2019-03-25T13:53:16.993 回答