1
- """
2
- Tor2web
3
- Copyright (C) 2012 Hermes No Profit Association - GlobaLeaks Project
4
-
5
- This program is free software: you can redistribute it and/or modify
6
- it under the terms of the GNU Affero General Public License as published by
7
- the Free Software Foundation, either version 3 of the License, or
8
- (at your option) any later version.
9
-
10
- This program is distributed in the hope that it will be useful,
11
- but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- GNU Affero General Public License for more details.
14
-
15
- You should have received a copy of the GNU Affero General Public License
16
- along with this program. If not, see <http://www.gnu.org/licenses/>.
17
- """
18
-
19
1
"""
20
2
21
3
:mod:`Tor2Web`
48
30
49
31
from zope .interface import implements
50
32
from twisted .spread import pb
51
- from twisted .internet import reactor , protocol , defer
33
+ from twisted .internet import reactor , protocol , defer , address
52
34
from twisted .internet .abstract import isIPAddress , isIPv6Address
53
35
from twisted .internet .endpoints import TCP4ClientEndpoint , SSL4ClientEndpoint
54
36
from twisted .protocols .policies import WrappingFactory
55
37
from twisted .web import http , client , _newclient
56
38
from twisted .web .error import SchemeNotSupported
57
- from twisted .web .http import datetimeToString , StringTransport , _IdentityTransferDecoder , _ChunkedTransferDecoder , \
58
- parse_qs
39
+ from twisted .web .http import datetimeToString , StringTransport , \
40
+ _IdentityTransferDecoder , _ChunkedTransferDecoder , parse_qs
59
41
from twisted .web .http_headers import Headers
60
42
from twisted .web .server import NOT_DONE_YET
61
43
from twisted .web .template import flattenString , XMLString
62
44
from twisted .web .iweb import IBodyProducer
63
45
from twisted .python import log , logfile
64
46
from twisted .python .compat import networkString , intToBytes
47
+ from twisted .python .failure import Failure
65
48
from twisted .python .filepath import FilePath
66
- from twisted .python .log import err
67
49
from twisted .internet .task import LoopingCall
68
50
from tor2web import __version__
69
51
from tor2web .utils .config import Config
@@ -230,7 +212,7 @@ def spawnT2W(father, childFDs, fds_https, fds_http):
230
212
childFDs = childFDs )
231
213
232
214
233
- class Tor2webObj ():
215
+ class Tor2webObj (object ):
234
216
def __init__ (self ):
235
217
# The destination hidden service identifier
236
218
self .onion = None
@@ -379,14 +361,13 @@ class RedirectAgent(client.RedirectAgent):
379
361
"""
380
362
Overridden client.RedirectAgent version where we evaluate and handle automatically only HTTPS redirects
381
363
"""
382
-
383
364
def _handleResponse (self , response , method , uri , headers , redirectCount ):
384
365
locationHeaders = response .headers .getRawHeaders ('location' , [])
385
366
if locationHeaders :
386
367
location = self ._resolveLocation (uri , locationHeaders [0 ])
387
368
parsed = client ._URI .fromBytes (location )
388
369
if parsed .scheme == 'https' :
389
- return client .RedirectAgent ._handleResponse (self , response , method , uri , header , redirectCount )
370
+ return client .RedirectAgent ._handleResponse (self , response , method , uri , headers , redirectCount )
390
371
391
372
return response
392
373
@@ -439,12 +420,6 @@ def __init__(self, channel, queued, reactor=reactor):
439
420
440
421
self .translation_rexp = {}
441
422
442
- def finish (self ):
443
- try :
444
- http .Request .finish ()
445
- except Exception :
446
- pass
447
-
448
423
def getRequestHostname (self ):
449
424
"""
450
425
Function overload to fix ipv6 bug:
@@ -496,7 +471,7 @@ def getForwarders(self):
496
471
port ))
497
472
else :
498
473
raise Exception
499
- except :
474
+ except Exception :
500
475
return []
501
476
502
477
return forwarders
@@ -841,7 +816,7 @@ def process(self):
841
816
ctype = ctype [0 ]
842
817
843
818
if self .method == b"POST" and ctype :
844
- key , pdict = parse_header (ctype )
819
+ key , _ = parse_header (ctype )
845
820
if key == b'application/x-www-form-urlencoded' :
846
821
args .update (parse_qs (content , 1 ))
847
822
# ################################################################
@@ -923,7 +898,7 @@ def process(self):
923
898
924
899
else :
925
900
if type (antanistaticmap [staticpath ]) == str :
926
- filename , ext = os .path .splitext (staticpath )
901
+ _ , ext = os .path .splitext (staticpath )
927
902
self .setHeader (b'content-type' , mimetypes .types_map [ext ])
928
903
content = antanistaticmap [staticpath ]
929
904
defer .returnValue (self .contentFinish (content ))
@@ -1236,71 +1211,6 @@ def registerProtocol(self, p):
1236
1211
1237
1212
1238
1213
def start_worker ():
1239
- global antanistaticmap
1240
- global templates
1241
- global pool
1242
- global ports
1243
-
1244
- ult = LoopingCall (updateListsTask )
1245
- ult .start (600 )
1246
-
1247
- # ##############################################################################
1248
- # Static Data loading
1249
- # Here we make a file caching to not handle I/O
1250
- # at run-time and achieve better performance
1251
- # ##############################################################################
1252
- antanistaticmap = {}
1253
-
1254
- # system default static files
1255
- sys_static_dir = os .path .join (config .sysdatadir , "static/" )
1256
- if os .path .exists (sys_static_dir ):
1257
- for root , dirs , files in os .walk (os .path .join (sys_static_dir )):
1258
- for basename in files :
1259
- filename = os .path .join (root , basename )
1260
- f = FilePath (filename )
1261
- antanistaticmap [filename .replace (sys_static_dir , "" )] = f .getContent ()
1262
-
1263
- # user defined static files
1264
- usr_static_dir = os .path .join (config .datadir , "static/" )
1265
- if usr_static_dir != sys_static_dir and os .path .exists (usr_static_dir ):
1266
- for root , dirs , files in os .walk (os .path .join (usr_static_dir )):
1267
- for basename in files :
1268
- filename = os .path .join (root , basename )
1269
- f = FilePath (filename )
1270
- antanistaticmap [filename .replace (usr_static_dir , "" )] = f .getContent ()
1271
- # ##############################################################################
1272
-
1273
- # ##############################################################################
1274
- # Templates loading
1275
- # Here we make a templates cache in order to not handle I/O
1276
- # at run-time and achieve better performance
1277
- # ##############################################################################
1278
- templates = {}
1279
-
1280
- # system default templates
1281
- sys_tpl_dir = os .path .join (config .sysdatadir , "templates/" )
1282
- if os .path .exists (sys_tpl_dir ):
1283
- files = FilePath (sys_tpl_dir ).globChildren ("*.tpl" )
1284
- for f in files :
1285
- f = FilePath (config .t2w_file_path (os .path .join ('templates' , f .basename ())))
1286
- templates [f .basename ()] = PageTemplate (XMLString (f .getContent ()))
1287
-
1288
- # user defined templates
1289
- usr_tpl_dir = os .path .join (config .datadir , "templates/" )
1290
- if usr_tpl_dir != sys_tpl_dir and os .path .exists (usr_tpl_dir ):
1291
- files = FilePath (usr_tpl_dir ).globChildren ("*.tpl" )
1292
- for f in files :
1293
- f = FilePath (config .t2w_file_path (os .path .join ('templates' , f .basename ())))
1294
- templates [f .basename ()] = PageTemplate (XMLString (f .getContent ()))
1295
- # ##############################################################################
1296
-
1297
- pool = client .HTTPConnectionPool (reactor , True )
1298
- pool .maxPersistentPerHost = config .sockmaxpersistentperhost
1299
- pool .cachedConnectionTimeout = config .sockcachedconnectiontimeout
1300
- pool .retryAutomatically = config .sockretryautomatically
1301
- def nullStartedConnecting (self , connector ): pass
1302
- pool ._factory .startedConnecting = nullStartedConnecting
1303
-
1304
1214
factory = T2WProxyFactory ()
1305
1215
1306
1216
# we do not want all workers to die in the same moment
@@ -1313,7 +1223,6 @@ def nullStartedConnecting(self, connector): pass
1313
1223
config .ssl_dh ,
1314
1224
config .cipher_list )
1315
1225
1316
-
1317
1226
fds_https , fds_http = [], []
1318
1227
if 'T2W_FDS_HTTPS' in os .environ :
1319
1228
fds_https = [int (x ) for x in os .environ ['T2W_FDS_HTTPS' ].split ("," ) if x ]
@@ -1364,8 +1273,6 @@ def set_hosts_map(d):
1364
1273
global hosts_map
1365
1274
hosts_map = d
1366
1275
1367
- global config
1368
-
1369
1276
rpc ("get_white_list" ).addCallback (set_white_list )
1370
1277
rpc ("get_black_list" ).addCallback (set_black_list )
1371
1278
rpc ("get_blocked_ua_list" ).addCallback (set_blocked_ua_list )
@@ -1463,8 +1370,61 @@ def umask(mask):
1463
1370
'html_t2w' : re .compile ( r'(href|src|url|action)([\ ]*=[\ ]*[\'\"]?)(?:http:|https:)?//([a-z0-9]{16})\.onion([\ \'\"/])' , re .I )
1464
1371
}
1465
1372
1466
- if 'T2W_FDS_HTTPS' not in os .environ and 'T2W_FDS_HTTP' not in os .environ :
1373
+ # ##############################################################################
1374
+ # Static Data loading
1375
+ # Here we make a file caching to not handle I/O
1376
+ # at run-time and achieve better performance
1377
+ # ##############################################################################
1378
+ antanistaticmap = {}
1379
+
1380
+ # system default static files
1381
+ sys_static_dir = os .path .join (config .sysdatadir , "static/" )
1382
+ if os .path .exists (sys_static_dir ):
1383
+ for root , dirs , files in os .walk (os .path .join (sys_static_dir )):
1384
+ for basename in files :
1385
+ filename = os .path .join (root , basename )
1386
+ f = FilePath (filename )
1387
+ antanistaticmap [filename .replace (sys_static_dir , "" )] = f .getContent ()
1388
+
1389
+ # user defined static files
1390
+ usr_static_dir = os .path .join (config .datadir , "static/" )
1391
+ if usr_static_dir != sys_static_dir and os .path .exists (usr_static_dir ):
1392
+ for root , dirs , files in os .walk (os .path .join (usr_static_dir )):
1393
+ for basename in files :
1394
+ filename = os .path .join (root , basename )
1395
+ f = FilePath (filename )
1396
+ antanistaticmap [filename .replace (usr_static_dir , "" )] = f .getContent ()
1397
+ # ##############################################################################
1467
1398
1399
+ templates = {}
1400
+
1401
+ # system default templates
1402
+ sys_tpl_dir = os .path .join (config .sysdatadir , "templates/" )
1403
+ if os .path .exists (sys_tpl_dir ):
1404
+ files = FilePath (sys_tpl_dir ).globChildren ("*.tpl" )
1405
+ for f in files :
1406
+ f = FilePath (config .t2w_file_path (os .path .join ('templates' , f .basename ())))
1407
+ templates [f .basename ()] = PageTemplate (XMLString (f .getContent ()))
1408
+
1409
+ # user defined templates
1410
+ usr_tpl_dir = os .path .join (config .datadir , "templates/" )
1411
+ if usr_tpl_dir != sys_tpl_dir and os .path .exists (usr_tpl_dir ):
1412
+ files = FilePath (usr_tpl_dir ).globChildren ("*.tpl" )
1413
+ for f in files :
1414
+ f = FilePath (config .t2w_file_path (os .path .join ('templates' , f .basename ())))
1415
+ templates [f .basename ()] = PageTemplate (XMLString (f .getContent ()))
1416
+ # ##############################################################################
1417
+
1418
+ ports = []
1419
+
1420
+ pool = client .HTTPConnectionPool (reactor , True )
1421
+ pool .maxPersistentPerHost = config .sockmaxpersistentperhost
1422
+ pool .cachedConnectionTimeout = config .sockcachedconnectiontimeout
1423
+ pool .retryAutomatically = config .sockretryautomatically
1424
+ def nullStartedConnecting (self , connector ): pass
1425
+ pool ._factory .startedConnecting = nullStartedConnecting
1426
+
1427
+ if 'T2W_FDS_HTTPS' not in os .environ and 'T2W_FDS_HTTP' not in os .environ :
1468
1428
set_proctitle ("tor2web" )
1469
1429
1470
1430
def open_listenin_socket (ip , port ):
@@ -1575,10 +1535,9 @@ def daemon_shutdown(self):
1575
1535
t2w_daemon .daemon_shutdown = daemon_shutdown
1576
1536
t2w_daemon .rpc_server = T2WRPCServer (config )
1577
1537
1578
- t2w_daemon .run (config )
1538
+ t2w_daemon .run ()
1579
1539
1580
1540
else :
1581
-
1582
1541
set_proctitle ("tor2web-worker" )
1583
1542
1584
1543
white_list = []
0 commit comments