Class: Whois::Server
- Inherits:
-
Object
- Object
- Whois::Server
- Defined in:
- lib/whois/server.rb,
lib/whois/server/adapters/web.rb,
lib/whois/server/adapters/arin.rb,
lib/whois/server/adapters/arpa.rb,
lib/whois/server/adapters/base.rb,
lib/whois/server/adapters/none.rb,
lib/whois/server/socket_handler.rb,
lib/whois/server/adapters/afilias.rb,
lib/whois/server/adapters/standard.rb,
lib/whois/server/adapters/verisign.rb,
lib/whois/server/adapters/formatted.rb,
lib/whois/server/adapters/not_implemented.rb
Overview
The Server class has two important roles:
-
it acts as a database for the WHOIS server definitions
-
it is responsible for selecting the right adapter used to handle the query to the WHOIS server(s).
Defined Under Namespace
Modules: Adapters Classes: SocketHandler
Constant Summary collapse
- TYPES =
Returns the definition types.
[ TYPE_TLD = :tld, TYPE_IPV4 = :ipv4, TYPE_IPV6 = :ipv6, TYPE_ASN16 = :asn16, TYPE_ASN32 = :asn32, ].freeze
Class Method Summary collapse
-
.clear_definitions ⇒ void
Clears the definition and reset them to an empty list.
-
.define(type, allocation, host, options = EMPTY_HASH) ⇒ void
Defines a new server for
:type
queries. -
.definitions(type) ⇒ { Symbol => Array }, Array<Hash>
Lookup and returns the definition list for given ‘type`.
-
.factory(type, allocation, host, options = {}) ⇒ Whois::Server::Adapters::Base
Creates a new server adapter from given arguments and returns the server instance.
-
.find_for_asn(string) ⇒ Whois::Server::Adapters::Base?
Searches for definition that matches given ASN string.
-
.find_for_domain(string) ⇒ Whois::Server::Adapters::Base?
Searches for definition that matches given domain.
-
.find_for_email(string) ⇒ Object
Searches for definition that matches given email.
-
.find_for_ip(string) ⇒ Whois::Server::Adapters::Base?
Searches for definition that matches given IP.
-
.guess(string) ⇒ Whois::Server::Adapters::Base
Parses
string
and tries to guess the right server. -
.load_definitions ⇒ void
Searches the
/definitions
folder for definition files and loads them. -
.load_json(file) ⇒ void
Loads the definitions from a JSON file.
Class Method Details
.clear_definitions ⇒ void
This method returns an undefined value.
Clears the definition and reset them to an empty list.
59 60 61 |
# File 'lib/whois/server.rb', line 59 def clear_definitions @definitions = {} end |
.define(type, allocation, host, options = EMPTY_HASH) ⇒ void
This method returns an undefined value.
Defines a new server for :type
queries.
158 159 160 161 162 163 |
# File 'lib/whois/server.rb', line 158 def define(type, allocation, host, = EMPTY_HASH) TYPES.include?(type) or raise(ArgumentError, "`#{type}` is not a valid definition type") _definitions(type)[allocation] = [allocation, host, .freeze] end |
.definitions(type) ⇒ { Symbol => Array }, Array<Hash>
Lookup and returns the definition list for given ‘type`.
114 115 116 117 118 119 |
# File 'lib/whois/server.rb', line 114 def definitions(type) TYPES.include?(type) or raise(ArgumentError, "`#{type}` is not a valid definition type") _definitions(type).values end |
.factory(type, allocation, host, options = {}) ⇒ Whois::Server::Adapters::Base
Creates a new server adapter from given arguments and returns the server instance.
By default, returns a new Whois::Server::Adapters::Standard instance. You can customize the behavior passing a custom adapter class as :adapter
option.
Whois::Server.factory :tld, "it", "whois.nic.it"
# => #<Whois::Servers::Adapter::Standard>
Whois::Server.factory :tld, "it", "whois.nic.it",
:option => Whois::Servers::Adapter::Custom
# => #<Whois::Servers::Adapter::Custom>
Please note that any adapter is responsible for a limited set of queries, which should be included in the range of the allocation
parameter. Use guess if you are not sure which adapter is the right one for a specific string.
199 200 201 202 203 204 |
# File 'lib/whois/server.rb', line 199 def factory(type, allocation, host, = {}) = .dup adapter = .delete(:adapter) || Adapters::Standard adapter = Adapters.const_get(camelize(adapter)) unless adapter.respond_to?(:new) adapter.new(type, allocation, host, ) end |
.find_for_asn(string) ⇒ Whois::Server::Adapters::Base?
Searches for definition that matches given ASN string.
321 322 323 324 325 326 327 328 329 330 |
# File 'lib/whois/server.rb', line 321 def find_for_asn(string) asn = string[/\d+/].to_i asn_type = asn <= 65_535 ? TYPE_ASN16 : TYPE_ASN32 _definitions(asn_type).each_value do |definition| if (range = definition.first.split.map(&:to_i)) && asn >= range.first && asn <= range.last return factory(asn_type, *definition) end end raise AllocationUnknown, "Unknown AS number - `#{asn}'." end |
.find_for_domain(string) ⇒ Whois::Server::Adapters::Base?
Searches for definition that matches given domain.
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/whois/server.rb', line 295 def find_for_domain(string) token = string defs = _definitions(TYPE_TLD) while token != "" if (found = defs[token]) return factory(:tld, *found) else index = token.index(".") break if index.nil? token = token[(index + 1)..] end end nil end |
.find_for_email(string) ⇒ Object
Searches for definition that matches given email.
286 287 288 |
# File 'lib/whois/server.rb', line 286 def find_for_email(string) raise ServerNotSupported, "No WHOIS server is known for email objects" end |
.find_for_ip(string) ⇒ Whois::Server::Adapters::Base?
Searches for definition that matches given IP.
267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/whois/server.rb', line 267 def find_for_ip(string) begin ip = IPAddr.new(string) type = ip.ipv4? ? TYPE_IPV4 : TYPE_IPV6 _definitions(type).each_value do |definition| return factory(type, *definition) if IPAddr.new(definition.first).include?(ip) end rescue ArgumentError # continue nil end raise AllocationUnknown, "IP Allocation for `#{string}' unknown" end |
.guess(string) ⇒ Whois::Server::Adapters::Base
Parses string
and tries to guess the right server.
It successfully detects the following query types:
-
ipv6
-
ipv4
-
top level domains (e.g. .com, .net, .it)
-
domain names (e.g. google.com, google.net, google.it)
-
emails
Note that not all query types actually have a corresponding adapter. For instance, the following request will result in a Whois::ServerNotSupported exception.
Whois::Server.guess "[email protected]"
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/whois/server.rb', line 236 def guess(string) # Top Level Domain match return factory(:tld, ".", "whois.iana.org") if matches_tld?(string) # IP address (secure match) return find_for_ip(string) if matches_ip?(string) # Email Address (secure match) return find_for_email(string) if matches_email?(string) # Domain Name match if (server = find_for_domain(string)) return server end # ASN match return find_for_asn(string) if matches_asn?(string) # Game Over raise ServerNotFound, "Unable to find a WHOIS server for `#{string}'" end |
.load_definitions ⇒ void
This method returns an undefined value.
Searches the /definitions
folder for definition files and loads them. This method is automatically invoked when this file is parsed by the Ruby interpreter (scroll down to the bottom of this file).
68 69 70 71 |
# File 'lib/whois/server.rb', line 68 def load_definitions clear_definitions Dir[File.('../../data/*.json', __dir__)].each { |f| load_json(f) } end |
.load_json(file) ⇒ void
This method returns an undefined value.
Loads the definitions from a JSON file.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/whois/server.rb', line 78 def load_json(file) type = File.basename(file, File.extname(file)).to_sym JSON.parse(File.read(file)).each do |allocation, settings| next if allocation == "_" settings.reject! { |k, _| k.start_with?("_") } host = settings.delete("host") host = intern_string(host) if host = if settings.empty? EMPTY_HASH else settings.to_h { |k, v| [k.to_sym, v.is_a?(String) ? intern_string(v) : v] }.freeze end define(type, allocation, host, ) end end |