-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathvanilla_tor.py
107 lines (85 loc) · 3.28 KB
/
vanilla_tor.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
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
# -*- encoding: utf-8 -*-
import os
import tempfile
import shutil
from twisted.python import usage
from twisted.internet import reactor, error
import txtorcon
from ooni.utils import log, onion, net
from ooni import nettest
class TorIsNotInstalled(Exception):
pass
class UsageOptions(usage.Options):
optParameters = [
['timeout', 't', 300,
'Specify the timeout after which to consider '
'the Tor bootstrapping process to have failed.'], ]
class VanillaTor(nettest.NetTestCase):
name = "Vanilla Tor"
description = "This test examines the reachability of the Tor network."
author = "Arturo Filastò"
version = "0.1.0"
usageOptions = UsageOptions
def requirements(self):
if not onion.find_tor_binary():
raise TorIsNotInstalled(
"For instructions on installing Tor see: "
"https://www.torproject.org/download/download")
def setUp(self):
self.tor_progress = 0
self.tor_details = onion.get_tor_details()
self.timeout = int(self.localOptions['timeout'])
fd, self.tor_logfile = tempfile.mkstemp()
os.close(fd)
self.tor_datadir = tempfile.mkdtemp()
self.report['error'] = None
self.report['success'] = None
self.report['timeout'] = self.timeout
self.report['transport_name'] = 'vanilla'
self.report['tor_version'] = str(self.tor_details['version'])
self.report['tor_progress'] = 0
self.report['tor_progress_tag'] = None
self.report['tor_progress_summary'] = None
self.report['tor_log'] = None
def test_full_tor_connection(self):
config = txtorcon.TorConfig()
config.ControlPort = net.randomFreePort()
config.SocksPort = net.randomFreePort()
config.DataDirectory = self.tor_datadir
log.msg(
"Connecting to tor %s" %
(onion.tor_details['version']))
config.log = ['notice stdout', 'notice file %s' % self.tor_logfile]
config.save()
def updates(prog, tag, summary):
log.msg("Progress is at: %s%%" % (prog))
self.report['tor_progress'] = int(prog)
self.report['tor_progress_tag'] = tag
self.report['tor_progress_summary'] = summary
d = txtorcon.launch_tor(config, reactor, tor_binary=onion.find_tor_binary(),
timeout=self.timeout,
progress_updates=updates)
@d.addCallback
def setup_complete(proto):
try:
proto.transport.signalProcess('TERM')
except error.ProcessExitedAlready:
proto.transport.loseConnection()
log.msg("Successfully connected to Tor")
self.report['success'] = True
@d.addErrback
def setup_failed(failure):
log.msg("Failed to connect to Tor")
self.report['success'] = False
self.report['error'] = 'timeout-reached'
return
@d.addCallback
def write_log(_):
with open(self.tor_logfile) as f:
self.report['tor_log'] = f.read()
os.remove(self.tor_logfile)
try:
shutil.rmtree(self.tor_datadir)
except:
pass
return d