mirror of
https://github.com/bettercap/bettercap
synced 2025-07-15 09:33:40 -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
|
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 {
|
type SessionModule struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Session *Session `json:"-"`
|
Session *Session `json:"-"`
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package session
|
package session
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -49,3 +50,20 @@ func (h *ModuleHandler) Parse(line string) (bool, []string) {
|
||||||
}
|
}
|
||||||
return false, nil
|
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 (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
@ -117,3 +118,24 @@ func (p ModuleParam) Help(padding int) string {
|
||||||
func (p ModuleParam) Register(s *Session) {
|
func (p ModuleParam) Register(s *Session) {
|
||||||
s.Env.Set(p.Name, p.Value)
|
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 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 {
|
type Session struct {
|
||||||
Options core.Options `json:"options"`
|
Options core.Options `json:"options"`
|
||||||
Interface *network.Endpoint `json:"interface"`
|
Interface *network.Endpoint `json:"interface"`
|
||||||
|
@ -65,20 +89,6 @@ type Session struct {
|
||||||
Firewall firewall.FirewallManager `json:"-"`
|
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) {
|
func New() (*Session, error) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue