Add a rack middleware to forbid access to unsafe IP addresses
parent
8dcb7f76c0
commit
475b749cf6
|
@ -1,7 +1,8 @@
|
|||
require 'net/dns'
|
||||
require "project_honeypot/url"
|
||||
require "project_honeypot/base"
|
||||
require "project_honeypot/rack"
|
||||
require "project_honeypot/rack/header"
|
||||
require "project_honeypot/rack/forbidden"
|
||||
|
||||
module ProjectHoneypot
|
||||
class << self
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
module ProjectHoneypot
|
||||
module ProjectHoneypot
|
||||
class Base
|
||||
def initialize(api_key)
|
||||
@api_key = api_key
|
||||
|
@ -11,7 +11,7 @@ module ProjectHoneypot
|
|||
Url.new(ip_address, honeypot_score)
|
||||
end
|
||||
|
||||
private
|
||||
private
|
||||
|
||||
def url_to_ip(url)
|
||||
return url if url.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
module ProjectHoneypot::Rack
|
||||
class Forbidden
|
||||
def initialize(app, options={})
|
||||
@app = app
|
||||
|
||||
raise ArgumentError, 'Must specify an API key' unless options[:api_key]
|
||||
ProjectHoneypot.api_key = options[:api_key]
|
||||
end
|
||||
|
||||
def call(env)
|
||||
request = ::Rack::Request.new(env)
|
||||
url = ProjectHoneypot.lookup(request.ip)
|
||||
|
||||
if url.safe?
|
||||
@app.call(request.env)
|
||||
else
|
||||
[403, {"Content-Type" => "text/html"}, ["Forbidden"]]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,5 +1,5 @@
|
|||
module ProjectHoneypot
|
||||
class Rack
|
||||
module ProjectHoneypot::Rack
|
||||
class Header
|
||||
def initialize(app, options={})
|
||||
@app = app
|
||||
|
Loading…
Reference in New Issue