Class: PassiveDNS::Provider::DNSDB

Inherits:
PassiveDNS::PassiveDB show all
Defined in:
lib/passivedns/client/provider/dnsdb.rb

Overview

Queries FarSight’s passive DNS database

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DNSDB

Options

  • :debug Sets the debug flag for the module

  • “APIKEY” REQUIRED: The API key associated with DNSDB

  • “URL” Alternate url for testing. Defaults to “api.dnsdb.info/lookup

Example Instantiation

options = {
  :debug => true,
  "APIKEY" => "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
  "URL" => "https://api.dnsdb.info/lookup"
}

PassiveDNS::Provider::DNSDB.new(options)


42
43
44
45
46
# File 'lib/passivedns/client/provider/dnsdb.rb', line 42

def initialize(options={})
  @debug = options[:debug] || false
    @key = options["APIKEY"] || raise("APIKEY option required for #{self.class}")
    @base = options["URL"] || "https://api.dnsdb.info/lookup"
end

Instance Attribute Details

#debugObject

:debug enables verbose logging to standard output



26
27
28
# File 'lib/passivedns/client/provider/dnsdb.rb', line 26

def debug
  @debug
end

Class Method Details

.config_section_nameObject

Sets the configuration section name to “dnsdb”



17
18
19
# File 'lib/passivedns/client/provider/dnsdb.rb', line 17

def self.config_section_name
  "dnsdb"
end

.nameObject

Sets the modules self-reported name to “DNSDB”



13
14
15
# File 'lib/passivedns/client/provider/dnsdb.rb', line 13

def self.name
  "DNSDB"
end

.option_letterObject

Sets the command line database argument to “d”



21
22
23
# File 'lib/passivedns/client/provider/dnsdb.rb', line 21

def self.option_letter
  "d"
end

Instance Method Details

#lookup(label, limit = nil) ⇒ Object

Takes a label (either a domain or an IP address) and returns an array of PassiveDNS::PDNSResult instances with the answers to the query



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/passivedns/client/provider/dnsdb.rb', line 50

def lookup(label, limit=nil)
  $stderr.puts "DEBUG: #{self.class.name}.lookup(#{label})" if @debug
  Timeout::timeout(240) {
    url = nil
    if label =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(\/\d{1,2})?$/
      label = label.gsub(/\//,',')
      url = "#{@base}/rdata/ip/#{label}"
    else
      url = "#{@base}/rrset/name/#{label}"
    end
    url = URI.parse url
    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = (url.scheme == 'https')
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    http.verify_depth = 5
      path = url.path
      if limit
        path << "?limit=#{limit}"
      end
    request = Net::HTTP::Get.new(path)
    request.add_field("User-Agent", "Ruby/#{RUBY_VERSION} passivedns-client rubygem v#{PassiveDNS::Client::VERSION}")
    request.add_field("X-API-Key", @key)
    request.add_field("Accept", "application/json")
    t1 = Time.now
    response = http.request(request)
    t2 = Time.now
    $stderr.puts response.body if @debug
    parse_json(response.body,t2-t1)
  }
rescue Timeout::Error => e
  $stderr.puts "#{self.class.name} lookup timed out: #{label}"
end