Skip to content

Commit

Permalink
new: exporting module parameters current value in api.rest
Browse files Browse the repository at this point in the history
  • Loading branch information
evilsocket committed Mar 14, 2019
1 parent ee8fe97 commit 0f42791
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
12 changes: 7 additions & 5 deletions session/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 14 additions & 2 deletions session/module_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}

Expand All @@ -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"`
}

Expand All @@ -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()
Expand Down

0 comments on commit 0f42791

Please sign in to comment.