Class: Ronin::Support::Network::ASN::List
- Includes:
- Enumerable
- Defined in:
- lib/ronin/support/network/asn/list.rb
Overview
Represents the entire list of all IPv4 and IPv6 ASNs.
Constant Summary collapse
- FILE_NAME =
File name of the combined IPv4 and IPv6 ASN list file.
'ip2asn-combined.tsv.gz'
- URL =
The
https://iptoasn.com/data/ip2asn-combined.tsv.gz
URL. "https://iptoasn.com/data/#{FILE_NAME}"
- PATH =
The path to
~/.local/share/ronin/ronin-support/ip2asn-combined.tsv.gz
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
-
#path ⇒ String
readonly
The path to the list file.
Attributes inherited from RecordSet
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 contents of the list file into memory.
-
.parse(path = PATH) {|record| ... } ⇒ Enumerator
Parses the contents of the list 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
-
#<<(record) ⇒ self
private
Adds a record to the list.
-
#initialize(path) ⇒ List
constructor
private
Initializes the list file.
-
#inspect ⇒ String
Inspects the list.
-
#ip(ip) ⇒ Record?
Finds the ASN record for the given IP address.
Methods included from Enumerable
Methods inherited from RecordSet
#countries, #country, #each, #include?, #ipv4, #ipv6, #length, #name, #names, #number, #numbers, #ranges, #to_a
Constructor Details
#initialize(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 list file.
65 66 67 68 69 70 71 72 |
# File 'lib/ronin/support/network/asn/list.rb', line 65 def initialize(path) super() @path = path @ipv4_prefixes = {} @ipv6_prefixes = {} end |
Instance Attribute Details
#path ⇒ String (readonly)
The path to the list file.
55 56 57 |
# File 'lib/ronin/support/network/asn/list.rb', line 55 def path @path end |
Class Method Details
.download(url: URL, path: PATH) ⇒ Object
Downloads the list file.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# 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 |
.downloaded?(path = PATH) ⇒ Boolean
Determines whether the list file has been previously downloaded.
82 83 84 |
# File 'lib/ronin/support/network/asn/list.rb', line 82 def self.downloaded?(path=PATH) File.file?(path) end |
.load_file(path = PATH) ⇒ List
Loads the contents of the list file into memory.
200 201 202 203 204 205 206 207 208 |
# 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 |
.parse(path = PATH) {|record| ... } ⇒ Enumerator
Parses the contents of the list file.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# 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 |
.stale?(path = PATH) ⇒ Boolean
Determines if the downloaded list file is older than one day.
97 98 99 |
# 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 |
.update(url: URL, path: PATH) ⇒ Object
Optionally update the cached list file if it is older than one day.
141 142 143 144 145 146 147 148 149 150 151 |
# 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 |
Instance Method Details
#<<(record) ⇒ 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 record to the list.
219 220 221 222 223 224 225 226 227 228 229 |
# 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 |
#inspect ⇒ String
Inspects the list.
266 267 268 |
# File 'lib/ronin/support/network/asn/list.rb', line 266 def inspect "#<#{self.class}: #{@path}>" end |
#ip(ip) ⇒ Record?
Finds the ASN record for the given IP address.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# 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 |