-
Notifications
You must be signed in to change notification settings - Fork 86
Use the request's configured lookup if client-side lookup is enabled #72
Conversation
Hey @pimterry thanks for this PR, I want to integrate it 🙂 Just a comment: do you think we can use Essentially the module is wrapping dns.lookup providing cache capabilities. Also, we can use |
Hi @Kikobeats - it's possible to do that, up to you, but honestly I wouldn't recommend it. In my case, I am actually using a cacheable-lookup instance as the lookup option for my requests, and I've also previously written an article talking about why more people should use it - I fully agree that it's useful! Even though it can be useful however, adding another layer of DNS caching in-process can be really confusing if you're not expecting it, and there are some subtle differences between the internal lookup behaviour of cacheable-lookup & I do think lots of Node.js apps should cache their DNS requests, but given the tradeoffs I don't think it should enabled for everybody by default in unrelated contexts (e.g. when enabling a proxy server). Notably even Got (the library cacheable-lookup was originally built for) doesn't seem to enable it by default - it just offers it via an option for people who need DNS caching. Does that make sense? Either way, with this PR in place, anybody who does want to use it can easily choose to so for themselves. |
Oh wow, thanks for the awesome explanation! I think you're right, there are some scenarios where adding another caching layer is not worth it, although to be honest the point of my suggestion was in the way to bring to users the ability to customize the DNS layer, since there is no possible right now. During the weekend, I released v6.2.0-beta.0 that it introduces https://github.com/TooTallNate/node-socks-proxy-agent/blob/master/src/index.ts#L138 What can be better there is to use the default DNS behavior rather than create a Although now I have a more high level question: why this library should to take care about DNS resolution? I mean, under which circumstances make sense to sets |
Sorry, just to be clear: the option this PR is introducing is just called
Hope that's clearer. Anyway, why would you want to configure a custom lookup callback? A few reasons:
In my specific example, some of the requests that I'm making are going to Docker containers on the machine, but I'm running code outside of Docker. To make this work, I run my own DNS server which connects to Docker to get all the names and possible addresses, and then I use this server to lookup IPs during requests to Docker containers, and use a SOCKS proxy that's hosted inside Docker to forward these requests (because containers are not externally routeable in virtualized Docker environments like Windows & Mac).
I see what you're going for, and being able to configure DNS is definitely useful, but I don't think this library needs lots of options like this to do it for you. DNS logic is almost entirely independent from proxying logic, and it's not necessary or helpful to mix the two together. I would recommend looking at the standard Node.js APIs and how they handle this. Node has a standard option that's used everywhere to configure DNS settings: you always just pass a custom With this PR, that same option works automatically here too. This is a great design by the Node team imo, because it allows users to do anything. By passing a callback, they can provide a lookup function that does caching, or a callback with custom overrides for some hostnames, or a callback that blocks all lookups, or a callback that uses custom DNS servers, or any other combination of that logic. It's just a callback that takes a name and returns an address. I would keep it simple. Don't add a dnsCache option, don't add dnsResolver or dnsRotate or dnsOverride options or anything else - just let users pass a callback and leave it up to them. |
agree with all 👏 can you resolve the conflicts? I can do it as well |
Client-side lookup is used for socks4 and socks5, but not socks, socks4a or socks5h, which delegate lookup to the remote proxy itself. This removes the dnsCache option that was briefly added in 6.2.0-beta.0 in favour of a more generic solution using the standard Node options, but includes a test that adds caching using this 'lookup' option as an alternative to using dnsCache.
f4cd606
to
9760a26
Compare
@Kikobeats this is now updated. It has the same changes as before, but it now replaces the As part of that, cacheable-lookup has moved to now be a dev-dependency, and there's a new dev dependency on dns2 which we use to create a quick demo DNS server in the tests. |
What a great PR, thanks for this! Shipped as |
HTTP requests can be configured with a
lookup
option, which provides a method that should be used instead ofdns.lookup
to resolve names. This is useful for lots of cases including DNS caching and custom resolution.When using the Socks proxy agent with client-side DNS lookups, this was ignored, and
dns.lookup
was always called instead. In some environments (e.g. my use case, where I use custom resolution to resolve certain otherwise-unresolvable hostnames) this means that requests will fail to resolve only when using a socks proxy, but work fine the rest of the time.This PR is a small change to fix that, by using the lookup param in preference to the default lookup method if it's set.