Recent

Author Topic: [SOLVED] Websocket. There is no handshake for wss.  (Read 301 times)

yus

  • Jr. Member
  • **
  • Posts: 52
[SOLVED] Websocket. There is no handshake for wss.
« on: August 07, 2022, 07:02:23 pm »
Hello.
I'm trying to figure out how WebSocket works using the wss protocol.
Why, when establishing a connection and trying to perform a handshake, it does not happen?
Minimal example.
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.  {$IFDEF UNIX}
  7.   cthreads,
  8.    {$ENDIF}
  9.   SysUtils,
  10.   Classes,
  11.   ssockets,
  12.   sslsockets,
  13.   opensslsockets,
  14.   fpopenssl,
  15.   base64 { you can add units after this };
  16.  
  17. const
  18.   CRLF = #13#10;
  19. var
  20.   Socket: TInetSocket;
  21.   SSLHandler: TSSLSocketHandler;
  22.   str: ansistring;
  23.   SecKey, res: string;
  24.   Buf: TBytes;
  25.  
  26. begin
  27.   SSLHandler := TSSLSocketHandler.GetDefaultHandler;
  28.   SSLHandler.VerifyPeerCert := False;
  29.  
  30.   Socket := TInetSocket.Create('demo.piesocket.com', 443, SSLHandler);
  31.   Socket.Connect;
  32.  
  33.   str := 'GET /v3/channel_1?api_key=VCXCEuvhGcBDP7XhiJJUDvR1e1D3eiVjgZ9VRiaV&notify_self HTTP/1.1'
  34.     + CRLF;
  35.   str := str + 'Host: demo.piesocket.com' + CRLF;
  36.   str := str + 'Connection: Upgrade' + CRLF;
  37.   str := str + 'Pragma: no-cache' + CRLF;
  38.   str := str + 'Cache-Control: no-cache' + CRLF;
  39.   str := str + 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.134 Safari/537.36'
  40.     + CRLF;
  41.   str := str + 'Upgrade: websocket' + CRLF;
  42.   str := str + 'Origin: https://www.piesocket.com' + CRLF;
  43.   str := str + 'Sec-WebSocket-Version: 13' + CRLF;
  44.   str := str + 'Accept-Encoding: gzip, deflate, br' + CRLF;
  45.   str := str + 'Accept-Language: ru,en;q=0.9' + CRLF;
  46.   SecKey := EncodeStringBase64(IntToHex(Random($7FFFFFFFFFFFFFFF), 16));
  47.   str := str + 'Sec-WebSocket-Key: ' + SecKey + CRLF;
  48.   str := str + 'Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits' + CRLF;
  49.   Str := Str + CRLF;
  50.   Writeln(Str);
  51.   Socket.WriteAnsiString(str);
  52.   if Socket.CanRead(5000) then
  53.   begin
  54.     SetLength(Buf, 1000);
  55.     Socket.Read(Buf[0], 1000);
  56.     res := TEncoding.UTF8.GetString(Buf);
  57.   end;
  58.   Writeln('---');
  59.   Writeln(res);
  60.   Readln;
  61. end.
  62.  
Log:
Quote
GET /v3/channel_1?api_key=VCXCEuvhGcBDP7XhiJJUDvR1e1D3eiVjgZ9VRiaV&notify_self HTTP/1.1
Host: demo.piesocket.com
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.134 Safari/537.36
Upgrade: websocket
Origin: https://www.piesocket.com
Sec-WebSocket-Version: 13
Accept-Encoding: gzip, deflate, br
Accept-Language: ru,en;q=0.9
Sec-WebSocket-Key: NkY2NDgyMkVDRDA0NEVCOQ==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

---
HTTP/1.1 400 Bad Request
Server: cloudflare
Date: Sun, 07 Aug 2022 17:04:16 GMT
Content-Type: text/html
Content-Length: 155
Connection: close
CF-RAY: -

<html>
<head><title>400 Bad Request</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<hr><center>cloudflare</center>
</body>
</html>


« Last Edit: August 07, 2022, 09:12:10 pm by yus »

yus

  • Jr. Member
  • **
  • Posts: 52
Re: [SOLVED] Websocket. There is no handshake for wss.
« Reply #1 on: August 07, 2022, 09:12:54 pm »
Change
Code: Pascal  [Select][+][-]
  1.  Socket.WriteAnsiString(str);
To
Code: Pascal  [Select][+][-]
  1.  Socket.WriteBuffer(@str[1], Length(Str));

 

TinyPortal © 2005-2018