mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 21:13:18 -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 {
|
type Caplet struct {
|
||||||
Name string
|
Name string `json:"name"`
|
||||||
Path string
|
Path string `json:"path"`
|
||||||
Size int64
|
Size int64 `json:"size"`
|
||||||
Code []string
|
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 {
|
func (cap *Caplet) Eval(argv []string, lineCb func(line string) error) error {
|
||||||
|
|
|
@ -16,22 +16,22 @@ var (
|
||||||
cacheLock = sync.Mutex{}
|
cacheLock = sync.Mutex{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func List() []Caplet {
|
func List() []*Caplet {
|
||||||
caplets := make([]Caplet, 0)
|
caplets := make([]*Caplet, 0)
|
||||||
for _, searchPath := range LoadPaths {
|
for _, searchPath := range LoadPaths {
|
||||||
files, _ := filepath.Glob(searchPath + "/*" + Suffix)
|
files, _ := filepath.Glob(searchPath + "/*" + Suffix)
|
||||||
files2, _ := filepath.Glob(searchPath + "/*/*" + Suffix)
|
files2, _ := filepath.Glob(searchPath + "/*/*" + Suffix)
|
||||||
|
|
||||||
for _, fileName := range append(files, files2...) {
|
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(fileName, searchPath+"/", "", -1)
|
||||||
base = strings.Replace(base, Suffix, "", -1)
|
base = strings.Replace(base, Suffix, "", -1)
|
||||||
|
|
||||||
caplets = append(caplets, Caplet{
|
if err, caplet := Load(base); err != nil {
|
||||||
Name: base,
|
fmt.Fprintf(os.Stderr, "wtf: %v\n", err)
|
||||||
Path: fileName,
|
} else {
|
||||||
Size: stats.Size(),
|
caplets = append(caplets, caplet)
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,6 +51,7 @@ func Load(name string) (error, *Caplet) {
|
||||||
return nil, caplet
|
return nil, caplet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
baseName := name
|
||||||
names := []string{}
|
names := []string{}
|
||||||
if !strings.HasSuffix(name, Suffix) {
|
if !strings.HasSuffix(name, Suffix) {
|
||||||
name += Suffix
|
name += Suffix
|
||||||
|
@ -64,16 +65,18 @@ func Load(name string) (error, *Caplet) {
|
||||||
names = append(names, name)
|
names = append(names, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, filename := range names {
|
for _, fileName := range names {
|
||||||
if fs.Exists(filename) {
|
if stats, err := os.Stat(fileName); err == nil {
|
||||||
cap := &Caplet{
|
cap := &Caplet{
|
||||||
Path: filename,
|
Name: baseName,
|
||||||
|
Path: fileName,
|
||||||
Code: make([]string, 0),
|
Code: make([]string, 0),
|
||||||
|
Size: stats.Size(),
|
||||||
}
|
}
|
||||||
cache[name] = cap
|
cache[name] = cap
|
||||||
|
|
||||||
if reader, err := fs.LineReader(filename); err != nil {
|
if reader, err := fs.LineReader(fileName); err != nil {
|
||||||
return fmt.Errorf("error reading caplet %s: %v", filename, err), nil
|
return fmt.Errorf("error reading caplet %s: %v", fileName, err), nil
|
||||||
} else {
|
} else {
|
||||||
for line := range reader {
|
for line := range reader {
|
||||||
if line == "" || line[0] == '#' {
|
if line == "" || line[0] == '#' {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package session
|
package session
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
@ -77,6 +78,23 @@ type Session struct {
|
||||||
Firewall firewall.FirewallManager `json:"-"`
|
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) {
|
func New() (*Session, error) {
|
||||||
opts, err := core.ParseOptions()
|
opts, err := core.ParseOptions()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -124,6 +142,26 @@ func New() (*Session, error) {
|
||||||
return s, nil
|
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() {
|
func (s *Session) Lock() {
|
||||||
s.Env.Lock()
|
s.Env.Lock()
|
||||||
s.Lan.Lock()
|
s.Lan.Lock()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue