Skip to content

Instantly share code, notes, and snippets.

Created October 15, 2009 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save anonymous/211049 to your computer and use it in GitHub Desktop.
Save anonymous/211049 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'net/dns/resolver'
DEFAULT_SERVERS = [ 'dnsbl.ahbl.org' ]
module Rack
class RBL
# List of RBL servers to check against
attr_accessor :servers
def initialize(app, options = {})
@app = app
self.servers = options[:servers] || DEFAULT_SERVERS
end
def call(env)
if blacklisted? env['REMOTE_ADDR']
[403, {"Content-Type" => "text/html"}, "Sorry, you are on a blacklist."]
else
@app.call(env)
end
end
def blacklisted?(addr)
# reverse IP address octets
raddr = addr.split('.').reverse.join('.')
servers.each do |s|
return true unless Resolver("#{raddr}.#{s}").answer.empty?
end
false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment