class Ronin::Support::Network::ASN::List
Represents the entire list of all IPv4 and IPv6 ASNs.
@api public
@since 1.0.0
Constants
- FILE_NAME
- ONE_DAY
-
One day in seconds.
- PATH
-
The path to ‘~/.local/share/ronin/ronin-support/ip2asn-combined.tsv.gz` list file.
- URL
Attributes
The path to the list file.
@return [String]
Public Class Methods
Source
# File lib/ronin/support/network/asn/list.rb, line 111 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
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.
Source
# File lib/ronin/support/network/asn/list.rb, line 82 def self.downloaded?(path=PATH) File.file?(path) end
Determines whether the list file has been previously downloaded.
@param [String] path
An optional alternate path to the list file.
@return [Boolean]
Source
# File lib/ronin/support/network/asn/list.rb, line 200 def self.load_file(path=PATH) list = new(path) parse(path) do |record| list << record end return list end
Loads the contents of the list file into memory.
@param [String] path
An optional alternate path to the list file.
@return [List]
The loaded list.
Source
# File lib/ronin/support/network/asn/list.rb, line 65 def initialize(path) super() @path = path @ipv4_prefixes = {} @ipv6_prefixes = {} end
Initializes the list file.
@param [String] path
The path to the list file.
@api private
Source
# File lib/ronin/support/network/asn/list.rb, line 169 def self.parse(path=PATH) return enum_for(__method__,path) unless block_given? io = if File.extname(path) == '.gz' then Zlib::GzipReader.open(path) else File.new(path) end io.each_line do |line| line.chomp! first, last, number, country_code, name = line.split("\t",5) range = IPRange::Range.new(first,last) number = number.to_i country_code = nil if country_code == 'None' name = nil if name == 'Not routed' yield Record.new(number,range,country_code,name) end end
Parses the contents of the list file.
@param [String] path
An optional alternate path to the list file.
@yield [record]
If a block is given, it will be passed each parsed record from the list file.
@yieldparam [Record] record
A parsed record in the list file.
@return [Enumerator]
If no block is given, an Enumerator object will be returned.
Source
# File lib/ronin/support/network/asn/list.rb, line 97 def self.stale?(path=PATH) !File.file?(path) || File.stat(path).mtime < (Time.now - ONE_DAY) 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]
Source
# File lib/ronin/support/network/asn/list.rb, line 141 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
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.
Public Instance Methods
Source
# File lib/ronin/support/network/asn/list.rb, line 219 def <<(record) super(record) prefixes = if record.range.ipv6? then @ipv6_prefixes else @ipv4_prefixes end records = (prefixes[record.range.prefix] ||= []) records << record return self end
Adds a record to the list.
@param [Record] record
@return [self]
@api private
Source
# File lib/ronin/support/network/asn/list.rb, line 266 def inspect "#<#{self.class}: #{@path}>" end
Inspects the list.
@return [String]
Source
# File lib/ronin/support/network/asn/list.rb, line 240 def ip(ip) address = ip.to_s ip = IPAddr.new(ip) unless ip.kind_of?(IPAddr) prefixes = if ip.ipv6? then @ipv6_prefixes else @ipv4_prefixes end prefixes.each do |prefix,records| if address.start_with?(prefix) records.each do |record| if record.include?(ip) return record end end end end return nil end
Finds the ASN
record for the given IP
address.
@param [IP, IPaddr, String] ip
The IP address to search for.
@return [Record, nil]
The ASN record for the IP address or `nil` if none could be found.