diff --git a/session/module.go b/session/module.go index 1f2c9563..b57b7b59 100644 --- a/session/module.go +++ b/session/module.go @@ -22,14 +22,6 @@ type Module interface { Stop() error } -type JSONModule struct { - Name string `json:"name"` - Description string `json:"description"` - Author string `json:"author"` - Parameters map[string]*ModuleParam `json:"parameters"` - Running bool `json:"running"` -} - type SessionModule struct { Name string `json:"name"` Session *Session `json:"-"` diff --git a/session/module_handler.go b/session/module_handler.go index 35b18027..1544c182 100644 --- a/session/module_handler.go +++ b/session/module_handler.go @@ -1,6 +1,7 @@ package session import ( + "encoding/json" "fmt" "regexp" "strconv" @@ -49,3 +50,20 @@ func (h *ModuleHandler) Parse(line string) (bool, []string) { } return false, nil } + +type JSONModuleHandler struct { + Name string `json:"name"` + Description string `json:"description"` + Parser string `json:"parser"` +} + +func (h ModuleHandler) MarshalJSON() ([]byte, error) { + j := JSONModuleHandler{ + Name: h.Name, + Description: h.Description, + } + if h.Parser != nil { + j.Parser = h.Parser.String() + } + return json.Marshal(j) +} diff --git a/session/module_param.go b/session/module_param.go index 489b692e..51ebbee7 100644 --- a/session/module_param.go +++ b/session/module_param.go @@ -2,6 +2,7 @@ package session import ( "crypto/rand" + "encoding/json" "fmt" "net" "regexp" @@ -117,3 +118,24 @@ func (p ModuleParam) Help(padding int) string { func (p ModuleParam) Register(s *Session) { s.Env.Set(p.Name, p.Value) } + +type JSONModuleParam struct { + Name string `json:"name"` + Type ParamType `json:"type"` + Description string `json:"description"` + Value string `json:"default_value"` + Validator string `json:"validator"` +} + +func (p ModuleParam) MarshalJSON() ([]byte, error) { + j := JSONModuleParam{ + Name: p.Name, + Type: p.Type, + Description: p.Description, + Value: p.Value, + } + if p.Validator != nil { + j.Validator = p.Validator.String() + } + return json.Marshal(j) +} diff --git a/session/session.go b/session/session.go index f07a8356..20366c0b 100644 --- a/session/session.go +++ b/session/session.go @@ -43,6 +43,30 @@ 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 Session struct { Options core.Options `json:"options"` Interface *network.Endpoint `json:"interface"` @@ -65,20 +89,6 @@ type Session struct { Firewall firewall.FirewallManager `json:"-"` } -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(), - Running: m.Running(), - }) - } - return json.Marshal(mods) -} - func New() (*Session, error) { var err error