コード例 #1
0
ファイル: test_integration.py プロジェクト: tomzhang/autopush
    def setUp(self):
        import cyclone.web
        from autobahn.twisted.websocket import WebSocketServerFactory
        from autobahn.twisted.resource import WebSocketResource
        from autopush.main import skip_request_logging
        from autopush.endpoint import EndpointHandler, MessageHandler, RegistrationHandler
        from autopush.settings import AutopushSettings
        from autopush.websocket import (
            PushServerProtocol,
            RouterHandler,
            NotificationHandler,
            DefaultResource,
            StatusResource,
        )
        from twisted.web.server import Site

        router_table = os.environ.get("ROUTER_TABLE", "router_int_test")
        storage_table = os.environ.get("STORAGE_TABLE", "storage_int_test")
        message_table = os.environ.get("MESSAGE_TABLE", "message_int_test")

        settings = AutopushSettings(
            hostname="localhost",
            statsd_host=None,
            endpoint_port="9020",
            router_port="9030",
            router_tablename=router_table,
            storage_tablename=storage_table,
            message_tablename=message_table,
        )

        # Websocket server
        self._ws_url = "ws://localhost:9010/"
        factory = WebSocketServerFactory(self._ws_url, debug=False)
        factory.protocol = PushServerProtocol
        factory.protocol.ap_settings = settings
        factory.setProtocolOptions(webStatus=False, openHandshakeTimeout=5)
        settings.factory = factory
        resource = DefaultResource(WebSocketResource(factory))
        resource.putChild("status", StatusResource())
        self.websocket = reactor.listenTCP(9010, Site(resource))

        # Websocket HTTP router
        # Internal HTTP notification router
        r = RouterHandler
        r.ap_settings = settings
        n = NotificationHandler
        n.ap_settings = settings
        ws_site = cyclone.web.Application(
            [(r"/push/([^\/]+)", r), (r"/notif/([^\/]+)(/([^\/]+))?", n)],
            default_host=settings.router_hostname,
            log_function=skip_request_logging,
            debug=False,
        )
        self.ws_website = reactor.listenTCP(9030, ws_site)

        # Endpoint HTTP router
        site = cyclone.web.Application(
            [
                (r"/push/([^\/]+)", EndpointHandler, dict(ap_settings=settings)),
                (r"/m/([^\/]+)", MessageHandler, dict(ap_settings=settings)),
                # PUT /register/ => connect info
                # GET /register/uaid => chid + endpoint
                (r"/register(?:/(.+))?", RegistrationHandler, dict(ap_settings=settings)),
            ],
            default_host=settings.hostname,
            log_function=skip_request_logging,
            debug=False,
        )
        self.website = reactor.listenTCP(9020, site)
        self._settings = settings
コード例 #2
0
ファイル: main.py プロジェクト: adamchainz/autopush
def connection_main(sysargs=None, use_files=True):
    """Main entry point to setup a connection node, aka the autopush script"""
    args, parser = _parse_connection(sysargs, use_files)
    log_format = "text" if args.human_logs else "json"
    log_level = args.log_level or ("debug" if args.debug else "info")
    sentry_dsn = bool(os.environ.get("SENTRY_DSN"))
    PushLogger.setup_logging(
        "Autopush",
        log_level=log_level,
        log_format=log_format,
        log_output=args.log_output,
        sentry_dsn=sentry_dsn,
        firehose_delivery_stream=args.firehose_stream_name)
    # Add some entropy to prevent potential conflicts.
    postfix = os.urandom(4).encode('hex').ljust(8, '0')
    settings = make_settings(
        args,
        port=args.port,
        endpoint_scheme=args.endpoint_scheme,
        endpoint_hostname=args.endpoint_hostname,
        endpoint_port=args.endpoint_port,
        router_scheme="https" if args.router_ssl_key else "http",
        router_hostname=args.router_hostname,
        router_port=args.router_port,
        env=args.env,
        hello_timeout=args.hello_timeout,
        preflight_uaid="deadbeef000000000deadbeef" + postfix,
    )

    r = RouterHandler
    r.ap_settings = settings
    n = NotificationHandler
    n.ap_settings = settings

    # Internal HTTP notification router
    site = cyclone.web.Application([
        (r"/push/([^\/]+)", r),
        (r"/notif/([^\/]+)(/([^\/]+))?", n),
    ],
                                   default_host=settings.router_hostname,
                                   debug=args.debug,
                                   log_function=skip_request_logging)
    mount_health_handlers(site, settings)

    # Public websocket server
    proto = "wss" if args.ssl_key else "ws"
    factory = WebSocketServerFactory(
        "%s://%s:%s/" % (proto, args.hostname, args.port), )
    factory.protocol = PushServerProtocol
    factory.protocol.ap_settings = settings
    factory.setProtocolOptions(
        webStatus=False,
        openHandshakeTimeout=5,
        autoPingInterval=args.auto_ping_interval,
        autoPingTimeout=args.auto_ping_timeout,
        maxConnections=args.max_connections,
        closeHandshakeTimeout=args.close_handshake_timeout,
    )
    settings.factory = factory

    settings.metrics.start()

    # Wrap the WebSocket server in a default resource that exposes the
    # `/status` handler, and delegates to the WebSocket resource for all
    # other requests.
    resource = DefaultResource(WebSocketResource(factory))
    resource.putChild("status", StatusResource())
    siteFactory = Site(resource)

    # Start the WebSocket listener.
    if args.ssl_key:
        contextFactory = AutopushSSLContextFactory(args.ssl_key, args.ssl_cert)
        if args.ssl_dh_param:
            contextFactory.getContext().load_tmp_dh(args.ssl_dh_param)

        reactor.listenSSL(args.port, siteFactory, contextFactory)
    else:
        reactor.listenTCP(args.port, siteFactory)

    # Start the internal routing listener.
    if args.router_ssl_key:
        contextFactory = AutopushSSLContextFactory(args.router_ssl_key,
                                                   args.router_ssl_cert)
        if args.ssl_dh_param:
            contextFactory.getContext().load_tmp_dh(args.ssl_dh_param)
        reactor.listenSSL(args.router_port, site, contextFactory)
    else:
        reactor.listenTCP(args.router_port, site)

    reactor.suggestThreadPoolSize(50)

    l = task.LoopingCall(periodic_reporter, settings)
    l.start(1.0)

    # Start the table rotation checker/updater
    l = task.LoopingCall(settings.update_rotating_tables)
    l.start(60)
    reactor.run()
コード例 #3
0
ファイル: main.py プロジェクト: martinthomson/autopush
def connection_main(sysargs=None):
    """Main entry point to setup a connection node, aka the autopush script"""
    args, parser = _parse_connection(sysargs)
    setup_logging("Autopush", args.human_logs)
    settings = make_settings(
        args,
        port=args.port,
        endpoint_scheme=args.endpoint_scheme,
        endpoint_hostname=args.endpoint_hostname,
        endpoint_port=args.endpoint_port,
        router_scheme="https" if args.router_ssl_key else "http",
        router_hostname=args.router_hostname,
        router_port=args.router_port,
        env=args.env,
        hello_timeout=args.hello_timeout,
    )

    r = RouterHandler
    r.ap_settings = settings
    n = NotificationHandler
    n.ap_settings = settings

    # Internal HTTP notification router
    site = cyclone.web.Application([
        (r"/push/([^\/]+)", r),
        (r"/notif/([^\/]+)(/([^\/]+))?", n),
    ],
        default_host=settings.router_hostname, debug=args.debug,
        log_function=skip_request_logging
    )
    mount_health_handlers(site, settings)

    # Public websocket server
    proto = "wss" if args.ssl_key else "ws"
    factory = WebSocketServerFactory(
        "%s://%s:%s/" % (proto, args.hostname, args.port),
        debug=args.debug,
        debugCodePaths=args.debug,
    )
    factory.protocol = PushServerProtocol
    factory.protocol.ap_settings = settings
    factory.setProtocolOptions(
        webStatus=False,
        maxFramePayloadSize=args.max_message_size,
        maxMessagePayloadSize=args.max_message_size,
        openHandshakeTimeout=5,
        autoPingInterval=args.auto_ping_interval,
        autoPingTimeout=args.auto_ping_timeout,
        maxConnections=args.max_connections,
        closeHandshakeTimeout=args.close_handshake_timeout,
    )
    settings.factory = factory

    settings.metrics.start()

    # Wrap the WebSocket server in a default resource that exposes the
    # `/status` handler, and delegates to the WebSocket resource for all
    # other requests.
    resource = DefaultResource(WebSocketResource(factory))
    resource.putChild("status", StatusResource())
    siteFactory = Site(resource)

    # Start the WebSocket listener.
    if args.ssl_key:
        contextFactory = AutopushSSLContextFactory(args.ssl_key,
                                                   args.ssl_cert)
        if args.ssl_dh_param:
            contextFactory.getContext().load_tmp_dh(args.ssl_dh_param)

        reactor.listenSSL(args.port, siteFactory, contextFactory)
    else:
        reactor.listenTCP(args.port, siteFactory)

    # Start the internal routing listener.
    if args.router_ssl_key:
        contextFactory = AutopushSSLContextFactory(args.router_ssl_key,
                                                   args.router_ssl_cert)
        if args.ssl_dh_param:
            contextFactory.getContext().load_tmp_dh(args.ssl_dh_param)
        reactor.listenSSL(args.router_port, site, contextFactory)
    else:
        reactor.listenTCP(args.router_port, site)

    reactor.suggestThreadPoolSize(50)

    l = task.LoopingCall(periodic_reporter, settings)
    l.start(1.0)

    # Start the table rotation checker/updater
    l = task.LoopingCall(settings.update_rotating_tables)
    l.start(60)
    reactor.run()
コード例 #4
0
    def setUp(self):
        import cyclone.web
        from autobahn.twisted.websocket import WebSocketServerFactory
        from autobahn.twisted.resource import WebSocketResource
        from autopush.main import skip_request_logging
        from autopush.endpoint import (
            EndpointHandler,
            MessageHandler,
            RegistrationHandler,
        )
        from autopush.settings import AutopushSettings
        from autopush.websocket import (
            PushServerProtocol,
            RouterHandler,
            NotificationHandler,
            DefaultResource,
            StatusResource,
        )
        from twisted.web.server import Site

        router_table = os.environ.get("ROUTER_TABLE", "router_int_test")
        storage_table = os.environ.get("STORAGE_TABLE", "storage_int_test")
        message_table = os.environ.get("MESSAGE_TABLE", "message_int_test")

        settings = AutopushSettings(
            hostname="localhost",
            statsd_host=None,
            endpoint_port="9020",
            router_port="9030",
            router_tablename=router_table,
            storage_tablename=storage_table,
            message_tablename=message_table,
        )

        # Websocket server
        self._ws_url = "ws://localhost:9010/"
        factory = WebSocketServerFactory(self._ws_url, debug=False)
        factory.protocol = PushServerProtocol
        factory.protocol.ap_settings = settings
        factory.setProtocolOptions(
            webStatus=False,
            maxFramePayloadSize=2048,
            maxMessagePayloadSize=2048,
            openHandshakeTimeout=5,
        )
        settings.factory = factory
        resource = DefaultResource(WebSocketResource(factory))
        resource.putChild("status", StatusResource())
        self.websocket = reactor.listenTCP(9010, Site(resource))

        # Websocket HTTP router
        # Internal HTTP notification router
        r = RouterHandler
        r.ap_settings = settings
        n = NotificationHandler
        n.ap_settings = settings
        ws_site = cyclone.web.Application(
            [
                (r"/push/([^\/]+)", r),
                (r"/notif/([^\/]+)(/([^\/]+))?", n),
            ],
            default_host=settings.router_hostname,
            log_function=skip_request_logging,
            debug=False,
        )
        self.ws_website = reactor.listenTCP(9030, ws_site)

        # Endpoint HTTP router
        site = cyclone.web.Application(
            [
                (r"/push/([^\/]+)", EndpointHandler,
                 dict(ap_settings=settings)),
                (r"/m/([^\/]+)", MessageHandler, dict(ap_settings=settings)),
                # PUT /register/ => connect info
                # GET /register/uaid => chid + endpoint
                (r"/register(?:/(.+))?", RegistrationHandler,
                 dict(ap_settings=settings)),
            ],
            default_host=settings.hostname,
            log_function=skip_request_logging,
            debug=False,
        )
        self.website = reactor.listenTCP(9020, site)
        self._settings = settings
コード例 #5
0
def connection_main(sysargs=None):
    """Main entry point to setup a connection node, aka the autopush script"""
    args, parser = _parse_connection(sysargs)
    setup_logging("Autopush", args.human_logs)
    settings = make_settings(
        args,
        port=args.port,
        endpoint_scheme=args.endpoint_scheme,
        endpoint_hostname=args.endpoint_hostname,
        endpoint_port=args.endpoint_port,
        router_scheme="https" if args.router_ssl_key else "http",
        router_hostname=args.router_hostname,
        router_port=args.router_port,
        env=args.env,
        hello_timeout=args.hello_timeout,
    )

    r = RouterHandler
    r.ap_settings = settings
    n = NotificationHandler
    n.ap_settings = settings

    # Internal HTTP notification router
    site = cyclone.web.Application([
        (r"/push/([^\/]+)", r),
        (r"/notif/([^\/]+)(/([^\/]+))?", n),
    ],
                                   default_host=settings.router_hostname,
                                   debug=args.debug,
                                   log_function=skip_request_logging)
    mount_health_handlers(site, settings)

    # Public websocket server
    proto = "wss" if args.ssl_key else "ws"
    factory = WebSocketServerFactory(
        "%s://%s:%s/" % (proto, args.hostname, args.port),
        debug=args.debug,
        debugCodePaths=args.debug,
    )
    factory.protocol = PushServerProtocol
    factory.protocol.ap_settings = settings
    factory.setProtocolOptions(
        webStatus=False,
        maxFramePayloadSize=args.max_message_size,
        maxMessagePayloadSize=args.max_message_size,
        openHandshakeTimeout=5,
        autoPingInterval=args.auto_ping_interval,
        autoPingTimeout=args.auto_ping_timeout,
        maxConnections=args.max_connections,
        closeHandshakeTimeout=args.close_handshake_timeout,
    )
    settings.factory = factory

    settings.metrics.start()

    # Wrap the WebSocket server in a default resource that exposes the
    # `/status` handler, and delegates to the WebSocket resource for all
    # other requests.
    resource = DefaultResource(WebSocketResource(factory))
    resource.putChild("status", StatusResource())
    siteFactory = Site(resource)

    # Start the WebSocket listener.
    if args.ssl_key:
        contextFactory = AutopushSSLContextFactory(args.ssl_key, args.ssl_cert)
        if args.ssl_dh_param:
            contextFactory.getContext().load_tmp_dh(args.ssl_dh_param)

        reactor.listenSSL(args.port, siteFactory, contextFactory)
    else:
        reactor.listenTCP(args.port, siteFactory)

    # Start the internal routing listener.
    if args.router_ssl_key:
        contextFactory = AutopushSSLContextFactory(args.router_ssl_key,
                                                   args.router_ssl_cert)
        if args.ssl_dh_param:
            contextFactory.getContext().load_tmp_dh(args.ssl_dh_param)
        reactor.listenSSL(args.router_port, site, contextFactory)
    else:
        reactor.listenTCP(args.router_port, site)

    reactor.suggestThreadPoolSize(50)

    l = task.LoopingCall(periodic_reporter, settings)
    l.start(1.0)
    reactor.run()
コード例 #6
0
def connection_main(sysargs=None, use_files=True):
    """Main entry point to setup a connection node, aka the autopush script"""
    args, parser = _parse_connection(sysargs, use_files)
    log_format = "text" if args.human_logs else "json"
    log_level = args.log_level or ("debug" if args.debug else "info")
    sentry_dsn = bool(os.environ.get("SENTRY_DSN"))
    PushLogger.setup_logging(
        "Autopush",
        log_level=log_level,
        log_format=log_format,
        log_output=args.log_output,
        sentry_dsn=sentry_dsn,
        firehose_delivery_stream=args.firehose_stream_name)
    # Add some entropy to prevent potential conflicts.
    postfix = os.urandom(4).encode('hex').ljust(8, '0')
    settings = make_settings(
        args,
        port=args.port,
        endpoint_scheme=args.endpoint_scheme,
        endpoint_hostname=args.endpoint_hostname,
        endpoint_port=args.endpoint_port,
        router_scheme="https" if args.router_ssl_key else "http",
        router_hostname=args.router_hostname,
        router_port=args.router_port,
        env=args.env,
        hello_timeout=args.hello_timeout,
        preflight_uaid="deadbeef000000000deadbeef" + postfix,
        debug=args.debug,
    )
    if not settings:
        return 1  # pragma: nocover

    # Internal HTTP notification router
    h_kwargs = dict(ap_settings=settings)
    site = cyclone.web.Application([
        (endpoint_paths['route'], RouterHandler, h_kwargs),
        (endpoint_paths['notification'], NotificationHandler, h_kwargs),
    ],
                                   default_host=settings.router_hostname,
                                   debug=args.debug,
                                   log_function=skip_request_logging)
    site.noisy = args.debug
    mount_health_handlers(site, settings)

    # Public websocket server
    proto = "wss" if args.ssl_key else "ws"
    factory = PushServerFactory(
        settings,
        "%s://%s:%s/" % (proto, args.hostname, args.port),
    )
    factory.setProtocolOptions(
        webStatus=False,
        openHandshakeTimeout=5,
        autoPingInterval=args.auto_ping_interval,
        autoPingTimeout=args.auto_ping_timeout,
        maxConnections=args.max_connections,
        closeHandshakeTimeout=args.close_handshake_timeout,
    )

    settings.metrics.start()

    # Wrap the WebSocket server in a default resource that exposes the
    # `/status` handler, and delegates to the WebSocket resource for all
    # other requests.
    resource = DefaultResource(WebSocketResource(factory))
    resource.putChild("status", StatusResource())
    site_factory = Site(resource)
    # Silence starting/stopping messages
    site_factory.noisy = args.debug
    site.noisy = args.debug

    # Start the WebSocket listener.
    if args.ssl_key:
        context_factory = AutopushSSLContextFactory(args.ssl_key,
                                                    args.ssl_cert,
                                                    dh_file=args.ssl_dh_param)
        reactor.listenSSL(args.port, site_factory, context_factory)
    else:
        reactor.listenTCP(args.port, site_factory)

    # Start the internal routing listener.
    if args.router_ssl_key:
        context_factory = AutopushSSLContextFactory(args.router_ssl_key,
                                                    args.router_ssl_cert,
                                                    dh_file=args.ssl_dh_param)
        reactor.listenSSL(args.router_port, site, context_factory)
    else:
        reactor.listenTCP(args.router_port, site)

    reactor.suggestThreadPoolSize(50)
    start_looping_call(1.0, periodic_reporter, settings, factory)
    # Start the table rotation checker/updater
    start_looping_call(60, settings.update_rotating_tables)
    if args.memusage_port:
        create_memusage_site(settings, args.memusage_port, args.debug)
    reactor.run()