Skip to content

feat(cli): add custom dns resolver option #587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 10, 2024

Conversation

PsypherPunk
Copy link
Collaborator

Note: this extends #531 (to which I don't have write-access) but retains all the commits therein (all credit to @khanhnt2).

relates #531

As discussed on that PR:

  • fixed the linting issues;
  • added some tests (not easy as Resolver hides a lot of its internal fields so validating the exact type built doesn't appear to be straightforward).
  • updated the derivation to be:
    • if --resolver is passed or resolver is set in config:
      • assume the value is a file and read that;
      • if not, assume it's comma-separated IP addresses;
    • if no argument is passed:

@PsypherPunk PsypherPunk marked this pull request as draft May 10, 2024 10:00
@PsypherPunk
Copy link
Collaborator Author

Converting to draft while I fix the conflicts…

@PsypherPunk PsypherPunk force-pushed the 531-custom-dns-resolver branch from cf6cf9f to 0e4d8a8 Compare May 10, 2024 11:37
@PsypherPunk
Copy link
Collaborator Author

PsypherPunk commented May 10, 2024

Resolved the conflicts but seeing a weird build error:

error: could not copy file from 'C:\Users\runneradmin\.cargo\bin\rustup-init.exe' to 'C:\Users\runneradmin\.cargo\bin\rustup.exe': Access is denied. (os error 5)
Unexpected error attempting to determine if executable file exists 'C:\Users\runneradmin\.cargo\bin\rustup.exe': Error: EPERM: operation not permitted, stat 'C:\Users\runneradmin\.cargo\bin\rustup.exe'

Update: it fixed itself on the next run.

- remove extraneous `std::net::` prefixes for already-imported `IpAddr`.
- move derivation to a function:
  - parse input/file as normal;
  - attempt to use the system DNS resolution;
  - fall back to previous CloudFlare behaviour.
@PsypherPunk PsypherPunk force-pushed the 531-custom-dns-resolver branch from 0e4d8a8 to 8fd1f6e Compare May 10, 2024 11:43
@PsypherPunk PsypherPunk marked this pull request as ready for review May 10, 2024 11:43
Comment on lines +259 to +280
#[test]
fn resolver_default_cloudflare() {
let opts = Opts::default();

let resolver = get_resolver(&opts.resolver);
let lookup = resolver.lookup_ip("www.example.com.").unwrap();

assert!(opts.resolver.is_none());
assert!(lookup.iter().next().is_some());
}

#[test]
fn resolver_args_google_dns() {
let mut opts = Opts::default();
// https://developers.google.com/speed/public-dns
opts.resolver = Some("8.8.8.8,8.8.4.4".to_owned());

let resolver = get_resolver(&opts.resolver);
let lookup = resolver.lookup_ip("www.example.com.").unwrap();

assert!(lookup.iter().next().is_some());
}
Copy link
Owner

Choose a reason for hiding this comment

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

I think we try to avoid network tests so you can test on machines without the internet, in this case mocking the test output 🤔

But I think for the things which require this (Homebrew requires no networked tests) we have our own different tests specific to it

Although generally I regualrly make the argument that you cannot test a network CLI tool without some kind of network 😂 so approved!

Copy link
Collaborator Author

@PsypherPunk PsypherPunk May 10, 2024

Choose a reason for hiding this comment

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

Yep, I mulled this one over for a while: the only thing I could think would be to wrap the Resolver in a "New Type" which could capture the config. sent to it an expose that. That way, we could:

a. better test that each branch of get_resolver results in the right DNS resolvers being referenced (and should mitigate the need to actually make DNS requests in the tests).
b. potentially derive a StructOpt from_os_str for it and create it upstream in Opts, isolating a bit more from the parsing code.

…but that seemed a bit much after a hefty rebase 😁

Copy link
Owner

Choose a reason for hiding this comment

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

I'm aware in PyTest that you can add "tags" to tests, so you could create tests like "pytest --tag online" to test the online features

that way we can keep it to testing offline for locally and online for CI etc....

Not sure if cargo supports that, and again it isn't really a good solution :(

Jeeze, testing networking tools is hard!

Copy link
Owner

@bee-san bee-san left a comment

Choose a reason for hiding this comment

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

Approved!! Thank you so much and thanks @khanhnt2

@bee-san bee-san merged commit 4ae4853 into bee-san:master May 10, 2024
10 checks passed
@PsypherPunk PsypherPunk deleted the 531-custom-dns-resolver branch May 10, 2024 15:14
@bee-san
Copy link
Owner

bee-san commented May 10, 2024

Hey @PsypherPunk I added you as a maintainer of RustScan! I think you should be able to push to master and run your own CI now? 🤔

@PsypherPunk
Copy link
Collaborator Author

Thanks, @bee-san!

shock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants