mirror of
https://github.com/bettercap/bettercap
synced 2025-07-15 01:23:42 -07:00
new: properly exposing module handlers and parameters from api.rest
This commit is contained in:
parent
87ac32cd6b
commit
b98db78926
4 changed files with 64 additions and 22 deletions
|
@ -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:"-"`
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue