Skip to content

Commit

Permalink
Replaced form nameserver variable to nameserver class for ddr & resol…
Browse files Browse the repository at this point in the history
…ver class
  • Loading branch information
taketo1113 committed Mar 16, 2024
1 parent ec8de4f commit 34fbd0a
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 78 deletions.
39 changes: 7 additions & 32 deletions lib/ddig/ddr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class Ddr
def initialize(nameservers: nil, ip: nil)
@ip = ip

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

# discover designated resolvers
query_svcb_records
Expand All @@ -23,7 +24,7 @@ def initialize(nameservers: nil, ip: nil)
def query_svcb_records
@svcb_records = []

if @nameservers.nil? || @nameservers.empty?
if @nameservers.empty?
return @svcb_records
end

Expand Down Expand Up @@ -124,42 +125,16 @@ def verify_discovery
end
end

def set_nameservers(nameservers)
if nameservers.nil?
@nameservers = Resolv::DNS::Config.default_config_hash[:nameserver]
else
if nameservers.is_a?(Array)
@nameservers = nameservers
else
@nameservers = [nameservers]
end

@nameservers.map! { |nameserver| IPAddr.new(nameserver).to_s rescue nil }.compact!
end
def set_nameservers
@nameservers = @nameserver.servers

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

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

def nameservers_ipv4
@nameservers.map do |nameserver|
if IPAddr.new(nameserver).ipv4?
nameserver
end
end.compact
end

def nameservers_ipv6
@nameservers.map do |nameserver|
if IPAddr.new(nameserver).ipv6?
nameserver
end
end.compact
end
end
end
16 changes: 16 additions & 0 deletions lib/ddig/nameserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ def default_servers
Resolv::DNS::Config.default_config_hash[:nameserver]
end

def servers_ipv4
@servers.map do |nameserver|
if IPAddr.new(nameserver).ipv4?
nameserver
end
end.compact
end

def servers_ipv6
@servers.map do |nameserver|
if IPAddr.new(nameserver).ipv6?
nameserver
end
end.compact
end

def validation_servers
@servers.uniq.each do |server|
addr = IPAddr.new(server) rescue nil
Expand Down
33 changes: 7 additions & 26 deletions lib/ddig/resolver/do53.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ def initialize(hostname:, nameservers: nil, ip: nil)
@hostname = hostname
@ip = ip

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

def lookup
if @nameservers.nil? || @nameservers.empty?
if @nameservers.empty?
return nil
end

Expand All @@ -32,37 +33,17 @@ def lookup
self
end

def set_nameservers(nameservers)
if nameservers.nil?
@nameservers = Resolv::DNS::Config.default_config_hash[:nameserver]
else
@nameservers = nameservers
end
def set_nameservers
@nameservers = @nameserver.servers

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

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

def nameservers_ipv4
@nameservers.map do |nameserver|
if IPAddr.new(nameserver).ipv4?
nameserver
end
end.compact
end

def nameservers_ipv6
@nameservers.map do |nameserver|
if IPAddr.new(nameserver).ipv6?
nameserver
end
end.compact
end
end
end
end
32 changes: 16 additions & 16 deletions spec/ddig/ddr_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
expect(svcb_record[:svcb_record].target.to_s).to eq 'dns.google'
end

it "return [] with empty namesevers" do
@ddr = Ddig::Ddr.new(nameservers: [])

expect(@ddr.svcb_records).to eq []
it "raise error with empty namesevers" do
expect {
@ddr = Ddig::Ddr.new(nameservers: [])
}.to raise_error(Ddig::Error)
end

it "return [] with invalid namesevers" do
@ddr = Ddig::Ddr.new(nameservers: 'invalid')

expect(@ddr.svcb_records).to eq []
it "raise error with invalid namesevers" do
expect {
@ddr = Ddig::Ddr.new(nameservers: 'invalid')
}.to raise_error(Ddig::Error)
end
end

Expand All @@ -50,16 +50,16 @@
expect(designated_resolver.verify_cert).not_to eq nil
end

it "return [] with empty namesevers" do
@ddr = Ddig::Ddr.new(nameservers: [])

expect(@ddr.designated_resolvers).to eq []
it "raise error with empty namesevers" do
expect {
@ddr = Ddig::Ddr.new(nameservers: [])
}.to raise_error(Ddig::Error)
end

it "return [] with invalid namesevers" do
@ddr = Ddig::Ddr.new(nameservers: 'invalid')

expect(@ddr.designated_resolvers).to eq []
it "raise error with invalid namesevers" do
expect {
@ddr = Ddig::Ddr.new(nameservers: 'invalid')
}.to raise_error(Ddig::Error)
end
end
end
28 changes: 28 additions & 0 deletions spec/ddig/nameserver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,32 @@
end
end
end

context "#servers_ipv4" do
it "return only ipv4 nameservers with namesevers" do
@nameserver = Ddig::Nameserver.new(nameservers: ['8.8.8.8', '2001:4860:4860::8888'])

expect(@nameserver.servers_ipv4).to eq ['8.8.8.8']
end

it "return empty array without ipv4 namesevers" do
@nameserver = Ddig::Nameserver.new(nameservers: ['2001:4860:4860::8888'])

expect(@nameserver.servers_ipv4).to eq []
end
end

context "#servers_ipv6" do
it "return only ipv6 nameservers with namesevers" do
@nameserver = Ddig::Nameserver.new(nameservers: ['8.8.8.8', '2001:4860:4860::8888'])

expect(@nameserver.servers_ipv6).to eq ['2001:4860:4860::8888']
end

it "return empty array without ipv6 namesevers" do
@nameserver = Ddig::Nameserver.new(nameservers: ['8.8.8.8'])

expect(@nameserver.servers_ipv6).to eq []
end
end
end
7 changes: 3 additions & 4 deletions spec/ddig/resolver/do53_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,13 @@
context "#lookup: with invalid nameserver" do
before(:each) do
@nameservers = ['invalid']

@do53 = Ddig::Resolver::Do53.new(hostname: 'dns.google', nameservers: @nameservers)
end

it "raise Error" do
expect {
@do53.lookup
}.to raise_error(StandardError) # ruby 3.3+: Socket::ResolutionError, ruby 3.2-: SocketError
@do53 = Ddig::Resolver::Do53.new(hostname: 'dns.google', nameservers: @nameservers)
#@do53.lookup
}.to raise_error(Ddig::Error)
end
end

Expand Down

0 comments on commit 34fbd0a

Please sign in to comment.