コード例 #1
0
ファイル: rpcproxy.py プロジェクト: xdrew/twoost
def make_http_pool_and_agent(params):

    cp_size = params.get('cp_size', 5)
    c_timeout = params.get('c_timeout', 30.0)

    # XXX: more extensibility
    auth = params.get('auth', 'authhmac')
    assert not auth or auth.lower() in ['none', 'authhmac', 'basic', 'digest']

    http_pool = client.HTTPConnectionPool(reactor)
    http_pool._factory = _NoiselessHTTP11ClientFactory
    http_pool.retryAutomatically = False
    http_pool.maxPersistentPerHost = cp_size
    agent = client.Agent(reactor, pool=http_pool, connectTimeout=c_timeout)

    if not auth or auth.lower() == 'none':
        pass
    elif auth.lower() == 'authhmac':
        access_key = params['access_key']
        secret_key = params.get('secret_key')
        agent = authhmac.AuthHMACAgent(agent, access_key, secret_key)
    elif auth.lower() == 'basic':
        username = params['username']
        password = params.get('password')
        agent = httprpc.BasicAuthAgent(agent, username, password)
    else:
        raise AssertionError("unknown %r auth" % auth)

    return http_pool, agent
コード例 #2
0
 def test_repr(self):
     """connection L{repr()} includes endpoint's L{repr()}"""
     pool = client.HTTPConnectionPool(reactor=None)
     ep = DummyEndPoint("this_is_probably_unique")
     d = pool.getConnection("someplace", ep)
     result = self.successResultOf(d)
     representation = repr(result)
     self.assertIn(repr(ep), representation)
コード例 #3
0
    def __init__(self, config=None, reactor=reactor):
        if config is None:
            raise ValueError("Missing configuration")

        super().__init__()
        self.config = config
        self.http_pool = client.HTTPConnectionPool(reactor)
        # TODO refactor path to config
        with open('templates/banner.xml', 'rb') as f:
            self.banner = f.read()
コード例 #4
0
ファイル: async.py プロジェクト: yuzi3150/SeatPJ2
 def build_pool(self):
     """Create connection pool
     """
     # XXX create limited number of instances
     pool = client.HTTPConnectionPool(reactor, persistent=True)
     # 1 connection for each proxy or thread
     # XXX will this take too much memory?
     pool.maxPersistentPerHost = len(
         self.D.settings.proxies) or self.settings.num_threads
     pool.cachedConnectionTimeout = 240
     return pool
コード例 #5
0
    def setUp(self):
        # set up a full master serving HTTP
        yield self.setUpRealDatabase(table_names=['masters', 'objects', 'object_state'],
                                     sqlite_memory=False)

        master = fakemaster.FakeMaster(reactor)

        master.config.db = dict(db_url=self.db_url)
        master.db = dbconnector.DBConnector('basedir')
        yield master.db.setServiceParent(master)
        yield master.db.setup(check_version=False)

        master.config.mq = dict(type='simple')
        master.mq = mqconnector.MQConnector()
        yield master.mq.setServiceParent(master)
        yield master.mq.setup()

        master.data = dataconnector.DataConnector()
        yield master.data.setServiceParent(master)

        master.config.www = dict(
            port='tcp:0:interface=127.0.0.1',
            debug=True,
            auth=auth.NoAuth(),
            authz=authz.Authz(),
            avatar_methods=[],
            logfileName='http.log')
        master.www = wwwservice.WWWService()
        yield master.www.setServiceParent(master)
        yield master.www.startService()
        yield master.www.reconfigServiceWithBuildbotConfig(master.config)
        session = mock.Mock()
        session.uid = "0"
        master.www.site.sessionFactory = mock.Mock(return_value=session)

        # now that we have a port, construct the real URL and insert it into
        # the config.  The second reconfig isn't really required, but doesn't
        # hurt.
        self.url = 'http://127.0.0.1:%d/' % master.www.getPortnum()
        self.url = unicode2bytes(self.url)
        master.config.buildbotURL = self.url
        yield master.www.reconfigServiceWithBuildbotConfig(master.config)

        self.master = master

        # build an HTTP agent, using an explicit connection pool if Twisted
        # supports it (Twisted 13.0.0 and up)
        if hasattr(client, 'HTTPConnectionPool'):
            self.pool = client.HTTPConnectionPool(reactor)
            self.agent = client.Agent(reactor, pool=self.pool)
        else:
            self.pool = None
            self.agent = client.Agent(reactor)
コード例 #6
0
ファイル: httpcheck.py プロジェクト: linearregression/ncolony
def makeService(opt):
    """Make a service

    :params opt: dictionary-like object with 'freq', 'config' and 'messages'
    :returns: twisted.application.internet.TimerService that at opt['freq']
              checks for stale processes in opt['config'], and sends
              restart messages through opt['messages']
    """
    restarter, path = beatcheck.parseConfig(opt)
    pool = client.HTTPConnectionPool(reactor)
    agent = client.Agent(reactor=reactor, pool=pool)
    settings = Settings(reactor=reactor, agent=agent)
    states = {}
    checker = functools.partial(check, settings, states, path)
    httpcheck = tainternet.TimerService(opt['freq'], run, restarter, checker)
    httpcheck.setName('httpcheck')
    return heart.wrapHeart(httpcheck)
コード例 #7
0
    def setUp(self):
        # set up a full master serving HTTP
        yield self.setUpRealDatabase(table_names=['masters'],
                                     sqlite_memory=False)

        master = fakemaster.FakeMaster()

        master.config.db = dict(db_url=self.db_url)
        master.db = dbconnector.DBConnector(master, 'basedir')
        yield master.db.setup(check_version=False)

        master.config.mq = dict(type='simple')
        master.mq = mqconnector.MQConnector(master)
        master.mq.setup()

        master.data = dataconnector.DataConnector(master)

        master.config.www = dict(port='tcp:0:interface=127.0.0.1',
                                 debug=True,
                                 auth=auth.NoAuth(),
                                 url="not yet known",
                                 avatar_methods=[])
        master.www = wwwservice.WWWService(master)
        yield master.www.startService()
        yield master.www.reconfigService(master.config)

        # now that we have a port, construct the real URL and insert it into
        # the config.  The second reconfig isn't really required, but doesn't
        # hurt.
        self.url = 'http://127.0.0.1:%d/' % master.www.getPortnum()
        master.config.www['url'] = self.url
        yield master.www.reconfigService(master.config)

        self.master = master

        # build an HTTP agent, using an explicit connection pool if Twisted
        # supports it (Twisted 13.0.0 and up)
        if hasattr(client, 'HTTPConnectionPool'):
            self.pool = client.HTTPConnectionPool(reactor)
            self.agent = client.Agent(reactor, pool=self.pool)
        else:
            self.pool = None
            self.agent = client.Agent(reactor)
コード例 #8
0
    def __init__(
        self,
        reactor: IReactorTCP,
        endpoint_url: str,
        id_generator: Optional[Callable[[], Iterator[Union[
            str, int]]]] = lambda: count(),
    ):
        """ Create a client for the Bitcoin RPC API.

        Arguments:

        - endpoint_url: example: 'http://*****:*****@host:port/'
        - id_generator: function/lambda that when called returns an iterator of ids, note: iterator must never stop
        """
        from base64 import b64encode
        from urllib.parse import urlparse

        self.log = logger.new()

        quietPool = client.HTTPConnectionPool(reactor)
        quietPool._factory = QuietHTTP11ClientFactory
        super().__init__(reactor, pool=quietPool)

        url = urlparse(endpoint_url)
        self._url = f'{url.scheme or "http"}://{url.hostname}:{url.port or 8332}{url.path or "/"}'.encode(
            'ascii')
        self._base_headers = {
            'User-Agent': [self.USER_AGENT],
        }
        auth = ':'.join([url.username or '', url.password or ''
                         ]) if url.username or url.password else None
        if auth:
            self._base_headers['Authorization'] = [
                'Basic ' + b64encode(auth.encode('ascii')).decode('ascii')
            ]
        self._iter_id = id_generator and id_generator() or None
コード例 #9
0
qeXUWaUr/GcZOfqTGBhs3t0lig4zFEfC7wFQeeT9adGnwKziV28CAwEAAaOBozCB
oDAfBgNVHSMEGDAWgBRI5mj5K9KylddH2CMgEE8zmJCf1DAdBgNVHQ4EFgQUv8Aw
6/VDET5nup6R+/xq2uNrEiQwEgYDVR0TAQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8E
BAMCAQYwOgYDVR0fBDMwMTAvoC2gK4YpaHR0cDovL2NybC5nZW90cnVzdC5jb20v
Y3Jscy9zZWN1cmVjYS5jcmwwDQYJKoZIhvcNAQEFBQADgYEAvprjecFG+iJsxzEF
ZUNgujFQodUovxOWZshcnDW7fZ7mTlk3zpeVJrGPZzhaDhvuJjIfKqHweFB7gwB+
ARlIjNvrPq86fpVg0NOTawALkSqOUMl3MynBQO+spR7EHcRbADQ/JemfTEh2Ycfl
vZqhEFBfurZkX0eTANq98ZvVfpg=
-----END CERTIFICATE-----"""))

# `t.w.client.HTTPConnectionPool` isn't available in Twisted-12.0.0
# (see ticket #11219: https://bugs.torproject.org/11219):
_connectionPoolAvailable = _twistedversion >= Version('twisted', 12, 1, 0)
if _connectionPoolAvailable:
    logging.info("Using HTTPConnectionPool for reCaptcha API server.")
    _pool = client.HTTPConnectionPool(reactor, persistent=False)
    _pool.maxPersistentPerHost = 5
    _pool.cachedConnectionTimeout = 30
    _agent = client.Agent(reactor, pool=_pool)
else:
    logging.warn("Twisted-%s is too old for HTTPConnectionPool! Disabling..."
                 % _twistedversion.short())
    _pool = None
    _agent = client.Agent(reactor)


# Twisted>=14.0.0 changed the way in which hostname verification works.
if _twistedversion >= Version('twisted', 14, 0, 0):
    from twisted.internet._sslverify import OpenSSLCertificateAuthorities

    class RecaptchaOpenSSLCertificateAuthorities(OpenSSLCertificateAuthorities):
コード例 #10
0
def main():
    config = SafeConfigParser()
    config.read("relaybot.config")
    defaults = config.defaults()

    # Webhook stuff
    webhooks = resource.Resource()
    pool = client.HTTPConnectionPool(reactor)
    agent = client.Agent(reactor, pool=pool)

    for section in config.sections():

        def get(option):
            if option in defaults or config.has_option(section, option):
                return config.get(section, option) or defaults[option]
            else:
                return None

        options = {'servername': section}
        for option in [
                "timeout", "host", "port", "nick", "channel", "channels",
                "heartbeat", "password", "username", "realname", "mode", "ssl",
                "fingerprint", "nickcolor", "topicsync"
        ]:
            options[option] = get(option)

        mode = get("mode")

        #Not using endpoints pending http://twistedmatrix.com/trac/ticket/4735
        #(ReconnectingClientFactory equivalent for endpoints.)
        factory = None
        if mode == "Default":
            factory = RelayFactory
        elif mode == "FLIP":
            factory = FLIPFactory
        elif mode == "NickServ":
            factory = NickServFactory
            options["nickServPassword"] = get("nickServPassword")
        elif mode == "ReadOnly":
            factory = ReadOnlyFactory
            options["nickServPassword"] = get("nickServPassword")
        # RelayByCommand: only messages with <nickname>: will be relayed.
        elif mode == "RelayByCommand":
            factory = CommandFactory
        elif mode == "Webhooks":
            options['webhookNonce'] = get('webhookNonce')
            options['outgoingWebhook'] = get('outgoingWebhook')
            webhooks.putChild(options['webhookNonce'], Webhook(agent, options))
            continue

        factory = factory(options)
        if options['ssl'] == "True":
            if options['fingerprint']:
                ctx = certoptions(fingerprint=options['fingerprint'],
                                  verifyDepth=0)
                reactor.connectSSL(options['host'], int(options['port']),
                                   factory, ctx, int(options['timeout']))
            else:
                reactor.connectSSL(options['host'], int(options['port']),
                                   factory, ssl.ClientContextFactory(),
                                   int(options['timeout']))
        else:
            reactor.connectTCP(options['host'], int(options['port']), factory,
                               int(options['timeout']))

    # Start incoming webhook server
    if 'webhook' in defaults:
        serverFromString(reactor,
                         defaults['webhook']).listen(server.Site(webhooks))

    reactor.callWhenRunning(signal, SIGINT, handler)