Class: PassiveDNS::Provider::TCPIPUtils

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

Overview

Queries TCPIPUtils’s passive DNS database

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TCPIPUtils

Options

Example Instantiation

options = {
  :debug => true,
  "APIKEY" => "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
  "URL" =>  "https://www.utlsapi.com/api.php?version=1.0&apikey="
}

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


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

def initialize(options={})
  @debug = options[:debug] || false
  @apikey = options["APIKEY"] || raise("#{self.class.name} requires an APIKEY.  See README.md")
  @url = options["URL"] || "https://www.utlsapi.com/api.php?version=1.0&apikey="
end

Instance Attribute Details

#debugObject

:debug enables verbose logging to standard output



27
28
29
# File 'lib/passivedns/client/provider/tcpiputils.rb', line 27

def debug
  @debug
end

Class Method Details

.config_section_nameObject

Sets the configuration section name to “tcpiputils”



18
19
20
# File 'lib/passivedns/client/provider/tcpiputils.rb', line 18

def self.config_section_name
  "tcpiputils"
end

.nameObject

Sets the modules self-reported name to “TCPIPUtils”



14
15
16
# File 'lib/passivedns/client/provider/tcpiputils.rb', line 14

def self.name
  "TCPIPUtils"
end

.option_letterObject

Sets the command line database argument to “t”



22
23
24
# File 'lib/passivedns/client/provider/tcpiputils.rb', line 22

def self.option_letter
  "t"
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



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
82
# File 'lib/passivedns/client/provider/tcpiputils.rb', line 51

def lookup(label, limit=nil)
    $stderr.puts "DEBUG: #{self.class.name}.lookup(#{label})" if @debug
    type = (label.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)) ? "domainneighbors" : "domainipdnshistory"
    url = "#{@url}#{@apikey}&type=#{type}&q=#{label}"
    recs = []
    Timeout::timeout(240) {
		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
		request = Net::HTTP::Get.new(url.path+"?"+url.query)
		request.add_field("User-Agent", "Ruby/#{RUBY_VERSION} passivedns-client rubygem v#{PassiveDNS::Client::VERSION}")
		t1 = Time.now
		response = http.request(request)
		delta = (Time.now - t1).to_f
		reply = JSON.parse(response.body)
      if reply["status"] and reply["status"] == "succeed"
        question = reply["data"]["question"]
        recs = format_recs(reply["data"], question, delta)
      elsif reply["status"] and reply["status"] == "error"
        raise "#{self.class.name}: error from web API: #{reply["data"]}"
      end
      if limit
        recs[0,limit]
      else
        recs
      end
	}
rescue Timeout::Error => e
	$stderr.puts "#{self.class.name} lookup timed out: #{label}"
end