def _handShake(self, hostAddress, portNumber):
     CRLF = "\r\n"
     IOT_ENDPOINT_PATTERN = r"^[0-9a-zA-Z]+(\.ats|-ats)?\.iot\.(.*)\.amazonaws\..*"
     matched = re.compile(IOT_ENDPOINT_PATTERN,
                          re.IGNORECASE).match(hostAddress)
     if not matched:
         raise ClientError("Invalid endpoint pattern for wss: %s" %
                           hostAddress)
     region = matched.group(2)
     signedURL = self._sigV4Handler.createWebsocketEndpoint(
         hostAddress, portNumber, region, "GET", "iotdata", "/mqtt")
     # Now we got a signedURL
     path = signedURL[signedURL.index("/mqtt"):]
     # Assemble HTTP request headers
     Method = "GET " + path + " HTTP/1.1" + CRLF
     Host = "Host: " + hostAddress + CRLF
     Connection = "Connection: " + "Upgrade" + CRLF
     Upgrade = "Upgrade: " + "websocket" + CRLF
     secWebSocketVersion = "Sec-WebSocket-Version: " + "13" + CRLF
     rawSecWebSocketKey = self._generateWSSKey()  # Bytes
     secWebSocketKey = "sec-websocket-key: " + rawSecWebSocketKey.decode(
         'utf-8') + CRLF  # Should be randomly generated...
     secWebSocketProtocol = "Sec-WebSocket-Protocol: " + "mqttv3.1" + CRLF
     secWebSocketExtensions = "Sec-WebSocket-Extensions: " + "permessage-deflate; client_max_window_bits" + CRLF
     # Send the HTTP request
     # Ensure that we are sending bytes, not by any chance unicode string
     handshakeBytes = Method + Host + Connection + Upgrade + secWebSocketVersion + secWebSocketProtocol + secWebSocketExtensions + secWebSocketKey + CRLF
     handshakeBytes = handshakeBytes.encode('utf-8')
     self._sslSocket.write(handshakeBytes)
     # Read it back (Non-blocking socket)
     timeStart = time.time()
     wssHandshakeResponse = bytearray()
     while len(wssHandshakeResponse) == 0:
         try:
             wssHandshakeResponse += self._sslSocket.read(
                 1024)  # Response is always less than 1024 bytes
         except socket.error as err:
             if err.errno == ssl.SSL_ERROR_WANT_READ or err.errno == ssl.SSL_ERROR_WANT_WRITE:
                 if time.time() - timeStart > self._getTimeoutSec():
                     raise err  # We make sure that reconnect gets retried in Paho upon a wss reconnect response timeout
             else:
                 raise err
     # Verify response
     # Now both wssHandshakeResponse and rawSecWebSocketKey are byte strings
     if not self._verifyWSSResponse(wssHandshakeResponse,
                                    rawSecWebSocketKey):
         raise wssHandShakeError()
     else:
         pass
Esempio n. 2
0
 def _handShake(self, hostAddress, portNumber):
     CRLF = "\r\n"
     hostAddressChunks = hostAddress.split(
         '.')  # <randomString>.iot.<region>.amazonaws.com
     region = hostAddressChunks[2]  # XXXX.<region>.beta
     signedURL = self._sigV4Handler.createWebsocketEndpoint(
         hostAddress, portNumber, region, "GET", "iotdata", "/mqtt")
     if signedURL == "":
         raise wssNoKeyInEnvironmentError()
     # Now we got a signedURL
     path = signedURL[signedURL.index("/mqtt"):]
     # Assemble HTTP request headers
     Method = "GET " + path + " HTTP/1.1" + CRLF
     Host = "Host: " + hostAddress + CRLF
     Connection = "Connection: " + "Upgrade" + CRLF
     Upgrade = "Upgrade: " + "websocket" + CRLF
     secWebSocketVersion = "Sec-WebSocket-Version: " + "13" + CRLF
     rawSecWebSocketKey = self._generateWSSKey()  # Bytes
     secWebSocketKey = "sec-websocket-key: " + rawSecWebSocketKey.decode(
         'utf-8') + CRLF  # Should be randomly generated...
     secWebSocketProtocol = "Sec-WebSocket-Protocol: " + "mqttv3.1" + CRLF
     secWebSocketExtensions = "Sec-WebSocket-Extensions: " + "permessage-deflate; client_max_window_bits" + CRLF
     # Send the HTTP request
     # Ensure that we are sending bytes, not by any chance unicode string
     handshakeBytes = Method + Host + Connection + Upgrade + secWebSocketVersion + secWebSocketProtocol + secWebSocketExtensions + secWebSocketKey + CRLF
     handshakeBytes = handshakeBytes.encode('utf-8')
     self._sslSocket.write(handshakeBytes)
     # Read it back (Non-blocking socket)
     timeStart = time.time()
     wssHandshakeResponse = bytearray()
     while len(wssHandshakeResponse) == 0:
         try:
             wssHandshakeResponse += self._sslSocket.read(
                 1024)  # Response is always less than 1024 bytes
         except socket.error as err:
             if err.errno == ssl.SSL_ERROR_WANT_READ or err.errno == ssl.SSL_ERROR_WANT_WRITE:
                 if time.time() - timeStart > self._getTimeoutSec():
                     raise err  # We make sure that reconnect gets retried in Paho upon a wss reconnect response timeout
             else:
                 raise err
     # Verify response
     # Now both wssHandshakeResponse and rawSecWebSocketKey are byte strings
     if not self._verifyWSSResponse(wssHandshakeResponse,
                                    rawSecWebSocketKey):
         raise wssHandShakeError()
     else:
         pass
Esempio n. 3
0
 def _handShake(self, hostAddress, portNumber):
     CRLF = "\r\n"
     IOT_ENDPOINT_PATTERN = r"^[0-9a-zA-Z]+(\.ats|-ats)?\.iot\.(.*)\.amazonaws\..*"
     matched = re.compile(IOT_ENDPOINT_PATTERN, re.IGNORECASE).match(hostAddress)
     if not matched:
         raise ClientError("Invalid endpoint pattern for wss: %s" % hostAddress)
     region = matched.group(2)
     signedURL = self._sigV4Handler.createWebsocketEndpoint(hostAddress, portNumber, region, "GET", "iotdata", "/mqtt")
     # Now we got a signedURL
     path = signedURL[signedURL.index("/mqtt"):]
     # Assemble HTTP request headers
     Method = "GET " + path + " HTTP/1.1" + CRLF
     Host = "Host: " + hostAddress + CRLF
     Connection = "Connection: " + "Upgrade" + CRLF
     Upgrade = "Upgrade: " + "websocket" + CRLF
     secWebSocketVersion = "Sec-WebSocket-Version: " + "13" + CRLF
     rawSecWebSocketKey = self._generateWSSKey()  # Bytes
     secWebSocketKey = "sec-websocket-key: " + rawSecWebSocketKey.decode('utf-8') + CRLF  # Should be randomly generated...
     secWebSocketProtocol = "Sec-WebSocket-Protocol: " + "mqttv3.1" + CRLF
     secWebSocketExtensions = "Sec-WebSocket-Extensions: " + "permessage-deflate; client_max_window_bits" + CRLF
     # Send the HTTP request
     # Ensure that we are sending bytes, not by any chance unicode string
     handshakeBytes = Method + Host + Connection + Upgrade + secWebSocketVersion + secWebSocketProtocol + secWebSocketExtensions + secWebSocketKey + CRLF
     handshakeBytes = handshakeBytes.encode('utf-8')
     self._sslSocket.write(handshakeBytes)
     # Read it back (Non-blocking socket)
     timeStart = time.time()
     wssHandshakeResponse = bytearray()
     while len(wssHandshakeResponse) == 0:
         try:
             wssHandshakeResponse += self._sslSocket.read(1024)  # Response is always less than 1024 bytes
         except socket.error as err:
             if err.errno == ssl.SSL_ERROR_WANT_READ or err.errno == ssl.SSL_ERROR_WANT_WRITE:
                 if time.time() - timeStart > self._getTimeoutSec():
                     raise err  # We make sure that reconnect gets retried in Paho upon a wss reconnect response timeout
             else:
                 raise err
     # Verify response
     # Now both wssHandshakeResponse and rawSecWebSocketKey are byte strings
     if not self._verifyWSSResponse(wssHandshakeResponse, rawSecWebSocketKey):
         raise wssHandShakeError()
     else:
         pass
 def _handShake(self, hostAddress, portNumber):
     CRLF = "\r\n"
     hostAddressChunks = hostAddress.split('.')  # <randomString>.iot.<region>.amazonaws.com
     region = hostAddressChunks[2]  # XXXX.<region>.beta
     signedURL = self._sigV4Handler.createWebsocketEndpoint(hostAddress, portNumber, region, "GET", "iotdata", "/mqtt")
     if signedURL == "":
         raise wssNoKeyInEnvironmentError()
     # Now we got a signedURL
     path = signedURL[signedURL.index("/mqtt"):]
     # Assemble HTTP request headers
     Method = "GET " + path + " HTTP/1.1" + CRLF
     Host = "Host: " + hostAddress + CRLF
     Connection = "Connection: " + "Upgrade" + CRLF
     Upgrade = "Upgrade: " + "websocket" + CRLF
     secWebSocketVersion = "Sec-WebSocket-Version: " + "13" + CRLF
     rawSecWebSocketKey = self._generateWSSKey()  # Bytes
     secWebSocketKey = "sec-websocket-key: " + rawSecWebSocketKey.decode('utf-8') + CRLF  # Should be randomly generated...
     secWebSocketProtocol = "Sec-WebSocket-Protocol: " + "mqttv3.1" + CRLF
     secWebSocketExtensions = "Sec-WebSocket-Extensions: " + "permessage-deflate; client_max_window_bits" + CRLF
     # Send the HTTP request
     # Ensure that we are sending bytes, not by any chance unicode string
     handshakeBytes = Method + Host + Connection + Upgrade + secWebSocketVersion + secWebSocketProtocol + secWebSocketExtensions + secWebSocketKey + CRLF
     handshakeBytes = handshakeBytes.encode('utf-8')
     self._sslSocket.write(handshakeBytes)
     # Read it back (Non-blocking socket)
     # Do we need a timeout here?
     wssHandshakeResponse = bytearray()
     while len(wssHandshakeResponse) == 0:
         try:
             wssHandshakeResponse += self._sslSocket.read(1024)  # Response is always less than 1024 bytes
         except socket.error as err:
             if err.errno == ssl.SSL_ERROR_WANT_READ or err.errno == ssl.SSL_ERROR_WANT_WRITE:
                 pass
     # Verify response
     # Now both wssHandshakeResponse and rawSecWebSocketKey are byte strings
     if not self._verifyWSSResponse(wssHandshakeResponse, rawSecWebSocketKey):
         raise wssHandShakeError()
     else:
         pass