forked from astro/socksify-ruby
/
tc_socksify.rb
156 lines (116 loc) · 3.61 KB
/
tc_socksify.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/ruby
require 'test/unit'
require 'net/http'
require 'uri'
$:.unshift "#{File::dirname($0)}/../lib/"
require 'socksify'
require 'socksify/http'
class SocksifyTest < Test::Unit::TestCase
def setup
Socksify::debug = true
end
def disable_socks
TCPSocket.socks_server = nil
TCPSocket.socks_port = nil
end
def enable_socks
TCPSocket.socks_server = "127.0.0.1"
TCPSocket.socks_port = 9050
end
def http_tor_proxy
Net::HTTP::SOCKSProxy("127.0.0.1", 9050)
end
def test_check_tor
[['Hostname', :check_tor],
['IPv4', :check_tor_ip]].each do |f_name, f|
disable_socks
tor_direct, ip_direct = send(f)
assert_equal(false, tor_direct)
enable_socks
tor_socks, ip_socks = send(f)
assert_equal(true, tor_socks)
assert(ip_direct != ip_socks)
end
end
def test_check_tor_via_net_http
disable_socks
[['Hostname', :check_tor],
['IPv4', :check_tor_ip]].each do |f_name, f|
tor_direct, ip_direct = send(f)
assert_equal(false, tor_direct)
tor_socks, ip_socks = send(f, http_tor_proxy)
assert_equal(true, tor_socks)
assert(ip_direct != ip_socks)
end
end
def test_ignores
disable_socks
tor_direct, ip_direct = check_tor
assert_equal(false, tor_direct)
enable_socks
TCPSocket.socks_ignores << 'check.torproject.org'
tor_socks_ignored, ip_socks_ignored = check_tor
assert_equal(false, tor_socks_ignored)
assert(ip_direct == ip_socks_ignored)
end
def check_tor(http_klass = Net::HTTP)
url = URI::parse('http://check.torproject.org/')
parse_check_response(http_klass.start(url.host, url.port) do |http|
http.get('/', "User-Agent"=>"ruby-socksify test").body
end)
end
def check_tor_ip(http_klass = Net::HTTP)
url = URI::parse('http://38.229.70.31/') # "check.torproject.org"
parse_check_response(http_klass.start(url.host, url.port) do |http|
http.get('/',
"Host"=>"check.torproject.org",
"User-Agent"=>"ruby-socksify test").body
end)
end
def parse_check_response(body)
if body.include? 'Your browser is configured to use Tor.'
true
elsif body.include? 'You are not using Tor.'
false
else
raise 'Bogus response'
end
end
def test_resolve
enable_socks
assert_equal("87.106.131.203", Socksify::resolve("spaceboyz.net"))
assert_raise SOCKSError::HostUnreachable do
Socksify::resolve("nonexistent.spaceboyz.net")
end
end
def test_resolve_reverse
enable_socks
assert_equal("spaceboyz.net", Socksify::resolve("87.106.131.203"))
assert_raise SOCKSError::HostUnreachable do
Socksify::resolve("0.0.0.0")
end
end
def test_proxy
enable_socks
default_server = TCPSocket.socks_server
default_port = TCPSocket.socks_port
Socksify.proxy('localhost.example.com', 60001) {
assert_equal TCPSocket.socks_server, 'localhost.example.com'
assert_equal TCPSocket.socks_port, 60001
}
assert_equal TCPSocket.socks_server, default_server
assert_equal TCPSocket.socks_port, default_port
end
def test_proxy_failback
enable_socks
default_server = TCPSocket.socks_server
default_port = TCPSocket.socks_port
assert_raise StandardError do
Socksify.proxy('localhost.example.com', 60001) {
raise StandardError.new('error')
}
end
assert_equal TCPSocket.socks_server, default_server
assert_equal TCPSocket.socks_port, default_port
end
end