Exemple #1
0
    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
Exemple #2
0
 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(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
Exemple #4
0
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
Exemple #5
0
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
Exemple #6
0
 def listen_inner_tcp(self, port):
     type_str = FlagType.trans_server_type(Context.GData.server_type)
     svr_info = Context.GData.server_info
     Context.Log.info(type_str, 'listen on port', port, 'with', self.innerProtocol)
     factory = ServerFactory()
     factory.protocol = self.innerProtocol
     reactor.listenTCP(port, factory, interface=svr_info['host'])
Exemple #7
0
    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))
Exemple #8
0
    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)
Exemple #9
0
def main(reactor, duration):
    concurrency = 50

    factory = ServerFactory()
    factory.protocol = CloseConnection

    interface = '127.0.0.%d' % (int(time()) % 254 + 1, )
    interface = '127.0.0.1'
    port = reactor.listenTCP(0, factory, interface=interface)

    client = Client(
        reactor,
        TCP4ClientEndpoint(reactor,
                           port.getHost().host,
                           port.getHost().port,
                           bindAddress=(interface, 0)))
    d = client.run(concurrency, duration)

    def cleanup(passthrough):
        d = port.stopListening()
        d.addCallback(lambda ignored: passthrough)
        return d

    d.addCallback(cleanup)
    return d
 def getOpenFlowServerFactory(self):
     f = ServerFactory()
     f.protocol = OpenFlowTunnelProtocol
     f.forward_openflow_msg = self.forward_openflow_msg
     f.add_tunnelConnection = self.add_tunnelConnection
     f.remove_tunnelConnection = self.remove_tunnelConnection
     return f
Exemple #11
0
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()
Exemple #12
0
    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
Exemple #13
0
    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))
Exemple #14
0
def AMP_server(game, port):
    factory = ServerFactory()
    factory.protocol = AMPServerProtocol
    factory.game = game
    service = internet.TCPServer(port, factory)
    service.setName("AMPServer")
    return service
Exemple #15
0
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()
Exemple #16
0
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()
Exemple #17
0
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 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,
                        backlog=settings.CACHE_QUERY_BACKLOG)
    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 getTelnetFactory(commands, prompt, **users):
    if not users:
        raise SSHServerError("You must provide at least one "
                             "username/password combination "
                             "to run this Telnet server.")
    cmds = {}
    for command in commands:
        cmds[command.name] = command
    commands = cmds

    for exit_cmd in ['_exit', 'exit']:
        if exit_cmd not in commands:
            commands[exit_cmd] = MockSSH.command_exit

    telnetRealm = TelnetRealm(prompt, commands)

    telnetPortal = portal.Portal(
        telnetRealm,
        (checkers.InMemoryUsernamePasswordDatabaseDontUse(**users), ))
    telnetPortal.registerChecker(
        checkers.InMemoryUsernamePasswordDatabaseDontUse(**users))

    telnetFactory = ServerFactory()
    telnetFactory.protocol = makeTelnetProtocol(telnetPortal, telnetRealm,
                                                users)

    return telnetFactory
Exemple #20
0
    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
Exemple #21
0
def main():

    factory = ServerFactory()
    factory.clients = {}
    factory.protocol = myFactory
    reactor.listenTCP(8000,factory)
    reactor.run()
Exemple #22
0
    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
Exemple #23
0
 def run_interface(config):
     factory = ServerFactory()
     factory.protocol = lambda: TelnetTransport(JunctionTelnetInterface)
     port = int(config['App']['TelnetInterfacePort'])
     logging.info("starting telnet cli interface on port %d" % port)
     service = TCPServer(port, factory)
     service.startService()
Exemple #24
0
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()
Exemple #25
0
    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)
Exemple #26
0
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
Exemple #27
0
def createCacheService(config):
    from carbon.cache import MetricCache, UDPForward
    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)

    # Turn on UDP forwarding
    if settings.ENABLE_UDP_FORWARDING:
        udp_forward = UDPForward()
        events.metricReceived.addHandler(udp_forward.sendDatapoint)

    if settings.USE_FLOW_CONTROL:
        events.cacheFull.addHandler(events.pauseReceivingMetrics)
        events.cacheSpaceAvailable.addHandler(events.resumeReceivingMetrics)

    return root_service
Exemple #28
0
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 _benchmark(byteCount, clientProtocol):
    result = {}
    finished = Deferred()

    def cbFinished(ignored):
        result[u'disconnected'] = time()
        result[u'duration'] = result[u'disconnected'] - result[u'connected']
        return result

    finished.addCallback(cbFinished)

    f = ServerFactory()
    f.protocol = lambda: ServerProtocol(byteCount, finished)
    server = reactor.listenTCP(0, f)

    f2 = ClientCreator(reactor, clientProtocol)
    proto = f2.connectTCP('127.0.0.1', server.getHost().port)

    def connected(proto):
        result[u'connected'] = time()
        return proto

    proto.addCallback(connected)
    proto.addCallback(_write, byteCount)
    return finished
 def getOpenFlowServerFactory(self):
     f = ServerFactory()
     f.protocol = OpenFlowServerProtocol
     f.handle_switch_openflow_msg = self.handle_switch_openflow_msg
     f.add_switchConnection = self.add_switchConnection
     f.remove_switchConnection = self.remove_switchConnection
     return f
Exemple #31
0
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 new_protocol_tcp(self):
        """
        Create a new client protocol connected to the server.
        :returns: a IRelayTestClient implementation
        """
        server_factory = ServerFactory()
        server_factory.protocol = TransitConnection
        server_factory.transit = self._transit_server
        server_factory.log_requests = self.log_requests
        server_protocol = server_factory.buildProtocol(('127.0.0.1', 0))

        @implementer(IRelayTestClient)
        class TransitClientProtocolTcp(Protocol):
            """
            Speak the transit client protocol used by the tests over TCP
            """
            _received = b""
            connected = False

            # override Protocol callbacks

            def connectionMade(self):
                self.connected = True
                return Protocol.connectionMade(self)

            def connectionLost(self, reason):
                self.connected = False
                return Protocol.connectionLost(self, reason)

            def dataReceived(self, data):
                self._received = self._received + data

            # IRelayTestClient

            def send(self, data):
                self.transport.write(data)

            def disconnect(self):
                self.transport.loseConnection()

            def reset_received_data(self):
                self._received = b""

            def get_received_data(self):
                return self._received

        client_factory = ClientFactory()
        client_factory.protocol = TransitClientProtocolTcp
        client_protocol = client_factory.buildProtocol(('127.0.0.1', 31337))

        pump = iosim.connect(
            server_protocol,
            iosim.makeFakeServer(server_protocol),
            client_protocol,
            iosim.makeFakeClient(client_protocol),
        )
        pump.flush()
        self._pumps.append(pump)
        return client_protocol
Exemple #33
0
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
Exemple #34
0
def serverFactoryFor(protocol):
    """
    Helper function which provides the signature L{ServerFactory} should
    provide.
    """
    factory = ServerFactory()
    factory.protocol = protocol
    return factory
    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
Exemple #36
0
def serverFactoryFor(protocol):
    """
    Helper function which provides the signature L{ServerFactory} should
    provide.
    """
    factory = ServerFactory()
    factory.protocol = protocol
    return factory
Exemple #37
0
 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()
Exemple #39
0
    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())
Exemple #40
0
    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())
Exemple #41
0
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 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 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
Exemple #44
0
        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)
Exemple #45
0
    def test_loseConnectionAfterHandshake(self):
        """
        L{TLSMemoryBIOProtocol.loseConnection} sends a TLS close alert and
        shuts down the underlying connection.
        """
        clientConnectionLost = Deferred()
        clientFactory = ClientFactory()
        clientFactory.protocol = (
            lambda: ConnectionLostNotifyingProtocol(clientConnectionLost))

        clientContextFactory, handshakeDeferred = (
            HandshakeCallbackContextFactory.factoryAndDeferred())
        wrapperFactory = TLSMemoryBIOFactory(clientContextFactory, True,
                                             clientFactory)
        sslClientProtocol = wrapperFactory.buildProtocol(None)

        serverProtocol = Protocol()
        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 handshake before dropping the connection.
        def cbHandshake(ignored):
            serverProtocol.transport.loseConnection()

            # Now wait for the client to notice.
            return clientConnectionLost

        handshakeDeferred.addCallback(cbHandshake)

        # Wait for the connection to end, then make sure the client was
        # notified of a handshake failure.
        def cbConnectionDone(clientProtocol):
            clientProtocol.lostConnectionReason.trap(ConnectionDone)

            # The server should have closed its underlying transport, in
            # addition to whatever it did to shut down the TLS layer.
            self.assertTrue(serverProtocol.transport.q.disconnect)

            # The client should also have closed its underlying transport once
            # it saw the server shut down the TLS layer, so as to avoid relying
            # on the server to close the underlying connection.
            self.assertTrue(clientProtocol.transport.q.disconnect)

        handshakeDeferred.addCallback(cbConnectionDone)
        return handshakeDeferred
Exemple #46
0
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()
Exemple #47
0
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()
Exemple #48
0
    def test_loseConnectionAfterHandshake(self):
        """
        L{TLSMemoryBIOProtocol.loseConnection} sends a TLS close alert and
        shuts down the underlying connection.
        """
        clientConnectionLost = Deferred()
        clientFactory = ClientFactory()
        clientFactory.protocol = (
            lambda: ConnectionLostNotifyingProtocol(
                clientConnectionLost))

        clientContextFactory, handshakeDeferred = (
            HandshakeCallbackContextFactory.factoryAndDeferred())
        wrapperFactory = TLSMemoryBIOFactory(
            clientContextFactory, True, clientFactory)
        sslClientProtocol = wrapperFactory.buildProtocol(None)

        serverProtocol = Protocol()
        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 handshake before dropping the connection.
        def cbHandshake(ignored):
            serverProtocol.transport.loseConnection()

            # Now wait for the client to notice.
            return clientConnectionLost
        handshakeDeferred.addCallback(cbHandshake)

        # Wait for the connection to end, then make sure the client was
        # notified of a handshake failure.
        def cbConnectionDone(clientProtocol):
            clientProtocol.lostConnectionReason.trap(ConnectionDone)

            # The server should have closed its underlying transport, in
            # addition to whatever it did to shut down the TLS layer.
            self.assertTrue(serverProtocol.transport.q.disconnect)

            # The client should also have closed its underlying transport once
            # it saw the server shut down the TLS layer, so as to avoid relying
            # on the server to close the underlying connection.
            self.assertTrue(clientProtocol.transport.q.disconnect)
        handshakeDeferred.addCallback(cbConnectionDone)
        return handshakeDeferred
Exemple #49
0
    def test_badContext(self):
        """
        If the context factory passed to L{ITCPTransport.startTLS} raises an
        exception from its C{getContext} method, that exception is raised by
        L{ITCPTransport.startTLS}.
        """
        reactor = self.buildReactor()

        brokenFactory = BrokenContextFactory()
        results = []

        serverFactory = ServerFactory()
        serverFactory.protocol = Protocol

        port = reactor.listenTCP(0, serverFactory, interface=self.interface)
        endpoint = self.endpoints.client(reactor, port.getHost())

        clientFactory = ClientFactory()
        clientFactory.protocol = Protocol
        connectDeferred = endpoint.connect(clientFactory)

        def connected(protocol):
            if not ITLSTransport.providedBy(protocol.transport):
                results.append("skip")
            else:
                results.append(
                    self.assertRaises(ValueError, protocol.transport.startTLS,
                                      brokenFactory))

        def connectFailed(failure):
            results.append(failure)

        def whenRun():
            connectDeferred.addCallback(connected)
            connectDeferred.addErrback(connectFailed)
            connectDeferred.addBoth(lambda ign: reactor.stop())

        needsRunningReactor(reactor, whenRun)

        self.runReactor(reactor)

        self.assertEqual(len(results), 1,
                         "more than one callback result: %s" % (results, ))

        if isinstance(results[0], Failure):
            # self.fail(Failure)
            results[0].raiseException()
        if results[0] == "skip":
            raise SkipTest("Reactor does not support ITLSTransport")
        self.assertEqual(BrokenContextFactory.message, str(results[0]))
Exemple #50
0
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

        factory = manhole.createManholeListener()
        service = TCPServer(settings.MANHOLE_PORT,
                            factory,
                            interface=settings.MANHOLE_INTERFACE)
        service.setServiceParent(root_service)
Exemple #51
0
def main(reactor, duration):
    concurrency = 15

    server = ServerFactory()
    server.protocol = lambda: AMP(locator=BenchmarkLocator())
    port = reactor.listenTCP(0, server)
    client = Client(reactor, port.getHost().port)
    d = client.run(concurrency, duration)
    def cleanup(passthrough):
        d = port.stopListening()
        d.addCallback(lambda ignored: passthrough)
        return d
    d.addCallback(cleanup)
    return d
Exemple #52
0
 def getFactory(self):
     """
     Return a server factory which creates AMP protocol instances.
     """
     factory = ServerFactory()
     def protocol():
         proto = CredReceiver()
         proto.portal = Portal(
             self.loginSystem,
             [self.loginSystem,
              OneTimePadChecker(self._oneTimePads)])
         return proto
     factory.protocol = protocol
     return factory
Exemple #53
0
    def test_stopStartReading(self):
        """
        This test verifies transport socket read state after multiple
        pause/resumeProducing calls.
        """
        sf = ServerFactory()
        reactor = sf.reactor = self.buildReactor()

        skippedReactors = ["Glib2Reactor", "Gtk2Reactor"]
        reactorClassName = reactor.__class__.__name__
        if reactorClassName in skippedReactors and platform.isWindows():
            raise SkipTest("This test is broken on gtk/glib under Windows.")

        sf.protocol = StopStartReadingProtocol
        sf.ready = Deferred()
        sf.stop = Deferred()
        p = reactor.listenTCP(0, sf)
        port = p.getHost().port

        def proceed(protos, port):
            """
            Send several IOCPReactor's buffers' worth of data.
            """
            self.assertTrue(protos[0])
            self.assertTrue(protos[1])
            protos = protos[0][1], protos[1][1]
            protos[0].transport.write('x' * (2 * 4096) + 'y' * (2 * 4096))
            return (sf.stop.addCallback(
                cleanup, protos, port).addCallback(lambda ign: reactor.stop()))

        def cleanup(data, protos, port):
            """
            Make sure IOCPReactor didn't start several WSARecv operations
            that clobbered each other's results.
            """
            self.assertEquals(data, 'x' * (2 * 4096) + 'y' * (2 * 4096),
                              'did not get the right data')
            return DeferredList([
                maybeDeferred(protos[0].transport.loseConnection),
                maybeDeferred(protos[1].transport.loseConnection),
                maybeDeferred(port.stopListening)
            ])

        cc = TCP4ClientEndpoint(reactor, '127.0.0.1', port)
        cf = ClientFactory()
        cf.protocol = Protocol
        d = DeferredList([cc.connect(cf), sf.ready]).addCallback(proceed, p)
        self.runReactor(reactor)
        return d
Exemple #54
0
def start(root_node,
          auth_backend=None,
          port=8023,
          logfile=None,
          extra_loggers=None):
    """
    Start telnet server
    """
    # Set logging
    if logfile:
        logging.basicConfig(filename=logfile, level=logging.DEBUG)
    else:
        logging.basicConfig(filename='/dev/stdout', level=logging.DEBUG)

    # Thread sensitive interface for stdout/stdin
    std.setup()

    # Telnet
    factory = ServerFactory()
    factory.connectionPool = set()  # List of currently, active connections
    factory.protocol = lambda: TelnetTransport(TelnetDeployer)
    factory.root_node = root_node()
    factory.auth_backend = auth_backend
    factory.extra_loggers = extra_loggers or []

    # Handle signals
    def handle_sigint(signal, frame):
        if factory.connectionPool:
            logging.info('Running, %i active session(s).' %
                         len(factory.connectionPool))
        else:
            logging.info('No active sessions, exiting')
            reactor.stop()

    signal.signal(signal.SIGINT, handle_sigint)

    # Run the reactor!
    logging.info(
        'Listening for incoming telnet connections on localhost:%s...' % port)

    # Set process name
    suffix = (' --log "%s"' % logfile if logfile else '')
    setproctitle('deploy:%s telnet-server --port %i%s' %
                 (root_node.__class__.__name__, port, suffix))

    # Run server
    reactor.listenTCP(port, factory)
    reactor.run()
Exemple #55
0
    def setup_transport(self):
        config = self.get_static_config()
        self._clients = {}

        def protocol():
            return TelnetTransport(self.protocol, self)

        factory = ServerFactory()
        factory.protocol = protocol

        self.telnet_server = yield config.twisted_endpoint.listen(factory)

        self._transport_type = config.transport_type
        self._to_addr = config.to_addr
        if self._to_addr is None:
            self._to_addr = self._format_addr(self.telnet_server.getHost())
def main(reactor, duration):
    concurrency = 15

    server = ServerFactory()
    server.protocol = lambda: AMP(locator=BenchmarkLocator())
    port = reactor.listenTCP(0, server)
    client = Client(reactor, port.getHost().port)
    d = client.run(concurrency, duration)

    def cleanup(passthrough):
        d = port.stopListening()
        d.addCallback(lambda ignored: passthrough)
        return d

    d.addCallback(cleanup)
    return d
Exemple #57
0
    def getFactory(self):
        """
        Return a server factory which creates AMP protocol instances.
        """
        factory = ServerFactory()

        def protocol():
            proto = CredReceiver()
            proto.portal = Portal(
                self.loginSystem,
                [self.loginSystem,
                 OneTimePadChecker(self._oneTimePads)])
            return proto

        factory.protocol = protocol
        return factory