Skip to content

Commit

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

* added support for inline URL username:password

* Update lib/proxy-agent.js

---------

Co-authored-by: Robert Nagy <ronagy@icloud.com>
  • Loading branch information
2 people authored and crysmags committed Feb 27, 2024
1 parent a9c0f93 commit a95931a
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

0 comments on commit a95931a

Please sign in to comment.