Class: PassiveDNS::TCPIPUtils
- Inherits:
-
Object
- Object
- PassiveDNS::TCPIPUtils
- Defined in:
- lib/passivedns/client/tcpiputils.rb
Instance Attribute Summary collapse
-
#debug ⇒ Object
Returns the value of attribute debug.
Instance Method Summary collapse
- #format_recs(reply_data, question, delta) ⇒ Object
-
#initialize(config = "#{ENV['HOME']}/.tcpiputils") ⇒ TCPIPUtils
constructor
A new instance of TCPIPUtils.
- #lookup(label, limit = nil) ⇒ Object
Constructor Details
#initialize(config = "#{ENV['HOME']}/.tcpiputils") ⇒ TCPIPUtils
Returns a new instance of TCPIPUtils.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/passivedns/client/tcpiputils.rb', line 11 def initialize(config="#{ENV['HOME']}/.tcpiputils") @debug = false if File.exist?(config) @apikey = File.open(config).read.split(/\n/)[0] $stderr.puts "DEBUG: TCPIPUtils#initialize(#{@apikey})" if @debug else raise "Error: Configuration file for TCPIPUtils is required for intialization Format of configuration file (default: #{ENV['HOME']}/.tcpiputils) is the 64 hex character apikey on one line. To obtain an API Key, go to http://www.tcpiputils.com/premium-access and purchase premium API access." end end |
Instance Attribute Details
#debug ⇒ Object
Returns the value of attribute debug.
10 11 12 |
# File 'lib/passivedns/client/tcpiputils.rb', line 10 def debug @debug end |
Instance Method Details
#format_recs(reply_data, question, delta) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/passivedns/client/tcpiputils.rb', line 23 def format_recs(reply_data, question, delta) recs = [] reply_data.each do |key, data| case key when "ipv4" data.each do |rec| recs << PDNSResult.new("tcpiputils", delta, question, rec["ip"], "A", nil, nil, rec["updatedate"], nil) end when "ipv6" data.each do |rec| recs << PDNSResult.new("tcpiputils", delta, question, rec["ip"], "AAAA", nil, nil, rec["updatedate"], nil) end when "dns" data.each do |rec| recs << PDNSResult.new("tcpiputils", delta, question, rec["dns"], "NS", nil, nil, rec["updatedate"], nil) end when "mx" data.each do |rec| recs << PDNSResult.new("tcpiputils", delta, question, rec["dns"], "MX", nil, nil, rec["updatedate"], nil) end end end recs end |
#lookup(label, limit = nil) ⇒ Object
48 49 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 |
# File 'lib/passivedns/client/tcpiputils.rb', line 48 def lookup(label, limit=nil) $stderr.puts "DEBUG: TCPIPUtils.lookup(#{label})" if @debug url = "https://www.utlsapi.com/api.php?version=1.0&apikey=#{@apikey}&type=domainipdnshistory&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 "TCPIPUtils: error from web API: #{reply["data"]}" end if limit recs[0,limit] else recs end } rescue Timeout::Error => e $stderr.puts "TCPIPUtils lookup timed out: #{label}" end |