Skip to content

Commit

Permalink
Use WebSocket check in pac-proxy-agent (#329)
Browse files Browse the repository at this point in the history
This mirrors the check in the core `proxy-agent` package and
serves the same purpose.
  • Loading branch information
addaleax authored Dec 6, 2024
1 parent 85b10b3 commit e90e2b2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/swift-lizards-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'pac-proxy-agent': patch
---

Properly forward WebSocket requests via PAC agents that resolve to HTTP proxies
3 changes: 2 additions & 1 deletion packages/pac-proxy-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export class PacProxyAgent<Uri extends string> extends Agent {
opts: AgentConnectOpts
): Promise<http.Agent | net.Socket> {
const { secureEndpoint } = opts;
const isWebSocket = req.getHeader('upgrade') === 'websocket';

// First, get a generated `FindProxyForURL()` function,
// either cached or retrieved from the source
Expand Down Expand Up @@ -261,7 +262,7 @@ export class PacProxyAgent<Uri extends string> extends Agent {
const proxyURL = `${
type === 'HTTPS' ? 'https' : 'http'
}://${target}`;
if (secureEndpoint) {
if (secureEndpoint || isWebSocket) {
agent = new HttpsProxyAgent(proxyURL, this.opts);
} else {
agent = new HttpProxyAgent(proxyURL, this.opts);
Expand Down
21 changes: 21 additions & 0 deletions packages/pac-proxy-agent/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,27 @@ describe('PacProxyAgent', () => {
assert('via' in data);
});

it('should work over an HTTP proxy for WebSockets', async () => {
httpServer.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
});

function FindProxyForURL() {
return 'PROXY localhost:PORT;';
}

const uri = `data:,${encodeURIComponent(
FindProxyForURL.toString().replace('PORT', proxyServerUrl.port)
)}`;
const agent = new PacProxyAgent(uri);

const res = await req(new URL('/test', httpServerUrl), { agent, headers: { upgrade: 'websocket' } });
const data = await json(res);
assert.equal(httpServerUrl.host, data.host);
assert(!('via' in data)); // Used CONNECT rather than plain HTTP proxy
assert.equal(data.upgrade, 'websocket');
});

it('should work over a SOCKS proxy', async () => {
httpServer.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
Expand Down

0 comments on commit e90e2b2

Please sign in to comment.