Skip to content

Commit

Permalink
Add hosts records to resolver cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jul 23, 2024
1 parent 4156465 commit f880630
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/async/dns/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ def age(now)
end

def fresh?(now = Async::Clock.now)
self.age(now) <= resource.ttl
if ttl = resource.ttl
self.age(now) <= ttl
else
true
end
end
end

Expand Down
34 changes: 34 additions & 0 deletions lib/async/dns/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def self.ipv6?
list.any? {|a| a.ipv6? && !a.ipv6_loopback? && !a.ipv6_linklocal? }
end

Resource = Struct.new(:address, :ttl)

# An interface for querying the system's hosts file.
class Hosts
# Hosts for the local system.
Expand Down Expand Up @@ -84,17 +86,31 @@ def add(address, names)
@addresses[address] += names

names.each do |name|
name = Resolv::DNS::Name.create(name).with_origin(nil)

@names[name] ||= []
@names[name] << address
end
end

def each(&block)
@names.each(&block)
end

# Parse a hosts file and add the entries.
def parse_hosts(io)
io.each do |line|
line.sub!(/#.*/, '')
address, hostname, *aliases = line.split(/\s+/)

if address =~ Resolv::IPv4::Regex
address = Resolv::IPv4.create(address)
elsif address =~ Resolv::IPv6::Regex
address = Resolv::IPv6.create(address)
else
next
end

add(address, [hostname] + aliases)
end
end
Expand Down Expand Up @@ -169,6 +185,24 @@ def self.resolver(**options)
options[:search] = [nil]
end

if hosts = Hosts.local
cache = options.fetch(:cache) do
options[:cache] = Cache.new
end

hosts.each do |name, addresses|
addresses.each do |address|
resource = Resource.new(address, nil)
case address
when Resolv::IPv4
cache.store(name, Resolv::DNS::Resource::IN::A, resource)
when Resolv::IPv6
cache.store(name, Resolv::DNS::Resource::IN::AAAA, resource)
end
end
end
end

timeout = options.delete(:timeout) || DEFAULT_TIMEOUT
endpoint = Endpoint.for(nameservers, timeout: timeout)

Expand Down

0 comments on commit f880630

Please sign in to comment.