class Ronin::Support::Network::TLD::List
Represents the [Top-Level Domains list].
[Top-Level Domains list]: www.icann.org/resources/pages/tlds-2012-02-25-en
@api public
@since 1.0.0
Constants
- FILE_NAME
- ONE_DAY
One day in seconds.
- PATH
The path to ‘~/.cache/ronin/ronin-support/tlds-alpha-by-domain.txt` list file.
- URL
Attributes
The list of all TLDs.
@return [Array<String>]
The path to the list file.
@return [String]
The tree of all TLD
TLDs.
@return [Hash{String => Hash}]
Public Class Methods
Downloads the list file.
@param [String] url
An optional alternate URL to download the `ip2asn-combined.tsv.gz` file.
@param [String] path
An optional alternate path to the list file.
# File lib/ronin/support/network/tld/list.rb, line 118 def self.download(url: URL, path: PATH) uri = URI(url) Net::HTTP.start(uri.host,uri.port, use_ssl: true) do |http| request = Net::HTTP::Get.new(uri.path) http.request(request) do |response| FileUtils.mkdir_p(File.dirname(path)) File.open("#{path}.part",'wb') do |file| response.read_body do |chunk| file.write(chunk) end end FileUtils.mv("#{path}.part",path) end end end
Determines whether the list file has been previously downloaded.
@param [String] path
An optional alternate path to the list file.
@return [Boolean]
# File lib/ronin/support/network/tld/list.rb, line 89 def self.downloaded?(path=PATH) File.file?(path) end
Loads the TLD
list from the given file.
@param [String] path
The path to the TLD list file.
@return [List]
The parsed TLD list file.
# File lib/ronin/support/network/tld/list.rb, line 169 def self.load_file(path=PATH) list = new(path) File.open(path) do |file| file.each_line(chomp: true) do |line| next if line.start_with?('#') list << line.downcase end end return list end
Initializes the TLD
list.
@param [String] path
The path to the list file.
@api private
# File lib/ronin/support/network/tld/list.rb, line 74 def initialize(path=PATH) @path = path @list = [] @tree = {} end
Determines if the downloaded list file is older than one day.
@param [String] path
An optional alternate path to the list file.
@return [Boolean]
# File lib/ronin/support/network/tld/list.rb, line 104 def self.stale?(path=PATH) !File.file?(path) || File.stat(path).mtime < (Time.now - ONE_DAY) end
Optionally update the cached list file if it is older than one day.
@param [String] url
An optional alternate URL to download the `ip2asn-combined.tsv.gz` file.
@param [String] path
An optional alternate path to the list file.
# File lib/ronin/support/network/tld/list.rb, line 148 def self.update(url: URL, path: PATH) if !downloaded?(path) download(url: url, path: path) elsif stale?(path) begin download(url: url, path: path) rescue # ignore any network failures end end end
Public Instance Methods
Adds a TLD
to the list.
@param [String] tld
The TLD String to add.
@return [self]
@api private
# File lib/ronin/support/network/tld/list.rb, line 193 def <<(tld) @list << tld return self end
Enumerates over each suffix in the list.
@yield [suffix]
If a block is given, it will be passed each suffix in the list.
@yieldparam [String] suffix
A domain suffix in the list.
@return [Enumerator]
If no block is given, an Enumerator object will be returned.
# File lib/ronin/support/network/tld/list.rb, line 210 def each(&block) @list.each(&block) end
Inspects the TLD
list.
@return [String]
The inspected list object.
# File lib/ronin/support/network/tld/list.rb, line 259 def inspect "#<#{self.class}: #{@path}>" end
Splits a hostname into it’s name and TLD
components.
@param [String] host_name
The host name to split.
The host name's name and TLD components.
@raise [InvalidHostname]
The given hostname does not end with a valid TLD.
# File lib/ronin/support/network/tld/list.rb, line 226 def split(host_name) unless (index = host_name.rindex('.')) raise(InvalidHostname,"hostname does not have a TLD: #{host_name.inspect}") end name = host_name[0...index] tld = host_name[(index + 1)..] unless @list.include?(tld) raise(InvalidHostname,"hostname does not have a valid TLD: #{host_name.inspect}") end return name, tld end
Creates a regular expression that can match every domain suffix in the list.
@return [Regexp]
# File lib/ronin/support/network/tld/list.rb, line 247 def to_regexp regexp = Regexp.union(@list) return /(?<=[^a-zA-Z0-9_-]|^)#{regexp}(?=[^\.a-z0-9-]|$)/ end