Skip to content

Commit

Permalink
Added protocol specification when connecting to the server
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Nikita authored and joeferner committed Mar 1, 2024
1 parent 1409bae commit ad79ddc
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions lib/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit ad79ddc

Please sign in to comment.