Class: Masscan::OutputFile

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/masscan/output_file.rb

Overview

Represents an output file.

Example

output_file = Masscan::OutputFile.new('masscan.json')
output_file.each do |record|
  p record
end

Constant Summary collapse

PARSERS =

Mapping of formats to parsers.

{
  binary: Parsers::Binary,
  list:   Parsers::List,
  json:   Parsers::JSON,
  ndjson: Parsers::JSON,
  # xml:    Parsers::XML,
}
FILE_FORMATS =

Mapping of file extensions to formats

{
  '.bin' => :binary,
  '.dat' => :binary,

  '.txt' => :list,
  '.list' => :list,

  '.json'   => :json,
  '.ndjson' => :ndjson,

  '.xml' => :xml
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, format: self.class.infer_format(path)) ⇒ OutputFile

Initializes the output file.

Parameters:

  • path (String)

    The path to the output file.

  • format (:binary, :list, :json, :ndjson) (defaults to: self.class.infer_format(path))

    The format of the output file. Defaults to infer_format.

Raises:

  • (ArgumentError)

    The output format was not given and it cannot be inferred.



62
63
64
65
66
67
68
69
# File 'lib/masscan/output_file.rb', line 62

def initialize(path, format: self.class.infer_format(path))
  @path   = path
  @format = format

  @parser = PARSERS.fetch(format) do
    raise(ArgumentError,"unknown format: #{format.inspect}")
  end
end

Instance Attribute Details

#formatSymbol (readonly)

The format of the output file.

Returns:

  • (Symbol)


41
42
43
# File 'lib/masscan/output_file.rb', line 41

def format
  @format
end

#parserParsers::Binary, ... (readonly)

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.

The parser for the output file format.



48
49
50
# File 'lib/masscan/output_file.rb', line 48

def parser
  @parser
end

#pathString (readonly)

The path to the output file.

Returns:

  • (String)


36
37
38
# File 'lib/masscan/output_file.rb', line 36

def path
  @path
end

Class Method Details

.infer_format(path) ⇒ :binary, ...

Infers the format from the output file's extension name.

Parameters:

  • path (String)

    The path to the output file.

Returns:

  • (:binary, :list, :json, :ndjson)

    The output format inferred from the file's extension name.

Raises:

  • (ArgumentError)

    The output format could not be inferred from the file's name.



101
102
103
104
105
# File 'lib/masscan/output_file.rb', line 101

def self.infer_format(path)
  FILE_FORMATS.fetch(File.extname(path)) do
    raise(ArgumentError,"could not infer format of #{path}")
  end
end

Instance Method Details

#each {|record| ... } ⇒ Enumerator

Parses the contents of the output file.

Yields:

  • (record)

    If a block is given, it will be passed each parsed record.

  • (Status, Banner)

    record A parsed record, either a Status or a Banner.

Returns:

  • (Enumerator)

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



119
120
121
122
123
124
125
# File 'lib/masscan/output_file.rb', line 119

def each(&block)
  return enum_for(__method__) unless block

  @parser.open(@path) do |file|
    @parser.parse(file,&block)
  end
end

#to_sString

Converts the output file to a String.

Returns:

  • (String)

    The path to the output file.

Since:

  • 0.2.0



135
136
137
# File 'lib/masscan/output_file.rb', line 135

def to_s
  @path
end