Class: Ddig::Resolver::Do53

Inherits:
Object
  • Object
show all
Defined in:
lib/ddig/resolver/do53.rb

Overview

DNS Resolver of UDP/53

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname:, nameservers: nil, ip: nil) ⇒ Do53

Returns a new instance of Do53.



11
12
13
14
15
16
17
# File 'lib/ddig/resolver/do53.rb', line 11

def initialize(hostname:, nameservers: nil, ip: nil)
  @hostname = hostname
  @ip = ip

  @nameserver = Ddig::Nameserver.new(nameservers: nameservers)
  set_nameservers
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



9
10
11
# File 'lib/ddig/resolver/do53.rb', line 9

def a
  @a
end

#aaaaObject (readonly)

Returns the value of attribute aaaa.



9
10
11
# File 'lib/ddig/resolver/do53.rb', line 9

def aaaa
  @aaaa
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



8
9
10
# File 'lib/ddig/resolver/do53.rb', line 8

def hostname
  @hostname
end

#ipObject (readonly)

Returns the value of attribute ip.



8
9
10
# File 'lib/ddig/resolver/do53.rb', line 8

def ip
  @ip
end

#nameserversObject (readonly)

Returns the value of attribute nameservers.



8
9
10
# File 'lib/ddig/resolver/do53.rb', line 8

def nameservers
  @nameservers
end

Instance Method Details

#as_jsonObject



37
38
39
40
41
42
43
44
45
# File 'lib/ddig/resolver/do53.rb', line 37

def as_json(*)
  {
    a: @a,
    aaaa: @aaaa,
    hostname: @hostname,
    nameservers: @nameservers,
    ip: @ip,
  }
end

#lookupObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ddig/resolver/do53.rb', line 19

def lookup
  if @nameservers.empty?
    return nil
  end

  @a = Resolv::DNS.open(nameserver: @nameservers) do |dns|
    ress = dns.getresources(@hostname, Resolv::DNS::Resource::IN::A)
    ress.map { |resource| resource.address.to_s }
  end

  @aaaa = Resolv::DNS.open(nameserver: @nameservers) do |dns|
    ress = dns.getresources(@hostname, Resolv::DNS::Resource::IN::AAAA)
    ress.map { |resource| resource.address.to_s }
  end

  self
end

#set_nameserversObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ddig/resolver/do53.rb', line 65

def set_nameservers
  @nameservers = @nameserver.servers

  if @ip == :ipv4
    @nameservers = @nameserver.servers_ipv4
  end

  if @ip == :ipv6
    @nameservers = @nameserver.servers_ipv6
  end
end

#to_cliObject



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ddig/resolver/do53.rb', line 51

def to_cli
  @a.each do |address|
    rr_type = 'A'
    puts "#{@hostname}\t#{rr_type}\t#{address}"
  end
  @aaaa.each do |address|
    rr_type = 'AAAA'
    puts "#{@hostname}\t#{rr_type}\t#{address}"
  end

  puts
  puts "# SERVER: #{@nameservers.join(', ')}"
end

#to_json(*args) ⇒ Object



47
48
49
# File 'lib/ddig/resolver/do53.rb', line 47

def to_json(*args)
  as_json.to_json
end