Skip to content

Commit

Permalink
Merge pull request #6 from taketo1113/resolve-with-ddr
Browse files Browse the repository at this point in the history
Resolve with Designated Resolvers (DDR)
  • Loading branch information
taketo1113 authored May 13, 2024
2 parents 6a38ed0 + 8cdbc9f commit fbaaa6d
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 63 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,43 @@ Usage: ddig [options] hostname
--version show version.
```

```sh
$ ddig --nameserver 8.8.8.8 dns.google
# Do53 (IPv4)
dns.google A 8.8.4.4
dns.google A 8.8.8.8
dns.google AAAA 2001:4860:4860::8844
dns.google AAAA 2001:4860:4860::8888

# SERVER: 8.8.8.8

# DDR
## DDR (#0) - dot: dns.google:853 (8.8.4.4), unencrypted_resolver: 8.8.8.8, verify cert: true
dns.google A 8.8.4.4
dns.google A 8.8.8.8
dns.google AAAA 2001:4860:4860::8844
dns.google AAAA 2001:4860:4860::8888

# SERVER(Address): 8.8.4.4
# PORT: 853

...

# SERVER(Address): 2001:4860:4860::8888
# PORT: 853

## DDR (#6) - h2: dns.google:443 (2001:4860:4860::8888), path: /dns-query{?dns}, unencrypted_resolver: 8.8.8.8, verify cert: true
dns.google A 8.8.8.8
dns.google A 8.8.4.4
dns.google AAAA 2001:4860:4860::8844
dns.google AAAA 2001:4860:4860::8888

# SERVER(Hostname): 2001:4860:4860::8888
# SERVER(Path): /dns-query{?dns}
# PORT: 443
...
```

- UDP(Do53)
```sh
$ ddig --udp --nameserver 8.8.8.8 dns.google
Expand Down
3 changes: 3 additions & 0 deletions lib/ddig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def self.lookup(hostname, nameservers: nil, use_ipv4: nil, use_ipv6: nil)
@do53_ipv6 = Ddig::Resolver::Do53.new(hostname: @hostname, nameservers: @nameserver.servers, ip: :ipv6).lookup unless @ip.ip_type == :ipv4

@ddr = Ddig::Ddr.new(nameservers: @nameservers, ip: @ip.ip_type)
@ddr.designated_resolvers.each do |designated_resolver|
designated_resolver.lookup(@hostname)
end

{
do53: {
Expand Down
100 changes: 37 additions & 63 deletions lib/ddig/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def parse_options
@option_parser = OptionParser.new do |opts|
opts.banner = "Usage: ddig [options] hostname"

opts.on("-d", "--dns-type={all|do53|dot}", "resolve type (default: all)") { |v| @options[:dns_type] = v }
opts.on("-d", "--dns-type={all|do53|dot|doh_h1}", "resolve type (default: all)") { |v| @options[:dns_type] = v }
opts.on("--udp", "use resolve type of udp(do53)") { |v| @options[:dns_type] = 'do53' }
opts.on("--dot", "use resolve type of dot") { |v| @options[:dns_type] = 'dot' }
opts.on("--doh-h1", "use resolve type of doh (http/1.1)") { |v| @options[:dns_type] = 'doh_h1' }
Expand Down Expand Up @@ -65,21 +65,20 @@ def exec
@use_ipv6 = @options[:ipv6] || false
end

unless @hostname.nil?
case @options[:dns_type]
when "all"
resolve_all
when "do53"
resolve_do53
when "dot"
resolve_dot
when "doh_h1"
resolve_doh_h1
end
end

if @options[:ddr]
resolve_ddr
exit
end

case @options[:dns_type]
when "all"
resolve_all
when "do53"
resolve_do53
when "dot"
resolve_dot
when "doh_h1"
resolve_doh_h1
end
end

Expand All @@ -88,9 +87,27 @@ def resolve_all

if @options[:format] == 'json'
# TODO: to_json
puts @ddig
else
puts @ddig
unless @ddig[:do53][:ipv4].nil?
puts "# Do53 (IPv4)"
@ddig[:do53][:ipv4].to_cli
puts
end

unless @ddig[:do53][:ipv6].nil?
puts "# Do53 (IPv6)"
@ddig[:do53][:ipv6].to_cli
puts
end

unless @ddig[:ddr].nil?
puts "# DDR"
@ddig[:ddr].each_with_index do |designated_resolver, index|
puts "## DDR (##{index}) - #{designated_resolver.to_s}"
designated_resolver.to_cli
puts
end
end
end
end

Expand All @@ -103,35 +120,13 @@ def resolve_do53
exit
end

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

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

def resolve_dot
dot = Ddig::Resolver::Dot.new(hostname: @hostname, server: @options[:nameserver], port: @options[:port]).lookup

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

puts
puts "# SERVER(Address): #{dot.server}"
#puts "# SERVER(Hostname): #{dot.server_name}"
puts "# PORT: #{dot.port}"
dot.to_cli
end

def resolve_doh_h1
Expand All @@ -142,35 +137,14 @@ def resolve_doh_h1

doh = Ddig::Resolver::DohH1.new(hostname: @hostname, server: @options[:nameserver], dohpath: @options[:doh_path], port: @options[:port]).lookup

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

puts
puts "# SERVER(Hostname): #{doh.server}"
puts "# SERVER(Path): #{doh.dohpath}"
puts "# PORT: #{doh.port}"
doh.to_cli
end

def resolve_ddr
ip = Ddig::Ip.new(use_ipv4: @use_ipv4, use_ipv6: @use_ipv6)
ddr = Ddig::Ddr.new(nameservers: @options[:nameserver], ip: ip.ip_type)

ddr.designated_resolvers.each_with_index do |designated_resolver, index|
if ['http/1.1', 'h2', 'h3'].include?(designated_resolver.protocol)
puts "#{designated_resolver.protocol}: #{designated_resolver.target}:#{designated_resolver.port} (#{designated_resolver.address}),\tpath: #{designated_resolver.dohpath},\tunencrypted_resolver: #{designated_resolver.unencrypted_resolver}, \tverify cert: #{designated_resolver.verify}"
else
puts "#{designated_resolver.protocol}: #{designated_resolver.target}:#{designated_resolver.port} (#{designated_resolver.address}),\tunencrypted_resolver: #{designated_resolver.unencrypted_resolver}, \tverify cert: #{designated_resolver.verify}"
end
end

puts
puts "# SERVER: #{ddr.nameservers.join(', ')}"
ddr.to_cli
end
end
end
9 changes: 9 additions & 0 deletions lib/ddig/ddr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ def verify_discovery
end
end

def to_cli
@designated_resolvers.each do |designated_resolver|
puts designated_resolver.to_s
end

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

def set_nameservers
@nameservers = @nameserver.servers

Expand Down
43 changes: 43 additions & 0 deletions lib/ddig/ddr/designated_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Ddr
class DesignatedResolver
attr_reader :unencrypted_resolver, :target, :protocol, :port, :dohpath, :address, :ip
attr_reader :verify_cert
attr_reader :hostname, :a, :aaaa, :resolver, :errors

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

Expand Down Expand Up @@ -30,6 +31,48 @@ def verify
@verify_cert.verify
end

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

def to_cli
@resolver.to_cli
end

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

# Set default port by protocol
# ref: https://www.rfc-editor.org/rfc/rfc9461.html#section-4.2
def set_default_port
Expand Down
14 changes: 14 additions & 0 deletions lib/ddig/resolver/do53.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ def lookup
self
end

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

def set_nameservers
@nameservers = @nameserver.servers

Expand Down
16 changes: 16 additions & 0 deletions lib/ddig/resolver/doh_h1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ def get_resources(hostname, typeclass)
return []
end
end

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
end
end
end
16 changes: 16 additions & 0 deletions lib/ddig/resolver/dot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ def get_socket
ssl_socket
end
end

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

0 comments on commit fbaaa6d

Please sign in to comment.