Skip to content

Commit

Permalink
fix: Added support for inline URL username:password proxy auth (#2473)
Browse files Browse the repository at this point in the history
* added support for inline URL username:password

* Update lib/proxy-agent.js

---------

Co-authored-by: Robert Nagy <ronagy@icloud.com>
  • Loading branch information
matt-way and ronag committed Nov 30, 2023
1 parent 28759f4 commit 08183ea
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,20 @@ class ProxyAgent extends DispatcherBase {
this[kProxyTls] = opts.proxyTls
this[kProxyHeaders] = opts.headers || {}

const resolvedUrl = new URL(opts.uri)
const { origin, port, host, username, password } = resolvedUrl

if (opts.auth && opts.token) {
throw new InvalidArgumentError('opts.auth cannot be used in combination with opts.token')
} else if (opts.auth) {
/* @deprecated in favour of opts.token */
this[kProxyHeaders]['proxy-authorization'] = `Basic ${opts.auth}`
} else if (opts.token) {
this[kProxyHeaders]['proxy-authorization'] = opts.token
} else if (username && password) {
this[kProxyHeaders]['proxy-authorization'] = `Basic ${Buffer.from(`${decodeURIComponent(username)}:${decodeURIComponent(password)}`).toString('base64')}`
}

const resolvedUrl = new URL(opts.uri)
const { origin, port, host } = resolvedUrl

const connect = buildConnector({ ...opts.proxyTls })
this[kConnectEndpoint] = buildConnector({ ...opts.requestTls })
this[kClient] = clientFactory(resolvedUrl, { connect })
Expand All @@ -100,7 +102,7 @@ class ProxyAgent extends DispatcherBase {
})
if (statusCode !== 200) {
socket.on('error', () => {}).destroy()
callback(new RequestAbortedError('Proxy response !== 200 when HTTP Tunneling'))
callback(new RequestAbortedError(`Proxy response (${statusCode}) !== 200 when HTTP Tunneling`))
}
if (opts.protocol !== 'https:') {
callback(null, socket)
Expand Down

1 comment on commit 08183ea

@PandaWorker
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you <3

Please sign in to comment.