示例#1
0
 def test_launch_tor_binary(self):
     tpp = Empty()
     tpp.tor_protocol = None
     h = tor.launch(tor_binary="/bin/tor")
     fake_reactor = object()
     with mock.patch("txtorcon.launch_tor", return_value=tpp) as lt:
         res = yield h.hint_to_endpoint("tor:foo.onion:29212", fake_reactor)
         self.assertEqual(len(lt.mock_calls), 1)
         args,kwargs = lt.mock_calls[0][1:]
         self.assertIdentical(args[0], h.config)
         self.assertIdentical(args[1], fake_reactor)
         self.assertEqual(kwargs, {"tor_binary": "/bin/tor"})
     ep, host = res
     self.assertIsInstance(ep, txtorcon.endpoints.TorClientEndpoint)
     self.assertEqual(host, "foo.onion")
     self.assert_(h._socks_desc.startswith("tcp:127.0.0.1:"), h._socks_desc)
示例#2
0
 def test_launch_tor_binary(self):
     tpp = Empty()
     tpp.tor_protocol = None
     h = tor.launch(tor_binary="/bin/tor")
     fake_reactor = object()
     with mock.patch("txtorcon.launch_tor", return_value=tpp) as lt:
         res = yield h.hint_to_endpoint("tor:foo.onion:29212", fake_reactor,
                                        discard_status)
         self.assertEqual(len(lt.mock_calls), 1)
         args, kwargs = lt.mock_calls[0][1:]
         self.assertIdentical(args[0], h.config)
         self.assertIdentical(args[1], fake_reactor)
         self.assertEqual(kwargs, {"tor_binary": "/bin/tor"})
     ep, host = res
     self.assertIsInstance(ep, txtorcon.endpoints.TorClientEndpoint)
     self.assertEqual(host, "foo.onion")
     self.assert_(h._socks_desc.startswith("tcp:127.0.0.1:"), h._socks_desc)
示例#3
0
 def test_launch_data_directory_exists(self):
     datadir = self.mktemp()
     os.mkdir(datadir)
     tpp = Empty()
     tpp.tor_protocol = None
     h = tor.launch(data_directory=datadir)
     fake_reactor = object()
     with mock.patch("txtorcon.launch_tor", return_value=tpp) as lt:
         res = yield h.hint_to_endpoint("tor:foo.onion:29212", fake_reactor,
                                        discard_status)
         self.assertEqual(len(lt.mock_calls), 1)
         args,kwargs = lt.mock_calls[0][1:]
         self.assertIdentical(args[0], h.config)
         self.assertIdentical(args[1], fake_reactor)
         self.assertEqual(kwargs, {"tor_binary": None})
         self.assertEqual(h.config.DataDirectory, datadir)
     ep, host = res
     self.assertIsInstance(ep, txtorcon.endpoints.TorClientEndpoint)
     self.assertEqual(host, "foo.onion")
     self.assert_(h._socks_desc.startswith("tcp:127.0.0.1:"), h._socks_desc)
示例#4
0
    def test_launch(self):
        tpp = Empty()
        tpp.tor_protocol = None
        h = tor.launch()
        fake_reactor = object()

        with mock.patch('txtorcon.launch_tor', return_value=tpp) as lt:
            res = yield h.hint_to_endpoint('tor:foo.onion:29212', fake_reactor,
                                           discard_status)
            self.assertEqual(len(lt.mock_calls), 1)
            args, kwargs = lt.mock_calls[0][1:]
            self.assertIdentical(args[0], h.config)
            self.assertIdentical(args[1], fake_reactor)
            self.assertEqual(kwargs, {'tor_binary': None})

        ep, host = res
        self.assertIsInstance(ep, txtorcon.endpoints.TorClientEndpoint)
        self.assertEqual(host, b'foo.onion')
        # launch_tor will allocate a local TCP port for SOCKS
        self.assertTrue(h._socks_desc.startswith('tcp:127.0.0.1:'),
                        h._socks_desc)
示例#5
0
 def test_launch_data_directory_exists(self):
     datadir = self.mktemp()
     os.mkdir(datadir)
     tpp = Empty()
     tpp.tor_protocol = None
     h = tor.launch(data_directory=datadir)
     fake_reactor = object()
     with mock.patch("txtorcon.launch_tor", return_value=tpp) as lt:
         res = yield h.hint_to_endpoint("tor:foo.onion:29212", fake_reactor,
                                        discard_status)
         self.assertEqual(len(lt.mock_calls), 1)
         args, kwargs = lt.mock_calls[0][1:]
         self.assertIdentical(args[0], h.config)
         self.assertIdentical(args[1], fake_reactor)
         self.assertEqual(kwargs, {"tor_binary": None})
         self.assertEqual(h.config.DataDirectory, datadir)
     ep, host = res
     self.assertIsInstance(ep, txtorcon.endpoints.TorClientEndpoint)
     self.assertEqual(host, "foo.onion")
     self.assertTrue(h._socks_desc.startswith("tcp:127.0.0.1:"),
                     h._socks_desc)
示例#6
0
    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()
    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])
        h = i2p.sam_endpoint(sam_ep)
    tub.removeAllConnectionHintHandlers()
    tub.addConnectionHintHandler("i2p", h)
    furl = "pb://%s@i2p:%s:%d/calculator" % (TUBID, I2P, I2PPORT)
else:
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):
        pass

@inlineCallbacks
def go():
    tub.startService()