Skip to content

Commit

Permalink
Default to system nameservers if a Resolver is initialized without an…
Browse files Browse the repository at this point in the history
…y. (#30)
  • Loading branch information
seandilda authored Jul 12, 2024
1 parent 1fa7784 commit 99ffdd5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand All @@ -35,6 +35,16 @@ end
# [#<Resolv::IPv4 202.124.127.240>, #<Resolv::IPv4 202.124.127.216>, #<Resolv::IPv4 202.124.127.223>, #<Resolv::IPv4 202.124.127.227>, #<Resolv::IPv4 202.124.127.234>, #<Resolv::IPv4 202.124.127.230>, #<Resolv::IPv4 202.124.127.208>, #<Resolv::IPv4 202.124.127.249>, #<Resolv::IPv4 202.124.127.219>, #<Resolv::IPv4 202.124.127.218>, #<Resolv::IPv4 202.124.127.212>, #<Resolv::IPv4 202.124.127.241>, #<Resolv::IPv4 202.124.127.238>, #<Resolv::IPv4 202.124.127.245>, #<Resolv::IPv4 202.124.127.251>, #<Resolv::IPv4 202.124.127.229>]
```

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:
Expand Down
5 changes: 3 additions & 2 deletions lib/async/dns/resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# THE SOFTWARE.

require_relative 'handler'
require_relative 'system'

require 'securerandom'
require 'async'
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spec/async/dns/resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 99ffdd5

Please sign in to comment.