mirror of
https://github.com/bettercap/bettercap
synced 2025-08-14 02:36:57 -07:00
new: preloading and exporting caplets from api.rest
This commit is contained in:
parent
6b9b27302e
commit
3edf80fc99
3 changed files with 67 additions and 17 deletions
|
@ -9,10 +9,19 @@ import (
|
|||
)
|
||||
|
||||
type Caplet struct {
|
||||
Name string
|
||||
Path string
|
||||
Size int64
|
||||
Code []string
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
Size int64 `json:"size"`
|
||||
Code []string `json:"code"`
|
||||
}
|
||||
|
||||
func NewCaplet(name string, path string, size int64) Caplet {
|
||||
return Caplet{
|
||||
Name: name,
|
||||
Path: path,
|
||||
Size: size,
|
||||
Code: make([]string, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (cap *Caplet) Eval(argv []string, lineCb func(line string) error) error {
|
||||
|
|
|
@ -16,22 +16,22 @@ var (
|
|||
cacheLock = sync.Mutex{}
|
||||
)
|
||||
|
||||
func List() []Caplet {
|
||||
caplets := make([]Caplet, 0)
|
||||
func List() []*Caplet {
|
||||
caplets := make([]*Caplet, 0)
|
||||
for _, searchPath := range LoadPaths {
|
||||
files, _ := filepath.Glob(searchPath + "/*" + Suffix)
|
||||
files2, _ := filepath.Glob(searchPath + "/*/*" + Suffix)
|
||||
|
||||
for _, fileName := range append(files, files2...) {
|
||||
if stats, err := os.Stat(fileName); err == nil {
|
||||
if _, err := os.Stat(fileName); err == nil {
|
||||
base := strings.Replace(fileName, searchPath+"/", "", -1)
|
||||
base = strings.Replace(base, Suffix, "", -1)
|
||||
|
||||
caplets = append(caplets, Caplet{
|
||||
Name: base,
|
||||
Path: fileName,
|
||||
Size: stats.Size(),
|
||||
})
|
||||
if err, caplet := Load(base); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "wtf: %v\n", err)
|
||||
} else {
|
||||
caplets = append(caplets, caplet)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -51,6 +51,7 @@ func Load(name string) (error, *Caplet) {
|
|||
return nil, caplet
|
||||
}
|
||||
|
||||
baseName := name
|
||||
names := []string{}
|
||||
if !strings.HasSuffix(name, Suffix) {
|
||||
name += Suffix
|
||||
|
@ -64,16 +65,18 @@ func Load(name string) (error, *Caplet) {
|
|||
names = append(names, name)
|
||||
}
|
||||
|
||||
for _, filename := range names {
|
||||
if fs.Exists(filename) {
|
||||
for _, fileName := range names {
|
||||
if stats, err := os.Stat(fileName); err == nil {
|
||||
cap := &Caplet{
|
||||
Path: filename,
|
||||
Name: baseName,
|
||||
Path: fileName,
|
||||
Code: make([]string, 0),
|
||||
Size: stats.Size(),
|
||||
}
|
||||
cache[name] = cap
|
||||
|
||||
if reader, err := fs.LineReader(filename); err != nil {
|
||||
return fmt.Errorf("error reading caplet %s: %v", filename, err), nil
|
||||
if reader, err := fs.LineReader(fileName); err != nil {
|
||||
return fmt.Errorf("error reading caplet %s: %v", fileName, err), nil
|
||||
} else {
|
||||
for line := range reader {
|
||||
if line == "" || line[0] == '#' {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
|
@ -77,6 +78,23 @@ type Session struct {
|
|||
Firewall firewall.FirewallManager `json:"-"`
|
||||
}
|
||||
|
||||
type sessionJSON struct {
|
||||
Options core.Options `json:"options"`
|
||||
Interface *network.Endpoint `json:"interface"`
|
||||
Gateway *network.Endpoint `json:"gateway"`
|
||||
Env *Environment `json:"env"`
|
||||
Lan *network.LAN `json:"lan"`
|
||||
WiFi *network.WiFi `json:"wifi"`
|
||||
BLE *network.BLE `json:"ble"`
|
||||
HID *network.HID `json:"hid"`
|
||||
Queue *packets.Queue `json:"packets"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
Active bool `json:"active"`
|
||||
GPS GPS `json:"gps"`
|
||||
Modules ModuleList `json:"modules"`
|
||||
Caplets []*caplets.Caplet `json:"caplets"`
|
||||
}
|
||||
|
||||
func New() (*Session, error) {
|
||||
opts, err := core.ParseOptions()
|
||||
if err != nil {
|
||||
|
@ -124,6 +142,26 @@ func New() (*Session, error) {
|
|||
return s, nil
|
||||
}
|
||||
|
||||
func (s *Session) MarshalJSON() ([]byte, error) {
|
||||
doc := sessionJSON{
|
||||
Options: s.Options,
|
||||
Interface: s.Interface,
|
||||
Gateway: s.Gateway,
|
||||
Env: s.Env,
|
||||
Lan: s.Lan,
|
||||
WiFi: s.WiFi,
|
||||
BLE: s.BLE,
|
||||
HID: s.HID,
|
||||
Queue: s.Queue,
|
||||
StartedAt: s.StartedAt,
|
||||
Active: s.Active,
|
||||
GPS: s.GPS,
|
||||
Modules: s.Modules,
|
||||
Caplets: caplets.List(),
|
||||
}
|
||||
return json.Marshal(doc)
|
||||
}
|
||||
|
||||
func (s *Session) Lock() {
|
||||
s.Env.Lock()
|
||||
s.Lan.Lock()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue