Class: PassiveDNS::CN360

Inherits:
Object
  • Object
show all
Defined in:
lib/passivedns/client/cn360.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configfile = "#{ENV["HOME"]}/.flint.conf") ⇒ CN360

Returns a new instance of CN360.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/passivedns/client/cn360.rb', line 15

def initialize(configfile="#{ENV["HOME"]}/.flint.conf")
  @debug = false
  if not File.exist?(configfile)
    if not File.exist?("/etc/flint.conf")
      raise "Cannot find a configuration file at #{configfile} or /etc/flint.conf"
    end
    configfile = "/etc/flint.conf"
  end
  
  @cp = ConfigParser.new(configfile)
  if not @cp["API"]
    raise "Field, API, is required in the configuration file.  It should specify the URL of the JSON Web API."
  end
  if not @cp["API_ID"]
    raise "Field, API_ID, is required in the configuration file.  It should specify the user ID for the API key."
  end
  if not @cp["API_KEY"]
    raise "Field, API_KEY, is required in the configuration file.  It should specify the API key."
  end
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



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

def debug
  @debug
end

Instance Method Details

#lookup(label, limit = 10000) ⇒ Object



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

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
      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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/passivedns/client/cn360.rb', line 36

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('cn360', response_time, row["rrname"], row["rdata"], row["rrtype"], time_first, time_last, count)
      end
      res
    rescue Exception => e
      $stderr.puts "360.cn Exception: #{e}"
      raise e
end