rack-tor-block 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ require "rack/tor_block"
@@ -0,0 +1,34 @@
1
+ require 'resolv'
2
+
3
+ module Rack
4
+ class IP
5
+
6
+ TOR_POSITIVE_IP = '127.0.0.2' #In case the DNS look up is positive, this is the IP address returned
7
+ TOR_DNSEL = 'ip-port.exitlist.torproject.org' #https://www.torproject.org/projects/tordnsel.html.en
8
+
9
+ GOOGLE_DNS_ADDR = '8.8.8.8'
10
+ GOOGLE_DNS_PORT = '53'
11
+
12
+ # client_addr is the address of the remote client we want to test to be an TOR node
13
+ # server_addr is the address of a public IP server we want to reach passing through client_addr
14
+ # server_port is a TCP port running on server_addr to test for positiviness to TOR network
15
+ def initialize(client_addr, server_addr = GOOGLE_DNS_ADDR, server_port=GOOGLE_DNS_PORT)
16
+ @client_addr, @server_port, @server_addr = client_addr.to_s, server_port.to_s, server_addr.to_s
17
+ end
18
+
19
+ def is_tor? #Implements https://www.torproject.org/projects/tordnsel.html.en
20
+ Resolv.getaddress(tor_hostname) == TOR_POSITIVE_IP
21
+ rescue Resolv::ResolvError => e
22
+ false
23
+ end
24
+
25
+ def tor_hostname
26
+ [reverse_ip_octets(@client_addr), @server_port, reverse_ip_octets(@server_addr), TOR_DNSEL].join('.')
27
+ end
28
+
29
+ def reverse_ip_octets(ip)
30
+ ip.split('.').reverse.join('.')
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ require 'rack'
2
+ require 'rack/ip'
3
+
4
+ module Rack
5
+ class TorBlock
6
+
7
+ DEFAULT_REDIRECT = 'https://sorry.google.com' #We're sorry from Google
8
+
9
+ def initialize(app)
10
+ @app = app
11
+ end
12
+
13
+ def call(env)
14
+ return [302, {'Content-Type' => 'text', 'Location' => DEFAULT_REDIRECT}, [] ] if Rack::IP.new(env['REMOTE_ADDR']).is_tor?
15
+
16
+ #Normal processing
17
+ @app.call(env)
18
+ end
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-tor-block
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Luca Bonmassar
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :runtime
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: Identify and prevent tor users to access a Rack / Rails application.
47
+ email:
48
+ - luca@gild.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - lib/rack-tor-block.rb
54
+ - lib/rack/ip.rb
55
+ - lib/rack/tor_block.rb
56
+ homepage: http://www.gild.com
57
+ licenses: []
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ segments:
69
+ - 0
70
+ hash: -3304184217374256655
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ segments:
78
+ - 0
79
+ hash: -3304184217374256655
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.24
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Prevent tor users to access a Racl / Rails application.
86
+ test_files: []