mirror of
https://github.com/bettercap/bettercap
synced 2025-08-22 06:23:18 -07:00
session: ModuleParam.Get() swap error return
session: ModuleParam.validate() swap error return session: fix calling functions to adjust to swapped returns wifi: fix calling functions to adjust to swapped returns
This commit is contained in:
parent
13c17cd30d
commit
c9cc78f9b3
3 changed files with 16 additions and 18 deletions
|
@ -172,7 +172,7 @@ func NewWiFiModule(s *session.Session) *WiFiModule {
|
||||||
"Minimum WiFi signal strength in dBm.")
|
"Minimum WiFi signal strength in dBm.")
|
||||||
|
|
||||||
mod.AddObservableParam(minRSSI, func(v string) {
|
mod.AddObservableParam(minRSSI, func(v string) {
|
||||||
if err, v := minRSSI.Get(s); err != nil {
|
if v, err := minRSSI.Get(s); err != nil {
|
||||||
mod.Error("%v", err)
|
mod.Error("%v", err)
|
||||||
} else if mod.minRSSI = v.(int); mod.Started {
|
} else if mod.minRSSI = v.(int); mod.Started {
|
||||||
mod.Info("wifi.rssi.min set to %d", mod.minRSSI)
|
mod.Info("wifi.rssi.min set to %d", mod.minRSSI)
|
||||||
|
@ -231,7 +231,7 @@ func NewWiFiModule(s *session.Session) *WiFiModule {
|
||||||
"Seconds of inactivity for an access points to be considered not in range anymore.")
|
"Seconds of inactivity for an access points to be considered not in range anymore.")
|
||||||
|
|
||||||
mod.AddObservableParam(apTTL, func(v string) {
|
mod.AddObservableParam(apTTL, func(v string) {
|
||||||
if err, v := apTTL.Get(s); err != nil {
|
if v, err := apTTL.Get(s); err != nil {
|
||||||
mod.Error("%v", err)
|
mod.Error("%v", err)
|
||||||
} else if mod.apTTL = v.(int); mod.Started {
|
} else if mod.apTTL = v.(int); mod.Started {
|
||||||
mod.Info("wifi.ap.ttl set to %d", mod.apTTL)
|
mod.Info("wifi.ap.ttl set to %d", mod.apTTL)
|
||||||
|
@ -243,7 +243,7 @@ func NewWiFiModule(s *session.Session) *WiFiModule {
|
||||||
"Seconds of inactivity for a client station to be considered not in range or not connected to its access point anymore.")
|
"Seconds of inactivity for a client station to be considered not in range or not connected to its access point anymore.")
|
||||||
|
|
||||||
mod.AddObservableParam(staTTL, func(v string) {
|
mod.AddObservableParam(staTTL, func(v string) {
|
||||||
if err, v := staTTL.Get(s); err != nil {
|
if v, err := staTTL.Get(s); err != nil {
|
||||||
mod.Error("%v", err)
|
mod.Error("%v", err)
|
||||||
} else if mod.staTTL = v.(int); mod.Started {
|
} else if mod.staTTL = v.(int); mod.Started {
|
||||||
mod.Info("wifi.sta.ttl set to %d", mod.staTTL)
|
mod.Info("wifi.sta.ttl set to %d", mod.staTTL)
|
||||||
|
|
|
@ -171,7 +171,7 @@ func (m SessionModule) ListParam(name string) (err error, values []string) {
|
||||||
|
|
||||||
func (m SessionModule) StringParam(name string) (error, string) {
|
func (m SessionModule) StringParam(name string) (error, string) {
|
||||||
if p, found := m.params[name]; found {
|
if p, found := m.params[name]; found {
|
||||||
if err, v := p.Get(m.Session); err != nil {
|
if v, err := p.Get(m.Session); err != nil {
|
||||||
return err, ""
|
return err, ""
|
||||||
} else {
|
} else {
|
||||||
return nil, v.(string)
|
return nil, v.(string)
|
||||||
|
@ -191,7 +191,7 @@ func (m SessionModule) IPParam(name string) (error, net.IP) {
|
||||||
|
|
||||||
func (m SessionModule) IntParam(name string) (error, int) {
|
func (m SessionModule) IntParam(name string) (error, int) {
|
||||||
if p, found := m.params[name]; found {
|
if p, found := m.params[name]; found {
|
||||||
if err, v := p.Get(m.Session); err != nil {
|
if v, err := p.Get(m.Session); err != nil {
|
||||||
return err, 0
|
return err, 0
|
||||||
} else {
|
} else {
|
||||||
return nil, v.(int)
|
return nil, v.(int)
|
||||||
|
@ -204,7 +204,7 @@ func (m SessionModule) IntParam(name string) (error, int) {
|
||||||
|
|
||||||
func (m SessionModule) DecParam(name string) (error, float64) {
|
func (m SessionModule) DecParam(name string) (error, float64) {
|
||||||
if p, found := m.params[name]; found {
|
if p, found := m.params[name]; found {
|
||||||
if err, v := p.Get(m.Session); err != nil {
|
if v, err := p.Get(m.Session); err != nil {
|
||||||
return err, 0
|
return err, 0
|
||||||
} else {
|
} else {
|
||||||
return nil, v.(float64)
|
return nil, v.(float64)
|
||||||
|
@ -216,7 +216,7 @@ func (m SessionModule) DecParam(name string) (error, float64) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m SessionModule) BoolParam(name string) (error, bool) {
|
func (m SessionModule) BoolParam(name string) (error, bool) {
|
||||||
if err, v := m.params[name].Get(m.Session); err != nil {
|
if v, err := m.params[name].Get(m.Session); err != nil {
|
||||||
return err, false
|
return err, false
|
||||||
} else {
|
} else {
|
||||||
return nil, v.(bool)
|
return nil, v.(bool)
|
||||||
|
|
|
@ -62,34 +62,32 @@ func NewDecimalParameter(name string, def_value string, desc string) *ModulePara
|
||||||
return NewModuleParameter(name, def_value, FLOAT, "^[\\d]+(\\.\\d+)?$", desc)
|
return NewModuleParameter(name, def_value, FLOAT, "^[\\d]+(\\.\\d+)?$", desc)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p ModuleParam) validate(value string) (error, interface{}) {
|
func (p ModuleParam) validate(value string) (interface{}, error) {
|
||||||
if p.Validator != nil {
|
if p.Validator != nil {
|
||||||
if !p.Validator.MatchString(value) {
|
if !p.Validator.MatchString(value) {
|
||||||
return fmt.Errorf("Parameter %s not valid: '%s' does not match rule '%s'.", tui.Bold(p.Name), value, p.Validator.String()), nil
|
return nil, fmt.Errorf("Parameter %s not valid: '%s' does not match rule '%s'.", tui.Bold(p.Name), value, p.Validator.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch p.Type {
|
switch p.Type {
|
||||||
case STRING:
|
case STRING:
|
||||||
return nil, value
|
return value, nil
|
||||||
case BOOL:
|
case BOOL:
|
||||||
lvalue := strings.ToLower(value)
|
lvalue := strings.ToLower(value)
|
||||||
if lvalue == "true" {
|
if lvalue == "true" {
|
||||||
return nil, true
|
return true, nil
|
||||||
} else if lvalue == "false" {
|
} else if lvalue == "false" {
|
||||||
return nil, false
|
return false, nil
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("Can't typecast '%s' to boolean.", value), nil
|
return fmt.Errorf("Can't typecast '%s' to boolean.", value), nil
|
||||||
}
|
}
|
||||||
case INT:
|
case INT:
|
||||||
i, err := strconv.Atoi(value)
|
return strconv.Atoi(value)
|
||||||
return err, i
|
|
||||||
case FLOAT:
|
case FLOAT:
|
||||||
i, err := strconv.ParseFloat(value, 64)
|
return strconv.ParseFloat(value, 64)
|
||||||
return err, i
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Errorf("Unhandled module parameter type %d.", p.Type), nil
|
return nil, fmt.Errorf("Unhandled module parameter type %d.", p.Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
const ParamIfaceName = "<interface name>"
|
const ParamIfaceName = "<interface name>"
|
||||||
|
@ -122,7 +120,7 @@ func (p ModuleParam) getUnlocked(s *Session) string {
|
||||||
return p.parse(s, v)
|
return p.parse(s, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p ModuleParam) Get(s *Session) (error, interface{}) {
|
func (p ModuleParam) Get(s *Session) (interface{}, error) {
|
||||||
_, v := s.Env.Get(p.Name)
|
_, v := s.Env.Get(p.Name)
|
||||||
v = p.parse(s, v)
|
v = p.parse(s, v)
|
||||||
return p.validate(v)
|
return p.validate(v)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue