Class: Masscan::OutputFile

Inherits:
Object
  • Object
show all
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.



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

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)


39
40
41
# File 'lib/masscan/output_file.rb', line 39

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.



46
47
48
# File 'lib/masscan/output_file.rb', line 46

def parser
  @parser
end

#pathString (readonly)

The path to the output file.

Returns:

  • (String)


34
35
36
# File 'lib/masscan/output_file.rb', line 34

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.



99
100
101
102
103
# File 'lib/masscan/output_file.rb', line 99

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.



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

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



133
134
135
# File 'lib/masscan/output_file.rb', line 133

def to_s
  @path
end