Skip to content

IP-Block-Ltd/ipblock-rails

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⚠️ 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).

IP Block — Rails / Rack Gem

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+.

Install

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.

Configure

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"]
end

Plain Rack (non-Rails)

require "ip_block"

IpBlock.configure { |c| c.site_id = "..."; c.api_key = "..." }

use IpBlock::Middleware
run MyApp

How it works

  • Builds {api_key, site_id, ip, user_agent, referrer} and POSTs it via Net::HTTP with 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 = false to fail closed).
  • Caches each decision for cache_ttl seconds in a thread-safe in-memory store, keyed by md5(ip|user_agent|referrer).
  • Honours whitelist (individual IPs and CIDR ranges via IPAddr).
  • Reads the real client IP; with behind_proxy = true it trusts CF-Connecting-IP then the first X-Forwarded-For hop.

About

IP Block protection for Ruby on Rails — ip-block.com integration. Untested at the moment; please feel free to modify. GPLv3.

Topics

Resources

License

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages