From 0f427911be67f2db1414790a82257bb9d2a9f9d6 Mon Sep 17 00:00:00 2001 From: evilsocket Date: Thu, 14 Mar 2019 21:05:33 +0100 Subject: [PATCH] new: exporting module parameters current value in api.rest --- session/environment.go | 12 +++++++----- session/module_param.go | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/session/environment.go b/session/environment.go index 7f8d48b0..3f496689 100644 --- a/session/environment.go +++ b/session/environment.go @@ -99,17 +99,19 @@ func (env *Environment) Set(name, value string) string { return old } -func (env *Environment) Get(name string) (bool, string) { - env.Lock() - defer env.Unlock() - +func (env *Environment) GetUnlocked(name string) (bool, string) { if value, found := env.Data[name]; found { return true, value } - return false, "" } +func (env *Environment) Get(name string) (bool, string) { + env.Lock() + defer env.Unlock() + return env.GetUnlocked(name) +} + func (env *Environment) GetInt(name string) (error, int) { if found, value := env.Get(name); found { if i, err := strconv.Atoi(value); err == nil { diff --git a/session/module_param.go b/session/module_param.go index 012a8436..47a85f64 100644 --- a/session/module_param.go +++ b/session/module_param.go @@ -97,8 +97,7 @@ const ParamIfaceAddress = "" const ParamSubnet = "" const ParamRandomMAC = "" -func (p ModuleParam) Get(s *Session) (error, interface{}) { - _, v := s.Env.Get(p.Name) +func (p ModuleParam) parse(s *Session, v string) string { switch v { case ParamIfaceName: v = s.Interface.Name() @@ -111,7 +110,18 @@ func (p ModuleParam) Get(s *Session) (error, interface{}) { rand.Read(hw) v = net.HardwareAddr(hw).String() } + return v +} + +func (p ModuleParam) getUnlocked(s *Session) string { + _, v := s.Env.GetUnlocked(p.Name) + return p.parse(s, v) +} + +func (p ModuleParam) Get(s *Session) (error, interface{}) { + _, v := s.Env.Get(p.Name) + v = p.parse(s, v) return p.Validate(v) } @@ -130,6 +140,7 @@ type JSONModuleParam struct { Type ParamType `json:"type"` Description string `json:"description"` Value string `json:"default_value"` + Current string `json:"current_value"` Validator string `json:"validator"` } @@ -139,6 +150,7 @@ func (p ModuleParam) MarshalJSON() ([]byte, error) { Type: p.Type, Description: p.Description, Value: p.Value, + Current: p.getUnlocked(I), // if we're here, Env is already locked } if p.Validator != nil { j.Validator = p.Validator.String()