mirror of
https://github.com/bettercap/bettercap
synced 2025-08-14 10:46:57 -07:00
refact: big refactoring and improvements of wifi.* modules
This commit is contained in:
parent
d0d3ce3ec7
commit
da5d1d46d4
6 changed files with 266 additions and 123 deletions
|
@ -6,50 +6,70 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type StationNewCallback func(s *Station)
|
||||
type StationLostCallback func(s *Station)
|
||||
type APNewCallback func(ap *AccessPoint)
|
||||
type APLostCallback func(ap *AccessPoint)
|
||||
|
||||
type WiFi struct {
|
||||
sync.Mutex
|
||||
|
||||
stations map[string]*Station
|
||||
iface *Endpoint
|
||||
newCb StationNewCallback
|
||||
lostCb StationLostCallback
|
||||
aps map[string]*AccessPoint
|
||||
iface *Endpoint
|
||||
newCb APNewCallback
|
||||
lostCb APLostCallback
|
||||
}
|
||||
|
||||
type wifiJSON struct {
|
||||
Stations []*Station `json:"stations"`
|
||||
AccessPoints []*AccessPoint `json:"aps"`
|
||||
}
|
||||
|
||||
func NewWiFi(iface *Endpoint, newcb StationNewCallback, lostcb StationLostCallback) *WiFi {
|
||||
func NewWiFi(iface *Endpoint, newcb APNewCallback, lostcb APLostCallback) *WiFi {
|
||||
return &WiFi{
|
||||
stations: make(map[string]*Station),
|
||||
iface: iface,
|
||||
newCb: newcb,
|
||||
lostCb: lostcb,
|
||||
aps: make(map[string]*AccessPoint),
|
||||
iface: iface,
|
||||
newCb: newcb,
|
||||
lostCb: lostcb,
|
||||
}
|
||||
}
|
||||
|
||||
func (w *WiFi) MarshalJSON() ([]byte, error) {
|
||||
doc := wifiJSON{
|
||||
Stations: make([]*Station, 0),
|
||||
AccessPoints: make([]*AccessPoint, 0),
|
||||
}
|
||||
|
||||
for _, s := range w.stations {
|
||||
doc.Stations = append(doc.Stations, s)
|
||||
for _, ap := range w.aps {
|
||||
doc.AccessPoints = append(doc.AccessPoints, ap)
|
||||
}
|
||||
|
||||
return json.Marshal(doc)
|
||||
}
|
||||
|
||||
func (w *WiFi) List() (list []*Station) {
|
||||
func (w *WiFi) EachAccessPoint(cb func(mac string, ap *AccessPoint)) {
|
||||
w.Lock()
|
||||
defer w.Unlock()
|
||||
|
||||
for m, ap := range w.aps {
|
||||
cb(m, ap)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *WiFi) Stations() (list []*Station) {
|
||||
w.Lock()
|
||||
defer w.Unlock()
|
||||
|
||||
list = make([]*Station, 0)
|
||||
for _, t := range w.stations {
|
||||
list = append(list, t)
|
||||
for _, ap := range w.aps {
|
||||
list = append(list, ap.Station)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (w *WiFi) List() (list []*AccessPoint) {
|
||||
w.Lock()
|
||||
defer w.Unlock()
|
||||
|
||||
list = make([]*AccessPoint, 0)
|
||||
for _, ap := range w.aps {
|
||||
list = append(list, ap)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -58,45 +78,45 @@ func (w *WiFi) Remove(mac string) {
|
|||
w.Lock()
|
||||
defer w.Unlock()
|
||||
|
||||
if s, found := w.stations[mac]; found {
|
||||
delete(w.stations, mac)
|
||||
if ap, found := w.aps[mac]; found {
|
||||
delete(w.aps, mac)
|
||||
if w.lostCb != nil {
|
||||
w.lostCb(s)
|
||||
w.lostCb(ap)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *WiFi) AddIfNew(ssid, mac string, isAp bool, frequency int, rssi int8) *Station {
|
||||
func (w *WiFi) AddIfNew(ssid, mac string, frequency int, rssi int8) *AccessPoint {
|
||||
w.Lock()
|
||||
defer w.Unlock()
|
||||
|
||||
mac = NormalizeMac(mac)
|
||||
if station, found := w.stations[mac]; found {
|
||||
station.LastSeen = time.Now()
|
||||
station.RSSI = rssi
|
||||
return station
|
||||
if ap, found := w.aps[mac]; found {
|
||||
ap.LastSeen = time.Now()
|
||||
ap.RSSI = rssi
|
||||
return ap
|
||||
}
|
||||
|
||||
newStation := NewStation(ssid, mac, isAp, frequency, rssi)
|
||||
w.stations[mac] = newStation
|
||||
newAp := NewAccessPoint(ssid, mac, frequency, rssi)
|
||||
w.aps[mac] = newAp
|
||||
|
||||
if w.newCb != nil {
|
||||
w.newCb(newStation)
|
||||
w.newCb(newAp)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *WiFi) Get(mac string) (*Station, bool) {
|
||||
func (w *WiFi) Get(mac string) (*AccessPoint, bool) {
|
||||
w.Lock()
|
||||
defer w.Unlock()
|
||||
|
||||
mac = NormalizeMac(mac)
|
||||
station, found := w.stations[mac]
|
||||
return station, found
|
||||
ap, found := w.aps[mac]
|
||||
return ap, found
|
||||
}
|
||||
|
||||
func (w *WiFi) Clear() error {
|
||||
w.stations = make(map[string]*Station)
|
||||
w.aps = make(map[string]*AccessPoint)
|
||||
return nil
|
||||
}
|
||||
|
|
88
network/wifi_ap.go
Normal file
88
network/wifi_ap.go
Normal file
|
@ -0,0 +1,88 @@
|
|||
package network
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type AccessPoint struct {
|
||||
*Station
|
||||
sync.Mutex
|
||||
|
||||
clients map[string]*Station
|
||||
}
|
||||
|
||||
type apJSON struct {
|
||||
*Station
|
||||
Clients []*Station `json:"clients"`
|
||||
}
|
||||
|
||||
func NewAccessPoint(essid, bssid string, frequency int, rssi int8) *AccessPoint {
|
||||
return &AccessPoint{
|
||||
Station: NewStation(essid, bssid, frequency, rssi),
|
||||
clients: make(map[string]*Station),
|
||||
}
|
||||
}
|
||||
|
||||
func (ap *AccessPoint) MarshalJSON() ([]byte, error) {
|
||||
doc := apJSON{
|
||||
Station: ap.Station,
|
||||
Clients: make([]*Station, 0),
|
||||
}
|
||||
|
||||
for _, c := range ap.clients {
|
||||
doc.Clients = append(doc.Clients, c)
|
||||
}
|
||||
|
||||
return json.Marshal(doc)
|
||||
}
|
||||
|
||||
func (ap *AccessPoint) Get(bssid string) (*Station, bool) {
|
||||
ap.Lock()
|
||||
defer ap.Unlock()
|
||||
|
||||
bssid = NormalizeMac(bssid)
|
||||
if s, found := ap.clients[bssid]; found == true {
|
||||
return s, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (ap *AccessPoint) AddClient(bssid string, frequency int, rssi int8) *Station {
|
||||
ap.Lock()
|
||||
defer ap.Unlock()
|
||||
|
||||
bssid = NormalizeMac(bssid)
|
||||
|
||||
if s, found := ap.clients[bssid]; found == true {
|
||||
// update
|
||||
s.Frequency = frequency
|
||||
s.RSSI = rssi
|
||||
s.LastSeen = time.Now()
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
s := NewStation("", bssid, frequency, rssi)
|
||||
ap.clients[bssid] = s
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (ap *AccessPoint) NumClients() int {
|
||||
ap.Lock()
|
||||
defer ap.Unlock()
|
||||
return len(ap.clients)
|
||||
}
|
||||
|
||||
func (ap *AccessPoint) Clients() (list []*Station) {
|
||||
ap.Lock()
|
||||
defer ap.Unlock()
|
||||
|
||||
list = make([]*Station, 0)
|
||||
for _, c := range ap.clients {
|
||||
list = append(list, c)
|
||||
}
|
||||
return
|
||||
}
|
|
@ -2,18 +2,16 @@ package network
|
|||
|
||||
type Station struct {
|
||||
*Endpoint
|
||||
IsAP bool
|
||||
Frequency int
|
||||
RSSI int8
|
||||
Sent uint64
|
||||
Received uint64
|
||||
Encryption string
|
||||
Frequency int `json:"frequency"`
|
||||
RSSI int8 `json:"rssi"`
|
||||
Sent uint64 `json:"sent"`
|
||||
Received uint64 `json:"received"`
|
||||
Encryption string `json:"encryption"`
|
||||
}
|
||||
|
||||
func NewStation(essid, bssid string, isAp bool, frequency int, rssi int8) *Station {
|
||||
func NewStation(essid, bssid string, frequency int, rssi int8) *Station {
|
||||
return &Station{
|
||||
Endpoint: NewEndpointNoResolve(MonitorModeAddress, bssid, essid, 0),
|
||||
IsAP: isAp,
|
||||
Frequency: frequency,
|
||||
RSSI: rssi,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue