-
-
Notifications
You must be signed in to change notification settings - Fork 258
/
start.rb
executable file
·280 lines (235 loc) · 5.87 KB
/
start.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env ruby
require 'erb'
require 'excon'
require 'logger'
$logger = Logger.new(STDOUT, ENV['DEBUG'] ? Logger::DEBUG : Logger::INFO)
module Service
class Base
attr_reader :port
def initialize(port)
@port = port
end
def service_name
self.class.name.downcase.split('::').last
end
def start
ensure_directories
$logger.info "starting #{service_name} on port #{port}"
end
def ensure_directories
%w{lib run log}.each do |dir|
path = "/var/#{dir}/#{service_name}"
Dir.mkdir(path) unless Dir.exists?(path)
end
end
def data_directory
"/var/lib/#{service_name}"
end
def pid_file
"/var/run/#{service_name}/#{port}.pid"
end
def executable
self.class.which(service_name)
end
def stop
$logger.info "stopping #{service_name} on port #{port}"
if File.exists?(pid_file)
pid = File.read(pid_file).strip
begin
self.class.kill(pid.to_i)
rescue => e
$logger.warn "couldn't kill #{service_name} on port #{port}: #{e.message}"
end
else
$logger.info "#{service_name} on port #{port} was not running"
end
end
def self.kill(pid, signal='SIGINT')
Process.kill(signal, pid)
end
def self.fire_and_forget(*args)
$logger.debug "running: #{args.join(' ')}"
pid = Process.fork
if pid.nil? then
# In child
exec args.join(" ")
else
# In parent
Process.detach(pid)
end
end
def self.which(executable)
path = `which #{executable}`.strip
if path == ""
return nil
else
return path
end
end
end
class Tor < Base
attr_reader :port, :control_port
def initialize(port, control_port)
@port = port
@control_port = control_port
end
def data_directory
"#{super}/#{port}"
end
def start
super
self.class.fire_and_forget(executable,
"--SocksPort #{port}",
"--ControlPort #{control_port}",
"--NewCircuitPeriod 15",
"--MaxCircuitDirtiness 15",
"--UseEntryGuards 0",
"--UseEntryGuardsAsDirGuards 0",
"--CircuitBuildTimeout 5",
"--ExitRelay 0",
"--RefuseUnknownExits 0",
"--ClientOnly 1",
"--AllowSingleHopCircuits 1",
"--DataDirectory #{data_directory}",
"--PidFile #{pid_file}",
"--Log \"warn syslog\"",
'--RunAsDaemon 1',
"| logger -t 'tor' 2>&1")
end
def newnym
self.class.fire_and_forget('/usr/local/bin/newnym.sh',
"#{control_port}",
"| logger -t 'newnym'")
end
end
class Polipo < Base
def initialize(port, tor:)
super(port)
@tor = tor
end
def start
super
# https://gitweb.torproject.org/torbrowser.git/blob_plain/1ffcd9dafb9dd76c3a29dd686e05a71a95599fb5:/build-scripts/config/polipo.conf
if File.exists?(pid_file)
File.delete(pid_file)
end
self.class.fire_and_forget(executable,
"proxyPort=#{port}",
"socksParentProxy=127.0.0.1:#{tor_port}",
"socksProxyType=socks5",
"diskCacheRoot=''",
"disableLocalInterface=true",
"allowedClients=127.0.0.1",
"localDocumentRoot=''",
"disableConfiguration=true",
"dnsUseGethostbyname='yes'",
"logSyslog=true",
"daemonise=true",
"pidFile=#{pid_file}",
"disableVia=true",
"allowedPorts='1-65535'",
"tunnelAllowedPorts='1-65535'",
"| logger -t 'polipo' 2>&1")
end
def tor_port
@tor.port
end
end
class Proxy
attr_reader :id
attr_reader :tor, :polipo
def initialize(id)
@id = id
@tor = Tor.new(tor_port, tor_control_port)
@polipo = Polipo.new(polipo_port, tor: tor)
end
def start
$logger.info "starting proxy id #{id}"
@tor.start
@polipo.start
end
def stop
$logger.info "stopping proxy id #{id}"
@tor.stop
@polipo.stop
end
def restart
stop
sleep 5
start
end
def tor_port
10000 + id
end
def tor_control_port
30000 + id
end
def polipo_port
tor_port + 10000
end
alias_method :port, :polipo_port
def test_url
ENV['test_url'] || 'http://icanhazip.com'
end
def working?
Excon.get(test_url, proxy: "http://127.0.0.1:#{port}", :read_timeout => 10).status == 200
rescue
false
end
end
class Haproxy < Base
attr_reader :backends
def initialize(port = 5566)
@config_erb_path = "/usr/local/etc/haproxy.cfg.erb"
@config_path = "/usr/local/etc/haproxy.cfg"
@backends = []
super(port)
end
def start
super
compile_config
self.class.fire_and_forget(executable,
"-f #{@config_path}",
"| logger 2>&1")
end
def soft_reload
self.class.fire_and_forget(executable,
"-f #{@config_path}",
"-p #{pid_file}",
"-sf #{File.read(pid_file)}",
"| logger 2>&1")
end
def add_backend(backend)
@backends << {:name => 'tor', :addr => '127.0.0.1', :port => backend.port}
end
private
def compile_config
File.write(@config_path, ERB.new(File.read(@config_erb_path)).result(binding))
end
end
end
haproxy = Service::Haproxy.new
proxies = []
tor_instances = ENV['tors'] || 10
tor_instances.to_i.times.each do |id|
proxy = Service::Proxy.new(id)
haproxy.add_backend(proxy)
proxy.start
proxies << proxy
end
haproxy.start
sleep 60
loop do
$logger.info "resetting circuits"
proxies.each do |proxy|
$logger.info "reset nym for #{proxy.id} (port #{proxy.port})"
proxy.tor.newnym
end
$logger.info "testing proxies"
proxies.each do |proxy|
$logger.info "testing proxy #{proxy.id} (port #{proxy.port})"
proxy.restart unless proxy.working?
end
$logger.info "sleeping for 60 seconds"
sleep 60
end