def manhole_factory(namespace, username, password): """ Produces a factory object which can be used to listen for telnet connections to the manhole. """ assert isinstance(namespace, dict) assert isinstance(username, STRING_TYPES) assert isinstance(password, STRING_TYPES) assert TelnetRealm.NAMESPACE is None, "namespace already set" # TODO: we should try to use the system to authorize users instead checker = InMemoryUsernamePasswordDatabaseDontUse() checker.addUser(username, password) # Setup the namespace namespace = namespace.copy() namespace.setdefault("pp", pprint) namespace.setdefault("show", show) realm = TelnetRealm() TelnetRealm.NAMESPACE = namespace portal = Portal(realm, [checker]) factory = ServerFactory() factory.protocol = TransportProtocolFactory(portal) return factory
def createBaseService(options): root_service = RurouniRootService() root_service.setName('rurouni') receive_services = ( (settings.LINE_RECEIVER_INTERFACE, settings.LINE_RECEIVER_PORT, protocols.MetricLineReceiver ), (settings.PICKLE_RECEIVER_INTERFACE, settings.PICKLE_RECEIVER_PORT, protocols.MetricPickleReceiver ), ) for interface, port, protocol in receive_services: if port: factory = ServerFactory() factory.protocol = protocol service = TCPServer(int(port), factory, interface=interface) service.setServiceParent(root_service) from rurouni.state.instrumentation import InstrumentationService service = InstrumentationService() service.setServiceParent(root_service) return root_service
def _serverGetConnectionAddressTest(self, client, interface, which): """ Connect C{client} to a server listening on C{interface} started with L{IReactorTCP.listenTCP} and return the address returned by one of the server transport's address lookup methods, C{getHost} or C{getPeer}. @param client: A C{SOCK_STREAM} L{socket.socket} created with an address family such that it will be able to connect to a server listening on C{interface}. @param interface: A C{str} giving an address for a server to listen on. This should almost certainly be the loopback address for some address family supported by L{IReactorTCP.listenTCP}. @param which: A C{str} equal to either C{"getHost"} or C{"getPeer"} determining which address will be returned. @return: Whatever object, probably an L{IAddress} provider, is returned from the method indicated by C{which}. """ class ObserveAddress(Protocol): def makeConnection(self, transport): reactor.stop() self.factory.address = getattr(transport, which)() reactor = self.buildReactor() factory = ServerFactory() factory.protocol = ObserveAddress port = reactor.listenTCP(0, factory, interface=interface) client.setblocking(False) try: client.connect((port.getHost().host, port.getHost().port)) except socket.error, (errnum, message): self.assertIn(errnum, (errno.EINPROGRESS, errno.EWOULDBLOCK))
def createCacheService(config): from carbon.cache import MetricCache from carbon.conf import settings from carbon.protocols import CacheManagementHandler # Configure application components events.metricReceived.addHandler(MetricCache.store) root_service = createBaseService(config) factory = ServerFactory() factory.protocol = CacheManagementHandler service = TCPServer(int(settings.CACHE_QUERY_PORT), factory, interface=settings.CACHE_QUERY_INTERFACE) service.setServiceParent(root_service) # have to import this *after* settings are defined from carbon.writer import WriterService service = WriterService() service.setServiceParent(root_service) if settings.USE_FLOW_CONTROL: events.cacheFull.addHandler(events.pauseReceivingMetrics) events.cacheSpaceAvailable.addHandler(events.resumeReceivingMetrics) return root_service
def test_addresses(self): """ A client's transport's C{getHost} and C{getPeer} return L{IPv4Address} instances which give the dotted-quad string form of the local and remote endpoints of the connection respectively. """ host, port = self._freePort() reactor = self.buildReactor() serverFactory = ServerFactory() serverFactory.protocol = Protocol server = reactor.listenTCP(0, serverFactory, interface=host) serverAddress = server.getHost() addresses = {"host": None, "peer": None} class CheckAddress(Protocol): def makeConnection(self, transport): addresses["host"] = transport.getHost() addresses["peer"] = transport.getPeer() reactor.stop() clientFactory = Stop(reactor) clientFactory.protocol = CheckAddress client = reactor.connectTCP("localhost", server.getHost().port, clientFactory, bindAddress=("127.0.0.1", port)) reactor.installResolver(FakeResolver({"localhost": "127.0.0.1"})) reactor.run() # self.runReactor(reactor) self.assertEqual(addresses["host"], IPv4Address("TCP", "127.0.0.1", port)) self.assertEqual(addresses["peer"], IPv4Address("TCP", "127.0.0.1", serverAddress.port))
def test_connectEvent(self): """ This test checks that we correctly get notifications event for a client. This ought to prevent a regression under Windows using the GTK2 reactor. See #3925. """ reactor = self.buildReactor() serverFactory = ServerFactory() serverFactory.protocol = Protocol server = reactor.listenTCP(0, serverFactory) connected = [] class CheckConnection(Protocol): def connectionMade(self): connected.append(self) reactor.stop() clientFactory = Stop(reactor) clientFactory.protocol = CheckConnection client = reactor.connectTCP("127.0.0.1", server.getHost().port, clientFactory) reactor.run() self.assertTrue(connected)
def writeBeforeHandshakeTest(self, sendingProtocol, bytes): """ Run test where client sends data before handshake, given the sending protocol and expected bytes. """ clientFactory = ClientFactory() clientFactory.protocol = sendingProtocol clientContextFactory, handshakeDeferred = ( HandshakeCallbackContextFactory.factoryAndDeferred()) wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverProtocol = AccumulatingProtocol(len(bytes)) serverFactory = ServerFactory() serverFactory.protocol = lambda: serverProtocol serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol) # Wait for the connection to end, then make sure the server received # the bytes sent by the client. def cbConnectionDone(ignored): self.assertEqual("".join(serverProtocol.received), bytes) connectionDeferred.addCallback(cbConnectionDone) return connectionDeferred
def main(): global world tornado.options.parse_command_line() # configure logging log_level = logging.DEBUG if options.debug else logging.INFO logging.basicConfig(level=log_level) # init world singleton world_module = import_module(options.world) world = World(world_module.world) # configure the Telnet server factory = ServerFactory() factory.protocol = MudTelnetProtocol reactor.listenTCP(options.telnetport, factory) # init Tornado app app = Application() app.listen(options.port) # configure tick interrupt ticker = tornado.ioloop.PeriodicCallback(world.tick, options.tick * 1000) ticker.start() # configure admin debug server reactor.listenTCP(2222, getManholeFactory(globals(), admin='admin')) # start the server(s) logging.info("Listening on port %d" % options.port) tornado.ioloop.IOLoop.instance().start()
def doStop(self, cv=None): """ Stops imap service (fetcher, factory and port). :param cv: A condition variable to which we can signal when imap indeed stops. :type cv: threading.Condition :return: a Deferred that stops and flushes the in memory store data to disk in another thread. :rtype: Deferred """ if DO_PROFILE: log.msg("Stopping PROFILING") pr.disable() pr.dump_stats(PROFILE_DAT) ServerFactory.doStop(self) if cv is not None: def _stop_imap_cb(): logger.debug('Stopping in memory store.') self._memstore.stop_and_flush() while not self._memstore.producer.is_queue_empty(): logger.debug('Waiting for queue to be empty.') # TODO use a gatherResults over the new/dirty # deferred list, # as in memorystore's expunge() method. time.sleep(1) # notify that service has stopped logger.debug('Notifying that service has stopped.') cv.acquire() cv.notify() cv.release() return threads.deferToThread(_stop_imap_cb)
def get_service(self): factory = ServerFactory() factory.protocol = SimpleTelnetSession factory.game = self.game service = internet.TCPServer(self.options.getint('port'), factory) service.setName("SimpleTelnetServer") return service
def makeService(options): # primary setup application = service.Application(meta.display_name) services = service.IServiceCollection(application) # setup message server serverFactory = ServerFactory() serverFactory.protocol = Listener serverFactory.publisher = PublisherFactory() msgServer = internet.TCPServer(config.listener.port, serverFactory) msgServer.setName(config.listener.servicename) msgServer.setServiceParent(services) # setup IRC message client if config.irc.sslEnabled: msgService = internet.SSLClient(config.irc.server, config.irc.port, serverFactory.publisher, ClientContextFactory()) else: msgService = internet.TCPClient(config.irc.server, config.irc.port, serverFactory.publisher) msgService.setName(config.irc.servicename) msgService.setServiceParent(services) # setup IRC log client logger = LoggerFactory(config.irc.server, config.log.channels) logService = internet.TCPClient(config.irc.server, config.irc.port, logger) logService.setName(config.log.servicename) logService.setServiceParent(services) # setuplog rotator rotService = internet.TimerService(config.log.rotate.checkInterval, logger.rotateLogs, logService) rotService.setName(config.log.rotate.servicename) rotService.setServiceParent(services) # setup log file web server webroot = static.File(config.log.http.docRoot) if config.log.http.vhostEnabled: vResource = vhost.VHostMonsterResource() webroot.putChild('vhost', vResource) if config.log.http.auth == 'basic': guarded = auth.guardResourceWithBasicAuth( webroot, config.log.http.realm, config.log.http.users) site = server.Site(guarded) else: site = server.Site(webroot) webserver = internet.TCPServer(config.log.http.port, site) webserver.setName(config.log.http.servicename) webserver.setServiceParent(services) # setup ssh access to a Python shell interpreterType = carapace_const.PYTHON sshFactory = getShellFactory( interpreterType, app=application, services=services) sshserver = internet.TCPServer(config.ssh.port, sshFactory) sshserver.setName(config.ssh.servicename) sshserver.setServiceParent(services) return services
def makeService(config): s = appservice.MultiService() conf = inetdconf.InetdConf() conf.parseFile(open(config['file'])) for service in conf.services: protocol = service.protocol if service.protocol.startswith('rpc/'): log.msg('Skipping rpc service due to lack of rpc support') continue if (protocol, service.socketType) not in [('tcp', 'stream'), ('udp', 'dgram')]: log.msg('Skipping unsupported type/protocol: %s/%s' % (service.socketType, service.protocol)) continue # Convert the username into a uid (if necessary) try: service.user = int(service.user) except ValueError: try: service.user = pwd.getpwnam(service.user)[2] except KeyError: log.msg('Unknown user: '******'s primary group service.group = pwd.getpwuid(service.user)[3] else: try: service.group = int(service.group) except ValueError: try: service.group = grp.getgrnam(service.group)[2] except KeyError: log.msg('Unknown group: ' + service.group) continue if service.program == 'internal': if config['nointernal']: continue # Internal services can use a standard ServerFactory if service.name not in inetd.internalProtocols: log.msg('Unknown internal service: ' + service.name) continue factory = ServerFactory() factory.protocol = inetd.internalProtocols[service.name] else: factory = inetd.InetdFactory(service) if protocol == 'tcp': internet.TCPServer(service.port, factory).setServiceParent(s) elif protocol == 'udp': raise RuntimeError("not supporting UDP") return s
def start(_settings, _backend, telnet_port=8023): """ Start telnet server """ global settings global backend backend = _backend # Settings settings = _settings() # Thread sensitive interface for stdout/stdin std.setup() # Telnet telnet_factory = ServerFactory() telnet_factory.protocol = lambda: TelnetTransport(TelnetDeployer) # Handle signals def handle_sigint(signal, frame): if active_sessions: print 'Running, %i active session(s).' % len(active_sessions) else: print 'No active sessions, exiting' reactor.stop() signal.signal(signal.SIGINT, handle_sigint) # Run the reactor! print 'Listening telnet on localhost:%s...' % telnet_port reactor.listenTCP(telnet_port, telnet_factory) reactor.run()
def main(): factory = ServerFactory() factory.clients = {} factory.protocol = myFactory reactor.listenTCP(8000,factory) reactor.run()
def test_handshake(self): """ The TLS handshake is performed when L{TLSMemoryBIOProtocol} is connected to a transport. """ clientFactory = ClientFactory() clientFactory.protocol = Protocol clientContextFactory, handshakeDeferred = ( HandshakeCallbackContextFactory.factoryAndDeferred()) wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverFactory = ServerFactory() serverFactory.protocol = Protocol serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol) # Only wait for the handshake to complete. Anything after that isn't # important here. return handshakeDeferred
def main(): # Initialize log log.Log.getInstance(sys.stdout) for protocol in ( Drop, ReadAndDrop, GarbageStatus, BadHeader, PauseHeader, Redirect, DataDrop, DropOnce, NoCR, PipeDrop, RedirectLoop, ReadAll, Timeout, SlowResponse, ): factory = ServerFactory() factory.protocol = protocol reactor.listenTCP(protocol.PORT, factory) reactor.run()
def test_writeSequence(self): """ Bytes written to L{TLSMemoryBIOProtocol} with C{writeSequence} are received by the protocol on the other side of the connection. """ bytes = "some bytes" class SimpleSendingProtocol(Protocol): def connectionMade(self): self.transport.writeSequence(list(bytes)) clientFactory = ClientFactory() clientFactory.protocol = SimpleSendingProtocol clientContextFactory = HandshakeCallbackContextFactory() wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverProtocol = AccumulatingProtocol(len(bytes)) serverFactory = ServerFactory() serverFactory.protocol = lambda: serverProtocol serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol) # Wait for the connection to end, then make sure the server received # the bytes sent by the client. def cbConnectionDone(ignored): self.assertEquals("".join(serverProtocol.received), bytes) connectionDeferred.addCallback(cbConnectionDone) return connectionDeferred
def setupReceivers(root_service, settings): from carbon.protocols import MetricLineReceiver, MetricPickleReceiver, MetricDatagramReceiver for protocol, interface, port in [ (MetricLineReceiver, settings.LINE_RECEIVER_INTERFACE, settings.LINE_RECEIVER_PORT), (MetricPickleReceiver, settings.PICKLE_RECEIVER_INTERFACE, settings.PICKLE_RECEIVER_PORT) ]: if port: factory = ServerFactory() factory.protocol = protocol service = TCPServer(port, factory, interface=interface) service.setServiceParent(root_service) if settings.ENABLE_UDP_LISTENER: service = UDPServer(int(settings.UDP_RECEIVER_PORT), MetricDatagramReceiver(), interface=settings.UDP_RECEIVER_INTERFACE) service.setServiceParent(root_service) if settings.ENABLE_AMQP: from carbon import amqp_listener amqp_host = settings.AMQP_HOST amqp_port = settings.AMQP_PORT amqp_user = settings.AMQP_USER amqp_password = settings.AMQP_PASSWORD amqp_verbose = settings.AMQP_VERBOSE amqp_vhost = settings.AMQP_VHOST amqp_spec = settings.AMQP_SPEC amqp_exchange_name = settings.AMQP_EXCHANGE factory = amqp_listener.createAMQPListener( amqp_user, amqp_password, vhost=amqp_vhost, spec=amqp_spec, exchange_name=amqp_exchange_name, verbose=amqp_verbose) service = TCPClient(amqp_host, amqp_port, factory) service.setServiceParent(root_service) if settings.ENABLE_MANHOLE: from carbon import manhole # Configure application components if settings.RELAY_METHOD == 'rules': router = RelayRulesRouter(settings["relay-rules"]) elif settings.RELAY_METHOD == 'consistent-hashing': router = ConsistentHashingRouter(settings.REPLICATION_FACTOR) elif settings.RELAY_METHOD == 'aggregated-consistent-hashing': from carbon.aggregator.rules import RuleManager RuleManager.read_from(settings["aggregation-rules"]) router = AggregatedConsistentHashingRouter(RuleManager, settings.REPLICATION_FACTOR) elif settings.RELAY_METHOD == 'remove-node-consistent-hashing': router = RemoveNodeConsistentHashingRouter(settings.REPLICATION_FACTOR, settings.REMOVE_NODE_INDEX) factory = manhole.createManholeListener() service = TCPServer( settings.MANHOLE_PORT, factory, interface=settings.MANHOLE_INTERFACE) service.setServiceParent(root_service)
def serverFactoryFor(protocol): """ Helper function which provides the signature L{ServerFactory} should provide. """ factory = ServerFactory() factory.protocol = protocol return factory
def unconnected_proxyserver(mocker): mocker.patch("twisted.test.iosim.FakeTransport.startTLS") mocker.patch("pappyproxy.proxy.load_certs_from_dir", new=mock_generate_cert) factory = ServerFactory() factory.protocol = ProxyServer protocol = factory.buildProtocol(('127.0.0.1', 0)) protocol.makeConnection(FakeTransport(protocol, True)) return protocol
def _create_server_factory(self): factory = ServerFactory() factory.protocol = IncomingProtocol factory.add_connection = self.incoming_connections.add factory.remove_connection = self.incoming_connections.remove self._init_factory(factory) return factory
def getYobotServerFactory(self): f = ServerFactory() f.protocol = YobotServer f.dispatch = self.dispatch f.doRegister = self.doRegister f.unregisterClient = self.unregisterClient #... return f
def main(): global sensor sensor = GridEye(0x68) """This runs the protocol on port 8000""" factory = ServerFactory() factory.protocol = Echo reactor.listenTCP(8000, factory) print("Server sarted on port 8080") reactor.run()
def doStart(self): ServerFactory.doStart(self) # Wait for AMQP to get ready self.log.info("Waiting for AMQP to get ready") yield self.pb['smppcm'].amqpBroker.channelReady # Load configuration profile proto = self.buildProtocol(('127.0.0.1', 0)) tr = proto_helpers.StringTransport() proto.makeConnection(tr) if (self.config.authentication and self.loadConfigProfileWithCreds['username'] is not None and self.loadConfigProfileWithCreds['password'] is not None): self.log.info( "OnStart loading configuration default profile with username: '******'", self.loadConfigProfileWithCreds['username']) if (self.loadConfigProfileWithCreds['username'] != self.config.admin_username or md5(self.loadConfigProfileWithCreds['password']).digest() != self.config.admin_password): self.log.error( "Authentication error, cannot load configuration profile with provided username: '******'", self.loadConfigProfileWithCreds['username']) proto.connectionLost(None) defer.returnValue(False) proto.dataReceived('%s\r\n' % self.loadConfigProfileWithCreds['username']) proto.dataReceived('%s\r\n' % self.loadConfigProfileWithCreds['password']) elif self.config.authentication: self.log.error( 'Authentication is required and no credentials given, config. profile will not be loaded') proto.connectionLost(None) defer.returnValue(False) else: self.log.info( "OnStart loading configuration default profile without credentials (auth. is not required)") proto.dataReceived('load\r\n') # Wait some more time till all configurations are loaded pending_load = ['mtrouter', 'morouter', 'filter', 'group', 'smppcc', 'httpcc', 'user'] while True: for _pl in pending_load: if re.match(r'.*%s configuration loaded.*' % _pl, tr.value(), re.DOTALL): self.log.info("%s configuration loaded.", _pl) pending_load.remove(_pl) if len(pending_load) > 0: waitDeferred = defer.Deferred() reactor.callLater(0.3, waitDeferred.callback, None) yield waitDeferred else: break proto.dataReceived('quit\r\n') proto.connectionLost(None) defer.returnValue(False)
def startFactory(self): ServerFactory.startFactory(self) from twisted.internet import reactor self.toServerFactory = MobileToClientServerFactory() self.serverPoint = TCP4ClientEndpoint( reactor, '192.168.0.91', self.service.serverAddr)
def setup_transport(self): self._clients = {} def protocol(): return TelnetTransport(TelnetTransportProtocol, self) factory = ServerFactory() factory.protocol = protocol self.telnet_server = yield reactor.listenTCP(self.telnet_port, factory) self._to_addr = self._format_addr(self.telnet_server.getHost())
def serverFactoryFor(protocol): """ Helper function which returns a L{ServerFactory} which will build instances of C{protocol}. @param protocol: A callable which returns an L{IProtocol} provider to be used to handle connections to the port the returned factory listens on. """ factory = ServerFactory() factory.protocol = protocol return factory
def proxyserver(mocker): mocker.patch("twisted.test.iosim.FakeTransport.startTLS") mocker.patch("pappyproxy.proxy.load_certs_from_dir", new=mock_generate_cert) factory = ServerFactory() factory.protocol = ProxyServer protocol = factory.buildProtocol(('127.0.0.1', 0)) protocol.makeConnection(FakeTransport(protocol, True)) protocol.lineReceived('CONNECT https://www.AAAA.BBBB:443 HTTP/1.1') protocol.lineReceived('') protocol.transport.getOutBuffer() return protocol
def properties_loaded(res): if not 'redir_url' in self.props: lg.error('redir_url not defined in configuration properties') self.redir_url = '(contact admin)' else: self.redir_url = str(self.props['redir_url'][0]) f = ServerFactory() f.protocol = LocalConnection f.redirproxy = self self.port = reactor.listenTCP(self.port, f)
def main(): serverFactory = ServerFactory() serverFactory.protocol = myHost() reactor.listenTCP(8000, serverFactory) reactor.run() host = raw_input("Enter chat screen name.\n") clientFactory = ClientFactory() clientFactory.protocol = myFactory() reactor.listenTCP(8000, serverFactory) reactor.run()
def can_listen_tcp(port, interface=''): """Attempts to create a tcp listener on a port/interface combo Args: port (int): Listening port interface (str): Hostname to bind to. If not specified binds to all Returns: ListenResult: the result of the test """ connection_server_factory = ServerFactory.forProtocol(Protocol) try: listener = reactor.listenTCP( port=port, factory=connection_server_factory, interface=interface, ) yield listener.stopListening() result = ListenResult(True, port, interface) except CannotListenError as e: result = _reason_port_is_used(port, interface, e, 'tcp') defer.returnValue(result)
def __init__(self, port=RESOURCE_MANAGER_PORT, parser=DEFAULT_PARSER, log_to_screen=True): """Initialize the resource manager server. Args: port (number): client listener port. parser (object): messages parser of type `AbstractParser`. log_to_screen (bool): Enable log prints to screen. """ self.logger = get_logger(log_to_screen) self._factory = ServerFactory() self._factory.protocol = Worker self._factory.logger = self.logger self._factory.protocol.parser = parser() self._port = port self._reactor = SelectReactor() self._reactor.listenTCP(port, self._factory) self._resource_manager = ManagerThread(self._reactor, self.logger) self._factory.request_queue = self._resource_manager.request_queue
def test_stopOnlyCloses(self): """ When the L{IListeningPort} returned by L{IReactorSocket.adoptStreamPort} is stopped using C{stopListening}, the underlying socket is closed but not shutdown. This allows another process which still has a reference to it to continue accepting connections over it. """ reactor = self.buildReactor() portSocket = socket.socket() self.addCleanup(portSocket.close) portSocket.bind(("127.0.0.1", 0)) portSocket.listen(1) portSocket.setblocking(False) # The file descriptor is duplicated by adoptStreamPort port = reactor.adoptStreamPort(portSocket.fileno(), portSocket.family, ServerFactory()) d = port.stopListening() def stopped(ignored): # Should still be possible to accept a connection on # portSocket. If it was shutdown, the exception would be # EINVAL instead. exc = self.assertRaises(socket.error, portSocket.accept) if platform.isWindows() and _PY3: self.assertEqual(exc.args[0], errno.WSAEWOULDBLOCK) else: self.assertEqual(exc.args[0], errno.EAGAIN) d.addCallback(stopped) d.addErrback(err, "Failed to accept on original port.") needsRunningReactor( reactor, lambda: d.addCallback(lambda ignored: reactor.stop())) reactor.run()
def test_invalidAddressFamily(self): """ An implementation of L{IReactorSocket.adoptStreamPort} raises L{UnsupportedAddressFamily} if passed an address family it does not support. """ reactor = self.buildReactor() port = socket.socket() port.bind(("127.0.0.1", 0)) port.listen(1) self.addCleanup(port.close) arbitrary = 2 ** 16 + 7 self.assertRaises( UnsupportedAddressFamily, reactor.adoptStreamPort, port.fileno(), arbitrary, ServerFactory(), )
def test_protocolGarbageAfterLostConnection(self): """ After the connection a protocol is being used for is closed, the reactor discards all of its references to the protocol. """ lostConnectionDeferred = Deferred() clientProtocol = ClosingLaterProtocol(lostConnectionDeferred) clientRef = ref(clientProtocol) reactor = self.buildReactor() portDeferred = self.endpoints.server(reactor).listen( ServerFactory.forProtocol(Protocol)) def listening(port): msg("Listening on %r" % (port.getHost(),)) endpoint = self.endpoints.client(reactor, port.getHost()) client = endpoint.connect( ClientFactory.forProtocol(lambda: clientProtocol)) def disconnect(proto): msg("About to disconnect %r" % (proto,)) proto.transport.loseConnection() client.addCallback(disconnect) client.addErrback(lostConnectionDeferred.errback) return lostConnectionDeferred def onListening(): portDeferred.addCallback(listening) portDeferred.addErrback(err) portDeferred.addBoth(lambda ignored: reactor.stop()) needsRunningReactor(reactor, onListening) self.runReactor(reactor) # Drop the reference and get the garbage collector to tell us if there # are no references to the protocol instance left in the reactor. clientProtocol = None collect() self.assertIs(None, clientRef())
def _acceptFailureTest(self, socketErrorNumber): """ Test behavior in the face of an exception from C{accept(2)}. On any exception which indicates the platform is unable or unwilling to allocate further resources to us, the existing port should remain listening, a message should be logged, and the exception should not propagate outward from doRead. @param socketErrorNumber: The errno to simulate from accept. """ class FakeSocket(object): """ Pretend to be a socket in an overloaded system. """ def accept(self): raise socket.error(socketErrorNumber, os.strerror(socketErrorNumber)) factory = ServerFactory() port = self.port(0, factory, interface='127.0.0.1') originalSocket = port.socket try: port.socket = FakeSocket() port.doRead() expectedFormat = "Could not accept new connection (%s)" expectedErrorCode = errno.errorcode[socketErrorNumber] expectedMessage = expectedFormat % (expectedErrorCode, ) for msg in self.messages: if msg.get('message') == (expectedMessage, ): break else: self.fail("Log event for failed accept not found in " "%r" % (self.messages, )) finally: port.socket = originalSocket
def __init__(self, reactor, cluster_state, configuration_service, endpoint, context_factory): """ :param reactor: See ``ControlServiceLocator.__init__``. :param ClusterStateService cluster_state: Object that records known cluster state. :param ConfigurationPersistenceService configuration_service: Persistence service for desired cluster configuration. :param endpoint: Endpoint to listen on. :param context_factory: TLS context factory. """ self.connections = set() self._current_command = {} self.cluster_state = cluster_state self.configuration_service = configuration_service self.endpoint_service = StreamServerEndpointService( endpoint, TLSMemoryBIOFactory( context_factory, False, ServerFactory.forProtocol(lambda: ControlAMP(reactor, self)))) # When configuration changes, notify all connected clients: self.configuration_service.register( lambda: self._send_state_to_connections(self.connections))
def test_invalidDescriptor(self): """ An implementation of L{IReactorSocket.adoptStreamPort} raises L{socket.error} if passed an integer which is not associated with a socket. """ reactor = self.buildReactor() probe = socket.socket() fileno = probe.fileno() probe.close() exc = self.assertRaises( socket.error, reactor.adoptStreamPort, fileno, socket.AF_INET, ServerFactory(), ) if platform.isWindows(): self.assertEqual(exc.args[0], errno.WSAENOTSOCK) else: self.assertEqual(exc.args[0], errno.EBADF)
def _acceptFailureTest(self, socketErrorNumber): """ Test behavior in the face of an exception from C{accept(2)}. On any exception which indicates the platform is unable or unwilling to allocate further resources to us, the existing port should remain listening, a message should be logged, and the exception should not propagate outward from doRead. @param socketErrorNumber: The errno to simulate from accept. """ class FakeSocket: """ Pretend to be a socket in an overloaded system. """ def accept(self): raise OSError(socketErrorNumber, os.strerror(socketErrorNumber)) factory = ServerFactory() port = self.port(0, factory, interface="127.0.0.1") self.patch(port, "socket", FakeSocket()) port.doRead() expectedFormat = "Could not accept new connection ({acceptError})" expectedErrorCode = errno.errorcode[socketErrorNumber] matchingMessages = [(msg.get("log_format") == expectedFormat and msg.get("acceptError") == expectedErrorCode) for msg in self.messages] self.assertGreater( len(matchingMessages), 0, "Log event for failed accept not found in " "%r" % (self.messages, ), )
def can_listen_ssl(port, ssl_ctx_factory, interface=''): """Attempts to create an ssl listener on a port/interface combo Args: port (int): Listening port ssl_ctx_factory (twisted.internet.ssl.ContextFactory): Factory that can create the ssl contexts to be used by the connections interface (str): Hostname to bind to. If not specified binds to all Returns: ListenResult: the result of the test """ connection_server_factory = ServerFactory.forProtocol(Protocol) try: listener = reactor.listenSSL( port=port, factory=connection_server_factory, interface=interface, contextFactory=ssl_ctx_factory, ) yield listener.stopListening() result = ListenResult(True, port, interface) except CannotListenError as e: result = _reason_port_is_used(port, interface, e, 'ssl') defer.returnValue(result)
def test_connectEvent(self): """ This test checks that we correctly get notifications event for a client. This ought to prevent a regression under Windows using the GTK2 reactor. See #3925. """ reactor = self.buildReactor() self.listen(reactor, ServerFactory.forProtocol(Protocol)) connected = [] class CheckConnection(Protocol): def connectionMade(self): connected.append(self) reactor.stop() clientFactory = Stop(reactor) clientFactory.protocol = CheckConnection needsRunningReactor(reactor, lambda: self.connect(reactor, clientFactory)) reactor.run() self.assertTrue(connected)
def buildProtocol(self, *args, **kw): log.msg("building server protocol") prot = ServerFactory.buildProtocol(self, *args, **kw) prot.setPeer(self.client) return prot
def test_mode(self): """ The UNIX socket created by L{IReactorUNIX.listenUNIX} is created with the mode specified. """ self._modeTest('listenUNIX', self.mktemp(), ServerFactory())
'reject': op_reject_call, 'hangup': hangup_call, 'info': info, } if __name__ == '__main__': # Setup number of operators and generate num_ops = 10 if len(sys.argv) > 1: num_ops = int(sys.argv[1]) # Initialize queue structures available_operators = generate_operators(num_ops) # op_id: op ringing_operators = OrderedDict() # op_id: op busy_operators = OrderedDict() # op_id: op wait_calls = [] # call_id handle_calls = OrderedDict() # call_id: op_id port = 5678 if len(sys.argv) > 3: port = int(sys.argv[3]) print('Listening on port', port) # Initialize Twisted server factory = ServerFactory() factory.protocol = ClientHandler reactor.listenTCP(port, factory) reactor.run()
def doStop(self): return ServerFactory.doStop(self)
def test_disconnectWhileProducing(self): """ If C{loseConnection} is called while a producer is registered with the transport, the connection is closed after the producer is unregistered. """ reactor = self.buildReactor() # For some reason, pyobject/pygtk will not deliver the close # notification that should happen after the unregisterProducer call in # this test. The selectable is in the write notification set, but no # notification ever arrives. Probably for the same reason #5233 led # win32eventreactor to be broken. skippedReactors = ["Glib2Reactor", "Gtk2Reactor"] reactorClassName = reactor.__class__.__name__ if reactorClassName in skippedReactors and platform.isWindows(): raise SkipTest("A pygobject/pygtk bug disables this functionality " "on Windows.") class Producer: def resumeProducing(self): log.msg("Producer.resumeProducing") self.listen(reactor, ServerFactory.forProtocol(Protocol)) finished = Deferred() finished.addErrback(log.err) finished.addCallback(lambda ign: reactor.stop()) class ClientProtocol(Protocol): """ Protocol to connect, register a producer, try to lose the connection, unregister the producer, and wait for the connection to actually be lost. """ def connectionMade(self): log.msg("ClientProtocol.connectionMade") self.transport.registerProducer(Producer(), False) self.transport.loseConnection() # Let the reactor tick over, in case synchronously calling # loseConnection and then unregisterProducer is the same as # synchronously calling unregisterProducer and then # loseConnection (as it is in several reactors). reactor.callLater(0, reactor.callLater, 0, self.unregister) def unregister(self): log.msg("ClientProtocol unregister") self.transport.unregisterProducer() # This should all be pretty quick. Fail the test # if we don't get a connectionLost event really # soon. reactor.callLater( 1.0, finished.errback, Failure(Exception("Connection was not lost"))) def connectionLost(self, reason): log.msg("ClientProtocol.connectionLost") finished.callback(None) clientFactory = ClientFactory() clientFactory.protocol = ClientProtocol self.connect(reactor, clientFactory) self.runReactor(reactor)
def main(): log.startLogging(sys.stdout) f = ServerFactory() f.protocol = RemoteAmarokServer reactor.listenTCP(8000, f) reactor.run()
def test_disconnectAfterWriteAfterStartTLS(self): """ L{ITCPTransport.loseConnection} ends a connection which was set up with L{ITLSTransport.startTLS} and which has recently been written to. This is intended to verify that a socket send error masked by the TLS implementation doesn't prevent the connection from being reported as closed. """ class ShortProtocol(Protocol): def connectionMade(self): if not ITLSTransport.providedBy(self.transport): # Functionality isn't available to be tested. finished = self.factory.finished self.factory.finished = None finished.errback(SkipTest("No ITLSTransport support")) return # Switch the transport to TLS. self.transport.startTLS(self.factory.context) # Force TLS to really get negotiated. If nobody talks, nothing # will happen. self.transport.write("x") def dataReceived(self, data): # Stuff some bytes into the socket. This mostly has the effect # of causing the next write to fail with ENOTCONN or EPIPE. # With the pyOpenSSL implementation of ITLSTransport, the error # is swallowed outside of the control of Twisted. self.transport.write("y") # Now close the connection, which requires a TLS close alert to # be sent. self.transport.loseConnection() def connectionLost(self, reason): # This is the success case. The client and the server want to # get here. finished = self.factory.finished if finished is not None: self.factory.finished = None finished.callback(reason) serverFactory = ServerFactory() serverFactory.finished = Deferred() serverFactory.protocol = ShortProtocol serverFactory.context = self.getServerContext() clientFactory = ClientFactory() clientFactory.finished = Deferred() clientFactory.protocol = ShortProtocol clientFactory.context = ClientContextFactory() clientFactory.context.method = serverFactory.context.method lostConnectionResults = [] finished = DeferredList( [serverFactory.finished, clientFactory.finished], consumeErrors=True) def cbFinished(results): lostConnectionResults.extend([results[0][1], results[1][1]]) finished.addCallback(cbFinished) reactor = self.buildReactor() port = reactor.listenTCP(0, serverFactory, interface='127.0.0.1') self.addCleanup(port.stopListening) connector = reactor.connectTCP(port.getHost().host, port.getHost().port, clientFactory) self.addCleanup(connector.disconnect) finished.addCallback(lambda ign: reactor.stop()) self.runReactor(reactor) lostConnectionResults[0].trap(ConnectionClosed) lostConnectionResults[1].trap(ConnectionClosed)
def useIt(reactor, contextFactory): return reactor.listenSSL(0, ServerFactory(), contextFactory)
def getListeningPort(self, reactor): """ Get a TCP port from a reactor """ return reactor.listenTCP(0, ServerFactory())
async def main(reactor, loops): """ Benchmark how long it takes to send `loops` messages. """ servers = [] def protocol(): p = LineCounter() servers.append(p) return p logger_factory = ServerFactory.forProtocol(protocol) logger_factory.wait_for = loops logger_factory.on_done = Deferred() port = reactor.listenTCP(0, logger_factory, interface="127.0.0.1") # A fake homeserver config. class Config: server_name = "synmark-" + str(loops) no_redirect_stdio = True hs_config = Config() # To be able to sleep. clock = Clock(reactor) errors = StringIO() publisher = LogPublisher() mock_sys = Mock() beginner = LogBeginner(publisher, errors, mock_sys, warnings, initialBufferSize=loops) log_config = { "version": 1, "loggers": { "synapse": { "level": "DEBUG", "handlers": ["tersejson"] } }, "formatters": { "tersejson": { "class": "synapse.logging.TerseJsonFormatter" } }, "handlers": { "tersejson": { "class": "synapse.logging.RemoteHandler", "host": "127.0.0.1", "port": port.getHost().port, "maximum_buffer": 100, "_reactor": reactor, } }, } logger = logging.getLogger("synapse.logging.test_terse_json") _setup_stdlib_logging( hs_config, log_config, logBeginner=beginner, ) # Wait for it to connect... for handler in logging.getLogger("synapse").handlers: if isinstance(handler, RemoteHandler): break else: raise RuntimeError("Improperly configured: no RemoteHandler found.") await handler._service.whenConnected() start = perf_counter() # Send a bunch of useful messages for i in range(0, loops): logger.info("test message %s", i) if len(handler._buffer) == handler.maximum_buffer: while len(handler._buffer) > handler.maximum_buffer / 2: await clock.sleep(0.01) await logger_factory.on_done end = perf_counter() - start handler.close() port.stopListening() return end
def createBaseService(config): from carbon.conf import settings from carbon.protocols import (MetricLineReceiver, MetricPickleReceiver, MetricDatagramReceiver) root_service = CarbonRootService() root_service.setName(settings.program) use_amqp = settings.get("ENABLE_AMQP", False) if use_amqp: from carbon import amqp_listener amqp_host = settings.get("AMQP_HOST", "localhost") amqp_port = settings.get("AMQP_PORT", 5672) amqp_user = settings.get("AMQP_USER", "guest") amqp_password = settings.get("AMQP_PASSWORD", "guest") amqp_verbose = settings.get("AMQP_VERBOSE", False) amqp_vhost = settings.get("AMQP_VHOST", "/") amqp_spec = settings.get("AMQP_SPEC", None) amqp_exchange_name = settings.get("AMQP_EXCHANGE", "graphite") for interface, port, backlog, protocol in ( (settings.LINE_RECEIVER_INTERFACE, settings.LINE_RECEIVER_PORT, settings.LINE_RECEIVER_BACKLOG, MetricLineReceiver), (settings.PICKLE_RECEIVER_INTERFACE, settings.PICKLE_RECEIVER_PORT, settings.PICKLE_RECEIVER_BACKLOG, MetricPickleReceiver)): if port: factory = ServerFactory() factory.protocol = protocol service = TCPServer(int(port), factory, interface=interface, backlog=backlog) service.setServiceParent(root_service) if settings.ENABLE_UDP_LISTENER: service = UDPServer(int(settings.UDP_RECEIVER_PORT), MetricDatagramReceiver(), interface=settings.UDP_RECEIVER_INTERFACE) service.setServiceParent(root_service) if use_amqp: factory = amqp_listener.createAMQPListener( amqp_user, amqp_password, vhost=amqp_vhost, spec=amqp_spec, exchange_name=amqp_exchange_name, verbose=amqp_verbose) service = TCPClient(amqp_host, int(amqp_port), factory) service.setServiceParent(root_service) if settings.ENABLE_MANHOLE: from carbon import manhole factory = manhole.createManholeListener() service = TCPServer(int(settings.MANHOLE_PORT), factory, interface=settings.MANHOLE_INTERFACE) service.setServiceParent(root_service) if settings.USE_WHITELIST: from carbon.regexlist import WhiteList, BlackList WhiteList.read_from(settings["whitelist"]) BlackList.read_from(settings["blacklist"]) # Instantiate an instrumentation service that will record metrics about # this service. from carbon.instrumentation import InstrumentationService service = InstrumentationService() service.setServiceParent(root_service) return root_service
def __init__(self, rpcVersions, rpcConf, proto, service): internet.TCPServer.__init__(0, ServerFactory()) self.rpcConf = rpcConf self.proto = proto self.service = service
def start(self): from . import proxy, plugin if self.config.crypt_session: if self.decrypt(): self.config.load_from_file('./config.json') self.config.global_load_from_file() self.delete_data_on_quit = False else: self.complete_defer.callback(None) return # If the data file doesn't exist, create it with restricted permissions if not os.path.isfile(self.config.datafile): with os.fdopen(os.open(self.config.datafile, os.O_CREAT, 0o0600), 'r'): pass self.dbpool = adbapi.ConnectionPool("sqlite3", self.config.datafile, check_same_thread=False, cp_openfun=set_text_factory, cp_max=1) try: yield schema.update.update_schema(self.dbpool, self.config.datafile) except Exception as e: print 'Error updating schema: %s' % e print 'Exiting...' self.complete_defer.callback(None) return http.init(self.dbpool) yield http.Request.cache.load_ids() context.reset_context_caches() # Run the proxy if self.config.debug_dir and os.path.exists(self.config.debug_dir): shutil.rmtree(self.config.debug_dir) print 'Removing old debugging output' listen_strs = [] self.ports = [] for listener in self.config.listeners: server_factory = proxy.ProxyServerFactory(save_all=True) try: if 'forward_host_ssl' in listener and listener[ 'forward_host_ssl']: server_factory.force_ssl = True server_factory.forward_host = listener['forward_host_ssl'] elif 'forward_host' in listener and listener['forward_host']: server_factory.force_ssl = False server_factory.forward_host = listener['forward_host'] port = reactor.listenTCP(listener['port'], server_factory, interface=listener['interface']) listener_str = 'port %d' % listener['port'] if listener['interface'] not in ('127.0.0.1', 'localhost'): listener_str += ' (bound to %s)' % listener['interface'] listen_strs.append(listener_str) self.ports.append(port) self.server_factories.append(server_factory) except CannotListenError as e: print repr(e) if listen_strs: print 'Proxy is listening on %s' % (', '.join(listen_strs)) else: print 'No listeners opened' com_factory = ServerFactory() com_factory.protocol = comm.CommServer # Make the port different for every instance of pappy, then pass it to # anything we run. Otherwise we can only have it running once on a machine self.comm_port = reactor.listenTCP(0, com_factory, interface='127.0.0.1') self.comm_port = self.comm_port.getHost().port # Load the scope yield context.load_scope(self.dbpool) context.reset_to_scope(main_context) sys.argv = [sys.argv[0]] # cmd2 tries to parse args self.cons = ProxyCmd(session=session) self.plugin_loader = plugin.PluginLoader(self.cons) for d in self.config.plugin_dirs: if not os.path.exists(d): os.makedirs(d) self.plugin_loader.load_directory(d) # Add cleanup to defer self.complete_defer = deferToThread(self.cons.cmdloop) self.complete_defer.addCallback(self.cleanup)
def makeService(config): s = appservice.MultiService() conf = inetdconf.InetdConf() conf.parseFile(open(config['file'])) rpcConf = inetdconf.RPCServicesConf() try: rpcConf.parseFile(open(config['rpc'])) except: # We'll survive even if we can't read /etc/rpc log.deferr() for service in conf.services: rpc = service.protocol.startswith('rpc/') protocol = service.protocol if rpc and not rpcOk: log.msg('Skipping rpc service due to lack of rpc support') continue if rpc: # RPC has extra options, so extract that protocol = protocol[4:] # trim 'rpc/' if not protocolDict.has_key(protocol): log.msg('Bad protocol: ' + protocol) continue try: name, rpcVersions = service.name.split('/') except ValueError: log.msg('Bad RPC service/version: ' + service.name) continue if not rpcConf.services.has_key(name): log.msg('Unknown RPC service: ' + repr(service.name)) continue try: if '-' in rpcVersions: start, end = map(int, rpcVersions.split('-')) rpcVersions = range(start, end+1) else: rpcVersions = [int(rpcVersions)] except ValueError: log.msg('Bad RPC versions: ' + str(rpcVersions)) continue if (protocol, service.socketType) not in [('tcp', 'stream'), ('udp', 'dgram')]: log.msg('Skipping unsupported type/protocol: %s/%s' % (service.socketType, service.protocol)) continue # Convert the username into a uid (if necessary) try: service.user = int(service.user) except ValueError: try: service.user = pwd.getpwnam(service.user)[2] except KeyError: log.msg('Unknown user: '******'s primary group service.group = pwd.getpwuid(service.user)[3] else: try: service.group = int(service.group) except ValueError: try: service.group = grp.getgrnam(service.group)[2] except KeyError: log.msg('Unknown group: ' + service.group) continue if service.program == 'internal': if config['nointernal']: continue # Internal services can use a standard ServerFactory if not inetd.internalProtocols.has_key(service.name): log.msg('Unknown internal service: ' + service.name) continue factory = ServerFactory() factory.protocol = inetd.internalProtocols[service.name] elif rpc: i = RPCServer(rpcVersions, rpcConf, proto, service) i.setServiceParent(s) continue else: # Non-internal non-rpc services use InetdFactory factory = inetd.InetdFactory(service) if protocol == 'tcp': internet.TCPServer(service.port, factory).setServiceParent(s) elif protocol == 'udp': raise RuntimeError("not supporting UDP") return s
def makeService(config): s = appservice.MultiService() conf = inetdconf.InetdConf() with open(config['file']) as f: conf.parseFile(f) for service in conf.services: protocol = service.protocol if service.protocol.startswith('rpc/'): log.msg('Skipping rpc service due to lack of rpc support') continue if (protocol, service.socketType) not in [('tcp', 'stream'), ('udp', 'dgram')]: log.msg('Skipping unsupported type/protocol: %s/%s' % (service.socketType, service.protocol)) continue # Convert the username into a uid (if necessary) try: service.user = int(service.user) except ValueError: try: service.user = pwd.getpwnam(service.user)[2] except KeyError: log.msg('Unknown user: '******'s primary group service.group = pwd.getpwuid(service.user)[3] else: try: service.group = int(service.group) except ValueError: try: service.group = grp.getgrnam(service.group)[2] except KeyError: log.msg('Unknown group: ' + service.group) continue if service.program == 'internal': if config['nointernal']: continue # Internal services can use a standard ServerFactory if service.name not in inetd.internalProtocols: log.msg('Unknown internal service: ' + service.name) continue factory = ServerFactory() factory.protocol = inetd.internalProtocols[service.name] else: factory = inetd.InetdFactory(service) if protocol == 'tcp': internet.TCPServer(service.port, factory).setServiceParent(s) elif protocol == 'udp': raise RuntimeError("not supporting UDP") return s
def doStart(self): ServerFactory.doStart(self) # Wait for AMQP to get ready self.log.info("Waiting for AMQP to get ready") yield self.pb['smppcm'].amqpBroker.channelReady # Load configuration profile proto = self.buildProtocol(('127.0.0.1', 0)) tr = proto_helpers.StringTransport() proto.makeConnection(tr) if (self.config.authentication and self.loadConfigProfileWithCreds['username'] is not None and self.loadConfigProfileWithCreds['password'] is not None): self.log.info( "OnStart loading configuration default profile with username: '******'", self.loadConfigProfileWithCreds['username']) if (self.loadConfigProfileWithCreds['username'] != self.config.admin_username or md5(self.loadConfigProfileWithCreds['password']).digest() != self.config.admin_password): self.log.error( "Authentication error, cannot load configuration profile with provided username: '******'", self.loadConfigProfileWithCreds['username']) proto.connectionLost(None) defer.returnValue(False) proto.dataReceived('%s\r\n' % self.loadConfigProfileWithCreds['username']) proto.dataReceived('%s\r\n' % self.loadConfigProfileWithCreds['password']) elif self.config.authentication: self.log.error( 'Authentication is required and no credentials given, config. profile will not be loaded' ) proto.connectionLost(None) defer.returnValue(False) else: self.log.info( "OnStart loading configuration default profile without credentials (auth. is not required)" ) proto.dataReceived('load\r\n') # Wait some more time till all configurations are loaded pending_load = [ 'mtrouter', 'morouter', 'filter', 'group', 'smppcc', 'httpcc', 'user' ] while True: for _pl in pending_load: if re.match(r'.*%s configuration loaded.*' % _pl, tr.value(), re.DOTALL): self.log.info("%s configuration loaded.", _pl) pending_load.remove(_pl) if len(pending_load) > 0: waitDeferred = defer.Deferred() reactor.callLater(0.3, waitDeferred.callback, None) yield waitDeferred else: break proto.dataReceived('quit\r\n') proto.connectionLost(None) defer.returnValue(False)
def buildProtocol(self, addr): protocol = ServerFactory.buildProtocol(self, addr) self.reactor.callLater(0, self.result.callback, protocol) return protocol
def buildProtocol(self, addr): log.debug('Creating a protocol for %s', addr) return ServerFactory.buildProtocol(self, addr)
def clientConnectionLost(self, connector, reason): self.server.transport.loseConnection() def clientConnectionFailed(self, connector, reason): self.server.transport.loseConnection() def verifyCallback(connection, x509, errnum, errdepth, ok): if not ok: log.msg('invalid cert from subject: %s' % x509.get_subject()) return False log.msg('accepted cert from subject: %s' % x509.get_subject()) return True serverFactory = ServerFactory() serverFactory.protocol = NNTPProxyServer serverFactory.protocol.clientFactory = NNTPProxyClientFactory if PROXY_SSL: sslFactory = ssl.DefaultOpenSSLContextFactory(PROXY_CERT_KEY, PROXY_CERT_PEM) sslContext = sslFactory.getContext() if PROXY_CA_VERIFY: sslContext.set_verify( SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT, verifyCallback) sslContext.set_verify_depth(10) sslContext.load_verify_locations(PROXY_CERT_CA) reactor.listenSSL(PROXY_PORT, serverFactory, sslFactory) else: reactor.listenTCP(PROXY_PORT, serverFactory) reactor.run()