Skip to content

Commit

Permalink
Merge pull request #5 from taketo1113/cli-ddr
Browse files Browse the repository at this point in the history
Add DDR option for CLI
  • Loading branch information
taketo1113 authored May 13, 2024
2 parents 3520073 + 1c381f2 commit 6a38ed0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 10 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ Usage: ddig [options] hostname
--dot use resolve type of dot
--doh-h1 use resolve type of doh (http/1.1)
--doh-path=doh-path doh service path
--ddr discover designated resolvers via ddr (discovery of designated resolvers)
-4, --ipv4 use IPv4 query transport only
-6, --ipv6 use IPv6 query transport only
-@ipaddress|doh-hostname, nameserver
Expand Down Expand Up @@ -178,6 +179,25 @@ dns.google AAAA 2001:4860:4860::8844
# PORT: 443
```

- DDR (Discovery of Designated Resolvers)
```sh
$ ddig --ddr --nameserver 8.8.8.8
dot: dns.google:853 (8.8.8.8), unencrypted_resolver: 8.8.8.8, verify cert: true
dot: dns.google:853 (8.8.4.4), unencrypted_resolver: 8.8.8.8, verify cert: true
dot: dns.google:853 (2001:4860:4860::8844), unencrypted_resolver: 8.8.8.8, verify cert: true
dot: dns.google:853 (2001:4860:4860::8888), unencrypted_resolver: 8.8.8.8, verify cert: true
h2: dns.google:443 (8.8.8.8), path: /dns-query{?dns}, unencrypted_resolver: 8.8.8.8, verify cert: true
h2: dns.google:443 (8.8.4.4), path: /dns-query{?dns}, unencrypted_resolver: 8.8.8.8, verify cert: true
h2: dns.google:443 (2001:4860:4860::8844), path: /dns-query{?dns}, unencrypted_resolver: 8.8.8.8, verify cert: true
h2: dns.google:443 (2001:4860:4860::8888), path: /dns-query{?dns}, unencrypted_resolver: 8.8.8.8, verify cert: true
h3: dns.google:443 (8.8.4.4), path: /dns-query{?dns}, unencrypted_resolver: 8.8.8.8, verify cert: true
h3: dns.google:443 (8.8.8.8), path: /dns-query{?dns}, unencrypted_resolver: 8.8.8.8, verify cert: true
h3: dns.google:443 (2001:4860:4860::8888), path: /dns-query{?dns}, unencrypted_resolver: 8.8.8.8, verify cert: true
h3: dns.google:443 (2001:4860:4860::8844), path: /dns-query{?dns}, unencrypted_resolver: 8.8.8.8, verify cert: true

# SERVER: 8.8.8.8
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
55 changes: 45 additions & 10 deletions lib/ddig/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(args)

parse_options

if @hostname.nil?
unless valid_options?
puts @option_parser
exit
end
Expand All @@ -28,6 +28,7 @@ def parse_options
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' }
opts.on("--doh-path=doh-path", "doh service path") { |v| @options[:doh_path] = v }
opts.on("--ddr", "discover designated resolvers via ddr (discovery of designated resolvers)") { |v| @options[:ddr] = v }
opts.on("-4", "--ipv4", "use IPv4 query transport only") { |v| @options[:ipv4] = v }
opts.on("-6", "--ipv6", "use IPv6 query transport only") { |v| @options[:ipv6] = v }
opts.on("-@", "--nameserver=ipaddress|doh-hostname", "nameserver") { |v| @options[:nameserver] = v }
Expand All @@ -46,21 +47,39 @@ def parse_options
@hostname = @args[0]
end

def valid_options?
if @hostname.nil?
if @options[:ddr]
return true
end

return false
end

return true
end

def exec
if @options[:ipv4] || @options[:ipv6]
@use_ipv4 = @options[:ipv4] || false
@use_ipv6 = @options[:ipv6] || false
end

case @options[:dns_type]
when "all"
resolve_all
when "do53"
resolve_do53
when "dot"
resolve_dot
when "doh_h1"
resolve_doh_h1
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
end
end

Expand Down Expand Up @@ -137,5 +156,21 @@ def resolve_doh_h1
puts "# SERVER(Path): #{doh.dohpath}"
puts "# PORT: #{doh.port}"
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(', ')}"
end
end
end

0 comments on commit 6a38ed0

Please sign in to comment.