Class: Ddig::Ddr::DesignatedResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/ddig/ddr/designated_resolver.rb

Constant Summary collapse

PROTOCOLS =
['http/1.1', 'h2', 'h3', 'dot', 'doq']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unencrypted_resolver:, target:, protocol: nil, port: nil, dohpath: nil, address: nil, ip: nil) ⇒ DesignatedResolver

Returns a new instance of DesignatedResolver.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ddig/ddr/designated_resolver.rb', line 12

def initialize(unencrypted_resolver:, target:, protocol: nil, port: nil, dohpath: nil, address: nil, ip: nil)
  @target = target
  @unencrypted_resolver = unencrypted_resolver
  @protocol = protocol
  @port = port
  @dohpath = dohpath
  @address = address
  @ip = ip

  # check protocol
  unless PROTOCOLS.include?(@protocol)
    raise Error.new("Not Supportted Protocol (protocol: #{@protocol}). Suported protocol is #{PROTOCOLS.join(' / ')}")
  end

  if @port.nil?
    set_default_port
  end
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



8
9
10
# File 'lib/ddig/ddr/designated_resolver.rb', line 8

def a
  @a
end

#aaaaObject (readonly)

Returns the value of attribute aaaa.



8
9
10
# File 'lib/ddig/ddr/designated_resolver.rb', line 8

def aaaa
  @aaaa
end

#addressObject (readonly)

Returns the value of attribute address.



6
7
8
# File 'lib/ddig/ddr/designated_resolver.rb', line 6

def address
  @address
end

#dohpathObject (readonly)

Returns the value of attribute dohpath.



6
7
8
# File 'lib/ddig/ddr/designated_resolver.rb', line 6

def dohpath
  @dohpath
end

#errorsObject (readonly)

Returns the value of attribute errors.



8
9
10
# File 'lib/ddig/ddr/designated_resolver.rb', line 8

def errors
  @errors
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



8
9
10
# File 'lib/ddig/ddr/designated_resolver.rb', line 8

def hostname
  @hostname
end

#ipObject (readonly)

Returns the value of attribute ip.



6
7
8
# File 'lib/ddig/ddr/designated_resolver.rb', line 6

def ip
  @ip
end

#portObject (readonly)

Returns the value of attribute port.



6
7
8
# File 'lib/ddig/ddr/designated_resolver.rb', line 6

def port
  @port
end

#protocolObject (readonly)

Returns the value of attribute protocol.



6
7
8
# File 'lib/ddig/ddr/designated_resolver.rb', line 6

def protocol
  @protocol
end

#resolverObject (readonly)

Returns the value of attribute resolver.



8
9
10
# File 'lib/ddig/ddr/designated_resolver.rb', line 8

def resolver
  @resolver
end

#targetObject (readonly)

Returns the value of attribute target.



6
7
8
# File 'lib/ddig/ddr/designated_resolver.rb', line 6

def target
  @target
end

#unencrypted_resolverObject (readonly)

Returns the value of attribute unencrypted_resolver.



6
7
8
# File 'lib/ddig/ddr/designated_resolver.rb', line 6

def unencrypted_resolver
  @unencrypted_resolver
end

#verify_certObject (readonly)

Returns the value of attribute verify_cert.



7
8
9
# File 'lib/ddig/ddr/designated_resolver.rb', line 7

def verify_cert
  @verify_cert
end

Instance Method Details

#as_jsonObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ddig/ddr/designated_resolver.rb', line 66

def as_json(*)
  {
    unencrypted_resolver: @unencrypted_resolver,
    target: @target,
    protocol: @protocol,
    port: @port,
    dohpath: @dohpath,
    address: @address,
    ip: @ip,
    verify: @verify_cert&.verify,
    hostname: @hostname,
    a: @a,
    aaaa: @aaaa,
    errors: @errors,
  }
end

#lookup(hostname) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ddig/ddr/designated_resolver.rb', line 36

def lookup(hostname)
  @hostname = hostname
  @errors = []

  case @protocol
  when 'dot'
    @resolver = Ddig::Resolver::Dot.new(hostname: @hostname, server: @address, server_name: @target, port: @port).lookup

    unless @resolver.nil?
      @a = @resolver.a
      @aaaa = @resolver.aaaa

      return self
    end

  when 'http/1.1', 'h2', 'h3'
    @resolver = Ddig::Resolver::DohH1.new(hostname: @hostname, server: @address, address: @address, dohpath: @dohpath, port: @port).lookup

    unless @resolver.nil?
      @a = @resolver.a
      @aaaa = @resolver.aaaa

      return self
    end

  when 'doq'
    @errors << "#{@protocol} is not supportted protocol"
  end
end

#set_default_portObject

Set default port by protocol ref: www.rfc-editor.org/rfc/rfc9461.html#section-4.2



101
102
103
104
105
106
107
108
# File 'lib/ddig/ddr/designated_resolver.rb', line 101

def set_default_port
  case @protocol
  when 'http/1.1', 'h2', 'h3'
    @port = 443
  when 'dot', 'doq'
    @port = 853
  end
end

#to_cliObject



87
88
89
# File 'lib/ddig/ddr/designated_resolver.rb', line 87

def to_cli
  @resolver.to_cli
end

#to_json(*args) ⇒ Object



83
84
85
# File 'lib/ddig/ddr/designated_resolver.rb', line 83

def to_json(*args)
  as_json.to_json
end

#to_sObject



91
92
93
94
95
96
97
# File 'lib/ddig/ddr/designated_resolver.rb', line 91

def to_s
  if ['http/1.1', 'h2', 'h3'].include?(@protocol)
    "#{@protocol}: #{@target}:#{@port} (#{@address}),\tpath: #{@dohpath},\tunencrypted_resolver: #{@unencrypted_resolver}, \tverify cert: #{@verify_cert.verify}"
  else
    "#{@protocol}: #{@target}:#{@port} (#{@address}),\tunencrypted_resolver: #{@unencrypted_resolver}, \tverify cert: #{@verify_cert.verify}"
  end
end

#uniq_keyObject



110
111
112
# File 'lib/ddig/ddr/designated_resolver.rb', line 110

def uniq_key
  "#{@unencrypted_resolver}-#{@target}-#{@protocol}-#{@port}-#{@dohpath}-#{@address}-#{@ip}"
end

#verifyObject



31
32
33
34
# File 'lib/ddig/ddr/designated_resolver.rb', line 31

def verify
  @verify_cert = VerifyCert.new(hostname: @target, address: @address, port: @port, unencrypted_resolver: @unencrypted_resolver)
  @verify_cert.verify
end