Skip to content

Commit 36a39e0

Browse files
authored
Merge pull request mattes#14 from milesrichardson/master
optimized tor args, and force change circuits via tor control port
2 parents 6d5af51 + 389cdb3 commit 36a39e0

File tree

5 files changed

+57
-10
lines changed

5 files changed

+57
-10
lines changed

Dockerfile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ RUN gem install excon -v 0.44.4
2121
ADD start.rb /usr/local/bin/start.rb
2222
RUN chmod +x /usr/local/bin/start.rb
2323

24+
ADD newnym.sh /usr/local/bin/newnym.sh
25+
RUN chmod +x /usr/local/bin/newnym.sh
26+
2427
ADD haproxy.cfg.erb /usr/local/etc/haproxy.cfg.erb
28+
ADD uncachable /etc/polipo/uncachable
2529

26-
EXPOSE 5566 1936
30+
EXPOSE 5566 4444
2731

28-
CMD /usr/local/bin/start.rb
32+
CMD /usr/local/bin/start.rb

haproxy.cfg.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ defaults
1414
timeout server 60s
1515

1616

17-
listen stats *:1936
17+
listen stats *:4444
1818
mode http
1919
log global
2020
maxconn 10

newnym.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
CONTROLPORT=$1
4+
5+
cat <<'EOF' | nc localhost $CONTROLPORT
6+
authenticate ""
7+
signal newnym
8+
quit
9+
EOF

start.rb

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ def self.which(executable)
8383

8484

8585
class Tor < Base
86+
attr_reader :port, :control_port
87+
88+
def initialize(port, control_port)
89+
@port = port
90+
@control_port = control_port
91+
end
92+
8693
def data_directory
8794
"#{super}/#{port}"
8895
end
@@ -91,13 +98,28 @@ def start
9198
super
9299
self.class.fire_and_forget(executable,
93100
"--SocksPort #{port}",
94-
"--NewCircuitPeriod 120",
101+
"--ControlPort #{control_port}",
102+
"--NewCircuitPeriod 15",
103+
"--MaxCircuitDirtiness 15",
104+
"--UseEntryGuards 0",
105+
"--UseEntryGuardsAsDirGuards 0",
106+
"--CircuitBuildTimeout 5",
107+
"--ExitRelay 0",
108+
"--RefuseUnknownExits 0",
109+
"--ClientOnly 1",
110+
"--AllowSingleHopCircuits 1",
95111
"--DataDirectory #{data_directory}",
96112
"--PidFile #{pid_file}",
97113
"--Log \"warn syslog\"",
98114
'--RunAsDaemon 1',
99115
"| logger -t 'tor' 2>&1")
100116
end
117+
118+
def newnym
119+
self.class.fire_and_forget('/usr/local/bin/newnym.sh',
120+
"#{control_port}",
121+
"| logger -t 'newnym'")
122+
end
101123
end
102124

103125
class Polipo < Base
@@ -142,7 +164,7 @@ class Proxy
142164

143165
def initialize(id)
144166
@id = id
145-
@tor = Tor.new(tor_port)
167+
@tor = Tor.new(tor_port, tor_control_port)
146168
@polipo = Polipo.new(polipo_port, tor: tor)
147169
end
148170

@@ -168,17 +190,21 @@ def tor_port
168190
10000 + id
169191
end
170192

193+
def tor_control_port
194+
30000 + id
195+
end
196+
171197
def polipo_port
172198
tor_port + 10000
173199
end
174200
alias_method :port, :polipo_port
175201

176202
def test_url
177-
ENV['test_url'] || 'http://echoip.com'
203+
ENV['test_url'] || 'http://icanhazip.com'
178204
end
179205

180206
def working?
181-
Excon.get(test_url, proxy: "http://127.0.0.1:#{port}").status == 200
207+
Excon.get(test_url, proxy: "http://127.0.0.1:#{port}", :read_timeout => 10).status == 200
182208
rescue
183209
false
184210
end
@@ -221,7 +247,6 @@ def compile_config
221247
end
222248
end
223249

224-
225250
haproxy = Service::Haproxy.new
226251
proxies = []
227252

@@ -238,12 +263,18 @@ def compile_config
238263
sleep 60
239264

240265
loop do
266+
$logger.info "resetting circuits"
267+
proxies.each do |proxy|
268+
$logger.info "reset nym for #{proxy.id} (port #{proxy.port})"
269+
proxy.tor.newnym
270+
end
271+
241272
$logger.info "testing proxies"
242273
proxies.each do |proxy|
243274
$logger.info "testing proxy #{proxy.id} (port #{proxy.port})"
244275
proxy.restart unless proxy.working?
245276
end
246277

247-
$logger.info "sleeping for 1800 seconds"
248-
sleep 1800
278+
$logger.info "sleeping for 60 seconds"
279+
sleep 60
249280
end

uncachable

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ipinfo.io
2+
icanhazip.com
3+
echoip.com

0 commit comments

Comments
 (0)