Class: PassiveDNS::CN360

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CN360

Returns a new instance of CN360.



23
24
25
26
27
28
29
30
31
# File 'lib/passivedns/client/providers/cn360.rb', line 23

def initialize(options={})
  @debug = options[:debug] || false
  ["API", "API_ID", "API_KEY"].each do |opt|
    if not options[opt]
      raise "Field #{opt} is required.  See README.md"
    end
  end
  @cp = options
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



22
23
24
# File 'lib/passivedns/client/providers/cn360.rb', line 22

def debug
  @debug
end

Class Method Details

.config_section_nameObject

override



14
15
16
# File 'lib/passivedns/client/providers/cn360.rb', line 14

def self.config_section_name
  "cn360"
end

.nameObject

override



10
11
12
# File 'lib/passivedns/client/providers/cn360.rb', line 10

def self.name
  "360.cn"
end

.option_letterObject

override



18
19
20
# File 'lib/passivedns/client/providers/cn360.rb', line 18

def self.option_letter
  "3"
end

Instance Method Details

#lookup(label, limit = 10000) ⇒ Object



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/providers/cn360.rb', line 49

def lookup(label, limit=10000)
  table = "rrset"
  if label =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ or label =~ /^[0-9a-fA-F]+:[0-9a-fA-F:]+[0-9a-fA-F]$/
    table = "rdata"
  end
  limit ||= 10000
  path = "/api/#{table}/keyword/#{label}/count/#{limit}/"
  url = @cp["API"]+path
      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 # I hate doing this
      http.verify_depth = 5
      request = Net::HTTP::Get.new(url.path)
      request.add_field("User-Agent", "Ruby/#{RUBY_VERSION} passivedns-client rubygem v#{PassiveDNS::Client::VERSION}")
  request.add_field('Accept', 'application/json')
  request.add_field("X-BashTokid", @cp["API_ID"])
  token = Digest::MD5.hexdigest(path+@cp["API_KEY"])
      $stderr.puts "DEBUG: cn360 url = #{url} token = #{token}" if @debug
  request.add_field("X-BashToken", token)
      t1 = Time.now
      response = http.request(request)
      t2 = Time.now
      recs = parse_json(response.body, label, t2-t1)
  if limit
    recs[0,limit]
  else
    recs
  end
end

#parse_json(page, query, response_time = 0) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/passivedns/client/providers/cn360.rb', line 33

def parse_json(page,query,response_time=0)
      res = []
      # need to remove the json_class tag or the parser will crap itself trying to find a class to align it to
      data = JSON.parse(page)
  data.each do |row|
    time_first = (row["time_first"]) ? Time.at(row["time_first"].to_i) : nil
    time_last = (row["time_last"]) ? Time.at(row["time_last"].to_i) : nil
    count = row["count"] || 0
    res << PDNSResult.new(self.class.name, response_time, row["rrname"], row["rdata"], row["rrtype"], time_first, time_last, count)
      end
      res
    rescue Exception => e
      $stderr.puts "#{self.class.name} Exception: #{e}"
      raise e
end