Class: Globetrotter

Inherits:
Object
  • Object
show all
Defined in:
lib/globetrotter.rb,
lib/globetrotter/version.rb

Constant Summary collapse

VERSION =
"0.0.6"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Globetrotter

Returns a new instance of Globetrotter.



11
12
13
14
15
16
17
18
# File 'lib/globetrotter.rb', line 11

def initialize(options)
  @ns_max_age_minutes = options.ns_max_age_minutes
  @ns_count_to_check = options.ns_count_to_check
  @timeout_seconds = options.timeout_seconds
  @query = options.domain
  @concurrency = options.ns_query_concurrency
  @ns_ips = fetch_ns_ips
end

Class Method Details

.run(options) ⇒ Object



65
66
67
# File 'lib/globetrotter.rb', line 65

def self.run(options)
  new(options).run
end

Instance Method Details

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
# File 'lib/globetrotter.rb', line 20

def run
  ns_count = @ns_ips.count
  @ns_count_to_check = ns_count if @ns_count_to_check > ns_count
  @ns_ips = @ns_ips.sample(@ns_count_to_check)

  message = "Found #{ns_count} nameserver(s). "\
            "Querying #{@ns_count_to_check} of those "\
            "for #{query}, #{@concurrency} at a time, "\
            "with a timeout of #{@timeout_seconds} second(s)."
  $stderr.puts message

  EM.run do
    result_ips = Set.new
    ok = 0
    nok = 0
    EM::Iterator.new(@ns_ips, @concurrency).each(
      proc do |ns_ip, iter|
        resolver = RubyDNS::Resolver.new(
          [[:udp, ns_ip.to_s, 53]],
          timeout: @timeout_seconds
        )
        resolver.query(query) do |response|
          case response
          when RubyDNS::Message
            response.answer.each do |answer|
              address = answer[2].address.to_s
              result_ips.add(address)
            end
            ok += 1
            iter.next
          when RubyDNS::ResolutionFailure
            nok += 1
            iter.next
          end
        end
      end,
      proc do
        EM.stop
        result_ips.each { |ip| puts ip }
        $stderr.puts "#{ok} succeeded, #{nok} failed (#{ok + nok} total)"
      end
    )
  end
end