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_control_endpoint_no_port(self): control_ep = FakeHostnameEndpoint(reactor, "localhost", 9051) h = tor.control_endpoint(control_ep) config = Empty() config.SocksPort = ["unparseable"] with mock.patch("txtorcon.build_tor_connection", return_value=None): with mock.patch("txtorcon.TorConfig.from_protocol", return_value=config): d = h.hint_to_endpoint("tor:foo.onion:29212", reactor, discard_status) f = yield self.assertFailure(d, ValueError) self.assertIn("could not use config.SocksPort", str(f))
def test_control_endpoint_no_port(self): control_ep = endpoints.HostnameEndpoint(reactor, "localhost", 9051) h = tor.control_endpoint(control_ep) config = Empty() config.SocksPort = ["unparseable"] with mock.patch("txtorcon.build_tor_connection", return_value=None): with mock.patch("txtorcon.TorConfig.from_protocol", return_value=config): d = h.hint_to_endpoint("tor:foo.onion:29212", reactor) f = yield self.assertFailure(d, ValueError) self.assertIn("could not use config.SocksPort", str(f))
def test_control_endpoint_non_numeric(self): control_ep = endpoints.HostnameEndpoint(reactor, "localhost", 9051) h = tor.control_endpoint(control_ep) config = Empty() config.SocksPort = ["unix:var/run/tor/socks WorldWritable", "1234"] with mock.patch("txtorcon.build_tor_connection", return_value=None): with mock.patch("txtorcon.TorConfig.from_protocol", return_value=config): res = yield h.hint_to_endpoint("tor:foo.onion:29212", reactor) ep, host = res self.assertIsInstance(ep, txtorcon.endpoints.TorClientEndpoint) self.assertEqual(host, "foo.onion") self.assertEqual(h._socks_desc, "tcp:127.0.0.1:1234")
def test_control_endpoint_default(self): control_ep = endpoints.HostnameEndpoint(reactor, "localhost", 9051) h = tor.control_endpoint(control_ep) config = Empty() config.SocksPort = [txtorcon.DEFAULT_VALUE] with mock.patch("txtorcon.build_tor_connection", return_value=None): with mock.patch("txtorcon.TorConfig.from_protocol", return_value=config): res = yield h.hint_to_endpoint("tor:foo.onion:29212", reactor) ep, host = res self.assertIsInstance(ep, txtorcon.endpoints.TorClientEndpoint) self.assertEqual(host, "foo.onion") self.assertEqual(h._socks_desc, "tcp:127.0.0.1:9050")
def test_control_endpoint_nested_list(self): control_ep = FakeHostnameEndpoint(reactor, "localhost", 9051) h = tor.control_endpoint(control_ep) config = Empty() config.SocksPort = [["unix:var/run/tor/socks WorldWritable", "1234"]] with mock.patch("txtorcon.build_tor_connection", return_value=None): with mock.patch("txtorcon.TorConfig.from_protocol", return_value=config): res = yield h.hint_to_endpoint("tor:foo.onion:29212", reactor, discard_status) ep, host = res self.assertIsInstance(ep, txtorcon.endpoints.TorClientEndpoint) self.assertEqual(host, "foo.onion") self.assertEqual(h._socks_desc, "tcp:127.0.0.1:1234")
def test_control_endpoint_default(self): control_ep = FakeHostnameEndpoint(reactor, "localhost", 9051) h = tor.control_endpoint(control_ep) config = Empty() config.SocksPort = [txtorcon.DEFAULT_VALUE] with mock.patch("txtorcon.build_tor_connection", return_value=None): with mock.patch("txtorcon.TorConfig.from_protocol", return_value=config): res = yield h.hint_to_endpoint("tor:foo.onion:29212", reactor, discard_status) ep, host = res self.assertIsInstance(ep, txtorcon.endpoints.TorClientEndpoint) self.assertEqual(host, "foo.onion") self.assertEqual(h._socks_desc, "tcp:127.0.0.1:9050")
def test_control_endpoint_non_numeric(self): control_ep = FakeHostnameEndpoint(reactor, 'localhost', 9051) h = tor.control_endpoint(control_ep) config = Empty() config.SocksPort = ['unix:var/run/tor/socks WorldWritable', '1234'] with mock.patch('txtorcon.build_tor_connection', return_value=None): with mock.patch('txtorcon.TorConfig.from_protocol', return_value=config): res = yield h.hint_to_endpoint('tor:foo.onion:29212', reactor, discard_status) ep, host = res self.assertIsInstance(ep, txtorcon.endpoints.TorClientEndpoint) self.assertEqual(host, b'foo.onion') self.assertEqual(h._socks_desc, 'tcp:127.0.0.1:1234')
def test_control_endpoint(self): control_ep = endpoints.HostnameEndpoint(reactor, "localhost", 9051) h = tor.control_endpoint(control_ep) # We don't actually care about the generated endpoint, just the state # that the handler builds up internally. But we need to provoke a # connection to build that state, and we need to prevent the handler # from actually talking to a Tor daemon (which probably doesn't exist # on this host). config = Empty() config.SocksPort = ["1234"] with mock.patch("txtorcon.build_tor_connection", return_value=None): with mock.patch("txtorcon.TorConfig.from_protocol", return_value=config): res = yield h.hint_to_endpoint("tor:foo.onion:29212", reactor) ep, host = res self.assertIsInstance(ep, txtorcon.endpoints.TorClientEndpoint) self.assertEqual(host, "foo.onion") self.assertEqual(h._socks_desc, "tcp:127.0.0.1:1234")
def test_control_endpoint(self): control_ep = FakeHostnameEndpoint(reactor, "localhost", 9051) h = tor.control_endpoint(control_ep) # We don't actually care about the generated endpoint, just the state # that the handler builds up internally. But we need to provoke a # connection to build that state, and we need to prevent the handler # from actually talking to a Tor daemon (which probably doesn't exist # on this host). config = Empty() config.SocksPort = ["1234"] with mock.patch("txtorcon.build_tor_connection", return_value=None): with mock.patch("txtorcon.TorConfig.from_protocol", return_value=config): res = yield h.hint_to_endpoint("tor:foo.onion:29212", reactor, discard_status) ep, host = res self.assertIsInstance(ep, txtorcon.endpoints.TorClientEndpoint) self.assertEqual(host, "foo.onion") self.assertEqual(h._socks_desc, "tcp:127.0.0.1:1234")
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()
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 if which == "i2p-default": h = i2p.default(reactor) else: sam_ep = clientFromString(reactor, sys.argv[2])
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]'") sys.exit(1) print("using %s: %s" % (which, furl)) class Observer(Referenceable): def remote_event(self, msg):