Skip to content
This repository has been archived by the owner on Oct 22, 2020. It is now read-only.

Commit

Permalink
Add module caching
Browse files Browse the repository at this point in the history
  • Loading branch information
rastating committed Jun 13, 2018
1 parent a2eff6f commit 4f153fd
Show file tree
Hide file tree
Showing 11 changed files with 404 additions and 4 deletions.
16 changes: 16 additions & 0 deletions db/migrations/005_add_logs.rb
@@ -0,0 +1,16 @@
# frozen_string_literal: true

Sequel.migration do
up do
create_table :logs do
primary_key :id

column :key, :string, size: 50, unique: true, null: false
column :value, :string, size: 100, null: false
end
end

down do
drop_table :logs
end
end
18 changes: 18 additions & 0 deletions db/migrations/006_create_modules.rb
@@ -0,0 +1,18 @@
# frozen_string_literal: true

Sequel.migration do
up do
create_table :modules do
primary_key :id

column :path, :string, size: 255, null: false, unique: true
column :name, :string, size: 255, null: false
column :type, :string, size: 11, null: false
column :class_name, :string, size: 255, null: false, unique: true
end
end

down do
drop_table :modules
end
end
18 changes: 14 additions & 4 deletions lib/cli/console.rb
Expand Up @@ -6,6 +6,7 @@
require 'cli/auto_complete'
require 'cli/context'
require 'cli/modules'
require 'cli/module_cache'
require 'cli/module_info'
require 'cli/loaded_module'
require 'cli/options'
Expand All @@ -19,6 +20,7 @@ class Console
include Cli::AutoComplete
include Cli::Help
include Cli::Modules
include Cli::ModuleCache
include Cli::LoadedModule
include Cli::Options
include Cli::Output
Expand Down Expand Up @@ -50,10 +52,6 @@ def clear
Gem.win_platform? ? (system 'cls') : (system 'clear')
end

def quit
exit
end

def prompt_for_input
prompt = 'wpxf'.underline.light_blue

Expand Down Expand Up @@ -132,10 +130,22 @@ def execute_user_command(command, args)
puts unless commands_without_output.include? command
end

def check_cache
return if cache_valid?

print_warning 'Refreshing the module cache...'
puts
rebuild_cache
end

def start
check_cache

loop do
begin
input = prompt_for_input
break if input =~ /exit|quit/

command, *args = input.split(/\s/)
execute_user_command(command, args) if command
rescue StandardError => e
Expand Down
54 changes: 54 additions & 0 deletions lib/cli/module_cache.rb
@@ -0,0 +1,54 @@
# frozen_string_literal: true

module Cli
# A mixin to handle the database caching of module data.
module ModuleCache
def initialize
super
self.current_version_number = File.read(File.join(Wpxf.app_path, 'VERSION'))
end

def cache_valid?
last_version_log = Models::Log.first(key: 'version')
return false if last_version_log.nil?

current_version = Gem::Version.new(current_version_number)
last_version = Gem::Version.new(last_version_log.value)

current_version == last_version
end

def create_module_models(type)
namespace = type == 'exploit' ? Wpxf::Exploit : Wpxf::Auxiliary
namespace.module_list.each do |mod|
instance = mod[:class].new

Models::Module.create(
path: mod[:name],
name: instance.module_name,
type: type,
class_name: mod[:class].to_s
)
end
end

def refresh_version_log
log = Models::Log.first(key: 'version')
log = Models::Log.new if log.nil?
log.key = 'version'
log.value = current_version_number
log.save
end

def rebuild_cache
Models::Module.truncate

create_module_models 'exploit'
create_module_models 'auxiliary'

refresh_version_log
end

attr_accessor :current_version_number
end
end
20 changes: 20 additions & 0 deletions lib/models/log.rb
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Models
# A miscellaneous log entry.
class Log < Sequel::Model
plugin :validation_helpers

def validate
super

validates_presence :key
validates_type String, :key
validates_unique :key
validates_presence :value

validates_max_length 50, :key
validates_max_length 100, :value
end
end
end
30 changes: 30 additions & 0 deletions lib/models/module.rb
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Models
# A cache of a {Wpxf::Module}'s metadata.
class Module < Sequel::Model
plugin :validation_helpers

def validate
super

validates_presence :path
validates_presence :name
validates_presence :type
validates_presence :class_name

validates_type String, :path
validates_type String, :name
validates_type String, :class_name

validates_unique :path
validates_unique :class_name

validates_max_length 255, :path
validates_max_length 255, :name
validates_max_length 255, :class_name

validates_format /^auxiliary|exploit$/, :type
end
end
end
3 changes: 3 additions & 0 deletions lib/wpxf/db.rb
Expand Up @@ -9,4 +9,7 @@ module Db

require 'models/workspace'
require 'models/credential'
require 'models/log'
require 'models/module'

require 'wpxf/db/credentials'
56 changes: 56 additions & 0 deletions spec/lib/cli/console_spec.rb
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require_relative '../../spec_helper'
require 'cli/console'

describe Cli::Console do
let(:subject) { Cli::Console.new }

before :each, 'setup spies' do
allow(subject).to receive(:print_warning)
allow(subject).to receive(:rebuild_cache)
allow(subject).to receive(:puts)
end

describe '#start' do
before :each, 'setup mocks' do
allow(subject).to receive(:prompt_for_input).and_return('exit')
allow(subject).to receive(:check_cache)
end

it 'should check the module cache' do
subject.start
expect(subject).to have_received(:check_cache).exactly(1).times
end
end

describe '#check_cache' do
context 'if the module cache is not valid' do
before(:each) do
allow(subject).to receive(:cache_valid?).and_return(false)
end

it 'should refresh the module cache' do
subject.check_cache
expect(subject).to have_received(:rebuild_cache).exactly(1).times
end

it 'should warn the user the cache is being refreshed' do
subject.check_cache
expect(subject).to have_received(:print_warning)
.with('Refreshing the module cache...')
end
end

context 'if the module cache is valid' do
before(:each) do
allow(subject).to receive(:cache_valid?).and_return(true)
end

it 'should not refresh the cache' do
subject.check_cache
expect(subject).to have_received(:rebuild_cache).exactly(0).times
end
end
end
end

0 comments on commit 4f153fd

Please sign in to comment.