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-
191"""
202
213:mod:`Tor2Web`
4830
4931from zope .interface import implements
5032from twisted .spread import pb
51- from twisted .internet import reactor , protocol , defer
33+ from twisted .internet import reactor , protocol , defer , address
5234from twisted .internet .abstract import isIPAddress , isIPv6Address
5335from twisted .internet .endpoints import TCP4ClientEndpoint , SSL4ClientEndpoint
5436from twisted .protocols .policies import WrappingFactory
5537from twisted .web import http , client , _newclient
5638from 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
5941from twisted .web .http_headers import Headers
6042from twisted .web .server import NOT_DONE_YET
6143from twisted .web .template import flattenString , XMLString
6244from twisted .web .iweb import IBodyProducer
6345from twisted .python import log , logfile
6446from twisted .python .compat import networkString , intToBytes
47+ from twisted .python .failure import Failure
6548from twisted .python .filepath import FilePath
66- from twisted .python .log import err
6749from twisted .internet .task import LoopingCall
6850from tor2web import __version__
6951from tor2web .utils .config import Config
@@ -230,7 +212,7 @@ def spawnT2W(father, childFDs, fds_https, fds_http):
230212 childFDs = childFDs )
231213
232214
233- class Tor2webObj ():
215+ class Tor2webObj (object ):
234216 def __init__ (self ):
235217 # The destination hidden service identifier
236218 self .onion = None
@@ -379,14 +361,13 @@ class RedirectAgent(client.RedirectAgent):
379361 """
380362 Overridden client.RedirectAgent version where we evaluate and handle automatically only HTTPS redirects
381363 """
382-
383364 def _handleResponse (self , response , method , uri , headers , redirectCount ):
384365 locationHeaders = response .headers .getRawHeaders ('location' , [])
385366 if locationHeaders :
386367 location = self ._resolveLocation (uri , locationHeaders [0 ])
387368 parsed = client ._URI .fromBytes (location )
388369 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 )
390371
391372 return response
392373
@@ -439,12 +420,6 @@ def __init__(self, channel, queued, reactor=reactor):
439420
440421 self .translation_rexp = {}
441422
442- def finish (self ):
443- try :
444- http .Request .finish ()
445- except Exception :
446- pass
447-
448423 def getRequestHostname (self ):
449424 """
450425 Function overload to fix ipv6 bug:
@@ -496,7 +471,7 @@ def getForwarders(self):
496471 port ))
497472 else :
498473 raise Exception
499- except :
474+ except Exception :
500475 return []
501476
502477 return forwarders
@@ -841,7 +816,7 @@ def process(self):
841816 ctype = ctype [0 ]
842817
843818 if self .method == b"POST" and ctype :
844- key , pdict = parse_header (ctype )
819+ key , _ = parse_header (ctype )
845820 if key == b'application/x-www-form-urlencoded' :
846821 args .update (parse_qs (content , 1 ))
847822 # ################################################################
@@ -923,7 +898,7 @@ def process(self):
923898
924899 else :
925900 if type (antanistaticmap [staticpath ]) == str :
926- filename , ext = os .path .splitext (staticpath )
901+ _ , ext = os .path .splitext (staticpath )
927902 self .setHeader (b'content-type' , mimetypes .types_map [ext ])
928903 content = antanistaticmap [staticpath ]
929904 defer .returnValue (self .contentFinish (content ))
@@ -1236,71 +1211,6 @@ def registerProtocol(self, p):
12361211
12371212
12381213def 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-
13041214 factory = T2WProxyFactory ()
13051215
13061216 # we do not want all workers to die in the same moment
@@ -1313,7 +1223,6 @@ def nullStartedConnecting(self, connector): pass
13131223 config .ssl_dh ,
13141224 config .cipher_list )
13151225
1316-
13171226 fds_https , fds_http = [], []
13181227 if 'T2W_FDS_HTTPS' in os .environ :
13191228 fds_https = [int (x ) for x in os .environ ['T2W_FDS_HTTPS' ].split ("," ) if x ]
@@ -1364,8 +1273,6 @@ def set_hosts_map(d):
13641273 global hosts_map
13651274 hosts_map = d
13661275
1367- global config
1368-
13691276 rpc ("get_white_list" ).addCallback (set_white_list )
13701277 rpc ("get_black_list" ).addCallback (set_black_list )
13711278 rpc ("get_blocked_ua_list" ).addCallback (set_blocked_ua_list )
@@ -1463,8 +1370,61 @@ def umask(mask):
14631370 'html_t2w' : re .compile ( r'(href|src|url|action)([\ ]*=[\ ]*[\'\"]?)(?:http:|https:)?//([a-z0-9]{16})\.onion([\ \'\"/])' , re .I )
14641371}
14651372
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+ # ##############################################################################
14671398
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 :
14681428 set_proctitle ("tor2web" )
14691429
14701430 def open_listenin_socket (ip , port ):
@@ -1575,10 +1535,9 @@ def daemon_shutdown(self):
15751535 t2w_daemon .daemon_shutdown = daemon_shutdown
15761536 t2w_daemon .rpc_server = T2WRPCServer (config )
15771537
1578- t2w_daemon .run (config )
1538+ t2w_daemon .run ()
15791539
15801540else :
1581-
15821541 set_proctitle ("tor2web-worker" )
15831542
15841543 white_list = []
0 commit comments