From 99ffdd55377901974649156b53cb00a2b67e11da Mon Sep 17 00:00:00 2001 From: Sean Dilda Date: Fri, 12 Jul 2024 11:06:23 -0400 Subject: [PATCH] Default to system nameservers if a Resolver is initialized without any. (#30) --- README.md | 12 +++++++++++- lib/async/dns/resolver.rb | 5 +++-- spec/async/dns/resolver_spec.rb | 9 +++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 27a8c52..9ad0ce5 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Here is a simple example showing how to use the resolver: ``` ruby Async::Reactor.run do - resolver = Async::DNS::Resolver.new([[:udp, "8.8.8.8", 53], [:tcp, "8.8.8.8", 53]]) + resolver = Async::DNS::Resolver.new() addresses = resolver.addresses_for("www.google.com.") @@ -35,6 +35,16 @@ end # [#, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #] ``` +You can also specify custom DNS servers: + +``` ruby +resolver = Async::DNS::Resolver.new(Async::DNS::System.standard_connections(['8.8.8.8'])) + +# or + +resolver = Async::DNS::Resolver.new([[:udp, "8.8.8.8", 53], [:tcp, "8.8.8.8", 53]]) +``` + ### Server Here is a simple example showing how to use the server: diff --git a/lib/async/dns/resolver.rb b/lib/async/dns/resolver.rb index 7c09074..276f82c 100644 --- a/lib/async/dns/resolver.rb +++ b/lib/async/dns/resolver.rb @@ -19,6 +19,7 @@ # THE SOFTWARE. require_relative 'handler' +require_relative 'system' require 'securerandom' require 'async' @@ -46,8 +47,8 @@ class Resolver # Servers are specified in the same manor as options[:listen], e.g. # [:tcp/:udp, address, port] # In the case of multiple servers, they will be checked in sequence. - def initialize(endpoints, origin: nil, logger: Console.logger, timeout: DEFAULT_TIMEOUT) - @endpoints = endpoints + def initialize(endpoints = nil, origin: nil, logger: Console.logger, timeout: DEFAULT_TIMEOUT) + @endpoints = endpoints || System.nameservers @origin = origin @logger = logger diff --git a/spec/async/dns/resolver_spec.rb b/spec/async/dns/resolver_spec.rb index aebd215..f2df2e5 100755 --- a/spec/async/dns/resolver_spec.rb +++ b/spec/async/dns/resolver_spec.rb @@ -101,4 +101,13 @@ expect(addresses.size).to be > 0 end + + it "should default to system resolvers" do + resolver = Async::DNS::Resolver.new() + + response = resolver.query('google.com') + + expect(response.class).to be == Async::DNS::Message + expect(response.answer.size).to be > 0 + end end