def buildProtocol(self, addr): """ Only accepting connections from local machine! """ if addr.host == '127.0.0.1': return Site.buildProtocol(self, addr) if not _Debug: return None if os.environ.get('BITDUST_API_PASS_EXTERNAL_CONNECTIONS', '0') != '1': return None return Site.buildProtocol(self, addr)
def _testRender(self, uri, expectedURI): """ Check that a request pointing at C{uri} produce a new proxy connection, with the path of this request pointing at C{expectedURI}. """ root = Resource() reactor = FakeReactor() resource = ReverseProxyResource("127.0.0.1", 1234, "/path", reactor) root.putChild('index', resource) site = Site(root) transport = StringTransportWithDisconnection() channel = site.buildProtocol(None) channel.makeConnection(transport) # Clear the timeout if the tests failed self.addCleanup(channel.connectionLost, None) channel.dataReceived("GET %s HTTP/1.1\r\nAccept: text/html\r\n\r\n" % (uri, )) # Check that one connection has been created, to the good host/port self.assertEquals(len(reactor.connect), 1) self.assertEquals(reactor.connect[0][0], "127.0.0.1") self.assertEquals(reactor.connect[0][1], 1234) # Check the factory passed to the connect, and its given path factory = reactor.connect[0][2] self.assertIsInstance(factory, ProxyClientFactory) self.assertEquals(factory.rest, expectedURI) self.assertEquals(factory.headers["host"], "127.0.0.1:1234")
def buildProtocol(self, addr): """ Only accepting connections from local machine! """ if addr.host != '127.0.0.1': return None return Site.buildProtocol(self, addr)
def _testRender(self, uri, expectedURI): """ Check that a request pointing at C{uri} produce a new proxy connection, with the path of this request pointing at C{expectedURI}. """ root = Resource() reactor = MemoryReactor() resource = ReverseProxyResource("127.0.0.1", 1234, "/path", reactor) root.putChild("index", resource) site = Site(root) transport = StringTransportWithDisconnection() channel = site.buildProtocol(None) channel.makeConnection(transport) # Clear the timeout if the tests failed self.addCleanup(channel.connectionLost, None) channel.dataReceived("GET %s HTTP/1.1\r\nAccept: text/html\r\n\r\n" % (uri,)) # Check that one connection has been created, to the good host/port self.assertEquals(len(reactor.tcpClients), 1) self.assertEquals(reactor.tcpClients[0][0], "127.0.0.1") self.assertEquals(reactor.tcpClients[0][1], 1234) # Check the factory passed to the connect, and its given path factory = reactor.tcpClients[0][2] self.assertIsInstance(factory, ProxyClientFactory) self.assertEquals(factory.rest, expectedURI) self.assertEquals(factory.headers["host"], "127.0.0.1:1234")
class PappyWebServer(object): """ A class that is used to serve pages for requests to http://pappy. It is a ghetto wrapper around a twisted web Site object. Give it a request object and it will add a response to it. NOINDEX """ from pappyproxy.pappy import session site_dir = session.config.pappy_dir+'/site' loader = FileSystemLoader(site_dir) env = Environment(loader=loader) def __init__(self): root = RootResource(self.site_dir) self.site = Site(root) @staticmethod def render_template(*args, **kwargs): return PappyWebServer.env.get_template(args[0]).render(args[1:], **kwargs).encode('utf-8') @defer.inlineCallbacks def handle_request(self, req): protocol = self.site.buildProtocol(None) tr = PappyStringTransport() protocol.makeConnection(tr) protocol.dataReceived(req.full_request) tr.waitForProducers() ## WORKING HERE # use loading functions to load response yield tr.complete_deferred rsp_raw = tr.value() rsp = Response(rsp_raw) req.response = rsp
def _testRender(self, uri, expectedURI): """ Check that a request pointing at C{uri} produce a new proxy connection, with the path of this request pointing at C{expectedURI}. """ root = Resource() reactor = MemoryReactor() resource = ReverseProxyResource("127.0.0.1", 1234, b"/path", reactor) root.putChild(b"index", resource) site = Site(root) transport = StringTransportWithDisconnection() channel = site.buildProtocol(None) channel.makeConnection(transport) # Clear the timeout if the tests failed self.addCleanup(channel.connectionLost, None) channel.dataReceived(b"GET " + uri + b" HTTP/1.1\r\nAccept: text/html\r\n\r\n") [(host, port, factory, _timeout, _bind_addr)] = reactor.tcpClients # Check that one connection has been created, to the good host/port self.assertEqual(host, "127.0.0.1") self.assertEqual(port, 1234) # Check the factory passed to the connect, and its given path self.assertIsInstance(factory, ProxyClientFactory) self.assertEqual(factory.rest, expectedURI) self.assertEqual(factory.headers[b"host"], b"127.0.0.1:1234")
def buildProtocol(self, addr): """ Only accepting connections from local machine! """ if addr.host != '127.0.0.1': lg.err('refused connection from remote host: %r' % addr.host) return None return Site.buildProtocol(self, addr)
def request(self, method, uri, headers=None, bodyProducer=None): """ Implement IAgent.request. """ # We want to use Agent to parse the HTTP response, so let's ask it to # make a request against our in-memory reactor. response = self._realAgent.request(method, uri, headers, bodyProducer) # If the request has already finished, just propagate the result. In # reality this would only happen in failure, but if the agent ever adds # a local cache this might be a success. already_called = [] def check_already_called(r): already_called.append(r) return r response.addBoth(check_already_called) if already_called: return response # That will try to establish an HTTP connection with the reactor's # connectTCP method, and MemoryReactor will place Agent's factory into # the tcpClients list. Alternately, it will try to establish an HTTPS # connection with the reactor's connectSSL method, and MemoryReactor # will place it into the sslClients list. We'll extract that. scheme = URLPath.fromBytes(uri).scheme host, port, factory, timeout, bindAddress = ( self._memoryReactor.tcpClients[-1]) serverAddress = IPv4Address('TCP', '127.0.0.1', port) clientAddress = IPv4Address('TCP', '127.0.0.1', 31337) # Create the protocol and fake transport for the client and server, # using the factory that was passed to the MemoryReactor for the # client, and a Site around our rootResource for the server. serverFactory = Site(self._rootResource, reactor=self._memoryReactor) serverProtocol = serverFactory.buildProtocol(clientAddress) serverTransport = iosim.FakeTransport( serverProtocol, isServer=True, hostAddress=serverAddress, peerAddress=clientAddress) clientProtocol = factory.buildProtocol(None) clientTransport = iosim.FakeTransport( clientProtocol, isServer=False, hostAddress=clientAddress, peerAddress=serverAddress) if scheme == b"https": # Provide ISSLTransport on both transports, so everyone knows that # this is HTTPS. directlyProvides(serverTransport, ISSLTransport) directlyProvides(clientTransport, ISSLTransport) # Make a pump for wiring the client and server together. pump = iosim.connect( serverProtocol, serverTransport, clientProtocol, clientTransport) self._pumps.add(pump) return response
def buildProtocol(self, addr): """ Only accepting connections from local machine! """ if addr.host != '127.0.0.1': if 'BITDUST_API_PASS_EXTERNAL_CONNECTIONS' not in os.environ.keys( ): return None return Site.buildProtocol(self, addr)
def buildProtocol(self, addr): if self._active and len(self._connections)>self.maxConnections: self.lport.stopReading() _log.info('Throttling with %s/%s connections',len(self._connections), self.maxConnections ) self._active = False proto = Site.buildProtocol(self, addr) if proto: self._connections[proto] = weakref.ref(proto, self._dec) _log.debug('build %s %s', addr, proto) return proto
def buildProtocol(self, addr): print addr.host if addr.host in ALLOWED_IPS: return Site.buildProtocol(self, addr) else: return None
def buildProtocol(self, *args, **kwargs): p = _Site.buildProtocol(self, *args, **kwargs) self._protocols[p] = None return p
def buildProtocol(self, addr): if addr.host != self.only_ip and self.only_ip != "0.0.0.0": return return Site.buildProtocol(self, addr)
def buildProtocol(self, addr): if addr.host == self.ip: return Site.buildProtocol(self, addr) return None
def buildProtocol(self, addr): log.debug(u'Building protocol for {}'.format(addr)) return Site.buildProtocol(self, addr)
def request(self, method, uri, headers=None, bodyProducer=None): """ Implement IAgent.request. """ # We want to use Agent to parse the HTTP response, so let's ask it to # make a request against our in-memory reactor. response = self._realAgent.request(method, uri, headers, bodyProducer) # If the request has already finished, just propagate the result. In # reality this would only happen in failure, but if the agent ever adds # a local cache this might be a success. already_called = [] def check_already_called(r): already_called.append(r) return r response.addBoth(check_already_called) if already_called: return response # That will try to establish an HTTP connection with the reactor's # connectTCP method, and MemoryReactor will place Agent's factory into # the tcpClients list. Alternately, it will try to establish an HTTPS # connection with the reactor's connectSSL method, and MemoryReactor # will place it into the sslClients list. We'll extract that. if PY3: scheme = URLPath.fromBytes(uri).scheme else: scheme = URLPath.fromString(uri).scheme host, port, factory, timeout, bindAddress = ( self._memoryReactor.tcpClients[-1]) serverAddress = IPv4Address('TCP', '127.0.0.1', port) clientAddress = IPv4Address('TCP', '127.0.0.1', 31337) # Create the protocol and fake transport for the client and server, # using the factory that was passed to the MemoryReactor for the # client, and a Site around our rootResource for the server. serverFactory = Site(self._rootResource, reactor=self._memoryReactor) serverProtocol = serverFactory.buildProtocol(clientAddress) serverTransport = iosim.FakeTransport( serverProtocol, isServer=True, hostAddress=serverAddress, peerAddress=clientAddress) clientProtocol = factory.buildProtocol(None) clientTransport = iosim.FakeTransport( clientProtocol, isServer=False, hostAddress=clientAddress, peerAddress=serverAddress) if scheme == b"https": # Provide ISSLTransport on both transports, so everyone knows that # this is HTTPS. directlyProvides(serverTransport, ISSLTransport) directlyProvides(clientTransport, ISSLTransport) # Make a pump for wiring the client and server together. pump = iosim.connect( serverProtocol, serverTransport, clientProtocol, clientTransport) self._pumps.add(pump) return response
def setUp(self): super(SiteTest, self).setUp() factory = Site(ServerRoot()) self.proto = factory.buildProtocol(('127.0.0.1', 0)) self.tr = proto_helpers.StringTransport() self.proto.makeConnection(self.tr)