mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 21:13:18 -07:00
new: exporting available network interfaces in /api/session for api.rest
This commit is contained in:
parent
4f54b12ee1
commit
72370dec58
2 changed files with 103 additions and 65 deletions
|
@ -1,7 +1,6 @@
|
||||||
package session
|
package session
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
@ -56,47 +55,26 @@ type GPS struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
Options core.Options `json:"options"`
|
Options core.Options
|
||||||
Interface *network.Endpoint `json:"interface"`
|
Interface *network.Endpoint
|
||||||
Gateway *network.Endpoint `json:"gateway"`
|
Gateway *network.Endpoint
|
||||||
Env *Environment `json:"env"`
|
Env *Environment
|
||||||
Lan *network.LAN `json:"lan"`
|
Lan *network.LAN
|
||||||
WiFi *network.WiFi `json:"wifi"`
|
WiFi *network.WiFi
|
||||||
BLE *network.BLE `json:"ble"`
|
BLE *network.BLE
|
||||||
HID *network.HID `json:"hid"`
|
HID *network.HID
|
||||||
Queue *packets.Queue `json:"packets"`
|
Queue *packets.Queue
|
||||||
StartedAt time.Time `json:"started_at"`
|
StartedAt time.Time
|
||||||
Active bool `json:"active"`
|
Active bool
|
||||||
GPS GPS `json:"gps"`
|
GPS GPS
|
||||||
Modules ModuleList `json:"modules"`
|
Modules ModuleList
|
||||||
|
|
||||||
Input *readline.Instance `json:"-"`
|
Input *readline.Instance
|
||||||
Prompt Prompt `json:"-"`
|
Prompt Prompt
|
||||||
CoreHandlers []CommandHandler `json:"-"`
|
CoreHandlers []CommandHandler
|
||||||
Events *EventPool `json:"-"`
|
Events *EventPool
|
||||||
UnkCmdCallback UnknownCommandCallback `json:"-"`
|
UnkCmdCallback UnknownCommandCallback
|
||||||
Firewall firewall.FirewallManager `json:"-"`
|
Firewall firewall.FirewallManager
|
||||||
}
|
|
||||||
|
|
||||||
type sessionJSON struct {
|
|
||||||
Version string `json:"version"`
|
|
||||||
OS string `json:"os"`
|
|
||||||
Arch string `json:"arch"`
|
|
||||||
GoVersion string `json:"goversion"`
|
|
||||||
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) {
|
||||||
|
@ -146,30 +124,6 @@ func New() (*Session, error) {
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) MarshalJSON() ([]byte, error) {
|
|
||||||
doc := sessionJSON{
|
|
||||||
Version: core.Version,
|
|
||||||
OS: runtime.GOOS,
|
|
||||||
Arch: runtime.GOARCH,
|
|
||||||
GoVersion: runtime.Version(),
|
|
||||||
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()
|
||||||
|
|
84
session/session_json.go
Normal file
84
session/session_json.go
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
package session
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net"
|
||||||
|
"runtime"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/bettercap/bettercap/caplets"
|
||||||
|
"github.com/bettercap/bettercap/core"
|
||||||
|
"github.com/bettercap/bettercap/network"
|
||||||
|
"github.com/bettercap/bettercap/packets"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ifaceJSON struct {
|
||||||
|
Index int `json:"index"`
|
||||||
|
MTU int `json:"mtu"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
MAC string `json:"mac"`
|
||||||
|
Flags net.Flags `json:"flags"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type sessionJSON struct {
|
||||||
|
Version string `json:"version"`
|
||||||
|
OS string `json:"os"`
|
||||||
|
Arch string `json:"arch"`
|
||||||
|
GoVersion string `json:"goversion"`
|
||||||
|
Interfaces []ifaceJSON `json:"interfaces"`
|
||||||
|
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 (s *Session) MarshalJSON() ([]byte, error) {
|
||||||
|
doc := sessionJSON{
|
||||||
|
Version: core.Version,
|
||||||
|
OS: runtime.GOOS,
|
||||||
|
Arch: runtime.GOARCH,
|
||||||
|
GoVersion: runtime.Version(),
|
||||||
|
Interfaces: make([]ifaceJSON, 0),
|
||||||
|
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(),
|
||||||
|
}
|
||||||
|
|
||||||
|
ifaces, err := net.Interfaces()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, iface := range ifaces {
|
||||||
|
doc.Interfaces = append(doc.Interfaces, ifaceJSON{
|
||||||
|
Index: iface.Index,
|
||||||
|
MTU: iface.MTU,
|
||||||
|
Name: iface.Name,
|
||||||
|
MAC: iface.HardwareAddr.String(),
|
||||||
|
Flags: iface.Flags,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Marshal(doc)
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue