NWConnection/WebSocket refuses to establish a connection

Hello,

I am trying to use NWConnection's WebSocket support to get to SSH clients behind web servers, but I never get past the .preparing stage of NWConnection, and I get these errors in the console:

[] nw_ws_validate_server_response [C1.1:1] server response doesn't match expected value
[] nw_protocol_copy_ws_definition_block_invoke [C1.1:1] nw_ws_validate_server_response

Then my state handler goes to .failed, and I get the following detail:

POSIXErorCode(rawValue: 53): Software caused connection abort

This is the code that I use to setup my connection (I have replaced the real hostname with 'HOST' below):

let parameters = NWParameters.tcp
let options = NWProtocolWebSocket.Options()
options.autoReplyPing = true
parameters.defaultProtocolStack.applicationProtocols.insert(options, at: 0)

connection = NWConnection (to: .url (URL (string: "wss://usaws1.sshstores.vip:8880")!), using: parameters)

On Wireshark, I can see that the connection is established, and I get a reply:

Client sends:

GET / HTTP/1.1
Host: usaws1.sshstores.vip
Sec-WebSocket-Version: 13
Upgrade: WebSocket
Sec-WebSocket-Key: wEUS7BsMfPNEMg9fhk/fxg==
Connection: Upgrade

And the server replies:

HTTP/1.1 101 Switching Protocol
\Content-Length: 1048576000000

I have tried also with a different server, and I get that in this case:

Client sends:

GET / HTTP/1.1
Host: sshws-sg.gameserver.pw
Sec-WebSocket-Version: 13
Upgrade: WebSocket
Sec-WebSocket-Key: k+jtlUsrn2L9SaFXBZpp7g==
Connection: Upgrade

Server responds:

HTTP/1.1 101 Switching Protocol
Date: Sat, 16 Apr 2022 18:08:15 GMT
Connection: upgrade
\Content-Length: 1048576000000
CF-Cache-Status: DYNAMIC
Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=bVwvSh2T19Z%2BWDGh%2BzQRMIJknR9ScAkx2bzkDg5NLTy26ga6sJpGmPax9iFUAO9fJ5c5ycMK0Hc7xILew2LiL8cZ0uWVRvsjTx%2FEjoHq84SEHEipl%2BpStQd1Nt8bDR%2BSWL7OC4xejM5m"}],"group":"cf-nel","max_age":604800}
NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
Server: cloudflare
CF-RAY: 6fcedd790d3c1962-EWR
alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400

In both cases the same error is reported back, and in both cases the message on the console is the same nw_ws_validate_server_response.

Is there some way for me to figure out what is tripping NWConnection, or what it is that it doesn't like?

I sat down to play with this and then noticed something weird: You’re creating your connection with .tcp, indicating no TLS, but then supply a wss: URL, indicating you want TLS. Which is it?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

NWConnection/WebSocket refuses to establish a connection
 
 
Q