def _make_connected_cm(self):
     cm = self._make_cm()
     transport = mock.Mock(spec_set=udp.Port)
     ret_val = address.IPv4Address('UDP', self.public_ip, self.port)
     transport.attach_mock(mock.Mock(return_value=ret_val), 'getHost')
     cm.makeConnection(transport)
     return cm
Exemple #2
0
    def test02_ipNumberProxy(self):
        from twisted.internet import address

        class Channel:
            def __init__(self, site):
                self.site = site

        class MockClient:
            pass

        addr = address.IPv4Address('TCP', 'proxy.example.org', 7775)
        r = pokersite.Request(Channel(self.site), True)
        r.client = addr
        self.assertEquals(r.findProxiedIP(),
                          ('client-ip', 'proxy.example.org'))

        # received_headers will become requestHeaders RSN, according to:
        # http://python.net/crew/mwh/apidocs/twisted.web.http.Request.html

        r.received_headers[
            'x-cluster-client-ip'] = 'cluster-player.example.com'

        self.assertEquals(
            r.findProxiedIP(),
            ('x-cluster-client-ip', 'cluster-player.example.com'))

        r.received_headers['x-forwarded-for'] = 'forward-player.example.com'
        self.assertEquals(r.findProxiedIP(),
                          ('x-forwarded-for', 'forward-player.example.com'))
Exemple #3
0
    def test_pathInfo(self):
        """
        L{twcgi.CGIScript.render} sets the process environment
        I{PATH_INFO} from the request path.
        """
        class FakeReactor:
            """
            A fake reactor recording the environment passed to spawnProcess.
            """
            def spawnProcess(self, process, filename, args, env, wdir):
                """
                Store the C{env} L{dict} to an instance attribute.

                @param process: Ignored
                @param filename: Ignored
                @param args: Ignored
                @param env: The environment L{dict} which will be stored
                @param wdir: Ignored
                """
                self.process_env = env

        _reactor = FakeReactor()
        resource = twcgi.CGIScript(self.mktemp(), reactor=_reactor)
        request = DummyRequest(['a', 'b'])
        request.client = address.IPv4Address('TCP', '127.0.0.1', 12345)
        _render(resource, request)

        self.assertEqual(_reactor.process_env["PATH_INFO"], "/a/b")
Exemple #4
0
    def getHost(self):
        """
        Returns an IPv4Address.

        This indicates the address from which I am connecting.
        """
        return address.IPv4Address('UDP', *self.socket.getsockname())
Exemple #5
0
    def test_writeLimit(self):
        """
        Check the writeLimit parameter: write data, and check for the pause
        status.
        """
        server = Server()
        tServer = TestableThrottlingFactory(task.Clock(), server, writeLimit=10)
        port = tServer.buildProtocol(address.IPv4Address('TCP', '127.0.0.1', 0))
        tr = StringTransportWithDisconnection()
        tr.protocol = port
        port.makeConnection(tr)
        port.producer = port.wrappedProtocol

        port.dataReceived(b"0123456789")
        port.dataReceived(b"abcdefghij")
        self.assertEqual(tr.value(), b"0123456789abcdefghij")
        self.assertEqual(tServer.writtenThisSecond, 20)
        self.assertFalse(port.wrappedProtocol.paused)

        # at this point server should've written 20 bytes, 10 bytes
        # above the limit so writing should be paused around 1 second
        # from 'now', and resumed a second after that
        tServer.clock.advance(1.05)
        self.assertEqual(tServer.writtenThisSecond, 0)
        self.assertTrue(port.wrappedProtocol.paused)

        tServer.clock.advance(1.05)
        self.assertEqual(tServer.writtenThisSecond, 0)
        self.assertFalse(port.wrappedProtocol.paused)
Exemple #6
0
    def getHost(self):
        """
        Returns an IPv4Address.

        This indicates the server's address.
        """
        return address.IPv4Address('TCP', *self.socket.getsockname())
Exemple #7
0
    def setCopyableState(self, state):
        """
        Initialize this L{twisted.web.distrib.Request} based on the copied
        state so that it closely resembles a L{twisted.web.server.Request}.
        """
        for k in "host", "client":
            tup = state[k]
            addrdesc = {"INET": "TCP", "UNIX": "UNIX"}[tup[0]]
            addr = {
                "TCP": lambda: address.IPv4Address(addrdesc, tup[1], tup[2]),
                "UNIX": lambda: address.UNIXAddress(tup[1]),
            }[addrdesc]()
            state[k] = addr
        state["requestHeaders"] = Headers(dict(state["requestHeaders"]))
        pb.RemoteCopy.setCopyableState(self, state)
        # Emulate the local request interface --
        self.content = BytesIO(self.content_data)
        self.finish = self.remote.remoteMethod("finish")
        self.setHeader = self.remote.remoteMethod("setHeader")
        self.addCookie = self.remote.remoteMethod("addCookie")
        self.setETag = self.remote.remoteMethod("setETag")
        self.setResponseCode = self.remote.remoteMethod("setResponseCode")
        self.setLastModified = self.remote.remoteMethod("setLastModified")

        # To avoid failing if a resource tries to write a very long string
        # all at once, this one will be handled slightly differently.
        self._write = self.remote.remoteMethod("write")
Exemple #8
0
    def test_registerProducers(self):
        """
        The proxy client registers itself as a producer of the proxy server and
        vice versa.
        """
        # create a ProxyServer instance
        addr = address.IPv4Address('TCP', '127.0.0.1', 0)
        server = portforward.ProxyFactory('127.0.0.1', 0).buildProtocol(addr)

        # set the reactor for this test
        reactor = proto_helpers.MemoryReactor()
        server.reactor = reactor

        # make the connection
        serverTransport = proto_helpers.StringTransport()
        server.makeConnection(serverTransport)

        # check that the ProxyClientFactory is connecting to the backend
        self.assertEqual(len(reactor.tcpClients), 1)
        # get the factory instance and check it's the one we expect
        host, port, clientFactory, timeout, _ = reactor.tcpClients[0]
        self.assertIsInstance(clientFactory, portforward.ProxyClientFactory)

        # Connect it
        client = clientFactory.buildProtocol(addr)
        clientTransport = proto_helpers.StringTransport()
        client.makeConnection(clientTransport)

        # check that the producers are registered
        self.assertIdentical(clientTransport.producer, serverTransport)
        self.assertIdentical(serverTransport.producer, clientTransport)
        # check the streaming attribute in both transports
        self.assertTrue(clientTransport.streaming)
        self.assertTrue(serverTransport.streaming)
Exemple #9
0
    def setCopyableState(self, state):
        """
        Initialize this L{cyclone.tw.distrib.Request} based on the copied
        state so that it closely resembles a L{cyclone.tw.server.Request}.
        """
        for k in 'host', 'client':
            tup = state[k]
            addrdesc = {'INET': 'TCP', 'UNIX': 'UNIX'}[tup[0]]
            addr = {
                'TCP':
                lambda: address.IPv4Address(
                    addrdesc, tup[1], tup[2], _bwHack='INET'),
                'UNIX':
                lambda: address.UNIXAddress(tup[1])
            }[addrdesc]()
            state[k] = addr
        state['requestHeaders'] = Headers(dict(state['requestHeaders']))
        pb.RemoteCopy.setCopyableState(self, state)
        # Emulate the local request interface --
        self.content = cStringIO.StringIO(self.content_data)
        self.finish = self.remote.remoteMethod('finish')
        self.setHeader = self.remote.remoteMethod('setHeader')
        self.addCookie = self.remote.remoteMethod('addCookie')
        self.setETag = self.remote.remoteMethod('setETag')
        self.setResponseCode = self.remote.remoteMethod('setResponseCode')
        self.setLastModified = self.remote.remoteMethod('setLastModified')

        # To avoid failing if a resource tries to write a very long string
        # all at once, this one will be handled slightly differently.
        self._write = self.remote.remoteMethod('write')
Exemple #10
0
    def getPeer(self):
        """
        Returns an IPv4Address.

        This indicates the address that I am connected to.
        """
        return address.IPv4Address('TCP', *(self.realAddress + ('INET', )))
Exemple #11
0
    def test_useReactorArgument(self):
        """
        L{twcgi.FilteredScript.runProcess} uses the reactor passed as an
        argument to the constructor.
        """
        class FakeReactor:
            """
            A fake reactor recording whether spawnProcess is called.
            """
            called = False

            def spawnProcess(self, *args, **kwargs):
                """
                Set the C{called} flag to C{True} if C{spawnProcess} is called.

                @param args: Positional arguments.
                @param kwargs: Keyword arguments.
                """
                self.called = True

        fakeReactor = FakeReactor()
        request = DummyRequest(['a', 'b'])
        request.client = address.IPv4Address('TCP', '127.0.0.1', 12345)
        resource = twcgi.FilteredScript("dummy-file", reactor=fakeReactor)
        _render(resource, request)

        self.assertTrue(fakeReactor.called)
Exemple #12
0
 def test_truncatedPacket(self):
     """
     Test that when a short datagram is received, datagramReceived does
     not raise an exception while processing it.
     """
     self.proto.datagramReceived(
         '', address.IPv4Address('UDP', '127.0.0.1', 12345))
     self.assertEquals(self.controller.messages, [])
Exemple #13
0
 def getPeer(self):
     if self.parent._options["proxy_header"] and self.request.requestHeaders.hasHeader(self.parent._options["proxy_header"]):
         ip = self.request.requestHeaders.getRawHeaders(self.parent._options["proxy_header"])[0].decode().split(",")[-1].strip()
         if re.match("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", ip):
             return address.IPv4Address("TCP", ip, None)
         else:
             return address.IPv6Address("TCP", ip, None)
     return ProtocolWrapper.getPeer(self)
Exemple #14
0
def returnConnected(server, client, clientAddress=None, serverAddress=None):
    """Take two Protocol instances and connect them.
    """
    if serverAddress is None:
        serverAddress = address.IPv4Address('TCP', 'localhost', 1)
    if clientAddress is None:
        clientAddress = address.IPv4Address('TCP', 'localhost', 2)
    clientTransport = FakeTransport(clientAddress, serverAddress)
    client.makeConnection(clientTransport)
    serverTransport = FakeTransport(serverAddress, clientAddress)
    server.makeConnection(serverTransport)
    pump = IOPump(client, server, clientTransport, serverTransport)
    # Challenge-response authentication:
    pump.flush()
    # Uh...
    pump.flush()
    return pump
 def connectClass(self, host, port, klass, *args):
     # fake it
     proto = klass(*args)
     proto.transport = StringTCPTransport()
     proto.transport.peer = address.IPv4Address('TCP', host, port)
     proto.connectionMade()
     self.driver_outgoing = proto
     return defer.succeed(proto)
Exemple #16
0
    def makeRequest(self, vars):
        headers = http_headers.Headers()
        http_vers = http.parseVersion(vars['SERVER_PROTOCOL'])
        if http_vers[0] != 'http' or http_vers[1] > 1:
            _abortWithError(
                responsecode.INTERNAL_SERVER_ERROR,
                "Twisted.web CGITransport: Unknown HTTP version: " %
                vars['SERVER_PROTOCOL'])

        secure = vars.get("HTTPS") in ("1", "on")  # apache extension?
        port = vars.get('SERVER_PORT') or 80
        server_host = vars.get('SERVER_NAME') or vars.get(
            'SERVER_ADDR') or 'localhost'

        self.hostinfo = address.IPv4Address('TCP', server_host,
                                            port), bool(secure)
        self.remoteinfo = address.IPv4Address('TCP',
                                              vars.get('REMOTE_ADDR', ''),
                                              vars.get('REMOTE_PORT', 0))

        uri = vars.get('REQUEST_URI')  # apache extension?
        if not uri:
            qstr = vars.get('QUERY_STRING', '')
            if qstr:
                qstr = "?" + urllib.quote(qstr, safe="")
            uri = urllib.quote(vars['SCRIPT_NAME']) + urllib.quote(
                vars.get('PATH_INFO', '')) + qstr

        for name, val in vars.iteritems():
            if name.startswith('HTTP_'):
                name = name[5:].replace('_', '-')
            elif name == 'CONTENT_TYPE':
                name = 'content-type'
            else:
                continue
            headers.setRawHeaders(name, (val, ))

        self._dataRemaining = int(vars.get('CONTENT_LENGTH', '0'))
        self.request = self.requestFactory(self,
                                           vars['REQUEST_METHOD'],
                                           uri,
                                           http_vers[1:3],
                                           self._dataRemaining,
                                           headers,
                                           prepathuri=vars['SCRIPT_NAME'])
Exemple #17
0
 def __init__(self, channel=DummyChannel(), queued=None, api_mode="test", api_version="0.1", api_name="TestAPI", uri="",
              method="GET", user="", password=""):
     self.client =  address.IPv4Address('TCP', "1.2.3.4", 40323)
     self.api_mode = api_mode
     self.api_version = api_version
     self.api_name = api_name
     self.save_channel = channel
     
     return foundation.ShijiRequest.__init__(self, channel, queued)
 def test_protocolLogPrefix(self):
     """
     L{ProtocolWrapper.logPrefix} is customized to mention both the original
     protocol and the wrapper.
     """
     server = Server()
     factory = policies.WrappingFactory(server)
     protocol = factory.buildProtocol(address.IPv4Address("TCP", "127.0.0.1", 35))
     self.assertEqual("EchoProtocol (ProtocolWrapper)", protocol.logPrefix())
Exemple #19
0
 def setUp(self):
     """
     Create an ESMTP instance attached to a StringTransport.
     """
     self.server = smtp.ESMTP({'LOGIN': imap4.LOGINCredentials})
     self.server.host = 'localhost'
     self.transport = StringTransport(
         peerAddress=address.IPv4Address('TCP', '127.0.0.1', 12345))
     self.server.makeConnection(self.transport)
Exemple #20
0
 def test_protocolFactoryAttribute(self):
     """
     Make sure protocol.factory is the wrapped factory, not the wrapping
     factory.
     """
     f = Server()
     wf = policies.WrappingFactory(f)
     p = wf.buildProtocol(address.IPv4Address('TCP', '127.0.0.1', 35))
     self.assertIs(p.wrappedProtocol.factory, f)
Exemple #21
0
 def testTruncatedPacket(self):
     """
     Test that when a short datagram is received, datagramReceived does
     not raise an exception while processing it.
     """
     controller = TestController()
     proto = dns.DNSDatagramProtocol(controller)
     proto.datagramReceived('',
                            address.IPv4Address('UDP', '127.0.0.1', 12345))
     self.assertEquals(controller.messages, [])
Exemple #22
0
    def setUp(self):

        self.server = test_policies.Server()
        tServer = TestableWebSocketFactory(task.Clock(), self.server)
        self.port = tServer.buildProtocol(
            address.IPv4Address('TCP', '127.0..0.1', 0))
        self.tr = proto_helpers.StringTransportWithDisconnection()
        self.tr.protocol = self.port
        self.port.makeConnection(self.tr)
        self.port.producer = self.port.wrappedProtocol
 def _serverSetup(self):
     # Create a server factory, get a protocol from it, connect it to a
     # transport, and return all three.
     wrappedFactory = protocol.ServerFactory()
     wrappedFactory.protocol = SimpleProtocol
     factory = policies.TimeoutFactory(wrappedFactory, 3)
     proto = factory.buildProtocol(address.IPv4Address('TCP', '127.0.0.1', 12345))
     transport = StringTransportWithDisconnection()
     transport.protocol = proto
     proto.makeConnection(transport)
     return factory, proto, transport
Exemple #24
0
 def __init__(self,
              user,
              realm,
              factory,
              address=address.IPv4Address('TCP', '127.0.0.1', 54321)):
     self.user = user
     self.transport = proto_helpers.StringTransportWithDisconnection()
     self.protocol = factory.buildProtocol(address)
     self.transport.protocol = self.protocol
     self.user.mind = self.protocol
     self.protocol.makeConnection(self.transport)
 def test_describe(self):
     f = transit.InboundConnectionFactory(None)
     addrH = address.HostnameAddress("example.com", 1234)
     self.assertEqual(f._describePeer(addrH), "<-example.com:1234")
     addr4 = address.IPv4Address("TCP", "1.2.3.4", 1234)
     self.assertEqual(f._describePeer(addr4), "<-1.2.3.4:1234")
     addr6 = address.IPv6Address("TCP", "::1", 1234)
     self.assertEqual(f._describePeer(addr6), "<-::1:1234")
     addrU = address.UNIXAddress("/dev/unlikely")
     self.assertEqual(f._describePeer(addrU),
                      "<-UNIXAddress('/dev/unlikely')")
Exemple #26
0
    def getHost(self):
        """
        Return the local address of the UDP connection

        @returns: the local address of the UDP connection
        @rtype: L{IPv4Address} or L{IPv6Address}
        """
        addr = self.socket.getsockname()
        if self.addressFamily == socket.AF_INET:
            return address.IPv4Address('UDP', *addr)
        elif self.addressFamily == socket.AF_INET6:
            return address.IPv6Address('UDP', *(addr[:2]))
 def test_invalidHeaderDisconnects(self):
     """
     Test if invalid headers result in connectionLost events.
     """
     factory = HAProxyWrappingFactory(Factory.forProtocol(StaticProtocol))
     proto = factory.buildProtocol(
         address.IPv4Address('TCP', b'127.1.1.1', 8080), )
     transport = StringTransportWithDisconnection()
     transport.protocol = proto
     proto.makeConnection(transport)
     proto.dataReceived(b'NOTPROXY anything can go here\r\n')
     self.assertFalse(transport.connected)
Exemple #28
0
    def test_loggingFactoryOpensLogfileAutomatically(self):
        """
        When the L{policies.TrafficLoggingFactory} builds a protocol, it
        automatically opens a unique log file for that protocol and attaches
        the logfile to the built protocol.
        """
        open_calls = []
        open_rvalues = []

        def mocked_open(*args, **kwargs):
            """
            Mock for the open call to prevent actually opening a log file.
            """
            open_calls.append((args, kwargs))
            io = NativeStringIO()
            io.name = args[0]
            open_rvalues.append(io)
            return io

        self.patch(builtins, 'open', mocked_open)

        wrappedFactory = protocol.ServerFactory()
        wrappedFactory.protocol = SimpleProtocol
        factory = policies.TrafficLoggingFactory(wrappedFactory, 'test')
        first_proto = factory.buildProtocol(address.IPv4Address('TCP',
                                                                '127.0.0.1',
                                                                12345))
        second_proto = factory.buildProtocol(address.IPv4Address('TCP',
                                                                 '127.0.0.1',
                                                                 12346))

        # We expect open to be called twice, with the files passed to the
        # protocols.
        first_call = (('test-1', 'w'), {})
        second_call = (('test-2', 'w'), {})
        self.assertEqual([first_call, second_call], open_calls)
        self.assertEqual(
            [first_proto.logfile, second_proto.logfile], open_rvalues
        )
Exemple #29
0
    def _timeoutTest(self, onDone, clientFactory):
        before = time.time()

        client = clientFactory.buildProtocol(
            address.IPv4Address('TCP', 'example.net', 25))
        server = protocol.Protocol()

        def check(ignored):
            after = time.time()
            self.failIf(after - before > 1.0)
            return self.assertFailure(onDone, smtp.SMTPTimeoutError)

        return self.loopback(client, server).addCallback(check)
 def test_invalidPartialHeaderDisconnects(self):
     """
     Test if invalid headers result in connectionLost events.
     """
     factory = HAProxyWrappingFactory(Factory.forProtocol(StaticProtocol))
     proto = factory.buildProtocol(
         address.IPv4Address("TCP", b"127.1.1.1", 8080), )
     transport = StringTransportWithDisconnection()
     transport.protocol = proto
     proto.makeConnection(transport)
     proto.dataReceived(b"PROXY TCP4 1.1.1.1\r\n")
     proto.dataReceived(b"2.2.2.2 8080\r\n")
     self.assertFalse(transport.connected)