Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_no_host(self):
self.assertRaises(
ValueError,
TorClientEndpoint, None, None, Mock(),
)
def test_client_connection_failed_user_password(self):
"""
Same as above, but with a username/password.
"""
tor_endpoint = FakeTorSocksEndpoint(
None, "fakehose", 9050,
failure=Failure(ConnectionRefusedError()),
)
endpoint = TorClientEndpoint(
'invalid host', 0,
socks_username='billy', socks_password='s333cure',
socks_endpoint=tor_endpoint)
d = endpoint.connect(None)
# XXX we haven't fixed socks.py to support user/pw yet ...
return self.assertFailure(d, RuntimeError)
return self.assertFailure(d, ConnectionRefusedError)
def test_factory(self, ftb):
reactor = Mock()
cp = Mock()
cp.get_conf = Mock(return_value=defer.succeed(dict()))
with patch(u'txtorcon.endpoints.available_tcp_port', return_value=9999):
ep = yield TorClientEndpoint.from_connection(reactor, cp, 'localhost', 1234)
self.assertTrue(isinstance(ep, TorClientEndpoint))
self.assertEqual(ep.host, 'localhost')
self.assertEqual(ep.port, 1234)
def test_good_port_retry(self):
"""
This tests that our Tor client endpoint retry logic works correctly.
We create a proxy endpoint that fires a ConnectionRefusedError
unless the connecting port matches. We attempt to connect with the
proxy endpoint for each port that the Tor client endpoint will try.
"""
success_ports = TorClientEndpoint.socks_ports_to_try
for port in success_ports:
tor_endpoint = FakeTorSocksEndpoint(
u"fakehost", "127.0.0.1", port,
accept_port=port,
failure=Failure(ConnectionRefusedError()),
)
endpoint = TorClientEndpoint(
'', 0,
socks_endpoint=tor_endpoint,
)
endpoint.connect(Mock())
self.assertEqual(tor_endpoint.transport.value(), b'\x05\x01\x00')
def hint_to_endpoint(self, hint, reactor, update_status):
# Return (endpoint, hostname), where "hostname" is what we pass to the
# HTTP "Host:" header so a dumb HTTP server can be used to redirect us.
mo = HINT_RE.search(hint)
if not mo:
raise InvalidHintError("unrecognized TCP/Tor hint")
host, portnum = mo.group(1), int(mo.group(2))
if is_non_public_numeric_address(host):
raise InvalidHintError("ignoring non-Tor-able ipaddr %s" % host)
with add_context(update_status, "connecting to a Tor"):
socks_endpoint = yield self._maybe_connect(reactor, update_status)
ep = txtorcon.TorClientEndpoint(host, portnum,
socks_endpoint=socks_endpoint)
returnValue( (ep, host) )
elif config['type'] == 'twisted':
endpoint = clientFromString(reactor, config['client_string'])
elif config['type'] == 'tor':
host = config['host']
port = config['port']
socks_port = config['tor_socks_port']
tls = config.get('tls', False)
if not tls and not host.endswith('.onion'):
log.warn("Non-TLS connection traversing Tor network; end-to-end encryption advised")
socks_endpoint = TCP4ClientEndpoint(
reactor, "127.0.0.1", socks_port,
)
endpoint = txtorcon.TorClientEndpoint(
host, port,
socks_endpoint=socks_endpoint,
reactor=reactor,
use_tls=tls,
)
else:
raise Exception("invalid endpoint type '{}'".format(config['type']))
return endpoint
def main(reactor):
tor_ep = clientFromString(reactor, "tcp:localhost:9050")
if True:
for domain in [u'www.torproject.org', u'meejah.ca']:
print("Looking up '{}' via Tor".format(domain))
ans = yield socks.resolve(tor_ep, domain)
print("...got answer: {}".format(ans))
print("Doing PTR on {}".format(ans))
ans = yield socks.resolve_ptr(tor_ep, ans)
print("...got answer: {}".format(ans))
ep = txtorcon.TorClientEndpoint(
'www.torproject.org', 80,
socks_endpoint=tor_ep,
)
factory = _AgentEndpointFactoryUsingTor(reactor, tor_ep)
agent = Agent.usingEndpointFactory(reactor, factory)
reply = yield agent.request('GET', 'https://www.torproject.org')
#reply = yield agent.request('GET', 'http://boingboing.net')
print("{}: {} ({} bytes)".format(reply.code, reply.phrase, reply.length))
text = yield readBody(reply)
if len(text) > 400:
print(text[:200], "...", text[-200:])
else:
print(text)