notsofast 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in notsofast.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Stewart McKee
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # Notsofast
2
+
3
+ Rate limiting rails rack gem. Add to gem file to start limiting based on IP
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'notsofast'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install notsofast
18
+
19
+ ## Usage
20
+
21
+ By default starts limiting on 100 requests per minute. This is on an IP address basis.
22
+
23
+ ## Configuration
24
+
25
+ Create a rails initializer (config/initializers/notsofast.rb) with the following as required
26
+
27
+ ### whitelist
28
+ Set an array of string values containing whitelisted ip addresses eg ["127.0.0.1"]
29
+
30
+ Notsofast::Config.whitelist=[]
31
+
32
+ ### blacklist
33
+ Set an array of string values containing blacklisted ip addresses eg ["127.0.0.1"]
34
+
35
+ Notsofast::Config.blacklist=[]
36
+
37
+ ### request_limit
38
+ Specifies the number of permitted request per limit_exipry period
39
+
40
+ Notsofast::Config.request_limit=100
41
+
42
+ ### response_types
43
+ Specifies the response types that the limit applies to (eg ignore images and other assets)
44
+
45
+ Notsofast::Config.response_types=["text/html"]
46
+
47
+ ### limit_expiry
48
+ Sets the time before limits are reset and within which the limit is reached
49
+
50
+ Notsofast::Config.limit_expiry=60
51
+
52
+
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/notsofast.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "notsofast/version"
2
+ require "notsofast/config"
3
+ require 'notsofast/railtie' if defined?(Rails)
4
+ require "notsofast/rate_limit"
@@ -0,0 +1,51 @@
1
+ module Notsofast
2
+ class Config
3
+
4
+ @whitelist = []
5
+ @blacklist = []
6
+ @request_limit = 100
7
+ @response_types = ["text/html"]
8
+ @limit_expiry = 60
9
+
10
+ def self.whitelist=(value)
11
+ @whitelist = value
12
+ end
13
+
14
+ def self.blacklist=(value)
15
+ @blacklist = value
16
+ end
17
+
18
+ def self.request_limit=(value)
19
+ @request_limit = value
20
+ end
21
+
22
+ def self.response_types=(value)
23
+ @response_types = value
24
+ end
25
+
26
+ def self.limit_expiry=(value)
27
+ @limit_expiry = value
28
+ end
29
+
30
+ def self.whitelist
31
+ @whitelist
32
+ end
33
+
34
+ def self.blacklist
35
+ @blacklist
36
+ end
37
+
38
+ def self.request_limit
39
+ @request_limit
40
+ end
41
+
42
+ def self.response_types
43
+ @response_types
44
+ end
45
+
46
+ def self.limit_expiry
47
+ @limit_expiry
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ class NotsofastRailtie < Rails::Railtie
2
+ initializer "notsofast_railtine.configure_rails_initialization" do |app|
3
+ app.middleware.use Notsofast::RateLimit
4
+ end
5
+ end
@@ -0,0 +1,60 @@
1
+ module Notsofast
2
+ class RateLimit
3
+
4
+ def initialize(app)
5
+ @app = app
6
+ @connections = {}
7
+ @connections_timestamp = timestamp
8
+ end
9
+
10
+ def timestamp
11
+ DateTime.now.to_i
12
+ end
13
+
14
+ def remote_ip(env)
15
+ env['HTTP_X_FORWARDED_FOR'] || env['REMOTE_ADDR']
16
+ end
17
+
18
+ def call(env)
19
+ status, headers, response = @app.call(env)
20
+
21
+ if timestamp - @connections_timestamp > Notsofast::Config.limit_expiry
22
+ puts "clearing connections cache"
23
+ @connections_timestamp = timestamp
24
+ @connections = {}
25
+ end
26
+
27
+ ip_address = remote_ip(env)
28
+
29
+ puts Notsofast::Config.whitelist.include?(ip_address)
30
+ unless Notsofast::Config.whitelist.include?(ip_address)
31
+ if Notsofast::Config.blacklist.include?(ip_address)
32
+ response = Rack::Response.new
33
+ response.write 'Your IP Address has been blacklisted'
34
+ response.status = 403
35
+ response.finish
36
+ return
37
+ else
38
+ puts "checking the content "
39
+ if (headers["Content-Type"].nil? && Notsofast::Config.response_types.empty?) || (headers["Content-Type"].present? && Notsofast::Config.response_types.present? && Notsofast::Config.response_types.select{|r| headers["Content-Type"].include?(r)}.count > 0)
40
+ puts "matched content type"
41
+ if @connections[ip_address]
42
+ @connections[ip_address] += 1
43
+ else
44
+ @connections[ip_address] = 1
45
+ end
46
+ puts @connections
47
+
48
+ if @connections[ip_address] > Notsofast::Config.request_limit
49
+ response = Rack::Response.new
50
+ response.write 'Rate Limited'
51
+ response.status = 429
52
+ response.finish
53
+ end
54
+ end
55
+ end
56
+ end
57
+ [status, headers, response]
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module Notsofast
2
+ VERSION = "0.0.1"
3
+ end
data/notsofast.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'notsofast/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "notsofast"
8
+ spec.version = Notsofast::VERSION
9
+ spec.authors = ["Stewart McKee"]
10
+ spec.email = ["stewart@theizone.co.uk"]
11
+ spec.description = "Rate limiting gem based on IP address "
12
+ spec.summary = "Rate limiting gem based on IP address"
13
+ spec.homepage = "http://github.com/stewartmckee/notsofast"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: notsofast
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stewart McKee
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: ! 'Rate limiting gem based on IP address '
47
+ email:
48
+ - stewart@theizone.co.uk
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/notsofast.rb
59
+ - lib/notsofast/config.rb
60
+ - lib/notsofast/railtie.rb
61
+ - lib/notsofast/rate_limit.rb
62
+ - lib/notsofast/version.rb
63
+ - notsofast.gemspec
64
+ homepage: http://github.com/stewartmckee/notsofast
65
+ licenses:
66
+ - MIT
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.24
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Rate limiting gem based on IP address
89
+ test_files: []