def test_default_socks_real(self): h = tor.default_socks() res = yield h.hint_to_endpoint('tcp:example.com:1234', reactor, discard_status) ep, host = res self.assertIsInstance(ep, txtorcon.endpoints.TorClientEndpoint) self.assertEqual(host, b'example.com') self.assertEqual(h.describe(), 'tor')
def test_default_socks_real(self): h = tor.default_socks() res = yield h.hint_to_endpoint("tcp:example.com:1234", reactor, discard_status) ep, host = res self.assertIsInstance(ep, txtorcon.endpoints.TorClientEndpoint) self.assertEqual(host, "example.com") self.assertEqual(h.describe(), "tor")
def test_default_socks(self): with mock.patch("foolscap.connections.tor.txtorcon.TorClientEndpoint" ) as tce: tce.return_value = expected_ep = object() h = tor.default_socks() res = yield h.hint_to_endpoint("tcp:example.com:1234", reactor) self.assertEqual(tce.mock_calls, [mock.call("example.com", 1234, socks_endpoint=None)]) ep, host = res self.assertIdentical(ep, expected_ep) self.assertEqual(host, "example.com")
def test_default_socks_badaddr(self): h = tor.default_socks() d = h.hint_to_endpoint('tcp:10.0.0.1:1234', reactor, discard_status) f = yield self.assertFailure(d, InvalidHintError) self.assertEqual(str(f), 'ignoring non-Tor-able ipaddr 10.0.0.1') d = h.hint_to_endpoint('tcp:127.0.0.1:1234', reactor, discard_status) f = yield self.assertFailure(d, InvalidHintError) self.assertEqual(str(f), 'ignoring non-Tor-able ipaddr 127.0.0.1') d = h.hint_to_endpoint('tcp:not@a@hint:123', reactor, discard_status) f = yield self.assertFailure(d, InvalidHintError) self.assertEqual(str(f), 'unrecognized TCP/Tor hint')
def test_default_socks_badaddr(self): h = tor.default_socks() d = h.hint_to_endpoint("tcp:10.0.0.1:1234", reactor) f = yield self.assertFailure(d, InvalidHintError) self.assertEqual(str(f), "ignoring non-Tor-able ipaddr 10.0.0.1") d = h.hint_to_endpoint("tcp:127.0.0.1:1234", reactor) f = yield self.assertFailure(d, InvalidHintError) self.assertEqual(str(f), "ignoring non-Tor-able ipaddr 127.0.0.1") d = h.hint_to_endpoint("tcp:not@a@hint:123", reactor) f = yield self.assertFailure(d, InvalidHintError) self.assertEqual(str(f), "unrecognized TCP/Tor hint")
def test_default_socks(self): with mock.patch( "foolscap.connections.tor.txtorcon.TorClientEndpoint") as tce: tce.return_value = expected_ep = object() h = tor.default_socks() res = yield h.hint_to_endpoint("tcp:example.com:1234", reactor, discard_status) self.assertEqual( tce.mock_calls, [mock.call("example.com", 1234, socks_endpoint=None)]) ep, host = res self.assertIdentical(ep, expected_ep) self.assertEqual(host, "example.com")
def _make_tor_handler(self): enabled = self.get_config("tor", "enabled", True, boolean=True) if not enabled: return None tor = _import_tor() if not tor: return None if self.get_config("tor", "launch", False, boolean=True): executable = self.get_config("tor", "tor.executable", None) datadir = os.path.join(self.basedir, "private", "tor-statedir") return tor.launch(data_directory=datadir, tor_binary=executable) socks_endpoint_desc = self.get_config("tor", "socks.port", None) if socks_endpoint_desc: socks_ep = endpoints.clientFromString(reactor, socks_endpoint_desc) return tor.socks_endpoint(socks_ep) controlport = self.get_config("tor", "control.port", None) if controlport: ep = endpoints.clientFromString(reactor, controlport) return tor.control_endpoint(ep) return tor.default_socks()
elif which == "socks": # "slogin -D 8013 HOSTNAME" starts a SOCKS server on localhost 8013, for # which connections will emerge from the other end. Check the server logs # to see the peer address of each addObserver call to verify that it is # coming from 127.0.0.1 rather than the client host. from foolscap.connections import socks h = socks.socks_endpoint(HostnameEndpoint(reactor, "localhost", 8013)) tub.removeAllConnectionHintHandlers() tub.addConnectionHintHandler("tcp", h) furl = "pb://%s@tcp:localhost:%d/calculator" % (TUBID, LOCALPORT) elif which in ("tor-default", "tor-socks", "tor-control", "tor-launch"): from foolscap.connections import tor if which == "tor-default": h = tor.default_socks() elif which == "tor-socks": h = tor.socks_port(int(sys.argv[2])) elif which == "tor-control": control_ep = clientFromString(reactor, sys.argv[2]) h = tor.control_endpoint(control_ep) elif which == "tor-launch": data_directory = None if len(sys.argv) > 2: data_directory = os.path.abspath(sys.argv[2]) h = tor.launch(data_directory) tub.removeAllConnectionHintHandlers() tub.addConnectionHintHandler("tor", h) furl = "pb://%s@tor:%s:%d/calculator" % (TUBID, ONION, ONIONPORT) elif which in ("i2p-default", "i2p-sam"): from foolscap.connections import i2p
import os, sys, time from twisted.internet import reactor from twisted.internet.defer import inlineCallbacks from twisted.internet.endpoints import clientFromString from foolscap.api import Referenceable, Tub tub = Tub() which = sys.argv[1] if len(sys.argv) > 1 else None if which == "tcp": furl = "pb://%s@tcp:%s:%d/calculator" % (TUBID, HOSTNAME, LOCALPORT) elif which in ("tor-default", "tor-socks", "tor-control", "tor-launch"): from foolscap.connections import tor if which == "tor-default": h = tor.default_socks() elif which == "tor-socks": h = tor.socks_port(int(sys.argv[2])) elif which == "tor-control": control_ep = clientFromString(reactor, sys.argv[2]) h = tor.control_endpoint(control_ep) elif which == "tor-launch": data_directory = None if len(sys.argv) > 2: data_directory = os.path.abspath(sys.argv[2]) h = tor.launch(data_directory) tub.removeAllConnectionHintHandlers() tub.addConnectionHintHandler("tor", h) furl = "pb://%s@tor:%s:%d/calculator" % (TUBID, ONION, ONIONPORT) else: print("run as 'check-connections-client.py [tcp|tor-default|tor-socks|tor-control|tor-launch]'")