misc: small fix or general refactoring i did not bother commenting

This commit is contained in:
evilsocket 2019-03-18 12:23:55 +01:00
commit ba4793f980
No known key found for this signature in database
GPG key ID: 1564D7F30393A456
2 changed files with 39 additions and 48 deletions

View file

@ -20,11 +20,41 @@ type Module interface {
Handlers() []ModuleHandler
Parameters() map[string]*ModuleParam
Extra() map[string]interface{}
Running() bool
Start() error
Stop() error
}
type ModuleList []Module
type moduleJSON struct {
Name string `json:"name"`
Description string `json:"description"`
Author string `json:"author"`
Parameters map[string]*ModuleParam `json:"parameters"`
Handlers []ModuleHandler `json:"handlers"`
Running bool `json:"running"`
State map[string]interface{} `json:"state"`
}
func (mm ModuleList) MarshalJSON() ([]byte, error) {
mods := []moduleJSON{}
for _, m := range mm {
mJSON := moduleJSON{
Name: m.Name(),
Description: m.Description(),
Author: m.Author(),
Parameters: m.Parameters(),
Handlers: m.Handlers(),
Running: m.Running(),
State: m.Extra(),
}
mods = append(mods, mJSON)
}
return json.Marshal(mods)
}
type SessionModule struct {
Name string
Session *Session
@ -37,12 +67,6 @@ type SessionModule struct {
tag string
}
type sessionModuleJSON struct {
Name string `json:"name"`
Started bool `json:"started"`
State map[string]interface{} `json:"state"`
}
func AsTag(name string) string {
return fmt.Sprintf("%s ", tui.Wrap(tui.BACKLIGHTBLUE, tui.Wrap(tui.FOREBLACK, name)))
}
@ -62,6 +86,15 @@ func NewSessionModule(name string, s *Session) SessionModule {
return m
}
func (m *SessionModule) Extra() map[string]interface{} {
extra := make(map[string]interface{})
m.State.Range(func(k, v interface{}) bool {
extra[k.(string)] = v
return true
})
return extra
}
func (m *SessionModule) InitState(keys ...string) {
for _, key := range keys {
m.State.Store(key, nil)
@ -75,21 +108,6 @@ func (m *SessionModule) ResetState() {
})
}
func (m *SessionModule) MarshalJSON() ([]byte, error) {
doc := sessionModuleJSON{
Name: m.Name,
Started: m.Started,
State: make(map[string]interface{}),
}
m.State.Range(func(k, v interface{}) bool {
doc.State[k.(string)] = v
return true
})
return json.Marshal(doc)
}
func (m *SessionModule) Debug(format string, args ...interface{}) {
m.Session.Events.Log(log.DEBUG, m.tag+format, args...)
}

View file

@ -1,7 +1,6 @@
package session
import (
"encoding/json"
"errors"
"fmt"
"net"
@ -45,32 +44,6 @@ var (
type UnknownCommandCallback func(cmd string) bool
type ModuleList []Module
type JSONModule struct {
Name string `json:"name"`
Description string `json:"description"`
Author string `json:"author"`
Parameters map[string]*ModuleParam `json:"parameters"`
Handlers []ModuleHandler `json:"handlers"`
Running bool `json:"running"`
}
func (mm ModuleList) MarshalJSON() ([]byte, error) {
mods := []JSONModule{}
for _, m := range mm {
mods = append(mods, JSONModule{
Name: m.Name(),
Description: m.Description(),
Author: m.Author(),
Parameters: m.Parameters(),
Handlers: m.Handlers(),
Running: m.Running(),
})
}
return json.Marshal(mods)
}
type GPS struct {
Latitude float64 // Latitude.
Longitude float64 // Longitude.