Skip to content
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

Default to system nameservers if a Resolver is initialized without any. #30

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading