Class: Ronin::Support::Network::TLD::List

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ronin/support/network/tld/list.rb

Overview

Represents the Top-Level Domains list.

Since:

  • 1.0.0

Constant Summary collapse

FILE_NAME =

File name of the TLD list.

Since:

  • 1.0.0

'tlds-alpha-by-domain.txt'
URL =

The https://data.iana.org/TLD/tlds-alpha-by-domain.txt

Since:

  • 1.0.0

"https://data.iana.org/TLD/#{FILE_NAME}"
PATH =

The path to ~/.cache/ronin/ronin-support/tlds-alpha-by-domain.txt list file.

Since:

  • 1.0.0

File.join(Home::CACHE_DIR,'ronin','ronin-support',FILE_NAME)
ONE_DAY =

One day in seconds.

Since:

  • 1.0.0

60 * 60 * 24

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#map_hash

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.

Parameters:

  • path (String) (defaults to: PATH)

    The path to the list file.

Since:

  • 1.0.0



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

#listArray<String> (readonly)

The list of all TLDs.

Returns:

Since:

  • 1.0.0



59
60
61
# File 'lib/ronin/support/network/tld/list.rb', line 59

def list
  @list
end

#pathString (readonly)

The path to the list file.

Returns:

Since:

  • 1.0.0



54
55
56
# File 'lib/ronin/support/network/tld/list.rb', line 54

def path
  @path
end

#treeHash{String => Hash} (readonly)

The tree of all TLD TLDs.

Returns:

Since:

  • 1.0.0



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.

Parameters:

  • url (String) (defaults to: URL)

    An optional alternate URL to download the ip2asn-combined.tsv.gz file.

  • path (String) (defaults to: PATH)

    An optional alternate path to the list file.

Since:

  • 1.0.0



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.

Parameters:

  • path (String) (defaults to: PATH)

    An optional alternate path to the list file.

Returns:

  • (Boolean)

Since:

  • 1.0.0



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.

Parameters:

  • path (String) (defaults to: PATH)

    The path to the TLD list file.

Returns:

  • (List)

    The parsed TLD list file.

Since:

  • 1.0.0



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.

Parameters:

  • path (String) (defaults to: PATH)

    An optional alternate path to the list file.

Returns:

  • (Boolean)

Since:

  • 1.0.0



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.

Parameters:

  • url (String) (defaults to: URL)

    An optional alternate URL to download the ip2asn-combined.tsv.gz file.

  • path (String) (defaults to: PATH)

    An optional alternate path to the list file.

Since:

  • 1.0.0



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.

Parameters:

  • tld (String)

    The TLD String to add.

Returns:

  • (self)

Since:

  • 1.0.0



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.

Yields:

  • (suffix)

    If a block is given, it will be passed each suffix in the list.

Yield Parameters:

  • suffix (String)

    A domain suffix in the list.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.

Since:

  • 1.0.0



210
211
212
# File 'lib/ronin/support/network/tld/list.rb', line 210

def each(&block)
  @list.each(&block)
end

#inspectString

Inspects the TLD list.

Returns:

  • (String)

    The inspected list object.

Since:

  • 1.0.0



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.

Parameters:

  • host_name (String)

    The host name to split.

Returns:

  • ((String, String))

    The host name's name and TLD components.

Raises:

Since:

  • 1.0.0



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

#to_regexpRegexp

Creates a regular expression that can match every domain suffix in the list.

Returns:

Since:

  • 1.0.0



247
248
249
250
251
# 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