def run_command(config): c = dispatch_table[config.subCommand]() tub = Tub() try: from twisted.internet import reactor from twisted.internet.endpoints import clientFromString from foolscap.connections import tor CONTROL = os.environ.get("FOOLSCAP_TOR_CONTROL_PORT", "") SOCKS = os.environ.get("FOOLSCAP_TOR_SOCKS_PORT", "") if CONTROL: h = tor.control_endpoint(clientFromString(reactor, CONTROL)) tub.addConnectionHintHandler("tor", h) elif SOCKS: h = tor.socks_endpoint(clientFromString(reactor, SOCKS)) tub.addConnectionHintHandler("tor", h) #else: # h = tor.default_socks() # tub.addConnectionHintHandler("tor", h) except ImportError: pass d = defer.succeed(None) d.addCallback(lambda _ign: tub.startService()) d.addCallback(lambda _ign: tub.getReference(config.furl)) d.addCallback(c.run, config.subOptions) # might provide tub here d.addBoth(lambda res: tub.stopService().addCallback(lambda _ign: res)) return d
def test_socks_endpoint_real(self): tor_socks_endpoint = clientFromString(reactor, "tcp:socks_host:100") h = tor.socks_endpoint(tor_socks_endpoint) res = yield h.hint_to_endpoint("tcp:example.com:1234", reactor) ep, host = res self.assertIsInstance(ep, txtorcon.endpoints.TorClientEndpoint) self.assertEqual(host, "example.com")
def test_socks_endpoint_real(self): tor_socks_endpoint = clientFromString(reactor, 'tcp:socks_host:100') h = tor.socks_endpoint(tor_socks_endpoint) 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')
def test_socks_endpoint(self): tor_socks_endpoint = clientFromString(reactor, "tcp:socks_host:100") with mock.patch("foolscap.connections.tor.txtorcon.TorClientEndpoint" ) as tce: tce.return_value = expected_ep = object() h = tor.socks_endpoint(tor_socks_endpoint) res = yield h.hint_to_endpoint("tcp:example.com:1234", reactor) self.assertEqual(tce.mock_calls, [mock.call("example.com", 1234, socks_endpoint=tor_socks_endpoint)]) ep, host = res self.assertIs(ep, expected_ep) self.assertEqual(host, "example.com")
def test_socks_endpoint(self): tor_socks_endpoint = clientFromString(reactor, "tcp:socks_host:100") with mock.patch( "foolscap.connections.tor.txtorcon.TorClientEndpoint") as tce: tce.return_value = expected_ep = object() h = tor.socks_endpoint(tor_socks_endpoint) 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=tor_socks_endpoint) ]) ep, host = res self.assertIs(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()