ip_blocker 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Manifest +5 -0
- data/README.rdoc +1 -0
- data/Rakefile +14 -0
- data/ip_blocker.gemspec +30 -0
- data/lib/ip_blocker.rb +30 -0
- data/test/ip_blocker_test.rb +45 -0
- metadata +59 -0
data/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
IpBlocker is a small rack middleware to block ip addresses defined in an array. In fact, the only feature this middleware has is to block ip addresses and return a 403 Forbidden response.
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('ip_blocker', '1.0.0') do |gem|
|
6
|
+
gem.description = "Block ip addresses in middleware"
|
7
|
+
gem.url = "http://github.com/maxmmurphy/ip_blocker"
|
8
|
+
gem.author = "Max M. Murphy"
|
9
|
+
gem.email = "max@maxmurphy.net"
|
10
|
+
gem.ignore_pattern = ["tmp/*"]
|
11
|
+
gem.development_dependencies = []
|
12
|
+
end
|
13
|
+
|
14
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
data/ip_blocker.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "ip_blocker"
|
5
|
+
s.version = "1.0.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Max M. Murphy"]
|
9
|
+
s.date = "2011-12-08"
|
10
|
+
s.description = "Block ip addresses in middleware"
|
11
|
+
s.email = "max@maxmurphy.net"
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/ip_blocker.rb"]
|
13
|
+
s.files = ["README.rdoc", "Rakefile", "lib/ip_blocker.rb", "test/ip_blocker_test.rb", "Manifest", "ip_blocker.gemspec"]
|
14
|
+
s.homepage = "http://github.com/maxmmurphy/ip_blocker"
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Ip_blocker", "--main", "README.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = "ip_blocker"
|
18
|
+
s.rubygems_version = "1.8.10"
|
19
|
+
s.summary = "Block ip addresses in middleware"
|
20
|
+
s.test_files = ["test/ip_blocker_test.rb"]
|
21
|
+
|
22
|
+
if s.respond_to? :specification_version then
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
data/lib/ip_blocker.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# IpBlocker middleware for rack applications to return a 403 to blocked ip addresses.
|
2
|
+
#
|
3
|
+
# usage:
|
4
|
+
#
|
5
|
+
# <tt> IpBlocker, :ips_to_block => ["1.2.3.4", "0.0.0.0", "127.0.0.1"] </tt>
|
6
|
+
|
7
|
+
class IpBlocker
|
8
|
+
|
9
|
+
# initialize with :ips_to_block as an array, all IP addresses in this array will be blocked
|
10
|
+
def initialize(app, options = {})
|
11
|
+
@app = app
|
12
|
+
@ips_to_block = options[:ips_to_block]
|
13
|
+
end
|
14
|
+
|
15
|
+
# call only if ip is not blocked
|
16
|
+
def call(env)
|
17
|
+
if ip_blocked?(env["REMOTE_ADDR"])
|
18
|
+
[403, {"Content-Type" => "text/html"}, ["<h1>403 Forbidden</h1>"]]
|
19
|
+
else
|
20
|
+
@app.call(env)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# If the ip is blocked, return true.
|
25
|
+
def ip_blocked?(ip)
|
26
|
+
@ips_to_block.include?(ip)
|
27
|
+
end
|
28
|
+
|
29
|
+
attr_reader :ips_to_block
|
30
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require File.join(File.dirname(__FILE__), '../' ,'lib/ip_blocker.rb')
|
3
|
+
|
4
|
+
class App
|
5
|
+
|
6
|
+
def call(env)
|
7
|
+
[200, {"Content-Type" => "text/html"}, "OK"]
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
class IpBlockerTest < Test::Unit::TestCase
|
13
|
+
|
14
|
+
def setup
|
15
|
+
@app = App.new
|
16
|
+
@ip_blocker = IpBlocker.new(@app, :ips_to_block => ["0.0.0.0", "1.2.3.4", "127.0.0.1"])
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_block_list
|
20
|
+
assert @ip_blocker.ip_blocked?("0.0.0.0")
|
21
|
+
assert @ip_blocker.ip_blocked?("1.2.3.4")
|
22
|
+
assert @ip_blocker.ip_blocked?("127.0.0.1")
|
23
|
+
assert !@ip_blocker.ip_blocked?("4.4.4.4")
|
24
|
+
assert !@ip_blocker.ip_blocked?("4.4.3.4")
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_denied_response
|
28
|
+
@env = {
|
29
|
+
'REMOTE_ADDR' => '0.0.0.0',
|
30
|
+
'PATH_INFO' => '/somewhere',
|
31
|
+
'HTTP_USER_AGENT' => 'AwesomeAgent 1.0'
|
32
|
+
}
|
33
|
+
assert_equal 403, @ip_blocker.call(@env).first
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_allowed_response
|
37
|
+
@env = {
|
38
|
+
'REMOTE_ADDR' => '17.24.25.26',
|
39
|
+
'PATH_INFO' => '/somewhere',
|
40
|
+
'HTTP_USER_AGENT' => 'AwesomeAgent 1.0'
|
41
|
+
}
|
42
|
+
assert_equal 200, @ip_blocker.call(@env).first
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ip_blocker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Max M. Murphy
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-08 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Block ip addresses in middleware
|
15
|
+
email: max@maxmurphy.net
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files:
|
19
|
+
- README.rdoc
|
20
|
+
- lib/ip_blocker.rb
|
21
|
+
files:
|
22
|
+
- README.rdoc
|
23
|
+
- Rakefile
|
24
|
+
- lib/ip_blocker.rb
|
25
|
+
- test/ip_blocker_test.rb
|
26
|
+
- Manifest
|
27
|
+
- ip_blocker.gemspec
|
28
|
+
homepage: http://github.com/maxmmurphy/ip_blocker
|
29
|
+
licenses: []
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options:
|
32
|
+
- --line-numbers
|
33
|
+
- --inline-source
|
34
|
+
- --title
|
35
|
+
- Ip_blocker
|
36
|
+
- --main
|
37
|
+
- README.rdoc
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '1.2'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project: ip_blocker
|
54
|
+
rubygems_version: 1.8.10
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: Block ip addresses in middleware
|
58
|
+
test_files:
|
59
|
+
- test/ip_blocker_test.rb
|