better_rate_limit 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c4d162e7489abec1ddc7da39b21c0ae310006ed0b95019d319cd078165535ef6
4
+ data.tar.gz: b17e2dcbd2a77f637a06a7402724863f29fb8331c7693128ae78650b35377dce
5
+ SHA512:
6
+ metadata.gz: 84678f8937e1e9c0289155a2879d2e7843f62545e358d002e6abe6e77b0adb4afa645328a09faa9e075464924db34dea816ae4fc85b14b8f1b247c87c0493426
7
+ data.tar.gz: 86b48ab0065c39fb44450053bc45152e5af7d11824380a3867d3c1a8f49b86a7c1d4b8dcd7a02ac0804b4683e3ff968082b46c47c8c8185f650baa62f07929b7
@@ -0,0 +1,70 @@
1
+ <<<<<<< HEAD
2
+ *.gem
3
+ *.rbc
4
+ /.config
5
+ /coverage/
6
+ /InstalledFiles
7
+ /pkg/
8
+ /spec/reports/
9
+ /spec/examples.txt
10
+ /test/tmp/
11
+ /test/version_tmp/
12
+ /tmp/
13
+
14
+ # Used by dotenv library to load environment variables.
15
+ # .env
16
+
17
+ # Ignore Byebug command history file.
18
+ .byebug_history
19
+
20
+ ## Specific to RubyMotion:
21
+ .dat*
22
+ .repl_history
23
+ build/
24
+ *.bridgesupport
25
+ build-iPhoneOS/
26
+ build-iPhoneSimulator/
27
+
28
+ ## Specific to RubyMotion (use of CocoaPods):
29
+ #
30
+ # We recommend against adding the Pods directory to your .gitignore. However
31
+ # you should judge for yourself, the pros and cons are mentioned at:
32
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
33
+ #
34
+ # vendor/Pods/
35
+
36
+ ## Documentation cache and generated files:
37
+ /.yardoc/
38
+ /_yardoc/
39
+ /doc/
40
+ /rdoc/
41
+
42
+ ## Environment normalization:
43
+ /.bundle/
44
+ /vendor/bundle
45
+ /lib/bundler/man/
46
+
47
+ # for a library or gem, you might want to ignore these files since the code is
48
+ # intended to run in multiple environments; otherwise, check them in:
49
+ # Gemfile.lock
50
+ # .ruby-version
51
+ # .ruby-gemset
52
+
53
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
54
+ .rvmrc
55
+
56
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
57
+ # .rubocop-https?--*
58
+ =======
59
+ /.bundle/
60
+ /.yardoc
61
+ /_yardoc/
62
+ /coverage/
63
+ /doc/
64
+ /pkg/
65
+ /spec/reports/
66
+ /tmp/
67
+
68
+ # rspec failure tracking
69
+ .rspec_status
70
+ >>>>>>> init gem
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.5
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rate-limit.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+
8
+ group :development, :test do
9
+ gem "pry"
10
+ end
11
+
12
+ group :test do
13
+ gem "timecop"
14
+ gem "mocha"
15
+ gem "mock_redis"
16
+ gem "rails-controller-testing"
17
+ end
@@ -0,0 +1,81 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ better_rate_limit (0.1.1)
5
+ actionpack (>= 5.0)
6
+ redis (>= 3.3)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionpack (5.2.4.3)
12
+ actionview (= 5.2.4.3)
13
+ activesupport (= 5.2.4.3)
14
+ rack (~> 2.0, >= 2.0.8)
15
+ rack-test (>= 0.6.3)
16
+ rails-dom-testing (~> 2.0)
17
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
18
+ actionview (5.2.4.3)
19
+ activesupport (= 5.2.4.3)
20
+ builder (~> 3.1)
21
+ erubi (~> 1.4)
22
+ rails-dom-testing (~> 2.0)
23
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
24
+ activesupport (5.2.4.3)
25
+ concurrent-ruby (~> 1.0, >= 1.0.2)
26
+ i18n (>= 0.7, < 2)
27
+ minitest (~> 5.1)
28
+ tzinfo (~> 1.1)
29
+ builder (3.2.4)
30
+ coderay (1.1.3)
31
+ concurrent-ruby (1.1.6)
32
+ crass (1.0.6)
33
+ erubi (1.9.0)
34
+ i18n (1.8.5)
35
+ concurrent-ruby (~> 1.0)
36
+ loofah (2.6.0)
37
+ crass (~> 1.0.2)
38
+ nokogiri (>= 1.5.9)
39
+ method_source (1.0.0)
40
+ mini_portile2 (2.4.0)
41
+ minitest (5.14.1)
42
+ mocha (1.11.2)
43
+ mock_redis (0.25.0)
44
+ nokogiri (1.10.10)
45
+ mini_portile2 (~> 2.4.0)
46
+ pry (0.13.1)
47
+ coderay (~> 1.1)
48
+ method_source (~> 1.0)
49
+ rack (2.2.3)
50
+ rack-test (0.6.3)
51
+ rack (>= 1.0)
52
+ rails-controller-testing (1.0.5)
53
+ actionpack (>= 5.0.1.rc1)
54
+ actionview (>= 5.0.1.rc1)
55
+ activesupport (>= 5.0.1.rc1)
56
+ rails-dom-testing (2.0.3)
57
+ activesupport (>= 4.2.0)
58
+ nokogiri (>= 1.6)
59
+ rails-html-sanitizer (1.3.0)
60
+ loofah (~> 2.3)
61
+ rake (12.3.3)
62
+ redis (4.2.1)
63
+ thread_safe (0.3.6)
64
+ timecop (0.9.1)
65
+ tzinfo (1.2.7)
66
+ thread_safe (~> 0.1)
67
+
68
+ PLATFORMS
69
+ ruby
70
+
71
+ DEPENDENCIES
72
+ better_rate_limit!
73
+ mocha
74
+ mock_redis
75
+ pry
76
+ rails-controller-testing
77
+ rake (~> 12.0)
78
+ timecop
79
+
80
+ BUNDLED WITH
81
+ 2.1.4
@@ -0,0 +1,50 @@
1
+ # BetterRateLimit
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/better_rate_limit`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'better_rate_limit', git: 'https://github.com/upscopeio/better_rate_limit'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install better_rate_limit
20
+
21
+ ## Usage
22
+
23
+ BetterRateLimit adds a controller class method `better_rate_limit`, you can add it to the `ApplicationController`, like the example below:
24
+
25
+ ```ruby
26
+ class ApplicationController < ActionController::Base
27
+ better_rate_limit 200, every: 1.minute
28
+ end
29
+ ```
30
+
31
+ #### Notifier
32
+ Implement your own notifier logic by subscribing to the `rate_limit.notify` event
33
+
34
+ ```ruby
35
+ ActiveSupport::Notifications.subscribe /rate_limit/ do |_name, _start, _finish, _id, payload|
36
+ # Your Notifier logic goes here
37
+ # The payload param will return the better_rate_limit_key
38
+ # e.g ( payload => { rate_limit_key: 'controller_throttle:auth/session:10:900:127.0.0.1' } )
39
+ end
40
+ ```
41
+
42
+ ## Development
43
+
44
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
45
+
46
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/upscopeio/better_rate_limit.
@@ -0,0 +1,15 @@
1
+ # froze_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+
5
+ require "rake/testtask"
6
+
7
+ desc "Run unit tests"
8
+ task default: :test
9
+
10
+ desc "Test BetterRateLimit"
11
+ Rake::TestTask.new(:test) do |t|
12
+ t.libs << "test"
13
+ t.pattern = "test/**/*_test.rb"
14
+ t.verbose = true
15
+ end
@@ -0,0 +1,28 @@
1
+ require_relative 'lib/better_rate_limit/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "better_rate_limit"
5
+ spec.version = BetterRateLimit::VERSION
6
+ spec.authors = ["Pablo Fonseca", "Joe d'Elia"]
7
+ spec.email = ["pablofonseca777@gmail.com", "joe@delia.is"]
8
+
9
+ spec.summary = %q{Rate limit requests}
10
+ spec.description = %q{Adds rate limit for requests}
11
+ spec.homepage = "https://github.com/upscopeio/better_rate_limit"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = spec.homepage
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency "redis", ">= 3.3"
27
+ spec.add_dependency "actionpack", ">= 5.0"
28
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rate_limit"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,68 @@
1
+ require 'ostruct'
2
+ require 'better_rate_limit/throttle'
3
+
4
+ module ActionController
5
+ module BetterRateLimit
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+ def rate_limit(max, every:, name: nil, scope: -> { real_ip }, only: [], except: [])
10
+ rate_limits << OpenStruct.new({
11
+ max: max,
12
+ every: every,
13
+ name: name || controller_path,
14
+ scope: scope,
15
+ only: only,
16
+ except: except,
17
+ controller_path: controller_path
18
+ })
19
+
20
+ before_action :perform_rate_limiting
21
+ end
22
+
23
+ def rate_limits
24
+ @rate_limits ||= []
25
+ end
26
+
27
+ def all_rate_limits
28
+ return rate_limits unless superclass.respond_to? :all_rate_limits
29
+
30
+ rate_limits + superclass.all_rate_limits
31
+ end
32
+ end
33
+
34
+ protected
35
+
36
+ def perform_rate_limiting
37
+ return if self.class.all_rate_limits.map { |r| under_rate_limit?(r) }.all? { |r| r === true }
38
+
39
+ return render json: { error: 'Too many requests' }, status: :too_many_requests if json?
40
+
41
+ send_file 'public/429.html', status: :too_many_requests
42
+ end
43
+
44
+ private
45
+
46
+ def json?
47
+ request.xhr? || request.format === :json
48
+ end
49
+
50
+ def real_ip
51
+ request.headers['X-Forwarded-For'].try(:split, ',').try(:[], -2..-2).try(:first).try(:strip)
52
+ end
53
+
54
+ def under_rate_limit?(limit)
55
+ if limit.controller_path == controller_path
56
+ return true if action_name.to_sym.in?([limit.except].flatten)
57
+ return true if !limit.only.empty? && !action_name.to_sym.in?([limit.only].flatten)
58
+ end
59
+
60
+ scope = limit.scope.is_a?(Proc) ? instance_exec(&limit.scope) : send(limit.scope)
61
+ scope = scope.to_param if scope.respond_to?(:to_param)
62
+
63
+ key = ['controller_throttle', limit.name, limit.max, limit.every, scope].join(':')
64
+
65
+ ::BetterRateLimit::Throttle.allow? key, limit: limit.max, time_window: limit.every
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,9 @@
1
+ require 'action_controller'
2
+
3
+ module ActionController
4
+ autoload :BetterRateLimit, 'action_controller/better_rate_limit'
5
+ end
6
+
7
+ ActiveSupport.on_load(:action_controller) do
8
+ include ActionController::BetterRateLimit
9
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'redis'
4
+
5
+ module BetterRateLimit
6
+ module RedisConnection
7
+ def self.included(host)
8
+ host.extend ClassMethods
9
+ end
10
+
11
+ module ClassMethods
12
+ def redis_client
13
+ @redis_client ||= Redis.new(url: ENV.fetch('REDIS_URL', 'redis://localhost:6379'))
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/numeric/time'
4
+ require 'active_support/core_ext/time/calculations'
5
+ require 'better_rate_limit/redis_connection'
6
+
7
+ module BetterRateLimit
8
+ module Throttle
9
+ include RedisConnection
10
+
11
+ class << self
12
+ def throttle(key, limit:, time_window:)
13
+ now = Time.now.utc
14
+ timestamps_count = redis_client.llen key
15
+
16
+ if timestamps_count < limit
17
+ redis_client.multi do
18
+ redis_client.rpush key, now
19
+ redis_client.expire key, time_window.to_i
20
+ end
21
+ true
22
+ else
23
+ first = redis_client.lpop(key)
24
+ redis_client.multi do
25
+ redis_client.rpush key, now
26
+ redis_client.expire key, time_window.to_i
27
+ end
28
+
29
+ passing = first.to_time(:utc) < time_window.ago
30
+
31
+ unless passing
32
+ notify(key) unless redis_client.exists('failing-rate-limits:' + key)
33
+ redis_client.setex('failing-rate-limits:' + key, time_window.to_i, '1')
34
+ end
35
+
36
+ passing
37
+ end
38
+ end
39
+
40
+ alias allow? throttle
41
+
42
+ def notify(key)
43
+ ActiveSupport::Notifications.instrument 'rate_limit.notify', { rate_limit_key: key }
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module BetterRateLimit
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8"/>
5
+ <title>Too Many Requests</title>
6
+ </head>
7
+ <body>
8
+ <h2>Too Many Requests</h2>
9
+ </body>
10
+ </html>
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: better_rate_limit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Pablo Fonseca
8
+ - Joe d'Elia
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2020-08-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redis
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '3.3'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '3.3'
28
+ - !ruby/object:Gem::Dependency
29
+ name: actionpack
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '5.0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '5.0'
42
+ description: Adds rate limit for requests
43
+ email:
44
+ - pablofonseca777@gmail.com
45
+ - joe@delia.is
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - ".travis.yml"
52
+ - Gemfile
53
+ - Gemfile.lock
54
+ - README.md
55
+ - Rakefile
56
+ - better_rate_limit.gemspec
57
+ - bin/console
58
+ - bin/setup
59
+ - lib/action_controller/better_rate_limit.rb
60
+ - lib/better_rate_limit.rb
61
+ - lib/better_rate_limit/redis_connection.rb
62
+ - lib/better_rate_limit/throttle.rb
63
+ - lib/better_rate_limit/version.rb
64
+ - public/429.html
65
+ homepage: https://github.com/upscopeio/better_rate_limit
66
+ licenses: []
67
+ metadata:
68
+ homepage_uri: https://github.com/upscopeio/better_rate_limit
69
+ source_code_uri: https://github.com/upscopeio/better_rate_limit
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 2.3.0
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.0.8
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Rate limit requests
89
+ test_files: []