Compatibility between EnvHttpProxyAgent and DNS interceptor #5303
Replies: 3 comments 1 reply
|
On the one hand, I still want to cache DNS lookups for hosts from NO_PROXY list |
|
By design coincidence, they are mutually exclusive, it was not meant to be like that from the beginning. Nonetheless, sadly I'm unsure whereas a backtrack can be easily implemented. If this stills the desire, a intermediate interceptor can be added to do that explicit backtrack. I can think of extending the |
|
Late to this, but the mechanism is worth pinning down — "mutually exclusive" turns out to be more specific than it sounds, and the thing you gave up (a DNS cache for Why composing them breaks it. // lib/dispatcher/env-http-proxy-agent.js
[kDispatch] (opts, handler) {
const url = new URL(opts.origin)
const agent = this.#getProxyAgentForUrl(url)and the DNS interceptor overwrites exactly that field before the dispatch reaches it, keeping the original host only in a header: // lib/interceptor/dns.js
const dispatchOpts = {
...origDispatchOpts,
servername: origin.hostname,
origin: newOrigin.origin, // ← now an IP
headers: withHostHeader(origin.host, origDispatchOpts.headers)
}Because Two things there are worth separating. The first is the Getting both. Take the proxy decision above the rewrite instead of below it, and give each branch its own dispatcher, so function proxyRouter ({ shouldProxy, direct, proxied }) {
return () => function routerDispatch (opts, handler) {
const url = opts.origin.constructor === URL ? opts.origin : new URL(opts.origin)
return (shouldProxy(url.hostname) ? proxied : direct).dispatch(opts, handler)
}
}
const direct = new Agent().compose(interceptors.dns())
const proxied = new ProxyAgent({ uri: process.env.HTTP_PROXY })
const dispatcher = proxied.compose(proxyRouter({
shouldProxy: (h) => !noProxySet.has(h),
direct,
proxied
}))Same harness, same Direct hosts stay direct and are DNS-cached; proxied hosts reach the proxy with the hostname intact. The cost is that And the smaller fix this implies for undici, as an alternative to the this[kNoProxyAgent] = new Agent(agentOpts)— with no way to compose onto that one specifically. A hook that let interceptors be attached to the inner no-proxy agent alone would make the two features composable, because the ordering problem disappears once the interceptor sits below the decision instead of above it. Happy to open an issue with the harness if that direction is interesting. Full harness — two files, run from a checkout of
|
Uh oh!
There was an error while loading. Please reload this page.
Hello!
I have a question about EnvHttpProxyAgent and dns interceptor.
Can you help me to understand, how I can handle relation between this two features?
We have a custom setup with replacible default undici Agent, and extensible interceptors list, like this:
Then, we have separate http proxy module where default Agent is replaced, like this:
The problem is, the same
interceptorslist is used, and DNS cache interceptor automatically passed intoEnvHttpProxyAgent.composelist. After than, requests hostname is resolved to IP before proxy, and regular proxy env rules with hostnames can't be applied.So, my question is more about how I need to treat relationship between this two:
All reactions