Forked from ybom/gist:55647540174d2bc983757a634505413f
Created
December 21, 2021 21:00
-
-
Save felipepodesta/7bc5f3aafd97e72ad8e60c4288aac1e7 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
class Rack::Attack | |
# `Rack::Attack` is configured to use the `Rails.cache` value by default, | |
# but you can override that by setting the `Rack::Attack.cache.store` value | |
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new | |
# Always allow requests from localhost | |
# (blocklist & throttles are skipped) | |
Rack::Attack.safelist('allow from localhost') do |req| | |
'127.0.0.1' == req.ip || '::1' == req.ip | |
end | |
# Throttle all requests by IP (20rpm) | |
throttle('req/ip', :limit => 100, :period => 5.minutes) do |req| | |
req.ip # unless req.path.start_with?('/assets') | |
end | |
Rack::Attack.throttled_response = lambda do |env| | |
now = Time.now | |
match_data = env['rack.attack.match_data'] | |
headers = { | |
'X-RateLimit-Limit' => match_data[:limit].to_s, | |
'X-RateLimit-Remaining' => '0', | |
'X-RateLimit-Reset' => (now + (match_data[:period] - now.to_i % match_data[:period])).to_s | |
} | |
[ 429, headers, ["Server Error\n"]] | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment