Module: Ronin::Core::ClassRegistry::ClassMethods

Defined in:
lib/ronin/core/class_registry.rb

Overview

Class-methods.

Instance Method Summary collapse

Instance Method Details

#class_dir(new_dir = nil) ⇒ String

Gets or sets the class directory path.

Examples:

class_dir "#{__dir__}/classes"

Parameters:

  • new_dir (String, nil) (defaults to: nil)

    The new class directory path.

Returns:

  • (String)

    The class directory path.

Raises:

  • (NotImplementedError)

    The class_dir method was not defined in the module.



101
102
103
104
105
106
107
# File 'lib/ronin/core/class_registry.rb', line 101

def class_dir(new_dir=nil)
  if new_dir
    @class_dir = new_dir
  else
    @class_dir || raise(NotImplementedError,"#{self} did not define a class_dir")
  end
end

#list_filesArray<String>

Lists all class files within #class_dir.

Returns:

  • (Array<String>)

    The list of class paths within #class_dir.



115
116
117
118
119
# File 'lib/ronin/core/class_registry.rb', line 115

def list_files
  paths = Dir.glob('{**/}*.rb', base: class_dir)
  paths.each { |path| path.chomp!('.rb') }
  return paths
end

#load_class(id) ⇒ Class

Loads a class from the #class_dir.

Parameters:

  • id (String)

    The class id to load.

Returns:

  • (Class)

    The loaded class.

Raises:

  • (ClassNotFound)

    The class file could not be found within #class_dir.or has a file/registered-name mismatch.

  • (LoadError)

    A load error occurred while requiring the other files required by the class file.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/ronin/core/class_registry.rb', line 221

def load_class(id)
  # short-circuit if the module is already loaded
  if (klass = registry[id])
    return klass
  end

  unless (path = path_for(id))
    raise(ClassNotFound,"could not find file for #{id.inspect}")
  end

  previous_entries = registry.keys

  require(path)

  unless (klass = registry[id])
    new_entries = registry.keys - previous_entries

    if new_entries.empty?
      raise(ClassNotFound,"file did not register a class: #{path.inspect}")
    else
      raise(ClassNotFound,"file registered a class with a different id (#{new_entries.map(&:inspect).join(', ')}): #{path.inspect}")
    end
  end

  return klass
end

#load_class_from_file(file) ⇒ Class

Loads a class from a file.

Parameters:

  • file (String)

    The file to load.

Returns:

  • (Class)

    The loaded class.

Raises:

  • (ClassNotFound)

    The file does not exist or the class id was not found within the file.

  • (LoadError)

    A load error occurred while requiring the other files required by the class file.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/ronin/core/class_registry.rb', line 186

def load_class_from_file(file)
  file = File.expand_path(file)

  unless File.file?(file)
    raise(ClassNotFound,"no such file or directory: #{file.inspect}")
  end

  require(file)

  registry.each_value do |worker_class|
    class_file, = worker_class.const_source_location(worker_class.name)

    return worker_class if class_file == file
  end

  raise(ClassNotFound,"file did not register a class: #{file.inspect}")
end

#path_for(id) ⇒ String?

Finds the path for the class id.

Examples:

Exploits.path_for('my_exploit')
# => "/path/to/lib/ronin/exploits/classes/my_exploit.rb"

Parameters:

  • id (String)

    The class id.

Returns:

  • (String, nil)

    The path for the module. If the module file does not exist in #class_dir then nil will be returned.



161
162
163
164
165
166
167
# File 'lib/ronin/core/class_registry.rb', line 161

def path_for(id)
  path = File.join(class_dir,"#{id}.rb")

  if File.file?(path)
    return path
  end
end

#register(id, mod) ⇒ Object

Registers a class with the registry.

Examples:

Exploits.register('myexploit',MyExploit)

Parameters:

  • id (String)

    The class id to be registered.

  • mod (Class)

    The class to be registered.



143
144
145
# File 'lib/ronin/core/class_registry.rb', line 143

def register(id,mod)
  registry[id] = mod
end

#registryHash{String => Class}

The class registry.

Returns:

  • (Hash{String => Class})

    The mapping of class id and classes.



127
128
129
# File 'lib/ronin/core/class_registry.rb', line 127

def registry
  @registry ||= {}
end