Skip to content

Commit ad79ddc

Browse files
Nikitajoeferner
authored andcommitted
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
1 parent 1409bae commit ad79ddc

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

lib/proxy.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -868,21 +868,29 @@ export class Proxy implements IProxy {
868868
} else {
869869
url = upgradeReq.url;
870870
}
871-
const ptosHeaders = {};
872-
const ctopHeaders = upgradeReq.headers;
873-
for (const key in ctopHeaders) {
874-
if (key.indexOf("sec-websocket") !== 0) {
875-
ptosHeaders[key] = ctopHeaders[key];
871+
const proxyToServerHeaders= {};
872+
const clientToProxyHeaders = upgradeReq.headers;
873+
for (const header in clientToProxyHeaders) {
874+
if (header.indexOf("sec-websocket") !== 0) {
875+
proxyToServerHeaders[header] = clientToProxyHeaders[header];
876876
}
877877
}
878+
879+
let protocols: string[] = [];
880+
if(clientToProxyHeaders["sec-websocket-protocol"]) {
881+
protocols = clientToProxyHeaders["sec-websocket-protocol"].split(",").map((p) => p.trim());
882+
}
883+
878884
ctx.proxyToServerWebSocketOptions = {
879885
url,
886+
protocols: protocols.length > 0 ? protocols : undefined,
880887
agent: ctx.isSSL ? self.httpsAgent : self.httpAgent,
881-
headers: ptosHeaders,
888+
headers: proxyToServerHeaders,
882889
};
883890
function makeProxyToServerWebSocket() {
884891
ctx.proxyToServerWebSocket = new WebSocket(
885892
ctx.proxyToServerWebSocketOptions!.url!,
893+
ctx.proxyToServerWebSocketOptions.protocols,
886894
ctx.proxyToServerWebSocketOptions
887895
);
888896
ctx.proxyToServerWebSocket.on(

0 commit comments

Comments
 (0)