refact: refactored module parameters api

This commit is contained in:
evilsocket 2018-01-11 00:27:53 +01:00
commit e543582257
12 changed files with 111 additions and 198 deletions

View file

@ -12,9 +12,6 @@ type Module interface {
Running() bool
Start() error
Stop() error
OnSessionStarted(s *Session)
OnSessionEnded(s *Session)
}
type SessionModule struct {
@ -53,13 +50,38 @@ func (m *SessionModule) Param(name string) *ModuleParam {
return m.params[name]
}
func (m SessionModule) StringParam(name string) (error, string) {
if err, v := m.params[name].Get(m.Session); err != nil {
return err, ""
} else {
return nil, v.(string)
}
}
func (m SessionModule) IntParam(name string) (error, int) {
if err, v := m.params[name].Get(m.Session); err != nil {
return err, 0
} else {
return nil, v.(int)
}
}
func (m SessionModule) BoolParam(name string) (error, bool) {
if err, v := m.params[name].Get(m.Session); err != nil {
return err, false
} else {
return nil, v.(bool)
}
}
func (m *SessionModule) AddHandler(h ModuleHandler) {
m.handlers = append(m.handlers, h)
}
func (m *SessionModule) AddParam(p *ModuleParam) {
func (m *SessionModule) AddParam(p *ModuleParam) *ModuleParam {
m.params[p.Name] = p
p.Register(m.Session)
return p
}
func (m *SessionModule) Running() bool {
@ -79,7 +101,3 @@ func (m *SessionModule) SetRunning(running bool) {
m.Session.Events.Add("mod.stopped", m.Name)
}
}
func (m *SessionModule) OnSessionStarted(s *Session) {
}

View file

@ -79,6 +79,9 @@ func (p ModuleParam) Validate(value string) (error, interface{}) {
return fmt.Errorf("Unhandled module parameter type %d.", p.Type), nil
}
const ParamIfaceAddress = "<interface address>"
const ParamSubnet = "<entire subnet>"
func (p ModuleParam) Get(s *Session) (error, interface{}) {
var v string
var found bool
@ -89,6 +92,12 @@ func (p ModuleParam) Get(s *Session) (error, interface{}) {
v = ""
}
if v == ParamIfaceAddress {
v = s.Interface.IpAddress
} else if v == ParamSubnet {
v = s.Interface.CIDR()
}
err, obj = p.Validate(v)
return err, obj

View file

@ -133,7 +133,9 @@ func (s *Session) Close() {
s.Events.Add("session.closing", nil)
for _, m := range s.Modules {
m.OnSessionEnded(s)
if m.Running() {
m.Stop()
}
}
s.Firewall.Restore()
@ -216,11 +218,6 @@ func (s *Session) Start() error {
}()
s.Active = true
for _, m := range s.Modules {
m.OnSessionStarted(s)
}
s.Events.Add("session.started", nil)
return nil