Class: Ronin::Support::Network::TLD::List
- Inherits:
-
Object
- Object
- Ronin::Support::Network::TLD::List
- Includes:
- Enumerable
- Defined in:
- lib/ronin/support/network/tld/list.rb
Overview
Represents the Top-Level Domains list.
Constant Summary collapse
- FILE_NAME =
File name of the TLD list.
'tlds-alpha-by-domain.txt'
- URL =
The
https://data.iana.org/TLD/tlds-alpha-by-domain.txt
"https://data.iana.org/TLD/#{FILE_NAME}"
- PATH =
The path to
~/.cache/ronin/ronin-support/tlds-alpha-by-domain.txt
list file. File.join(Home::CACHE_DIR,'ronin','ronin-support',FILE_NAME)
- ONE_DAY =
One day in seconds.
60 * 60 * 24
Instance Attribute Summary collapse
-
#list ⇒ Array<String>
readonly
The list of all TLDs.
-
#path ⇒ String
readonly
The path to the list file.
-
#tree ⇒ Hash{String => Hash}
readonly
The tree of all TLD TLDs.
Class Method Summary collapse
-
.download(url: URL, path: PATH) ⇒ Object
Downloads the list file.
-
.downloaded?(path = PATH) ⇒ Boolean
Determines whether the list file has been previously downloaded.
-
.load_file(path = PATH) ⇒ List
Loads the TLD list from the given file.
-
.stale?(path = PATH) ⇒ Boolean
Determines if the downloaded list file is older than one day.
-
.update(url: URL, path: PATH) ⇒ Object
Optionally update the cached list file if it is older than one day.
Instance Method Summary collapse
-
#<<(tld) ⇒ self
private
Adds a TLD to the list.
-
#each {|suffix| ... } ⇒ Enumerator
Enumerates over each suffix in the list.
-
#initialize(path = PATH) ⇒ List
constructor
private
Initializes the TLD list.
-
#inspect ⇒ String
Inspects the TLD list.
-
#split(host_name) ⇒ (String, String)
Splits a hostname into it's name and TLD components.
-
#to_regexp ⇒ Regexp
Creates a regular expression that can match every domain suffix in the list.
Methods included from Enumerable
Constructor Details
#initialize(path = PATH) ⇒ List
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes the TLD list.
74 75 76 77 78 79 |
# File 'lib/ronin/support/network/tld/list.rb', line 74 def initialize(path=PATH) @path = path @list = [] @tree = {} end |
Instance Attribute Details
#list ⇒ Array<String> (readonly)
The list of all TLDs.
59 60 61 |
# File 'lib/ronin/support/network/tld/list.rb', line 59 def list @list end |
#path ⇒ String (readonly)
The path to the list file.
54 55 56 |
# File 'lib/ronin/support/network/tld/list.rb', line 54 def path @path end |
#tree ⇒ Hash{String => Hash} (readonly)
The tree of all TLD TLDs.
64 65 66 |
# File 'lib/ronin/support/network/tld/list.rb', line 64 def tree @tree end |
Class Method Details
.download(url: URL, path: PATH) ⇒ Object
Downloads the list file.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# 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 |
.downloaded?(path = PATH) ⇒ Boolean
Determines whether the list file has been previously downloaded.
89 90 91 |
# File 'lib/ronin/support/network/tld/list.rb', line 89 def self.downloaded?(path=PATH) File.file?(path) end |
.load_file(path = PATH) ⇒ List
Loads the TLD list from the given file.
169 170 171 172 173 174 175 176 177 178 179 180 181 |
# 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 |
.stale?(path = PATH) ⇒ Boolean
Determines if the downloaded list file is older than one day.
104 105 106 |
# 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 |
.update(url: URL, path: PATH) ⇒ Object
Optionally update the cached list file if it is older than one day.
148 149 150 151 152 153 154 155 156 157 158 |
# 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 |
Instance Method Details
#<<(tld) ⇒ self
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds a TLD to the list.
193 194 195 196 |
# File 'lib/ronin/support/network/tld/list.rb', line 193 def <<(tld) @list << tld return self end |
#each {|suffix| ... } ⇒ Enumerator
Enumerates over each suffix in the list.
210 211 212 |
# File 'lib/ronin/support/network/tld/list.rb', line 210 def each(&block) @list.each(&block) end |
#inspect ⇒ String
Inspects the TLD list.
259 260 261 |
# File 'lib/ronin/support/network/tld/list.rb', line 259 def inspect "#<#{self.class}: #{@path}>" end |
#split(host_name) ⇒ (String, String)
Splits a hostname into it's name and TLD components.
226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# 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 |