rack-tor-block 0.0.4 → 0.0.5

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.
Files changed (4) hide show
  1. checksums.yaml +5 -13
  2. data/lib/rack/ip.rb +10 -12
  3. data/lib/rack/tor_block.rb +25 -8
  4. metadata +9 -9
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- N2ZmMDlmYzk2MDBjMGM2ZjU1MTJjN2VlMTBiZTZlZTBiZjliYjc3Mw==
5
- data.tar.gz: !binary |-
6
- MmE2NWNkMjIwMGUzMWVkMGI1N2EwMzcyZWFhODQzNzc0N2U5NzQ5MQ==
2
+ SHA1:
3
+ metadata.gz: 9280322a0acf1e973c97d21ae00e2628caf12365
4
+ data.tar.gz: f09f20716d64ea24ccf9b97d0a69333f25eaaed4
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- NGExNTdmZGJhZTgxMzc1ZTlmZmZjODE4NTM2OTE5YTNkYzJmZWE5OGE2NzBh
10
- MjY2MmVjMjk3YjlkMDRkNjBiY2U3NGFhNTQ5MTU5NDNhZTFkOGM5MTBkMmYx
11
- MjYyMmRkZGE0MjExYzlkZDA4OWY4MmI4ZWIxNjViYWQ1MzQ5ZDc=
12
- data.tar.gz: !binary |-
13
- YjE3MmIwMDIyZDAzZmU3MWEyOTMwMWQ0NzI2MDgwMmM1YjhkYWJiZTI0Zjk0
14
- MDZlNTdhZWU3MjVjY2ZhNzM1YzEyODFhOWU1MDk3YzBlOWZlYWEzMzc5N2Q4
15
- Y2I3MzZhMTgyMmYzZjdkY2ZmMDhmYjVlMDc1OWI0YjZiODc3Zjk=
6
+ metadata.gz: d22c46a7ae77eaffd35f23e0a7260fee876817439ae39bb375a773d84d17ce25f4b91a6ad75279d27255c6f1423f719eda02dc0afae40e75fdcd332885c0517c
7
+ data.tar.gz: 2cfc159f6cf645c640ca39d12394070ab1532b259553f4e54d2708602365cbbd394c55a68f107e465462ab0afaed960a1f3c24fef471a2230d48dc37ba88bef7
@@ -2,26 +2,25 @@ require 'resolv'
2
2
 
3
3
  module Rack
4
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
-
5
+ TOR_POSITIVE_IP = '127.0.0.2' # In case the DNS look up is positive, this is the IP address returned
6
+ TOR_DNSEL = 'ip-port.exitlist.torproject.org' # https://www.torproject.org/projects/tordnsel.html.en
9
7
  GOOGLE_DNS_ADDR = '8.8.8.8'
10
8
  GOOGLE_DNS_PORT = '53'
11
-
9
+
12
10
  # client_addr is the address of the remote client we want to test to be an TOR node
13
11
  # server_addr is the address of a public IP server we want to reach passing through client_addr
14
12
  # 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)
13
+ def initialize(client_addr, server_addr = GOOGLE_DNS_ADDR, server_port = GOOGLE_DNS_PORT)
16
14
  @client_addr, @server_port, @server_addr = client_addr.to_s, server_port.to_s, server_addr.to_s
17
15
  end
18
-
19
- def is_tor? #Implements https://www.torproject.org/projects/tordnsel.html.en
16
+
17
+ # Implements https://www.torproject.org/projects/tordnsel.html.en
18
+ def tor?
20
19
  Resolv.getaddress(tor_hostname) == TOR_POSITIVE_IP
21
- rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH, Resolv::ResolvError => e
20
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH, Resolv::ResolvError
22
21
  false
23
22
  end
24
-
23
+
25
24
  def tor_hostname
26
25
  [reverse_ip_octets(@client_addr), @server_port, reverse_ip_octets(@server_addr), TOR_DNSEL].join('.')
27
26
  end
@@ -29,6 +28,5 @@ module Rack
29
28
  def reverse_ip_octets(ip)
30
29
  ip.split('.').reverse.join('.')
31
30
  end
32
-
33
31
  end
34
- end
32
+ end
@@ -3,19 +3,36 @@ require 'rack/ip'
3
3
 
4
4
  module Rack
5
5
  class TorBlock
6
-
7
- DEFAULT_REDIRECT = 'https://sorry.google.com' #We're sorry from Google
6
+ DEFAULT_REDIRECT = 'https://sorry.google.com'.freeze # We're sorry from Google
7
+ GO_AWAY = [302, { 'Content-Type' => 'text', 'Location' => DEFAULT_REDIRECT }, []].freeze
8
+ REMOTE_IP_KEY = 'action_dispatch.remote_ip'.freeze
8
9
 
9
10
  def initialize(app)
10
11
  @app = app
11
12
  end
12
13
 
13
14
  def call(env)
14
- req = Rack::Request.new(env)
15
- return [302, {'Content-Type' => 'text', 'Location' => DEFAULT_REDIRECT}, [] ] if Rack::IP.new(env['action_dispatch.remote_ip'] || req.ip).is_tor?
16
-
17
- #Normal processing
18
- @app.call(env)
15
+ remote_ip = env[REMOTE_IP_KEY] || Rack::Request.new(env).ip
16
+ if tor?(remote_ip)
17
+ GO_AWAY
18
+ else
19
+ @app.call(env)
20
+ end
21
+ end
22
+
23
+ if defined? Rails
24
+ def tor?(remote_ip)
25
+ key = "tor/#{remote_ip}"
26
+ Rails.cache.fetch(key, expires_in: 10.minutes) do
27
+ Rack::IP.new(remote_ip).tor?
28
+ end
29
+ rescue # In case of cache failure
30
+ false
31
+ end
32
+ else
33
+ def tor?(remote_ip)
34
+ Rack::IP.new(remote_ip).tor?
35
+ end
19
36
  end
20
37
  end
21
- end
38
+ end
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-tor-block
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Bonmassar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-10 00:00:00.000000000 Z
11
+ date: 2015-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ! '>'
31
+ - - ">"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ! '>'
38
+ - - ">"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: Identify and prevent tor users to access a Rack / Rails application.
@@ -57,17 +57,17 @@ require_paths:
57
57
  - lib
58
58
  required_ruby_version: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - ! '>='
60
+ - - ">="
61
61
  - !ruby/object:Gem::Version
62
62
  version: '0'
63
63
  required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  requirements:
65
- - - ! '>='
65
+ - - ">="
66
66
  - !ruby/object:Gem::Version
67
67
  version: '0'
68
68
  requirements: []
69
69
  rubyforge_project:
70
- rubygems_version: 2.1.11
70
+ rubygems_version: 2.2.2
71
71
  signing_key:
72
72
  specification_version: 4
73
73
  summary: Prevent tor users to access a Rack / Rails application.