Exemple #1
0
    def test_tls_socks_with_endpoint(self, ep_mock):
        """
        Same as above, except we provide an explicit endpoint
        """

        if not _HAVE_TLS:
            print ("no TLS support")
            return

        class FakeWrappedProto(object):
            wrappedProtocol = object()

        wrap = FakeWrappedProto()
        proto = defer.succeed(wrap)

        class FakeSocks5(object):
            def __init__(self, *args, **kw):
                pass

            def connect(self, *args, **kw):
                return proto

        ep_mock.side_effect = FakeSocks5
        endpoint = TorClientEndpoint(
            "torproject.org", 0, socks_endpoint=clientFromString(Mock(), "tcp:localhost:9050"), tls=True
        )
        p2 = yield endpoint.connect(None)
        self.assertTrue(wrap.wrappedProtocol is p2)
Exemple #2
0
    def test_tls_socks_with_endpoint(self, ep_mock):
        """
        Same as above, except we provide an explicit endpoint
        """
        the_proto = object()
        proto_d = defer.succeed(the_proto)

        class FakeSocks5(object):

            def __init__(self, *args, **kw):
                pass

            def connect(self, *args, **kw):
                return proto_d

            def _get_address(self):
                return defer.succeed(None)

        ep_mock.side_effect = FakeSocks5
        endpoint = TorClientEndpoint(
            u'torproject.org', 0,
            socks_endpoint=clientFromString(Mock(), "tcp:localhost:9050"),
            tls=True,
        )
        p2 = yield endpoint.connect(None)
        self.assertTrue(p2 is the_proto)
Exemple #3
0
    def test_default_socks_ports_happy(self, ep_mock):
        """
        Ensure we iterate over the default socks ports
        """

        proto = object()
        ports_attempted = []

        class FakeSocks5(object):
            def __init__(self, tor_ep, *args, **kw):
                self.tor_port = tor_ep._port

            def connect(self, *args, **kw):
                ports_attempted.append(self.tor_port)
                if self.tor_port != 9150:
                    return Failure(error.ConnectError("foo"))
                else:
                    return proto

            def _get_address(self):
                return defer.succeed(None)

        ep_mock.side_effect = FakeSocks5
        endpoint = TorClientEndpoint('', 0)
        p2 = yield endpoint.connect(None)
        self.assertTrue(proto is p2)
        self.assertEqual(ports_attempted, [9050, 9150])

        # now, if we re-use the endpoint, we should again attempt the
        # two ports
        p3 = yield endpoint.connect(None)
        self.assertTrue(proto is p3)
        self.assertEqual(ports_attempted, [9050, 9150, 9050, 9150])
Exemple #4
0
    def test_tls_socks_with_endpoint(self, ep_mock):
        """
        Same as above, except we provide an explicit endpoint
        """

        if not _HAVE_TLS:
            print("no TLS support")
            return

        class FakeWrappedProto(object):
            wrappedProtocol = object()

        wrap = FakeWrappedProto()
        proto = defer.succeed(wrap)

        class FakeSocks5(object):
            def __init__(self, *args, **kw):
                pass

            def connect(self, *args, **kw):
                return proto

        ep_mock.side_effect = FakeSocks5
        endpoint = TorClientEndpoint(
            'torproject.org',
            0,
            socks_endpoint=clientFromString(Mock(), "tcp:localhost:9050"),
            tls=True,
        )
        p2 = yield endpoint.connect(None)
        self.assertTrue(wrap.wrappedProtocol is p2)
 def test_default_factory(self):
     """
     This test is equivalent to txsocksx's TestSOCKS5ClientEndpoint.test_defaultFactory
     """
     def TorSocksEndpointGenerator(*args, **kw):
         return FakeTorSocksEndpoint(*args, **kw)
     endpoint = TorClientEndpoint('', 0, _proxy_endpoint_generator=TorSocksEndpointGenerator)
     endpoint.connect(None)
     self.assertEqual(endpoint.tor_socks_endpoint.transport.value(), '\x05\x01\x00')
Exemple #6
0
 def test_bad_port_retry(self):
     """
     This tests failure to connect to the ports on the "try" list.
     """
     fail_ports = [1984, 666]
     for port in fail_ports:
         ep = FakeTorSocksEndpoint("", "", 0, accept_port=port, failure=Failure(ConnectionRefusedError()))
         endpoint = TorClientEndpoint("", 0, socks_endpoint=ep)
         d = endpoint.connect(None)
         return self.assertFailure(d, ConnectionRefusedError)
 def test_client_connection_failed(self):
     """
     This test is equivalent to txsocksx's
     TestSOCKS4ClientEndpoint.test_clientConnectionFailed
     """
     def fail_tor_socks_endpoint_generator(*args, **kw):
         kw['failure'] = Failure(ConnectionRefusedError())
         return FakeTorSocksEndpoint(*args, **kw)
     endpoint = TorClientEndpoint('', 0, _proxy_endpoint_generator=fail_tor_socks_endpoint_generator)
     d = endpoint.connect(None)
     return self.assertFailure(d, ConnectionRefusedError)
Exemple #8
0
    def test_default_factory(self):
        """
        This test is equivalent to txsocksx's TestSOCKS5ClientEndpoint.test_defaultFactory
        """

        args = "fakehost"
        kw = dict()
        tor_endpoint = FakeTorSocksEndpoint(*args, **kw)
        endpoint = TorClientEndpoint('', 0, socks_endpoint=tor_endpoint)
        endpoint.connect(Mock)
        self.assertEqual(tor_endpoint.transport.value(), '\x05\x01\x00')
Exemple #9
0
    def test_default_factory(self):
        """
        This test is equivalent to txsocksx's TestSOCKS5ClientEndpoint.test_defaultFactory
        """

        args = "fakehost"
        kw = dict()
        tor_endpoint = FakeTorSocksEndpoint(*args, **kw)
        endpoint = TorClientEndpoint("", 0, socks_endpoint=tor_endpoint)
        endpoint.connect(Mock)
        self.assertEqual(tor_endpoint.transport.value(), "\x05\x01\x00")
Exemple #10
0
 def test_success(self, socks5_factory):
     ep = MagicMock()
     gold_proto = object()
     ep.connect = MagicMock(return_value=gold_proto)
     socks5_factory.return_value = ep
     args = "fakehost"
     kw = dict()
     tor_endpoint = FakeTorSocksEndpoint(*args, **kw)
     endpoint = TorClientEndpoint("", 0, socks_endpoint=tor_endpoint)
     other_proto = yield endpoint.connect(MagicMock())
     self.assertEqual(other_proto, gold_proto)
Exemple #11
0
 def test_success(self, socks5_factory):
     ep = MagicMock()
     gold_proto = object()
     ep.connect = MagicMock(return_value=gold_proto)
     socks5_factory.return_value = ep
     args = "fakehost"
     kw = dict()
     tor_endpoint = FakeTorSocksEndpoint(*args, **kw)
     endpoint = TorClientEndpoint('', 0, socks_endpoint=tor_endpoint)
     other_proto = yield endpoint.connect(MagicMock())
     self.assertEqual(other_proto, gold_proto)
Exemple #12
0
 def test_success(self):
     with patch.object(_TorSocksFactory, "protocol", FakeSocksProto):
         tor_endpoint = FakeTorSocksEndpoint(Mock(), "fakehost", 9050)
         endpoint = TorClientEndpoint(
             u'meejah.ca', 443,
             socks_endpoint=tor_endpoint,
         )
         proto = yield endpoint.connect(MagicMock())
         self.assertTrue(isinstance(proto, FakeSocksProto))
         self.assertEqual(u"meejah.ca", proto.host)
         self.assertEqual(443, proto.port)
         self.assertEqual('CONNECT', proto.method)
 def test_good_no_guess_socks_port(self):
     """
     This tests that if a SOCKS port is specified, we *only* attempt to
     connect to that SOCKS port.
     """
     def TorSocksEndpointGenerator(*args, **kw):
         kw['acceptPort'] = 6669
         kw['failure'] = connectionRefusedFailure
         return FakeTorSocksEndpoint(*args, **kw)
     endpoint = TorClientEndpoint('', 0, _proxy_endpoint_generator=TorSocksEndpointGenerator, socks_port=6669)
     endpoint.connect(None)
     self.assertEqual(endpoint.tor_socks_endpoint.transport.value(), '\x05\x01\x00')
Exemple #14
0
    def test_success(self, socks5_factory):
        ep = MagicMock()
        gold_proto = object()
        ep.connect = MagicMock(return_value=gold_proto)
        socks5_factory.return_value = ep

        def tor_socks_endpoint_generator(*args, **kw):
            return FakeTorSocksEndpoint(*args, **kw)

        endpoint = TorClientEndpoint('', 0, _proxy_endpoint_generator=tor_socks_endpoint_generator)
        other_proto = yield endpoint.connect(MagicMock())
        self.assertEqual(other_proto, gold_proto)
Exemple #15
0
 def test_client_connection_failed(self):
     """
     This test is equivalent to txsocksx's
     TestSOCKS4ClientEndpoint.test_clientConnectionFailed
     """
     args = "host123"
     kw = dict()
     kw["failure"] = Failure(ConnectionRefusedError())
     tor_endpoint = FakeTorSocksEndpoint(*args, **kw)
     endpoint = TorClientEndpoint("", 0, socks_endpoint=tor_endpoint)
     d = endpoint.connect(None)
     return self.assertFailure(d, ConnectionRefusedError)
Exemple #16
0
 def test_client_connection_failed(self):
     """
     This test is equivalent to txsocksx's
     TestSOCKS4ClientEndpoint.test_clientConnectionFailed
     """
     args = "host123"
     kw = dict()
     kw['failure'] = Failure(ConnectionRefusedError())
     tor_endpoint = FakeTorSocksEndpoint(*args, **kw)
     endpoint = TorClientEndpoint('', 0, socks_endpoint=tor_endpoint)
     d = endpoint.connect(None)
     return self.assertFailure(d, ConnectionRefusedError)
Exemple #17
0
    def test_default_factory(self):
        """
        This test is equivalent to txsocksx's
        TestSOCKS5ClientEndpoint.test_defaultFactory
        """

        tor_endpoint = FakeTorSocksEndpoint(None, "fakehost", 9050)
        endpoint = TorClientEndpoint(
            '', 0,
            socks_endpoint=tor_endpoint,
        )
        endpoint.connect(Mock)
        self.assertEqual(tor_endpoint.transport.value(), b'\x05\x01\x00')
Exemple #18
0
    def test_client_endpoint_get_address(self):
        """
        Test the old API of passing socks_host, socks_port
        """

        reactor = Mock()
        endpoint = TorClientEndpoint(
            'torproject.org', 0,
            socks_endpoint=clientFromString(Mock(), "tcp:localhost:9050"),
            reactor=reactor,
        )
        d = endpoint._get_address()
        self.assertTrue(not d.called)
Exemple #19
0
 def test_bad_port_retry(self):
     """
     This tests failure to connect to the ports on the "try" list.
     """
     fail_ports = [1984, 666]
     for port in fail_ports:
         def tor_socks_endpoint_generator(*args, **kw):
             kw['accept_port'] = port
             kw['failure'] = Failure(ConnectionRefusedError())
             return FakeTorSocksEndpoint(*args, **kw)
         endpoint = TorClientEndpoint('', 0, _proxy_endpoint_generator=tor_socks_endpoint_generator)
         d = endpoint.connect(None)
         return self.assertFailure(d, ConnectionRefusedError)
Exemple #20
0
 def test_bad_no_guess_socks_port(self):
     """
     This tests that are connection fails if we try to connect to an unavailable
     specified SOCKS port... even if there is a valid SOCKS port listening on
     the socks_ports_to_try list.
     """
     def tor_socks_endpoint_generator(*args, **kw):
         kw['accept_port'] = 9050
         kw['failure'] = Failure(ConnectionRefusedError())
         return FakeTorSocksEndpoint(*args, **kw)
     endpoint = TorClientEndpoint('', 0, _proxy_endpoint_generator=tor_socks_endpoint_generator, socks_port=6669)
     d = endpoint.connect(None)
     self.assertFailure(d, ConnectionRefusedError)
Exemple #21
0
    def test_default_factory(self):
        """
        This test is equivalent to txsocksx's TestSOCKS5ClientEndpoint.test_defaultFactory
        """
        endpoints = []

        def tor_socks_endpoint_generator(*args, **kw):
            endpoints.append(FakeTorSocksEndpoint(*args, **kw))
            return endpoints[-1]
        endpoint = TorClientEndpoint('', 0, _proxy_endpoint_generator=tor_socks_endpoint_generator)
        endpoint.connect(Mock)
        self.assertEqual(1, len(endpoints))
        self.assertEqual(endpoints[0].transport.value(), '\x05\x01\x00')
Exemple #22
0
 def test_client_connection_failed_user_password(self):
     """
     Same as above, but with a username/password.
     """
     args = "fakehost"
     kw = dict()
     kw["failure"] = Failure(ConnectionRefusedError())
     tor_endpoint = FakeTorSocksEndpoint(*args, **kw)
     endpoint = TorClientEndpoint(
         "invalid host", 0, socks_username="******", socks_password="******", socks_endpoint=tor_endpoint
     )
     d = endpoint.connect(None)
     return self.assertFailure(d, ConnectionRefusedError)
Exemple #23
0
    def test_success(self, socks5_factory):
        ep = MagicMock()
        gold_proto = object()
        ep.connect = MagicMock(return_value=gold_proto)
        socks5_factory.return_value = ep

        def tor_socks_endpoint_generator(*args, **kw):
            return FakeTorSocksEndpoint(*args, **kw)

        endpoint = TorClientEndpoint(
            '', 0, _proxy_endpoint_generator=tor_socks_endpoint_generator)
        other_proto = yield endpoint.connect(MagicMock())
        self.assertEqual(other_proto, gold_proto)
Exemple #24
0
    def test_client_connection_failed(self):
        """
        This test is equivalent to txsocksx's
        TestSOCKS4ClientEndpoint.test_clientConnectionFailed
        """
        def fail_tor_socks_endpoint_generator(*args, **kw):
            kw['failure'] = Failure(ConnectionRefusedError())
            return FakeTorSocksEndpoint(*args, **kw)

        endpoint = TorClientEndpoint(
            '', 0, _proxy_endpoint_generator=fail_tor_socks_endpoint_generator)
        d = endpoint.connect(None)
        return self.assertFailure(d, ConnectionRefusedError)
Exemple #25
0
 def test_client_connection_failed_user_password(self):
     """
     Same as above, but with a username/password.
     """
     def fail_tor_socks_endpoint_generator(*args, **kw):
         kw['failure'] = Failure(ConnectionRefusedError())
         return FakeTorSocksEndpoint(*args, **kw)
     endpoint = TorClientEndpoint(
         'invalid host', 0,
         socks_username='******', socks_password='******',
         _proxy_endpoint_generator=fail_tor_socks_endpoint_generator)
     d = endpoint.connect(None)
     return self.assertFailure(d, ConnectionRefusedError)
Exemple #26
0
 def test_bad_port_retry(self):
     """
     This tests failure to connect to the ports on the "try" list.
     """
     fail_ports = [1984, 666]
     for port in fail_ports:
         ep = FakeTorSocksEndpoint(
             '', '', 0,
             accept_port=port,
             failure=Failure(ConnectionRefusedError()),
         )
         endpoint = TorClientEndpoint('', 0, socks_endpoint=ep)
         d = endpoint.connect(None)
         return self.assertFailure(d, ConnectionRefusedError)
Exemple #27
0
    def test_bad_port_retry(self):
        """
        This tests failure to connect to the ports on the "try" list.
        """
        fail_ports = [1984, 666]
        for port in fail_ports:

            def TorSocksEndpointGenerator(*args, **kw):
                kw["acceptPort"] = port
                kw["failure"] = connectionRefusedFailure
                return FakeTorSocksEndpoint(*args, **kw)

            endpoint = TorClientEndpoint("", 0, _proxy_endpoint_generator=TorSocksEndpointGenerator)
            d = endpoint.connect(None)
            return self.assertFailure(d, ConnectionRefusedError)
Exemple #28
0
    def test_default_socks_ports_fails(self, ep_mock):
        """
        Ensure we iterate over the default socks ports
        """
        class FakeSocks5(object):
            def __init__(self, *args, **kw):
                pass

            def connect(self, *args, **kw):
                raise ConnectionRefusedError()

        ep_mock.side_effect = FakeSocks5
        endpoint = TorClientEndpoint('', 0)  #, socks_endpoint=ep)
        d = endpoint.connect(None)
        self.assertFailure(d, ConnectionRefusedError)
Exemple #29
0
    def test_default_factory(self):
        """
        This test is equivalent to txsocksx's TestSOCKS5ClientEndpoint.test_defaultFactory
        """
        endpoints = []

        def tor_socks_endpoint_generator(*args, **kw):
            endpoints.append(FakeTorSocksEndpoint(*args, **kw))
            return endpoints[-1]

        endpoint = TorClientEndpoint(
            '', 0, _proxy_endpoint_generator=tor_socks_endpoint_generator)
        endpoint.connect(Mock)
        self.assertEqual(1, len(endpoints))
        self.assertEqual(endpoints[0].transport.value(), '\x05\x01\x00')
Exemple #30
0
    def test_client_endpoint_old_api(self, reactor):
        """
        Test the old API of passing socks_host, socks_port
        """

        endpoint = TorClientEndpoint("torproject.org", 0, socks_hostname="localhost", socks_port=9050)
        self.assertTrue(isinstance(endpoint.socks_endpoint, TCP4ClientEndpoint))

        d = endpoint.connect(Mock())
        calls = reactor.mock_calls
        self.assertEqual(1, len(calls))
        name, args, kw = calls[0]
        self.assertEqual("connectTCP", name)
        self.assertEqual("localhost", args[0])
        self.assertEqual(9050, args[1])
Exemple #31
0
 def test_client_connection_failed_user_password(self):
     """
     Same as above, but with a username/password.
     """
     args = "fakehost"
     kw = dict()
     kw['failure'] = Failure(ConnectionRefusedError())
     tor_endpoint = FakeTorSocksEndpoint(*args, **kw)
     endpoint = TorClientEndpoint('invalid host',
                                  0,
                                  socks_username='******',
                                  socks_password='******',
                                  socks_endpoint=tor_endpoint)
     d = endpoint.connect(None)
     return self.assertFailure(d, ConnectionRefusedError)
Exemple #32
0
 def test_client_connection_failed(self, ggt):
     """
     This test is equivalent to txsocksx's
     TestSOCKS4ClientEndpoint.test_clientConnectionFailed
     """
     tor_endpoint = FakeTorSocksEndpoint(
         None, "host123", 9050,
         failure=Failure(ConnectionRefusedError()),
     )
     endpoint = TorClientEndpoint(
         '', 0,
         socks_endpoint=tor_endpoint,
     )
     d = endpoint.connect(None)
     return self.assertFailure(d, ConnectionRefusedError)
Exemple #33
0
    def test_bad_port_retry(self):
        """
        This tests failure to connect to the ports on the "try" list.
        """
        fail_ports = [1984, 666]
        for port in fail_ports:

            def tor_socks_endpoint_generator(*args, **kw):
                kw['accept_port'] = port
                kw['failure'] = Failure(ConnectionRefusedError())
                return FakeTorSocksEndpoint(*args, **kw)

            endpoint = TorClientEndpoint(
                '', 0, _proxy_endpoint_generator=tor_socks_endpoint_generator)
            d = endpoint.connect(None)
            return self.assertFailure(d, ConnectionRefusedError)
Exemple #34
0
    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(
                "fakehost", "127.0.0.1", port, accept_port=port, failure=Failure(ConnectionRefusedError())
            )

            endpoint = TorClientEndpoint("", 0, socks_endpoint=tor_endpoint)
            endpoint.connect(None)
            self.assertEqual(tor_endpoint.transport.value(), "\x05\x01\x00")
Exemple #35
0
    def test_good_no_guess_socks_port(self):
        """
        This tests that if a SOCKS port is specified, we *only* attempt to
        connect to that SOCKS port.
        """
        endpoints = []

        def tor_socks_endpoint_generator(*args, **kw):
            kw['accept_port'] = 6669
            kw['failure'] = Failure(ConnectionRefusedError())
            endpoints.append(FakeTorSocksEndpoint(*args, **kw))
            return endpoints[-1]
        endpoint = TorClientEndpoint('', 0, _proxy_endpoint_generator=tor_socks_endpoint_generator, socks_port=6669)
        endpoint.connect(None)
        self.assertEqual(1, len(endpoints))
        self.assertEqual(endpoints[-1].transport.value(), '\x05\x01\x00')
Exemple #36
0
    def test_client_connection_failed_user_password(self):
        """
        Same as above, but with a username/password.
        """
        def fail_tor_socks_endpoint_generator(*args, **kw):
            kw['failure'] = Failure(ConnectionRefusedError())
            return FakeTorSocksEndpoint(*args, **kw)

        endpoint = TorClientEndpoint(
            'invalid host',
            0,
            socks_username='******',
            socks_password='******',
            _proxy_endpoint_generator=fail_tor_socks_endpoint_generator)
        d = endpoint.connect(None)
        return self.assertFailure(d, ConnectionRefusedError)
Exemple #37
0
 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='******', socks_password='******',
         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)
Exemple #38
0
    def test_default_socks_ports_fails(self, ep_mock):
        """
        Ensure we iterate over the default socks ports
        """

        class FakeSocks5(object):
            def __init__(self, *args, **kw):
                pass

            def connect(self, *args, **kw):
                raise ConnectionRefusedError()

        ep_mock.side_effect = FakeSocks5
        endpoint = TorClientEndpoint("", 0)  # , socks_endpoint=ep)
        d = endpoint.connect(None)
        self.assertFailure(d, ConnectionRefusedError)
 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 connectionRefusedFailure
     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:
         def TorSocksEndpointGenerator(*args, **kw):
             kw['acceptPort'] = port
             kw['failure'] = connectionRefusedFailure
             return FakeTorSocksEndpoint(*args, **kw)
         endpoint = TorClientEndpoint('', 0, _proxy_endpoint_generator=TorSocksEndpointGenerator)
         endpoint.connect(None)
         self.assertEqual(endpoint.tor_socks_endpoint.transport.value(), '\x05\x01\x00')
Exemple #40
0
    def test_tls_socks_no_endpoint(self, ep_mock):
        the_proto = object()
        proto = defer.succeed(the_proto)

        class FakeSocks5(object):
            def __init__(self, *args, **kw):
                pass

            def connect(self, *args, **kw):
                return proto

            def _get_address(self):
                return defer.succeed(None)

        ep_mock.side_effect = FakeSocks5
        endpoint = TorClientEndpoint('torproject.org', 0, tls=True)
        p2 = yield endpoint.connect(None)
        self.assertTrue(the_proto is p2)
Exemple #41
0
    def test_default_socks_ports_happy(self, ep_mock):
        """
        Ensure we iterate over the default socks ports
        """

        proto = object()

        class FakeSocks5(object):
            def __init__(self, *args, **kw):
                pass

            def connect(self, *args, **kw):
                return proto

        ep_mock.side_effect = FakeSocks5
        endpoint = TorClientEndpoint('', 0)
        p2 = yield endpoint.connect(None)
        self.assertTrue(proto is p2)
Exemple #42
0
    def test_bad_no_guess_socks_port(self):
        """
        This tests that are connection fails if we try to connect to an unavailable
        specified SOCKS port... even if there is a valid SOCKS port listening on
        the socks_ports_to_try list.
        """
        def tor_socks_endpoint_generator(*args, **kw):
            kw['accept_port'] = 9050
            kw['failure'] = Failure(ConnectionRefusedError())
            return FakeTorSocksEndpoint(*args, **kw)

        endpoint = TorClientEndpoint(
            '',
            0,
            _proxy_endpoint_generator=tor_socks_endpoint_generator,
            socks_port=6669)
        d = endpoint.connect(None)
        self.assertFailure(d, ConnectionRefusedError)
Exemple #43
0
    def test_default_socks_ports_happy(self, ep_mock):
        """
        Ensure we iterate over the default socks ports
        """

        proto = object()

        class FakeSocks5(object):
            def __init__(self, *args, **kw):
                pass

            def connect(self, *args, **kw):
                return proto

        ep_mock.side_effect = FakeSocks5
        endpoint = TorClientEndpoint("", 0)
        p2 = yield endpoint.connect(None)
        self.assertTrue(proto is p2)
Exemple #44
0
    def test_client_connection_failed_user_password(self):
        """
        Same as above, but with a username/password.
        """

        def FailTorSocksEndpointGenerator(*args, **kw):
            kw["failure"] = connectionRefusedFailure
            return FakeTorSocksEndpoint(*args, **kw)

        endpoint = TorClientEndpoint(
            "invalid host",
            0,
            socks_username="******",
            socks_password="******",
            _proxy_endpoint_generator=FailTorSocksEndpointGenerator,
        )
        d = endpoint.connect(None)
        return self.assertFailure(d, ConnectionRefusedError)
Exemple #45
0
    def test_tls_socks_no_endpoint(self, ep_mock):
        the_proto = object()
        proto = defer.succeed(the_proto)

        class FakeSocks5(object):

            def __init__(self, *args, **kw):
                pass

            def connect(self, *args, **kw):
                return proto

            def _get_address(self):
                return defer.succeed(None)

        ep_mock.side_effect = FakeSocks5
        endpoint = TorClientEndpoint('torproject.org', 0, tls=True)
        p2 = yield endpoint.connect(None)
        self.assertTrue(the_proto is p2)
Exemple #46
0
    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(
                "fakehost",
                "127.0.0.1",
                port,
                accept_port=port,
                failure=Failure(ConnectionRefusedError()),
            )

            endpoint = TorClientEndpoint('', 0, socks_endpoint=tor_endpoint)
            endpoint.connect(None)
            self.assertEqual(tor_endpoint.transport.value(), '\x05\x01\x00')
Exemple #47
0
    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)
Exemple #48
0
    def test_default_socks_ports_fails(self, ep_mock):
        """
        Ensure we iterate over the default socks ports
        """

        class FakeSocks5(object):

            def __init__(self, *args, **kw):
                pass

            def connect(self, *args, **kw):
                raise ConnectionRefusedError()

            def _get_address(self):
                return defer.succeed(None)

        ep_mock.side_effect = FakeSocks5
        endpoint = TorClientEndpoint('', 0)
        d = endpoint.connect(Mock())
        self.assertFailure(d, ConnectionRefusedError)
Exemple #49
0
    def test_client_endpoint_old_api(self, reactor):
        """
        Test the old API of passing socks_host, socks_port
        """

        endpoint = TorClientEndpoint(
            'torproject.org',
            0,
            socks_hostname='localhost',
            socks_port=9050,
        )
        self.assertTrue(isinstance(endpoint.socks_endpoint,
                                   TCP4ClientEndpoint))

        d = endpoint.connect(Mock())
        calls = reactor.mock_calls
        self.assertEqual(1, len(calls))
        name, args, kw = calls[0]
        self.assertEqual("connectTCP", name)
        self.assertEqual("localhost", args[0])
        self.assertEqual(9050, args[1])
Exemple #50
0
    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
        endpoints = []
        for port in success_ports:

            def tor_socks_endpoint_generator(*args, **kw):
                kw['accept_port'] = port
                kw['failure'] = Failure(ConnectionRefusedError())
                endpoints.append(FakeTorSocksEndpoint(*args, **kw))
                return endpoints[-1]

            endpoint = TorClientEndpoint(
                '', 0, _proxy_endpoint_generator=tor_socks_endpoint_generator)
            endpoint.connect(None)
            self.assertEqual(endpoints[-1].transport.value(), '\x05\x01\x00')
Exemple #51
0
    def test_good_no_guess_socks_port(self):
        """
        This tests that if a SOCKS port is specified, we *only* attempt to
        connect to that SOCKS port.
        """
        endpoints = []

        def tor_socks_endpoint_generator(*args, **kw):
            kw['accept_port'] = 6669
            kw['failure'] = Failure(ConnectionRefusedError())
            endpoints.append(FakeTorSocksEndpoint(*args, **kw))
            return endpoints[-1]

        endpoint = TorClientEndpoint(
            '',
            0,
            _proxy_endpoint_generator=tor_socks_endpoint_generator,
            socks_port=6669)
        endpoint.connect(None)
        self.assertEqual(1, len(endpoints))
        self.assertEqual(endpoints[-1].transport.value(), '\x05\x01\x00')
Exemple #52
0
    def get_endpoint_for(self, host, port):
        assert isinstance(port, six.integer_types)
        if self.is_non_public_numeric_address(host):
            return None

        # txsocksx doesn't like unicode: it concatenates some binary protocol
        # bytes with the hostname when talking to the SOCKS server, so the
        # py2 automatic unicode promotion blows up
        host = host.encode("ascii")
        ep = TorClientEndpoint(host,
                               port,
                               socks_endpoint=self._tor_socks_endpoint)
        return ep
Exemple #53
0
    def test_tls_socks_no_endpoint(self, ep_mock):

        if not _HAVE_TLS:
            print("no TLS support")
            return

        class FakeWrappedProto(object):
            wrappedProtocol = object()

        wrap = FakeWrappedProto()
        proto = defer.succeed(wrap)

        class FakeSocks5(object):
            def __init__(self, *args, **kw):
                pass

            def connect(self, *args, **kw):
                return proto

        ep_mock.side_effect = FakeSocks5
        endpoint = TorClientEndpoint('torproject.org', 0, tls=True)
        p2 = yield endpoint.connect(None)
        self.assertTrue(wrap.wrappedProtocol is p2)