TooTallNate / proxy-agents

Node.js HTTP Proxy Agents Monorepo
https://proxy-agents.n8.io
872 stars 229 forks source link

PAC proxy with `*_PROXY` env vars is broken #219

Closed Drarig29 closed 11 months ago

Drarig29 commented 11 months ago

With proxy-agent@5.0.0, the following code worked (example taken from here):

// index.mjs

import * as http from 'http'
import ProxyAgent from 'proxy-agent'

// The correct proxy `Agent` implementation to use will be determined
// via the `http_proxy` / `https_proxy` / `no_proxy` / etc. env vars
const agent = new ProxyAgent()

// The rest works just like any other normal HTTP request
http.get('http://jsonip.com', {agent}, (res) => {
  console.log(res.statusCode, res.headers)
  res.pipe(process.stdout)
})

Test command line (this uses a PAC file which only returns "DIRECT"):

DEBUG=proxy-agent,pac-proxy-agent HTTP_PROXY=pac+https://gist.githubusercontent.com/Drarig29/d6dcf7f2995921e063006afb01b776db/raw/84fed7334300698e6e7390a4928de592d5666e99/noop-proxy.pac node index.mjs

After upgrading to proxy-agent@6.2.1 (and doing this little change:)

- import ProxyAgent from 'proxy-agent'
+ import {ProxyAgent} from 'proxy-agent'

Then it fails with this:

  proxy-agent Creating new ProxyAgent instance: undefined +0ms
  proxy-agent Request URL: 'http://jsonip.com/' +3ms
  proxy-agent Proxy URL: 'pac+https://gist.githubusercontent.com/Drarig29/d6dcf7f2995921e063006afb01b776db/raw/84fed7334300698e6e7390a4928de592d5666e99/noop-proxy.pac' +0ms

node:events:491
      throw er; // Unhandled 'error' event
      ^

Error: Unsupported protocol for proxy URL: pac+https://gist.githubusercontent.com/Drarig29/d6dcf7f2995921e063006afb01b776db/raw/84fed7334300698e6e7390a4928de592d5666e99/noop-proxy.pac
    at ProxyAgent.connect (/Users/corentin.girard/playground/mcve-bug-pac-proxy-agent/node_modules/proxy-agent/dist/index.js:113:23)
    at /Users/corentin.girard/playground/mcve-bug-pac-proxy-agent/node_modules/agent-base/dist/index.js:73:30
Emitted 'error' event on ClientRequest instance at:
    at _destroy (node:_http_client:874:13)
    at onSocketNT (node:_http_client:891:5)
    at processTicksAndRejections (node:internal/process/task_queues:84:21)

I think that's because isValidProtocol() https://github.com/TooTallNate/proxy-agents/blob/08487f0a7a72d3986b62d85eba8e777a1ff274c7/packages/proxy-agent/src/index.ts#L133-L135

uses those protocols: https://github.com/TooTallNate/proxy-agents/blob/08487f0a7a72d3986b62d85eba8e777a1ff274c7/packages/pac-proxy-agent/src/index.ts#L58-L64

TooTallNate commented 11 months ago

Whoops, ya, good catch.

TooTallNate commented 11 months ago

Fixed in proxy-agent v6.2.2.