⚠️ Status: untested. This extension is provided as-is and has not been tested in production. Please feel free to fork, modify, improve, and open pull requests.Licensed under GNU GPLv3 (see LICENSE).
Rack middleware (packaged as a gem, with a Railtie for Rails) that checks each request against the ip-block.com service and blocks disallowed clients.
Targets: Rails 5.2+ / Rack 2.0+ / Ruby 2.7+.
Add to your Gemfile:
gem "ip_block"Then bundle install. In a Rails app the Railtie inserts the middleware
automatically — no manual config.middleware.use needed.
Create config/initializers/ip_block.rb:
IpBlock.configure do |c|
c.enabled = true
c.site_id = ENV["IP_BLOCK_SITE_ID"]
c.api_key = ENV["IP_BLOCK_API_KEY"]
c.api_url = "https://api.ip-block.com/v1/check" # default
c.fail_open = true # allow on error/timeout (default)
c.cache_ttl = 300 # seconds (in-memory cache)
c.timeout = 1.0 # seconds
c.behind_proxy = false # trust X-Forwarded-For / CF-Connecting-IP
c.block_action = "403" # "403" or "redirect"
c.redirect_url = "https://www.ip-block.com/blocked.php"
c.block_message = "Access denied."
c.whitelist = ["127.0.0.1", "10.0.0.0/8"]
endrequire "ip_block"
IpBlock.configure { |c| c.site_id = "..."; c.api_key = "..." }
use IpBlock::Middleware
run MyApp- Builds
{api_key, site_id, ip, user_agent, referrer}andPOSTs it viaNet::HTTPwith a 1 second open/read timeout. - Blocks only when the response is
{"action":"block"}. - Fails open on any error/timeout/non-2xx/missing
action(c.fail_open = falseto fail closed). - Caches each decision for
cache_ttlseconds in a thread-safe in-memory store, keyed bymd5(ip|user_agent|referrer). - Honours
whitelist(individual IPs and CIDR ranges viaIPAddr). - Reads the real client IP; with
behind_proxy = trueit trustsCF-Connecting-IPthen the firstX-Forwarded-Forhop.