Created
October 15, 2009 15:59
-
-
Save anonymous/211049 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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