From 3be19cc5ba17e084c10c8f5d0eb114b01e53f733 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Tue, 8 Aug 2017 15:59:53 +0300 Subject: Use a case-statement for both kinds of IP-matching. --- lib/custodian/protocoltest/http.rb | 64 +++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/lib/custodian/protocoltest/http.rb b/lib/custodian/protocoltest/http.rb index 32073f8..9ff734c 100644 --- a/lib/custodian/protocoltest/http.rb +++ b/lib/custodian/protocoltest/http.rb @@ -61,44 +61,52 @@ module Custodian # def ignore_failure?( protocol ) - # IP addresses we found for the host - ips = [] - # Get the hostname we're connecting to. u = URI.parse(@url) target = u.host - # - # Resolve the target to an IP, unless it is already an address. - # - if (target =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/) || - (target =~ /^([0-9a-f:]+)$/) - ips.push(target) + # IPs for the target + ips = [] + + case protocol + when :ipv4 + if (target =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/) + ips << target + end + when :ipv6 + if (target =~ /^([0-9a-f:]+)$/) + ips << target + end else + raise ArgumentError, "Sanity-checking DNS-failure of unknown type: #{protocol}" + end - # - # OK if it didn't look like an IP address then attempt to - # look it up, as both IPv4 and IPv6. - # - begin - timeout(30) do + # Early termination? + return true unless ips.empty? + # + # OK if it didn't look like an IP address then attempt to + # look it up, as both IPv4 and IPv6. + # + begin + timeout(30) do + + type = case protocol + when :ipv4 + Resolv::DNS::Resource::IN::A + when :ipv6 + Resolv::DNS::Resource::IN::AAAA + else + raise ArgumentError, "Sanity-checking DNS-failure of unknown type: #{protocol}" + end + + begin Resolv::DNS.open do |dns| - - if ( protocol == :ipv4 ) - ress = dns.getresources(target, Resolv::DNS::Resource::IN::A) - ress.map { |r| ips.push(r.address.to_s) } - elsif ( protocol == :ipv6 ) - - ress = dns.getresources(target, Resolv::DNS::Resource::IN::AAAA) - ress.map { |r| ips.push(r.address.to_s) } - else - raise ArgumentError, "Sanity-checking DNS-failure of unknown type" - end + ips = dns.getresources(target, type) end + rescue Timeout::Error => _e + # NOP end - rescue Timeout::Error => _e - # NOP end end -- cgit v1.2.1