예제 #1
0
  def __init__(
      self, url, headers, ignore_certs, proxy_info, on_data, on_close):
    self._on_data = on_data
    self._on_close = on_close
    self._proxy_info = proxy_info
    self._receiving_thread = None

    ca_certs = utils.CheckCACertsFile(ignore_certs)
    self._sslopt = {'cert_reqs': ssl.CERT_REQUIRED,
                    'ca_certs': ca_certs}
    if ignore_certs:
      self._sslopt['cert_reqs'] = ssl.CERT_NONE
      self._sslopt['check_hostname'] = False

    caa_config = context_aware.Config()
    if caa_config:
      cert_path = caa_config.encrypted_client_cert_path
      log.debug('Using client certificate %s', cert_path)
      self._sslopt['certfile'] = cert_path
      self._sslopt['password'] = caa_config.encrypted_client_cert_password

    # Disable most of random logging in websocket library itself except in DEBUG
    if log.GetVerbosity() != logging.DEBUG:
      logging.getLogger('websocket').setLevel(logging.CRITICAL)

    self._is_closed = False
    self._error_msg = ''
    self._websocket = websocket.WebSocketApp(
        url, header=headers, on_close=self._OnClose, on_data=self._OnData,
        on_error=self._OnError, subprotocols=[utils.SUBPROTOCOL_NAME])
예제 #2
0
    def __init__(self, url, headers, ignore_certs, proxy_info, on_data,
                 on_close):
        self._on_data = on_data
        self._on_close = on_close
        self._proxy_info = proxy_info
        self._receiving_thread = None

        ca_certs = utils.CheckCACertsFile(ignore_certs)
        self._sslopt = {'cert_reqs': ssl.CERT_REQUIRED, 'ca_certs': ca_certs}
        if ignore_certs:
            self._sslopt['cert_reqs'] = ssl.CERT_NONE
            self._sslopt['check_hostname'] = False

        # Disable most of random logging in websocket library itself
        logging.getLogger('websocket').setLevel(logging.CRITICAL)

        self._is_closed = False
        self._error_msg = ''
        self._websocket = websocket.WebSocketApp(
            url,
            header=headers,
            on_close=self._OnClose,
            on_data=self._OnData,
            on_error=self._OnError,
            subprotocols=[utils.SUBPROTOCOL_NAME])
    def InitiateConnection(self):
        """Initiate the WebSocket connection."""
        utils.CheckPythonVersion(self._ignore_certs)
        utils.ValidateParameters(self._tunnel_target)
        self._ca_certs = utils.CheckCACertsFile(self._ignore_certs)

        self._connect_url = utils.CreateWebSocketUrl(CONNECT_ENDPOINT,
                                                     self._tunnel_target)
        headers = [
            'User-Agent: ' + http.MakeUserAgentString(),
            'Sec-WebSocket-Protocol: ' + utils.SUBPROTOCOL_NAME
        ]
        if self._access_token:
            headers += ['Authorization: Bearer ' + self._access_token]
        log.info('Connecting to with URL %r', self._connect_url)
        self._websocket_errors = []
        self._connection_sid = None

        if log.GetVerbosity() == logging.DEBUG:
            websocket.enableTrace(True)
        else:
            websocket_logger = logging.getLogger('websocket')
            websocket_logger.setLevel(logging.CRITICAL)

        self._websocket = websocket.WebSocketApp(self._connect_url,
                                                 header=headers,
                                                 on_error=self._OnError,
                                                 on_close=self._OnClose,
                                                 on_data=self._OnData)
        log.info('Starting WebSocket receive thread.')
        self._websocket_thread = threading.Thread(
            target=self._ReceiveFromWebSocket)
        self._websocket_thread.daemon = True
        self._websocket_thread.start()