Ejemplo n.º 1
0
    def _start_futures_socket(self,
                              path,
                              callback,
                              prefix='stream?streams=',
                              TESTNETFLAG=False):
        if path in self._conns:
            return False

        if TESTNETFLAG:
            factory_url = self.TESTNET_FUTURES_STREAM_URL + prefix + path
        else:
            factory_url = self.FSTREAM_URL + prefix + path

        # factory_url = self.FSTREAM_URL + prefix + path
        factory = BinanceClientFactory(factory_url)
        factory.protocol = BinanceClientProtocol
        factory.callback = callback
        factory.reconnect = True
        context_factory = ssl.ClientContextFactory()

        self._conns[path] = connectWS(factory, context_factory)
        return path
Ejemplo n.º 2
0
    def onClose(self, wasClean, code, reason):

        print("onClose")
        print("WebSocket connection closed: {0}".format(reason), "code: ",
              code, "clean: ", wasClean, "reason: ", reason)
        self.summary[self.uttNumber]['status']['code'] = code
        self.summary[self.uttNumber]['status']['reason'] = reason
        if (code == 1000):
            self.summary[self.uttNumber]['status']['successful'] = True

        # create a new WebSocket connection if there are still utterances in the queue that need to be processed
        self.queue.task_done()

        if self.factory.prepareUtterance() == False:
            return

        # SSL client context: default
        if self.factory.isSecure:
            contextFactory = ssl.ClientContextFactory()
        else:
            contextFactory = None
        connectWS(self.factory, contextFactory)
Ejemplo n.º 3
0
def main():
    sys.path.append(os.path.join(sys.path[0], 'lib'))

    config = read_config()
    # if config not found or can't validate it, exit with error
    if not config or not validate_config(config):
        sys.exit(1)

    init_logging(config.get('logging', {}))

    factory = PyFiBotFactory(config)
    for network, settings in config['networks'].items():
        # settings = per network, config = global
        nick = settings.get('nick', None) or config['nick']
        linerate = settings.get('linerate', None) or config.get(
            'linerate', None)
        password = settings.get('password', None)
        is_ssl = bool(settings.get('is_ssl', False))
        port = int(settings.get('port', 6667))

        # normalize channel names to prevent internal confusion
        chanlist = []
        for channel in settings['channels']:
            if channel[0] not in '&#!+':
                channel = '#' + channel
            chanlist.append(channel)

        server_name = settings['server']

        factory.createNetwork((server_name, port), network, nick, chanlist,
                              linerate, password, is_ssl)
        if is_ssl:
            log.info("connecting via SSL to %s:%d" % (server_name, port))
            reactor.connectSSL(server_name, port, factory,
                               ssl.ClientContextFactory())
        else:
            log.info("connecting to %s:%d" % (server_name, port))
            reactor.connectTCP(server_name, port, factory)
    reactor.run()
Ejemplo n.º 4
0
    def __init__(self, url, outputfile, contextFactory=None, *args, **kwargs):
        if hasattr(client, '_parse'):
            scheme, host, port, path = client._parse(url)
        else:
            from twisted.web.client import URI
            uri = URI.fromBytes(url)
            scheme = uri.scheme
            host = uri.host
            port = uri.port
            path = uri.path
# ======= another twisted fix possibility
#		parsed = urlparse(url)
#		scheme = parsed.scheme
#		host = parsed.hostname
#		port = parsed.port or (443 if scheme == 'https' else 80)

        self.factory = HTTPProgressDownloader(url, outputfile, *args, **kwargs)
        if scheme == "https":
            self.connection = reactor.connectSSL(host, port, self.factory,
                                                 ssl.ClientContextFactory())
        else:
            self.connection = reactor.connectTCP(host, port, self.factory)
Ejemplo n.º 5
0
    def callback_test(self,
                      func,
                      wait_notifications=0,
                      timeout=None,
                      caps=PREFERRED_CAP,
                      add_default_callbacks=False,
                      use_ssl=False,
                      **kwargs):
        """Create a client and call callback on connection."""
        if add_default_callbacks:

            @wraps(func)
            def wrapped(client, **kwargs):
                """Wrapper which wires up test_done/test_fail."""
                d = func(client, **kwargs)
                d.addCallbacks(client.test_done, client.test_fail)
                return d
        else:
            wrapped = func
        f = FactoryHelper(wrapped,
                          factory=self.buildFactory(),
                          wait_notifications=wait_notifications,
                          timeout=timeout,
                          caps=caps,
                          **kwargs)
        # there are 3 ways to connect to a server.
        # tcp and ssl will work in the tests
        if use_ssl:
            reactor.connectSSL("localhost", self.ssl_port, f,
                               ssl.ClientContextFactory())
        else:
            reactor.connectTCP("localhost", self.port, f)

        # https connect requires a working proxy and a server on
        # the default port running (we are not setting this up for
        # automated testing yet)
        # proxy_tunnel.connectHTTPS('localhost', 3128, "localhost", 20101, f,
        #     user="******", passwd="test")
        return f.test_deferred
Ejemplo n.º 6
0
    def connect(self):
        """
        Connect to an instant messaging server.

        @return: True on success
        """
        if self.isConnected():
            return

        # start a fresh connection
        if self.clientConnection:
            self.clientConnection.disconnect()

        status_bar.status_message('connecting to %s' % self.str())
        if self.useSSL:
            self.logger.info('connecting to %s with ssl' % self.str())
            self.clientConnection = reactor.connectSSL(
                self.host, self.port, self, ssl.ClientContextFactory())
        else:
            self.logger.info('connecting to %s' % self.str())
            self.clientConnection = reactor.connectTCP(self.host, self.port,
                                                       self)
Ejemplo n.º 7
0
    def test_openSSLBuffering(self):
        serverProto = self.serverProto = SingleLineServerProtocol()
        clientProto = self.clientProto = RecordingClientProtocol()

        server = protocol.ServerFactory()
        client = self.client = protocol.ClientFactory()

        server.protocol = lambda: serverProto
        client.protocol = lambda: clientProto

        sCTX = ssl.DefaultOpenSSLContextFactory(certPath, certPath)
        cCTX = ssl.ClientContextFactory()

        port = reactor.listenSSL(0, server, sCTX, interface="127.0.0.1")
        self.addCleanup(port.stopListening)

        clientConnector = reactor.connectSSL("127.0.0.1",
                                             port.getHost().port, client, cCTX)
        self.addCleanup(clientConnector.disconnect)

        return clientProto.deferred.addCallback(self.assertEqual,
                                                b"+OK <some crap>\r\n")
Ejemplo n.º 8
0
    def _start_socket(self, path, callback, prefix='ws/'):
        if path in self._conns:
            return False

        factory_url = self.STREAM_URL + prefix + path
        factory = BinanceClientFactory(factory_url)
        factory.protocol = BinanceClientProtocol
        factory.callback = callback
        factory.reconnect = True

        proxy_url = os.environ.get("PROXY_URL")
        if proxy_url:
            host, port = proxy_url.split(":")
            factory.proxy = {'host': host, 'port': port}

        if factory.host.startswith('testnet.binance'):
            context_factory = ssl.optionsForClientTLS(factory.host)
        else:
            context_factory = ssl.ClientContextFactory()

        self._conns[path] = connectWS(factory, context_factory)
        return path
Ejemplo n.º 9
0
    def got_results(results):
        for r in results:
            if name == '-'.join(r.name_short):
                factory = HTTPClientFactory(r.url)

                if factory.scheme == 'https':
                    if ssl:
                        reactor.connectSSL(factory.host, factory.port, factory,
                                           ssl.ClientContextFactory())
                    else:
                        d_result.errback(
                            Exception(
                                "{} is not available because this installation does not have SSL support!"
                                .format(name)))
                else:
                    reactor.connectTCP(factory.host, factory.port, factory)

                factory.deferred.addCallback(lambda d: got_data(factory, d))
                factory.deferred.addErrback(d_result.errback)
                return

        d_result.errback(Exception("{} is not available!".format(name)))
Ejemplo n.º 10
0
    def handleStatus_301(self):
        l = self.headers.get('location')
        if not l:
            self.handleStatusDefault()
            return
        url = l[0]
        if self.followRedirect:
            scheme, host, port, path = \
                _parse(url, defaultPort=self.transport.getPeer().port)

            self.factory._redirectCount += 1
            if self.factory._redirectCount >= self.factory.redirectLimit:
                err = error.InfiniteRedirection(
                    self.status,
                    'Infinite redirection detected',
                    location=url)
                self.factory.noPage(failure.Failure(err))
                self.quietLoss = True
                self.transport.loseConnection()
                return

            self.factory.setURL(url)

            if self.factory.scheme == 'https':
                from twisted.internet import ssl
                contextFactory = ssl.ClientContextFactory()
                reactor.connectSSL(self.factory.host, self.factory.port,
                                   self.factory, contextFactory)
            else:
                reactor.connectTCP(self.factory.host, self.factory.port,
                                   self.factory)
        else:
            self.handleStatusDefault()
            self.factory.noPage(
                failure.Failure(
                    error.PageRedirect(
                        self.status, self.message, location = url)))
        self.quietLoss = True
        self.transport.loseConnection()
Ejemplo n.º 11
0
    def download(self, url, fakeoutfile, outputfile, *args, **kwargs):
        """
        """
        try:
            parsed = compat.urllib_parse.urlparse(url)
            scheme = parsed.scheme
            host = parsed.hostname
            port = parsed.port or (443 if scheme == 'https' else 80)
            path = parsed.path or '/'
            if scheme != b'http' and scheme != b'https':
                raise NotImplementedError
        except:
            self.errorWrite(
                'curl: (1) Protocol "{}" not supported or disabled in libcurl\n'
                .format(scheme))
            self.exit()
            return None

        factory = HTTPProgressDownloader(self, fakeoutfile, url, outputfile,
                                         *args, **kwargs)
        out_addr = None
        if CONFIG.has_option('honeypot', 'out_addr'):
            out_addr = (CONFIG.get('honeypot', 'out_addr'), 0)

        if scheme == 'https':
            contextFactory = ssl.ClientContextFactory()
            contextFactory.method = SSL.SSLv23_METHOD
            reactor.connectSSL(host,
                               port,
                               factory,
                               contextFactory,
                               bindAddress=out_addr)
        else:  # Can only be http
            self.connection = reactor.connectTCP(host,
                                                 port,
                                                 factory,
                                                 bindAddress=out_addr)

        return factory.deferred
Ejemplo n.º 12
0
    def collect(self, config):

        ds0 = config.datasources[0]

        credentials = (ds0.zWBEMUsername, ds0.zWBEMPassword)

        factory = ExecQuery(credentials,
                            ds0.params['query_language'],
                            ds0.params['query'],
                            namespace=ds0.params['namespace'])

        if ds0.zWBEMUseSSL:
            reactor.connectSSL(host=ds0.manageIp,
                               port=int(ds0.zWBEMPort),
                               factory=factory,
                               contextFactory=ssl.ClientContextFactory())
        else:
            reactor.connectTCP(host=ds0.manageIp,
                               port=int(ds0.zWBEMPort),
                               factory=factory)

        return factory.deferred
Ejemplo n.º 13
0
    def connectionMade(self):
        try:
            # we'll handle all the handshake logic here
            self.peer = yield connectProtocol(self.factory.dest,
                                              MySQLForwardClientProtocol(self))
            server_handshake = yield self.peer.read_packet()
            server_handshake = self.peer.modify_server_handshake(
                server_handshake)
            self.transport.write(server_handshake)

            # ok, now get the client handshake
            client_handshake = yield self.read_packet()

            # use the client handshake to generate an SSL request
            ssl_request = self.make_ssl_request(client_handshake)
            self.peer.transport.write(ssl_request)
            self.peer.transport.startTLS(ssl.ClientContextFactory())

            # then send the original client handshake packet. For this
            # and the next couple packets, the server and client will
            # have different ideas of what the packet sequence IDs
            # should be (we just sent an extra packet to the server
            # and the client doesn't know about it). So every client
            # -> server packet needs to have its seq_id incremented;
            # server -> client packets need to have them decremented
            client_handshake = self.modify_seq(client_handshake, 1)
            self.peer.transport.write(client_handshake)

            # send packets back and forth until the client resets
            # the sequence numbers
            yield self.forward_until_seq_reset()

            # start forwarding traffic with no further manipulation
            self.stop_buffering()
            self.peer.stop_buffering()

        except Exception:
            self.transport.loseConnection()
            log.err()
Ejemplo n.º 14
0
    def connect(self, protocolFactory):
        """
        Return a deferred firing when the SOCKS connection is established.
        """

        try:
            # Connect with an intermediate SOCKS factory/protocol,
            # which then hands control to the provided protocolFactory
            # once a SOCKS connection has been established.
            f = self.factory()
            f.postHandshakeEndpoint = self._endpoint
            f.postHandshakeFactory = protocolFactory
            f.handshakeDone = defer.Deferred()
            wf = _WrappingFactory(f)
            if self._ssl:
                self._reactor.connectTCP(self._host, self._port, wf,
                                         ssl.ClientContextFactory())
            else:
                self._reactor.connectTCP(self._host, self._port, wf)
            return f.handshakeDone
        except:
            return defer.fail()
Ejemplo n.º 15
0
def connectDeviceHive(device_hive_url, factory):
    """
    Connects a DeviceHive client.
    
    @type device_hive_url: C{str}
    @param device_hive_url: 
    
    @type factory: C{object}
    @param factory: a factory object which implements C{IProtoFactory} interface.
    """
    url, host, port = parse_url(device_hive_url)
    factory.url = url
    factory.host = host
    factory.port = port
    if url.startswith('http://'):
        log.msg('Establishing NON-SECURE connection with {0}.'.format(url))
        reactor.connectTCP(host, port, factory)
    else:
        ccf = ssl.ClientContextFactory()
        ccf.method = SSL.SSLv23_METHOD
        log.msg('Establishing SECURE connection with {0}.'.format(url))
        reactor.connectSSL(host, port, factory, ccf)
Ejemplo n.º 16
0
    def startStt(self, audioFd):

        # logging
        log.startLogging(sys.stdout)

        hostname = "stream.watsonplatform.net"
        headers = {}

        # authentication header
        if self.configData["tokenauth"]:
            headers['X-Watson-Authorization-Token'] = Utils.getAuthenticationToken("https://" + hostname,
                                                                                   'speech-to-text',
                                                                                   self.configData["user"],
                                                                                   self.configData["password"])
        else:
            string = self.configData["user"] + ":" + self.configData["password"]
            headers["Authorization"] = "Basic " + base64.b64encode(string)

        # create a WS server factory with our protocol
        url = "wss://" + hostname + "/speech-to-text/api/v1/recognize?model=" + self.configData["model"]
        summary = {}
        factory = WSInterfaceFactory(audioFd,
                                     summary,
                                     self.CONTENT_TYPE,
                                     self.configData["model"],
                                     url,
                                     headers,
                                     debug=False)
        factory.setListeners(self.listeners)
        factory.protocol = WSInterfaceProtocol

        if factory.isSecure:
            contextFactory = ssl.ClientContextFactory()
        else:
            contextFactory = None
        connectWS(factory, contextFactory)

        reactor.run()
Ejemplo n.º 17
0
def connectWS(factory, contextFactory=None, timeout=30, bindAddress=None):
    """
    Establish WebSocket connection to a server. The connection parameters like target
    host, port, resource and others are provided via the factory.

    :param factory: The WebSocket protocol factory to be used for creating client protocol instances.
    :type factory: An :class:`autobahn.websocket.WebSocketClientFactory` instance.
    :param contextFactory: SSL context factory, required for secure WebSocket connections ("wss").
    :type contextFactory: A `twisted.internet.ssl.ClientContextFactory <http://twistedmatrix.com/documents/current/api/twisted.internet.ssl.ClientContextFactory.html>`_ instance.
    :param timeout: Number of seconds to wait before assuming the connection has failed.
    :type timeout: int
    :param bindAddress: A (host, port) tuple of local address to bind to, or None.
    :type bindAddress: tuple

    :returns: The connector.
    :rtype: An object which implements `twisted.interface.IConnector <http://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.IConnector.html>`_.
    """
    # lazy import to avoid reactor install upon module import
    if hasattr(factory, 'reactor'):
        reactor = factory.reactor
    else:
        from twisted.internet import reactor

    if factory.isSecure:
        if contextFactory is None:
            # create default client SSL context factory when none given
            from twisted.internet import ssl
            contextFactory = ssl.ClientContextFactory()

    if factory.proxy is not None:
        factory.contextFactory = contextFactory
        conn = reactor.connectTCP(factory.proxy[u'host'], factory.proxy[u'port'], factory, timeout, bindAddress)
    else:
        if factory.isSecure:
            conn = reactor.connectSSL(factory.host, factory.port, factory, contextFactory, timeout, bindAddress)
        else:
            conn = reactor.connectTCP(factory.host, factory.port, factory, timeout, bindAddress)
    return conn
Ejemplo n.º 18
0
def getPage(url, contextFactory=None, *args, **kwargs):
    if hasattr(client, '_parse'):
        scheme, host, port, path = _parse(url)
    else:
        # _URI class renamed to URI in 15.0.0
        try:
            from twisted.web.client import _URI as URI
        except ImportError:
            from twisted.web.client import URI
        uri = URI.fromBytes(url)
        scheme = uri.scheme
        host = uri.host
        port = uri.port
        path = uri.path
    factory = LimitedHTTPClientFactory(url, *args, **kwargs)
    if scheme == 'https':
        from twisted.internet import ssl
        if contextFactory is None:
            contextFactory = ssl.ClientContextFactory()
        reactor.connectSSL(host, port, factory, contextFactory)
    else:
        reactor.connectTCP(host, port, factory)
    return factory.deferred
Ejemplo n.º 19
0
    def testOpenSSLBuffering(self):
        serverProto = self.serverProto = SingleLineServerProtocol()
        clientProto = self.clientProto = RecordingClientProtocol()

        server = protocol.ServerFactory()
        client = self.client = protocol.ClientFactory()

        server.protocol = lambda: serverProto
        client.protocol = lambda: clientProto
        client.buffer = []

        sCTX = ssl.DefaultOpenSSLContextFactory(certPath, certPath)
        cCTX = ssl.ClientContextFactory()

        port = self.port = reactor.listenSSL(0, server, sCTX, interface='127.0.0.1')
        reactor.connectSSL('127.0.0.1', port.getHost().port, client, cCTX)

        i = 0
        while i < 5000 and not client.buffer:
            i += 1
            reactor.iterate()

        self.assertEquals(client.buffer, ["+OK <some crap>\r\n"])
Ejemplo n.º 20
0
    def __init__(self, url, outputfile, contextFactory=None, *args, **kwargs):
        if hasattr(client, '_parse'):
            scheme, host, port, path = client._parse(url)
        else:
            try:
                from twisted.web.client import _URI as URI
            except ImportError:
                from twisted.web.client import URI
            uri = URI.fromBytes(url)
            scheme = uri.scheme
            host = uri.host
            port = uri.port or (443 if scheme == 'https' else 80)
            path = uri.path

        self.factory = HTTPProgressDownloader(url, outputfile, *args, **kwargs)
        if scheme == "https":
            from twisted.internet import ssl
            if contextFactory is None:
                contextFactory = ssl.ClientContextFactory()
            self.connection = reactor.connectSSL(host, port, self.factory,
                                                 contextFactory)
        else:
            self.connection = reactor.connectTCP(host, port, self.factory)
Ejemplo n.º 21
0
def downloadPageFactory(url, file, progressCallback=None,
                        agent="BitTorrent client",
                        bindAddress=None,
                        contextFactory=None):
    """Download a web page to a file.

    @param file: path to file on filesystem, or file-like object.
    """
    scheme, host, port, path = client._parse(url)
    factory = ProgressHTTPDownloader(url, file,
                                     progressCallback=progressCallback,
                                     agent=agent,
                                     supportPartial=0)
    if scheme == 'https':
        from twisted.internet import ssl
        if contextFactory is None:
            contextFactory = ssl.ClientContextFactory()
        reactor.connectSSL(host, port, factory, contextFactory,
                           bindAddress=bindAddress)
    else:
        reactor.connectTCP(host, port, factory,
                           bindAddress=bindAddress)
    return factory
Ejemplo n.º 22
0
    def _sendmail(self, to_addrs, msg):
        # Import twisted.mail here because it is not available in python3
        from twisted.mail.smtp import ESMTPSenderFactory
        msg = BytesIO(msg)
        d = defer.Deferred()
        factory = ESMTPSenderFactory(self.smtpuser,
                                     self.smtppass,
                                     self.mailfrom,
                                     to_addrs,
                                     msg,
                                     d,
                                     heloFallback=True,
                                     requireAuthentication=False,
                                     requireTransportSecurity=self.smtptls)
        factory.noisy = False

        if self.smtpssl:
            reactor.connectSSL(self.smtphost, self.smtpport, factory,
                               ssl.ClientContextFactory())
        else:
            reactor.connectTCP(self.smtphost, self.smtpport, factory)

        return d
Ejemplo n.º 23
0
def connect(port):
    factory = example.ProtocolFactory()
    factory.address = "www.google.com"
    factory.port = port

    configuration = \
    {
        "PROXY_SERVERS":
        [
            {
                "TYPE": "SOCKS5",
                "ADDRESS": "127.0.0.1",
                "PORT": 1080
            }
        ]
    }

    contextFactory = None
    if factory.port == 443:
        contextFactory = ssl.ClientContextFactory()

    tunnel = proxy_server.createTunnel(configuration)
    tunnel.connect(factory.address, factory.port, factory, contextFactory)
Ejemplo n.º 24
0
	def __init__(self, url, outputfile, contextFactory=None, *args, **kwargs):
		if hasattr(client, '_parse'):
			scheme, host, port, path = client._parse(url)
		else:
			# _URI class renamed to URI in 15.0.0
			try:
				from twisted.web.client import _URI as URI
			except ImportError:
				from twisted.web.client import URI
			# twisted wants bytes
			if isinstance(url, str):
				url = url.encode("UTF-8")
			uri = URI.fromBytes(url)
			scheme = uri.scheme
			host = uri.host
			port = uri.port
			path = uri.path

		self.factory = HTTPProgressDownloader(url, outputfile, *args, **kwargs)
		if scheme == "https":
			self.connection = reactor.connectSSL(host, port, self.factory, ssl.ClientContextFactory())
		else:
			self.connection = reactor.connectTCP(host, port, self.factory)
Ejemplo n.º 25
0
def get_json(url, *args, **kwargs):
    """
    :param json: JSON data
    :param url:
    :param args:
    :param kwargs:
    :return:
    """
    j = kwargs.pop('json', None)
    if j:
        kwargs['postdata'] = as_json(j)
    kwargs.setdefault('agent', 'Twisted JSON Adapter')
    uri = URI.fromBytes(url)
    factory = HTTPClientFactory(url, *args, **kwargs)
    factory.noisy = 0
    if uri.scheme == b'https':
        from twisted.internet import ssl
        contextFactory = ssl.ClientContextFactory()
        reactor.connectSSL(nativeString(uri.host), uri.port, factory,
                           contextFactory)
    else:
        reactor.connectTCP(nativeString(uri.host), uri.port, factory)
    return factory.deferred.addCallback(_json_loads)
Ejemplo n.º 26
0
def getPage(url, bindAddress=None, *arg, **kw):
    # reimplemented here to insert bindAddress

    uri = URI.fromBytes(url)
    scheme = uri.scheme
    host = uri.host
    port = uri.port
    path = uri.path

    factory = HTTPClientFactory(url, *arg, **kw)
    factory.noisy = False

    if scheme == 'https':
        from twisted.internet import ssl
        context = ssl.ClientContextFactory()
        reactor.connectSSL(host,
                           port,
                           factory,
                           context,
                           bindAddress=bindAddress)
    else:
        reactor.connectTCP(host, port, factory, bindAddress=bindAddress)
    return factory.deferred
Ejemplo n.º 27
0
 def __init__(self, poolSize=10, agent=None, stopWhenDone=False, grow=5.0):
     self.sslContext = ssl.ClientContextFactory()
     # The base fetcher keeps track of requests as a list
     self.requests = []
     # A limit on the number of requests that can be in flight
     # at the same time
     self.poolSize = poolSize
     # Keeping tabs on counts. The lock is necessary to avoid
     # contentious access to numFlight, processed, remaining.
     # numFlight => the number of requests currently active
     # processed => the number of requests completed
     # remaining => how many requests are left
     self.lock = threading.RLock()
     self.numFlight = 0
     self.processed = 0
     self.remaining = 0
     # Use this user agent when making requests
     self.agent = agent or 'rogerbot/1.0'
     self.stopWhenDone = stopWhenDone
     self.period = grow
     # The object that represents our repeated call to grow
     self.growLater = reactor.callLater(self.period, self.grow,
                                        self.poolSize)
Ejemplo n.º 28
0
 def connect(self):
     factory = self.factoryClass(self._parameters, self._bindings)
     if self._parameters.ssl_options:
         serv = SSLClient(
             host=self._parameters.host,
             port=self._parameters.port,
             factory=factory,
             contextFactory=ssl.ClientContextFactory(),
         )
     else:
         serv = TCPClient(
             host=self._parameters.host,
             port=self._parameters.port,
             factory=factory,
         )
     serv.factory = factory
     name = '{}{}:{}'.format(
         'ssl:' if self._parameters.ssl_options else '',
         self._parameters.host,
         self._parameters.port,
     )
     serv.setName(name)
     serv.setServiceParent(self)
Ejemplo n.º 29
0
    def __init__(self, host, nick, channels, port=6667, allowForce=False,
                 categories=None, password=None, notify_events={},
                 noticeOnChannel = False, showBlameList = True,
                 useSSL=False):
        base.StatusReceiverMultiService.__init__(self)

        assert allowForce in (True, False) # TODO: implement others

        # need to stash these so we can detect changes later
        self.host = host
        self.port = port
        self.nick = nick
        self.channels = channels
        self.password = password
        self.allowForce = allowForce
        self.categories = categories
        self.notify_events = notify_events
        log.msg('Notify events %s' % notify_events)
        self.f = IrcStatusFactory(self.nick, self.password,
                                  self.channels, self.categories, self.notify_events,
                                  noticeOnChannel = noticeOnChannel,
                                  showBlameList = showBlameList)

        # don't set up an actual ClientContextFactory if we're running tests.
        if self.in_test_harness:
            return

        if useSSL:
            # SSL client needs a ClientContextFactory for some SSL mumbo-jumbo
            if not have_ssl:
                raise RuntimeError("useSSL requires PyOpenSSL")
            cf = ssl.ClientContextFactory()
            c = internet.SSLClient(self.host, self.port, self.f, cf)
        else:
            c = internet.TCPClient(self.host, self.port, self.f)

        c.setServiceParent(self)
Ejemplo n.º 30
0
        def onClose(self, wasClean, code, reason):

            log.msg("onClose")

            if not code == 1000:
                log.msg("WebSocket connection error: {0}".format(reason),
                        "code: ", code, "clean: ", wasClean, "reason: ",
                        reason)
                print(
                    "Stopping websocket reactor due to an error. Connection code: {0}. Warning: The websocket reactor can be called only once per application. You may need to restart the interpreter before running again."
                    .format(code))
                #clear the queue, shutdown the reactor
                self.factory.killReactor(wasClean, code, reason)
                return

            else:
                log.msg("WebSocket connection closed: {0}".format(reason),
                        "code: ", code, "clean: ", wasClean, "reason: ",
                        reason)
                print("WebSocket connection closed. Status code: {0}".format(
                    code))

            # create a new WebSocket connection if there are still
            # utterances in the queue that need to be processed
            self.queue.task_done()

            if self.factory.prepareUtterance() is False:
                return

            # SSL client context: default
            if self.factory.isSecure:
                contextFactory = ssl.ClientContextFactory()
            else:
                contextFactory = None

            # start new websocket connection, continue loop
            connectWS(self.factory, contextFactory)