11import warnings
22import hashlib
33
4- from twisted .internet import interfaces , reactor , defer , protocol
4+ from twisted .internet import reactor , defer , protocol
55from twisted .internet .endpoints import TCP4ClientEndpoint
6- from twisted .web .client import (SchemeNotSupported , Agent , BrowserLikePolicyForHTTPS ,
7- ResponseDone , PotentialDataLoss , PartialDownloadError )
8- from txsocksx .client import SOCKS5ClientFactory
9- from txsocksx .tls import TLSWrapClientEndpoint
10- from zope .interface import implementer
6+ from twisted .web .client import (ResponseDone , PotentialDataLoss , PartialDownloadError )
117
128from bwscanner .logger import log
139
1410
11+ def fetch (tor_state , path , url ):
12+ d = tor_state .build_circuit (path , False )
13+ sport = get_tor_socks_endpoint (tor_state )
14+ d .addCallback (lambda c : c .when_built ())
15+ d .addCallback (lambda c : c .web_agent (reactor , sport ))
16+ return d .addCallback (lambda a : a .request ("GET" , url ))
17+
18+
1519def get_tor_socks_endpoint (tor_state ):
1620 proxy_endpoint = tor_state .protocol .get_conf ("SocksPort" )
1721
@@ -31,78 +35,6 @@ def extract_port_value(result):
3135 return proxy_endpoint
3236
3337
34- @implementer (interfaces .IStreamClientEndpoint )
35- class OnionRoutedTCPClientEndpoint (object ):
36- def __init__ (self , host , port , state , path ):
37- """
38- @param reactor: An L{IReactorTCP} provider
39-
40- @param host: A hostname, used when connecting
41- @type host: str
42-
43- @param port: The port number, used when connecting
44- @type port: int
45-
46- @param path: A list of relay identities.
47- @type path: list
48-
49- This endpoint will be routed through Tor over a circuit
50- defined by path.
51- """
52- self .host = host
53- self .port = port
54- self .path = path
55- self .state = state
56-
57- self .tor_socks_endpoint = get_tor_socks_endpoint (state )
58-
59- def connect (self , protocol_factory ):
60- """
61- Implements L{IStreamClientEndpoint.connect} to connect via TCP, after
62- SOCKS5 negotiation and Tor circuit construction is done.
63- """
64- proxy_factory = SOCKS5ClientFactory (self .host , self .port , protocol_factory )
65- self .tor_socks_endpoint .addCallback (lambda end : end .connect (proxy_factory ))
66-
67- def _create_circ (proto ):
68- hp = proto .transport .getHost ()
69- d = self .state ._attacher .create_circuit (hp .host , hp .port , self .path )
70- d .addErrback (proxy_factory .deferred .errback )
71- return proxy_factory .deferred
72-
73- return self .tor_socks_endpoint .addCallback (_create_circ )
74-
75-
76- class OnionRoutedAgent (Agent ):
77- _tlsWrapper = TLSWrapClientEndpoint
78- _policyForHTTPS = BrowserLikePolicyForHTTPS
79-
80- def __init__ (self , * args , ** kw ):
81- self .path = kw .pop ('path' )
82- self .state = kw .pop ('state' )
83- super (OnionRoutedAgent , self ).__init__ (* args , ** kw )
84-
85- def _getEndpoint (self , parsedURI , host = None , port = None ):
86- try :
87- host , port = parsedURI .host , parsedURI .port
88- scheme = parsedURI .scheme
89- except AttributeError :
90- scheme = parsedURI
91- if scheme not in ('http' , 'https' ):
92- raise SchemeNotSupported ('unsupported scheme' , scheme )
93- endpoint = OnionRoutedTCPClientEndpoint (host , port , self .state ,
94- self .path )
95- if scheme == 'https' :
96- if hasattr (self , '_wrapContextFactory' ):
97- tls_policy = self ._wrapContextFactory (host , port )
98- elif hasattr (self , '_policyForHTTPS' ):
99- tls_policy = self ._policyForHTTPS ().creatorForNetloc (host , port )
100- else :
101- raise NotImplementedError ("Cannot create a TLS validation policy." )
102- endpoint = self ._tlsWrapper (tls_policy , endpoint )
103- return endpoint
104-
105-
10638class hashingReadBodyProtocol (protocol .Protocol ):
10739 """
10840 Protocol that collects data sent to it and hashes it.
0 commit comments