Class: PassiveDNS::DNSDB

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DNSDB

Returns a new instance of DNSDB.



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

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

Returns the value of attribute debug.



23
24
25
# File 'lib/passivedns/client/dnsdb.rb', line 23

def debug
  @debug
end

Class Method Details

.config_section_nameObject

override



15
16
17
# File 'lib/passivedns/client/dnsdb.rb', line 15

def self.config_section_name
  "dnsdb"
end

.nameObject

override



11
12
13
# File 'lib/passivedns/client/dnsdb.rb', line 11

def self.name
  "DNSDB"
end

.option_letterObject

override



19
20
21
# File 'lib/passivedns/client/dnsdb.rb', line 19

def self.option_letter
  "d"
end

Instance Method Details

#lookup(label, limit = nil) ⇒ Object



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
82
83
84
# File 'lib/passivedns/client/dnsdb.rb', line 53

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

#parse_json(page, response_time) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/passivedns/client/dnsdb.rb', line 30

def parse_json(page,response_time)
  res = []
  raise "Error: unable to parse request" if page =~ /Error: unable to parse request/
  # need to remove the json_class tag or the parser will crap itself trying to find a class to align it to
  rows = page.split(/\n/)
  rows.each do |row|
    record = JSON.parse(row)
    record['rdata'] = [record['rdata']] if record['rdata'].class == String
    record['rdata'].each do |rdata|
      if record['time_first']
        res << PDNSResult.new(self.class.name,response_time,record['rrname'],rdata,record['rrtype'],0,Time.at(record['time_first'].to_i).utc.strftime("%Y-%m-%dT%H:%M:%SZ"),Time.at(record['time_last'].to_i).utc.strftime("%Y-%m-%dT%H:%M:%SZ"),record['count'])
      else
        res << PDNSResult.new(self.class.name,response_time,record['rrname'],rdata,record['rrtype'])
      end
    end
  end
  res
rescue Exception => e
  $stderr.puts "#{self.class.name} Exception: #{e}"
  $stderr.puts page
  raise e
end