From c135ed057d3ecd7edd192396bd99a86e04f66376 Mon Sep 17 00:00:00 2001 From: Nikita Date: Fri, 1 Mar 2024 16:45:59 +0100 Subject: [PATCH] Added protocol specification when connecting to the server In the previous implementation, when a client sends a proxy specification with the sec-websocket-protocol protocol, the proxy would disregard it and establish a connection to the server without this specification. This could lead to an incorrect connection and ultimately result in data not being sent. In my commit, I've modified the code to include the utilized protocols in the options and then pass them to the WebSocket constructor, ensuring the protocol specification is honored during the connection process --- lib/proxy.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/proxy.ts b/lib/proxy.ts index 5a8154e..f2f3d7d 100644 --- a/lib/proxy.ts +++ b/lib/proxy.ts @@ -868,21 +868,29 @@ export class Proxy implements IProxy { } else { url = upgradeReq.url; } - const ptosHeaders = {}; - const ctopHeaders = upgradeReq.headers; - for (const key in ctopHeaders) { - if (key.indexOf("sec-websocket") !== 0) { - ptosHeaders[key] = ctopHeaders[key]; + const proxyToServerHeaders= {}; + const clientToProxyHeaders = upgradeReq.headers; + for (const header in clientToProxyHeaders) { + if (header.indexOf("sec-websocket") !== 0) { + proxyToServerHeaders[header] = clientToProxyHeaders[header]; } } + + let protocols: string[] = []; + if(clientToProxyHeaders["sec-websocket-protocol"]) { + protocols = clientToProxyHeaders["sec-websocket-protocol"].split(",").map((p) => p.trim()); + } + ctx.proxyToServerWebSocketOptions = { url, + protocols: protocols.length > 0 ? protocols : undefined, agent: ctx.isSSL ? self.httpsAgent : self.httpAgent, - headers: ptosHeaders, + headers: proxyToServerHeaders, }; function makeProxyToServerWebSocket() { ctx.proxyToServerWebSocket = new WebSocket( ctx.proxyToServerWebSocketOptions!.url!, + ctx.proxyToServerWebSocketOptions.protocols, ctx.proxyToServerWebSocketOptions ); ctx.proxyToServerWebSocket.on(