Class: PassiveDNS::Provider::CN360

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

Overview

Queries 360.cn’s passive DNS database

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CN360

Options

  • :debug Sets the debug flag for the module

  • “API” REQUIRED: some.web.address.for.their.api

  • “API_ID” REQUIRED: a username that is given when you register

  • “API_KEY” REQUIRED: a long and random password of sorts that is used along with the page request to generate a per page API key

Example Instantiation

options = {
  :debug => true,
  "API" => "http://some.web.address.for.their.api",
  "API_ID" => "360user",
  "API_KEY" => "360apikey"
}

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


44
45
46
47
48
49
50
51
52
# File 'lib/passivedns/client/provider/cn360.rb', line 44

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

:debug enables verbose logging to standard output



26
27
28
# File 'lib/passivedns/client/provider/cn360.rb', line 26

def debug
  @debug
end

Class Method Details

.config_section_nameObject

Sets the configuration section name to “cn360”



17
18
19
# File 'lib/passivedns/client/provider/cn360.rb', line 17

def self.config_section_name
  "cn360"
end

.nameObject

Sets the modules self-reported name to “360.cn”



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

def self.name
  "360.cn"
end

.option_letterObject

Sets the command line database argument to “3”



21
22
23
# File 'lib/passivedns/client/provider/cn360.rb', line 21

def self.option_letter
  "3"
end

Instance Method Details

#lookup(label, limit = 10000) ⇒ 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



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
85
# File 'lib/passivedns/client/provider/cn360.rb', line 56

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