class DNSAdapter::ResolvClient

An adapter client for the internal Resolv DNS client.

Constants

SUPPORTED_RR_TYPES
TRAILING_DOT_REGEXP

Public Class Methods

type_class(rr_type) click to toggle source
# File lib/dns_adapter/resolv_client.rb, line 51
def self.type_class(rr_type)
  raise ArgumentError, "Unknown RR type: #{rr_type}" unless SUPPORTED_RR_TYPES.include?(rr_type)
  Resolv::DNS::Resource::IN.const_get(rr_type)
end

Public Instance Methods

fetch_a_records(domain) click to toggle source
# File lib/dns_adapter/resolv_client.rb, line 7
def fetch_a_records(domain)
  fetch_a_type_records(domain, 'A')
end
fetch_aaaa_records(domain) click to toggle source
# File lib/dns_adapter/resolv_client.rb, line 11
def fetch_aaaa_records(domain)
  fetch_a_type_records(domain, 'AAAA')
end
fetch_cname_records(domain) click to toggle source
# File lib/dns_adapter/resolv_client.rb, line 42
def fetch_cname_records(domain)
  fetch_name_records(domain, 'CNAME')
end
fetch_mx_records(domain) click to toggle source
# File lib/dns_adapter/resolv_client.rb, line 15
def fetch_mx_records(domain)
  fetch_records(domain, 'MX') do |record|
    {
      type: 'MX',
      exchange: record.exchange.to_s,
      preference: record.preference,
      ttl: record.ttl
    }
  end
end
fetch_ns_records(domain) click to toggle source
# File lib/dns_adapter/resolv_client.rb, line 38
def fetch_ns_records(domain)
  fetch_name_records(domain, 'NS')
end
fetch_ptr_records(arpa_address) click to toggle source
# File lib/dns_adapter/resolv_client.rb, line 26
def fetch_ptr_records(arpa_address)
  fetch_name_records(arpa_address, 'PTR')
end
fetch_spf_records(domain) click to toggle source
# File lib/dns_adapter/resolv_client.rb, line 34
def fetch_spf_records(domain)
  fetch_txt_type_records(domain, 'SPF')
end
fetch_txt_records(domain) click to toggle source
# File lib/dns_adapter/resolv_client.rb, line 30
def fetch_txt_records(domain)
  fetch_txt_type_records(domain, 'TXT')
end
timeouts=(timeouts) click to toggle source
# File lib/dns_adapter/resolv_client.rb, line 46
def timeouts=(timeouts)
  dns_resolver.timeouts = timeouts
end

Private Instance Methods

dns_lookup(domain, rr_type) click to toggle source
# File lib/dns_adapter/resolv_client.rb, line 102
def dns_lookup(domain, rr_type)
  domain = normalize_domain(domain)
  resources = getresources(domain, rr_type)

  unless resources
    raise DNSAdapter::Error,
          "Unknown error on DNS '#{rr_type}' lookup of '#{domain}'"
  end

  resources
end
dns_resolver() click to toggle source
# File lib/dns_adapter/resolv_client.rb, line 124
def dns_resolver
  @dns_resolver ||= Resolv::DNS.new
end
fetch_a_type_records(domain, type) click to toggle source
# File lib/dns_adapter/resolv_client.rb, line 58
def fetch_a_type_records(domain, type)
  fetch_records(domain, type) do |record|
    {
      type: type,
      address: record.address.to_s,
      ttl: record.ttl
    }
  end
end
fetch_name_records(domain, type) click to toggle source
# File lib/dns_adapter/resolv_client.rb, line 82
def fetch_name_records(domain, type)
  fetch_records(domain, type) do |record|
    {
      type: type,
      name: record.name.to_s,
      ttl: record.ttl
    }
  end
end
fetch_records(domain, type, &block) click to toggle source
# File lib/dns_adapter/resolv_client.rb, line 92
def fetch_records(domain, type, &block)
  records = dns_lookup(domain, type)
  records.map(&block)
end
fetch_txt_type_records(domain, type) click to toggle source
# File lib/dns_adapter/resolv_client.rb, line 68
def fetch_txt_type_records(domain, type)
  fetch_records(domain, type) do |record|
    {
      type: type,
      # Use strings.join('') to avoid JRuby issue where
      # data only returns the first string
      text: record.strings.join('').encode('US-ASCII', invalid: :replace,
                                                       undef: :replace,
                                                       replace: '?'),
      ttl: record.ttl
    }
  end
end
getresources(domain, rr_type) click to toggle source
# File lib/dns_adapter/resolv_client.rb, line 114
def getresources(domain, rr_type)
  rr_class = self.class.type_class(rr_type)
  dns_resolver.getresources(domain, rr_class)
rescue Resolv::ResolvTimeout
  raise DNSAdapter::TimeoutError,
        "Time-out on DNS '#{rr_type}' lookup of '#{domain}'"
rescue Resolv::ResolvError
  raise DNSAdapter::Error, "Error on DNS lookup of '#{domain}'"
end
normalize_domain(domain) click to toggle source
# File lib/dns_adapter/resolv_client.rb, line 98
def normalize_domain(domain)
  (domain.sub(TRAILING_DOT_REGEXP, '') || domain).downcase
end