new: exporting module parameters current value in api.rest

This commit is contained in:
evilsocket 2019-03-14 21:05:33 +01:00
commit 0f427911be
No known key found for this signature in database
GPG key ID: 1564D7F30393A456
2 changed files with 21 additions and 7 deletions

View file

@ -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 {

View file

@ -97,8 +97,7 @@ const ParamIfaceAddress = "<interface address>"
const ParamSubnet = "<entire subnet>"
const ParamRandomMAC = "<random mac>"
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()