Can’t decode handshake request from client


I’m trying to connect a python3 socket with a JavaScript WebSocket. I’m testing the server code on localhost:8080, and connecting with Chrome(94.0).

The WebSocket client and the socket server do establish a connection. However, the connection would be shut down if the server did not follow the protocol and respond to the handshake. According to the protocol, the client WebSocket sends a utf-8 encoded GET request to the socket server, and the server must parse that request in order to complete the handshake.

What I get from the client is this:

x16x03x01x02x00x01x00x01xfcx03x03x97txb1xf7xacx99x91xd4xf5nxdfxc3Xx8af<xbex99(0x88x9a!xc6xc9x17]xfexd9sPxcc xa8xcfx90{x8axfcmxbcj-5xdfxf90x81xc8Yxc1x85q"xfe!Cxbbtxbd}xe0x8dxf1xe5x00 x1ax1ax13x01x13x02x13x03xc0+xc0/xc0,xc00xccxa9xccxa8xc0x13xc0x14x00x9cx00x9dx00/x005x01x00x01x93x1ax1ax00x00x00x17x00x00xffx01x00x01x00x00nx00nx00x08xfaxfax00x1dx00x17x00x18x00x0bx00x02x01x00x00#x00x00x00x05x00x05x01x00x00x00x00x00rx00x12x00x10x04x03x08x04x04x01x05x03x08x05x05x01x08x06x06x01x00x12x00x00x003x00+x00)xfaxfax00x01x00x00x1dx00 xfa)xd5x85<x81.x7fxc0x87wA!xf1xc9xf80xebx01xdcxed3mxd1x98xd6xd8xd7x7f#rMx00-x00x02x01x01x00+x00x0bnx8ax8ax03x04x03x03x03x02x03x01x00x1bx00x03x02x00x02JJx00x01x00x00x15x00xf7x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00

The client consistently sends these bytes, although it this does not look like a legitimate GET request. This can’t be decoded by utf-8 or by other encodings including utf-16-le, utf-16-be, unicode-escape, etc.

client source:

var client = new WebSocket('wss://127.0.0.1:8080')

client.onopen = function(e) {
    console.log("open");
}

client.onmessage = function(e) {
    console.log(e.data)
}

server handshake handler:

def handshake(client, address):
    req = client.recv(1024).decode('utf-8')
    key = (re.search('Sec-WebSocket-Key:s+(.*?)[nr]+', req).groups()[0].strip())
    response_key = b64encode(sha1(key + sockey).digest())
    response = 'rn'.join(websock_ans).format(key=response_key)
    client.send(response.encode('utf-8'))

As the client request isn’t utf-8 bytes, this gives a decoding error in line 2.

following the websocket protocol (https://datatracker.ietf.org/doc/html/rfc6455), sockey and websock_ans are defined as follows:

sockey = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
websock_ans = {
    'HTTP/1.1 101 Switching Protocols',
    'Upgrade: websocket',
    'Connection: Upgrade',
    'Sec-WebSocket-Accept: {key}rnrn',
}

Does WebSocket compress/encrypt its GET requests? As far as I checked, the documents do not state any compression/encryption algorithms.

Thanks in advance.

Source: JavaSript – Stack Overflow

November 9, 2021
Category : News
Tags: compression | encoding | javascript | python | websocket

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.