Class: Ddig::Resolver::DohH1

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

Overview

DNS over HTTPS (HTTP/1.1)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname:, server:, address: nil, dohpath:, port: 443) ⇒ DohH1

Returns a new instance of DohH1.



15
16
17
18
19
20
21
22
23
# File 'lib/ddig/resolver/doh_h1.rb', line 15

def initialize(hostname:, server:, address: nil, dohpath:, port: 443)
  @hostname = hostname
  @server = server
  @address = address
  @dohpath = dohpath
  @port = port || 443

  @open_timeout = 10
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



13
14
15
# File 'lib/ddig/resolver/doh_h1.rb', line 13

def a
  @a
end

#aaaaObject (readonly)

Returns the value of attribute aaaa.



13
14
15
# File 'lib/ddig/resolver/doh_h1.rb', line 13

def aaaa
  @aaaa
end

#addressObject (readonly)

Returns the value of attribute address.



12
13
14
# File 'lib/ddig/resolver/doh_h1.rb', line 12

def address
  @address
end

#dohpathObject (readonly)

Returns the value of attribute dohpath.



12
13
14
# File 'lib/ddig/resolver/doh_h1.rb', line 12

def dohpath
  @dohpath
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



12
13
14
# File 'lib/ddig/resolver/doh_h1.rb', line 12

def hostname
  @hostname
end

#portObject (readonly)

Returns the value of attribute port.



12
13
14
# File 'lib/ddig/resolver/doh_h1.rb', line 12

def port
  @port
end

#serverObject (readonly)

Returns the value of attribute server.



12
13
14
# File 'lib/ddig/resolver/doh_h1.rb', line 12

def server
  @server
end

Instance Method Details

#as_jsonObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ddig/resolver/doh_h1.rb', line 61

def as_json(*)
  {
    a: @a,
    aaaa: @aaaa,
    hostname: @hostname,
    server: @server,
    address: @address,
    dohpath: @dohpath,
    port: @port,
  }
end

#get_resources(hostname, typeclass) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ddig/resolver/doh_h1.rb', line 37

def get_resources(hostname, typeclass)
  # send query
  payload = DnsMessage.encode(hostname, typeclass)

  path_with_query = @dohpath.gsub('{?dns}', '?dns=' + Base64.urlsafe_encode64(payload, padding: false))

  http_response = Net::HTTP.start(@server, @port, use_ssl: true, ipaddr: @address) do |http|
    header = {}
    header['Accept'] = 'application/dns-message'
    #http.open_timeout = @open_timeout

    http.get(path_with_query, header)
  end

  case http_response
  when Net::HTTPSuccess
    # recive answer
    return DnsMessage.getresources(http_response.body)
  else
    http_response.value
    return []
  end
end

#lookupObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ddig/resolver/doh_h1.rb', line 25

def lookup
  if @server.nil?
    return nil
  end

  @a = get_resources(@hostname, Resolv::DNS::Resource::IN::A).map { |resource| resource.address.to_s if resource.is_a?(Resolv::DNS::Resource::IN::A) }.compact

  @aaaa = get_resources(@hostname, Resolv::DNS::Resource::IN::AAAA).map { |resource| resource.address.to_s if resource.is_a?(Resolv::DNS::Resource::IN::AAAA) }.compact

  self
end

#to_cliObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ddig/resolver/doh_h1.rb', line 77

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(Hostname): #{@server}"
  puts "# SERVER(Path): #{@dohpath}"
  puts "# PORT: #{@port}"
end

#to_json(*args) ⇒ Object



73
74
75
# File 'lib/ddig/resolver/doh_h1.rb', line 73

def to_json(*args)
  as_json.to_json
end