forked from ooni/probe
/
proxy_plgoo.py
69 lines (60 loc) · 2.4 KB
/
proxy_plgoo.py
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
#!/usr/bin/python
import sys
from twisted.internet import reactor, endpoints
from twisted.web import client
from ooni.plugooni import Plugoo
from ooni.socksclient import SOCKSv4ClientProtocol, SOCKSWrapper
class HttpPlugin(Plugoo):
def __init__(self):
self.name = ""
self.type = ""
self.paranoia = ""
self.modules_to_import = []
self.output_dir = ""
self.buf = ''
def cb(self, type, content):
print "got %d bytes from %s" % (len(content), type) # DEBUG
if not self.buf:
self.buf = content
else:
if self.buf == content:
print "SUCCESS"
else:
print "FAIL"
reactor.stop()
def endpoint(self, scheme, host, port):
ep = None
if scheme == 'http':
ep = endpoints.TCP4ClientEndpoint(reactor, host, port)
elif scheme == 'https':
from twisted.internet import ssl
ep = endpoints.SSL4ClientEndpoint(reactor, host, port,
ssl.ClientContextFactory())
return ep
def ooni_main(self, cmd):
# We don't have the Command object so cheating for now.
url = cmd.hostname
# FIXME: validate that url is on the form scheme://host[:port]/path
scheme, host, port, path = client._parse(url)
ctrl_dest = self.endpoint(scheme, host, port)
if not ctrl_dest:
raise Exception('unsupported scheme %s in %s' % (scheme, url))
if cmd.controlproxy:
assert scheme != 'https', "no support for proxied https atm, sorry"
_, proxy_host, proxy_port, _ = client._parse(cmd.controlproxy)
control = SOCKSWrapper(reactor, proxy_host, proxy_port, ctrl_dest)
print "proxy: ", proxy_host, proxy_port
else:
control = ctrl_dest
f = client.HTTPClientFactory(url)
f.deferred.addCallback(lambda x: self.cb('control', x))
control.connect(f)
exp_dest = self.endpoint(scheme, host, port)
if not exp_dest:
raise Exception('unsupported scheme %s in %s' % (scheme, url))
# FIXME: use the experiment proxy if there is one
experiment = exp_dest
f = client.HTTPClientFactory(url)
f.deferred.addCallback(lambda x: self.cb('experiment', x))
experiment.connect(f)
reactor.run()