Skip to content

Commit

Permalink
Expanded the goptions command to set and unset global options from th…
Browse files Browse the repository at this point in the history
…e module context. Closes #47
  • Loading branch information
lanmaster53 committed Jan 13, 2020
1 parent cbae5b3 commit 055de2c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
25 changes: 14 additions & 11 deletions recon/core/framework.py
Expand Up @@ -709,7 +709,7 @@ def _load_config(self):
# invalid key, contnue to load valid keys # invalid key, contnue to load valid keys
continue continue


def _save_config(self, name): def _save_config(self, name, module=None, options=None):
config_path = os.path.join(self.workspace, 'config.dat') config_path = os.path.join(self.workspace, 'config.dat')
# create a config file if one doesn't exist # create a config file if one doesn't exist
open(config_path, 'a').close() open(config_path, 'a').close()
Expand All @@ -720,17 +720,20 @@ def _save_config(self, name):
except ValueError: except ValueError:
# file is empty or corrupt, nothing to load # file is empty or corrupt, nothing to load
config_data = {} config_data = {}
# override implicit defaults if specified
module = module or self._modulename
options = options or self.options
# create a container for the current module # create a container for the current module
if self._modulename not in config_data: if module not in config_data:
config_data[self._modulename] = {} config_data[module] = {}
# set the new option value in the config # set the new option value in the config
config_data[self._modulename][name] = self.options[name] config_data[module][name] = options[name]
# remove the option if it has been unset # remove the option if it has been unset
if config_data[self._modulename][name] is None: if config_data[module][name] is None:
del config_data[self._modulename][name] del config_data[module][name]
# remove the module container if it is empty # remove the module container if it is empty
if not config_data[self._modulename]: if not config_data[module]:
del config_data[self._modulename] del config_data[module]
# write the new config data to the config file # write the new config data to the config file
with open(config_path, 'w') as config_file: with open(config_path, 'w') as config_file:
json.dump(config_data, config_file, indent=4) json.dump(config_data, config_file, indent=4)
Expand Down Expand Up @@ -911,7 +914,7 @@ def _do_options_list(self, params):
def _do_options_set(self, params): def _do_options_set(self, params):
'''Sets a current context option''' '''Sets a current context option'''
option, value = self._parse_params(params) option, value = self._parse_params(params)
if not option and value: if not (option and value):
self._help_options_set() self._help_options_set()
return return
name = option.upper() name = option.upper()
Expand Down Expand Up @@ -1297,11 +1300,11 @@ def help_options(self):


def _help_options_set(self): def _help_options_set(self):
print(getattr(self, '_do_options_set').__doc__) print(getattr(self, '_do_options_set').__doc__)
print(f"{os.linesep}Usage: set <option> <value>{os.linesep}") print(f"{os.linesep}Usage: options set <option> <value>{os.linesep}")


def _help_options_unset(self): def _help_options_unset(self):
print(getattr(self, '_do_options_unset').__doc__) print(getattr(self, '_do_options_unset').__doc__)
print(f"{os.linesep}Usage: unset <option>{os.linesep}") print(f"{os.linesep}Usage: options unset <option>{os.linesep}")


def help_keys(self): def help_keys(self):
print(getattr(self, 'do_keys').__doc__) print(getattr(self, 'do_keys').__doc__)
Expand Down
42 changes: 40 additions & 2 deletions recon/core/module.py
Expand Up @@ -221,6 +221,32 @@ def _do_goptions_list(self, params):
'''Shows the global context options''' '''Shows the global context options'''
self._list_options(self._global_options) self._list_options(self._global_options)


def _do_goptions_set(self, params):
'''Sets a global context option'''
option, value = self._parse_params(params)
if not (option and value):
self._help_goptions_set()
return
name = option.upper()
if name in self._global_options:
self._global_options[name] = value
print(f"{name} => {value}")
self._save_config(name, 'base', self._global_options)
else:
self.error('Invalid option name.')

def _do_goptions_unset(self, params):
'''Unsets a global context option'''
option, value = self._parse_params(params)
if not option:
self._help_goptions_unset()
return
name = option.upper()
if name in self._global_options:
self._do_goptions_set(' '.join([name, 'None']))
else:
self.error('Invalid option name.')

def _do_modules_load(self, params): def _do_modules_load(self, params):
'''Loads a module''' '''Loads a module'''
if not params: if not params:
Expand Down Expand Up @@ -347,8 +373,16 @@ def do_run(self, params):
#================================================== #==================================================


def help_goptions(self): def help_goptions(self):
print(getattr(self, 'do_options').__doc__) print(getattr(self, 'do_goptions').__doc__)
print(f"{os.linesep}Usage: goptions <list>{os.linesep}") print(f"{os.linesep}Usage: goptions <{'|'.join(self._parse_subcommands('goptions'))}> [...]{os.linesep}")

def _help_goptions_set(self):
print(getattr(self, '_do_goptions_set').__doc__)
print(f"{os.linesep}Usage: goptions set <option> <value>{os.linesep}")

def _help_goptions_unset(self):
print(getattr(self, '_do_goptions_unset').__doc__)
print(f"{os.linesep}Usage: goptions unset <option>{os.linesep}")


#================================================== #==================================================
# COMPLETE METHODS # COMPLETE METHODS
Expand All @@ -364,6 +398,10 @@ def complete_goptions(self, text, line, *ignored):
def _complete_goptions_list(self, text, *ignored): def _complete_goptions_list(self, text, *ignored):
return [] return []


def _complete_goptions_set(self, text, *ignored):
return [x for x in self._global_options if x.startswith(text.upper())]
_complete_goptions_unset = _complete_goptions_set

def complete_reload(self, text, *ignored): def complete_reload(self, text, *ignored):
return [] return []
complete_info = complete_input = complete_run = complete_reload complete_info = complete_input = complete_run = complete_reload
Expand Down

0 comments on commit 055de2c

Please sign in to comment.