This repository has been archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
get_mirrors.py
353 lines (291 loc) · 11 KB
/
get_mirrors.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# -*- coding: utf-8 -*-
#
# This file is part of GetTor
#
# :authors: Israel Leiva <ilv@torproject.org>
# see also AUTHORS file
#
# :license: This is Free Software. See LICENSE for license information.
"""get_mirrors -- Download the list of tpo's mirrors for GetTor."""
import os
import json
import codecs
import logging
import argparse
from OpenSSL import SSL
from OpenSSL import crypto
from twisted.web import client
from twisted.python import log
from twisted.internet import ssl
from twisted.internet import defer
from twisted.internet import protocol
from twisted.internet import reactor
from twisted.internet.error import TimeoutError
from twisted.internet.error import DNSLookupError
from twisted.internet.error import ConnectionRefusedError
from gettor import utils
# Associate each protocol with its column name in tor-mirrors.csv
PROTOS = {
'https': 'httpsWebsiteMirror',
'http': 'httpWebsiteMirror',
'rsync': 'rsyncWebsiteMirror',
'https-dist': 'httpsDistMirror',
'http-dist': 'httpDistMirror',
'rsync-dist': 'rsyncDistMirror',
'ftp': 'ftpWebsiteMirror',
'onion': 'hiddenServiceMirror',
}
# Tor Project's website certificate
# $ openssl s_client -showcerts -connect tpo:443 < /dev/null > tpo.pem
CERT_TPO = '/path/to/gettor/tpo.pem'
# Taken from get-tor-exits (BridgeDB)
class FileWriter(protocol.Protocol):
"""Read a downloaded file incrementally and write to file."""
def __init__(self, finished, file):
"""Create a FileWriter.
.. warning:: We currently only handle the first 2MB of a file. Files
over 2MB will be truncated prematurely. *note*: this should be
enough for the mirrors file.
:param finished: A :class:`~twisted.internet.defer.Deferred` which
will fire when another portion of the download is complete.
"""
self.finished = finished
self.remaining = 1024 * 1024 * 2
self.fh = file
def dataReceived(self, bytes):
"""Write a portion of the download with ``bytes`` size to disk."""
if self.remaining:
display = bytes[:self.remaining]
self.fh.write(display)
self.fh.flush()
self.remaining -= len(display)
def connectionLost(self, reason):
"""Called when the download is complete."""
logging.info('Finished receiving mirrors list: %s'
% reason.getErrorMessage())
self.finished.callback(None)
# Based in tor2web.utils.ssl (Tor2web)
class HTTPSVerifyingContextFactory(ssl.ClientContextFactory):
def __init__(self, cn):
self.cn = cn
#
# From https://docs.python.org/2/library/ssl.html#ssl-security
#
# "SSL versions 2 and 3 are considered insecure and are therefore
# dangerous to use. If you want maximum compatibility between clients
# and servers, it is recommended to use PROTOCOL_SSLv23 as the protocol
# version and then disable SSLv2 and SSLv3 explicitly"
#
self.method = SSL.SSLv23_METHOD
def getContext(self, hostname, port):
"""Get this connection's OpenSSL context.
We disable SSLv2 and SSLv3. We also check the certificate received
is the one we expect (using the "common name").
"""
ctx = self._contextFactory(self.method)
ctx.set_options(SSL.OP_NO_SSLv2)
ctx.set_options(SSL.OP_NO_SSLv3)
ctx.set_verify(
SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT,
self.verifyCN
)
return ctx
def verifyCN(self, connection, x509, errno, depth, preverifyOK):
# DEBUG: print "%s == %s ?" % (self.cn, x509.get_subject().commonName)
# Somehow, if I don't set this to true, the verifyCN doesn't go
# down in the chain, I don't know if this is OK
verify = True
if depth == 0:
if self.cn == x509.get_subject().commonName:
verify = True
else:
verify = False
return verify
# Based in get-tor-exits (BridgeDB)
def handle(failure):
"""Handle a **failure**."""
if failure.type == ConnectionRefusedError:
logging.error("Couldn't download file; connection was refused.")
elif failure.type == DNSLookupError:
logging.error("Couldn't download file; domain resolution failed.")
elif failure.type == TimeoutError:
logging.error("Couldn't download file; connection timed out.")
else:
logging.error("Couldn't download file.")
print "Couldn't download file. Check the log."
os._exit(-1)
# Taken from get-tor-exits (BridgeDB)
def writeToFile(response, filename):
"""Write requested content to filename."""
finished = defer.Deferred()
response.deliverBody(FileWriter(finished, filename))
return finished
def is_json(my_json):
"""Check if json generated is valid."""
try:
json_object = json.loads(my_json)
except ValueError, e:
return False
return True
def add_tpo_link(url):
"""Add the download link for Tor Browser."""
uri = 'projects/torbrowser.html.en#downloads'
if url.endswith('/'):
return "%s%s" % (url, uri)
else:
return "%s/%s" % (url, uri)
def add_entry(mirrors, columns, elements):
"""Add entry to mirrors list."""
entry = {}
count = 0
for e in elements:
e = e.replace("\n", '')
entry[columns[count]] = e
count = count + 1
mirrors.append(entry)
def add_mirror(file, entry, proto):
"""Add mirror to mirrors list."""
# if proto requested is http(s), we add link to download section
if PROTOS[proto] == 'http' or PROTOS[proto] == 'https':
uri = add_tpo_link(entry[proto])
else:
uri = entry[proto]
file.write(
"%s - by %s (%s)\n" % (
uri,
entry['orgName'],
entry['subRegion'],
)
)
def main():
"""Script to get the list of tpo's mirrors from tpo and adapt it to
be used by GetTor.
Usage: python2.7 get_mirrors.py [-h] [--proto protocol]
By default, the protocol is 'https'. Possible values of protcol are:
http, https, rsync, ftp, onion, http-dist, https-dist, rsync-dist.
"""
parser = argparse.ArgumentParser(
description="Utility to download tpo's mirrors and make it usable\
by GetTor."
)
parser.add_argument(
'-p', '--proto',
default='https',
help='Protocol filter. Possible values: http, https, rsync, ftp, onion\
http-dist, https-dist, rsync-dist. Default to https.')
args = parser.parse_args()
p = args.proto
gettor_path = '/path/to/gettor/'
csv_path = os.path.join(gettor_path, 'tor-mirrors.csv')
json_path = os.path.join(gettor_path, 'tor-mirrors')
mirrors_list = os.path.join(gettor_path, 'mirrors-list.txt')
# Load tpo certificate and extract common name, we'll later compare this
# with the certificate sent by tpo to check we're really taltking to it
try:
data = open(CERT_TPO).read()
x509 = crypto.load_certificate(crypto.FILETYPE_PEM, data)
cn_tpo = x509.get_subject().commonName
except Exception as e:
logging.error("Error with certificate: %s" % str(e))
return
# While we wait the json of mirrors to be implemented in tpo, we need
# to download the csv file and transform it to json
# The code below is based in get-tor-exits script from BridgeDB and
# the tor2web.utils.ssl module from Tor2web
url = 'https://www.torproject.org/include/tor-mirrors.csv'
try:
fh = open(csv_path, 'w')
except IOError as e:
logging.error("Could not open %s" % csv_path)
return
logging.info("Requesting %s..." % url)
# If certificate don't match an exception will be raised
# this is my first experience with twisted, maybe I'll learn to handle
# this better some time in the future...
contextFactory = HTTPSVerifyingContextFactory(cn_tpo)
agent = client.Agent(reactor, contextFactory)
d = agent.request("GET", url)
d.addCallback(writeToFile, fh)
d.addErrback(handle)
d.addCallbacks(log.msg, log.err)
if not reactor.running:
d.addCallback(lambda ignored: reactor.stop())
reactor.run()
logging.info("File downloaded!")
# Now transform it to json -- I couldn't find out how to use a
# two-character delimiter with the csv package, so I decided to handle
# the csv data by hand. We are doing this until #16601 gets deployed.
# https://trac.torproject.org/projects/tor/ticket/16601
# Code below is based in update-mirrors-json.py script from tpo
# These are the names of each column e.g. adminContact
columns = []
# List of mirrors to be built
mirrors = []
logging.info("Transforming csv data into json...")
logging.info("Getting data from csv")
try:
with codecs.open(csv_path, "rb", "utf-8") as csvfile:
for line in csvfile:
elements = line.split(", ")
# first entry have the names of the columns
if not columns:
columns = elements
else:
add_entry(mirrors, columns, elements)
except IOError as e:
logging.error("Couldn't read csv file: %s" % str(e))
return
logging.info("Creating json")
if is_json(json.dumps(mirrors)):
try:
with codecs.open(json_path, "w", "utf-8") as jsonfile:
# Make pretty json
json.dump(
mirrors,
jsonfile,
sort_keys=True,
indent=4,
separators=(',', ': '),
encoding="utf-8",
)
except IOError as e:
logging.error("Couldn't write json: %s" % str(e))
return
else:
logging.error("Invalid json file")
return
# Now make the mirrors list to be used by GetTor
logging.info("Reading json")
try:
mirrors_json = codecs.open(json_path, "rb", "utf-8")
mirrors = json.load(mirrors_json)
except IOError as e:
logging.error("Couldn't open %s" % json_path)
return
logging.info("Creating new list with protocol: %s" % p)
try:
list = codecs.open(mirrors_list, "w", "utf-8")
for entry in mirrors:
if args.proto is not 'all':
for e in entry:
if e == PROTOS[p] and entry[PROTOS[p]]:
add_mirror(list, entry, PROTOS[p])
else:
for k, v in PROTOS:
if entry[v]:
add_mirror(list, entry, v)
logging.info("List created: %s" % mirrors_list)
except IOError as e:
logging.error("Couldn't open %s" % mirrors_list)
return
if __name__ == "__main__":
logging_format = utils.get_logging_format()
logging.basicConfig(
filename='/path/to/gettor/log/get-mirrors.log',
format=logging_format,
datefmt="%Y-%m-%d %H:%M:%S",
level=logging.INFO
)
logging.info("Started")
main()
logging.info("Finished")