mirror of
https://github.com/bettercap/bettercap
synced 2025-07-16 10:03:39 -07:00
fix: using custom json marshalling in order to export in json unexported fields
This commit is contained in:
parent
d8ec87036e
commit
d0d3ce3ec7
2 changed files with 57 additions and 26 deletions
|
@ -1,6 +1,7 @@
|
||||||
package network
|
package network
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -16,7 +17,7 @@ type EndpointLostCallback func(e *Endpoint)
|
||||||
type LAN struct {
|
type LAN struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
|
|
||||||
Hosts map[string]*Endpoint `json:"hosts"`
|
hosts map[string]*Endpoint
|
||||||
iface *Endpoint
|
iface *Endpoint
|
||||||
gateway *Endpoint
|
gateway *Endpoint
|
||||||
ttl map[string]uint
|
ttl map[string]uint
|
||||||
|
@ -26,6 +27,10 @@ type LAN struct {
|
||||||
aliasesFileName string
|
aliasesFileName string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type lanJSON struct {
|
||||||
|
Hosts []*Endpoint `json:"hosts"`
|
||||||
|
}
|
||||||
|
|
||||||
func NewLAN(iface, gateway *Endpoint, newcb EndpointNewCallback, lostcb EndpointLostCallback) *LAN {
|
func NewLAN(iface, gateway *Endpoint, newcb EndpointNewCallback, lostcb EndpointLostCallback) *LAN {
|
||||||
err, aliases := LoadAliases()
|
err, aliases := LoadAliases()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -35,7 +40,7 @@ func NewLAN(iface, gateway *Endpoint, newcb EndpointNewCallback, lostcb Endpoint
|
||||||
return &LAN{
|
return &LAN{
|
||||||
iface: iface,
|
iface: iface,
|
||||||
gateway: gateway,
|
gateway: gateway,
|
||||||
Hosts: make(map[string]*Endpoint),
|
hosts: make(map[string]*Endpoint),
|
||||||
ttl: make(map[string]uint),
|
ttl: make(map[string]uint),
|
||||||
aliases: aliases,
|
aliases: aliases,
|
||||||
newCb: newcb,
|
newCb: newcb,
|
||||||
|
@ -43,12 +48,24 @@ func NewLAN(iface, gateway *Endpoint, newcb EndpointNewCallback, lostcb Endpoint
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *LAN) MarshalJSON() ([]byte, error) {
|
||||||
|
doc := lanJSON{
|
||||||
|
Hosts: make([]*Endpoint, 0),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, h := range l.hosts {
|
||||||
|
doc.Hosts = append(doc.Hosts, h)
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Marshal(doc)
|
||||||
|
}
|
||||||
|
|
||||||
func (lan *LAN) SetAliasFor(mac, alias string) bool {
|
func (lan *LAN) SetAliasFor(mac, alias string) bool {
|
||||||
lan.Lock()
|
lan.Lock()
|
||||||
defer lan.Unlock()
|
defer lan.Unlock()
|
||||||
|
|
||||||
mac = NormalizeMac(mac)
|
mac = NormalizeMac(mac)
|
||||||
if e, found := lan.Hosts[mac]; found {
|
if e, found := lan.hosts[mac]; found {
|
||||||
lan.aliases.Set(mac, alias)
|
lan.aliases.Set(mac, alias)
|
||||||
e.Alias = alias
|
e.Alias = alias
|
||||||
return true
|
return true
|
||||||
|
@ -60,7 +77,7 @@ func (lan *LAN) Get(mac string) (*Endpoint, bool) {
|
||||||
lan.Lock()
|
lan.Lock()
|
||||||
defer lan.Unlock()
|
defer lan.Unlock()
|
||||||
|
|
||||||
if e, found := lan.Hosts[mac]; found == true {
|
if e, found := lan.hosts[mac]; found == true {
|
||||||
return e, true
|
return e, true
|
||||||
}
|
}
|
||||||
return nil, false
|
return nil, false
|
||||||
|
@ -71,7 +88,7 @@ func (lan *LAN) List() (list []*Endpoint) {
|
||||||
defer lan.Unlock()
|
defer lan.Unlock()
|
||||||
|
|
||||||
list = make([]*Endpoint, 0)
|
list = make([]*Endpoint, 0)
|
||||||
for _, t := range lan.Hosts {
|
for _, t := range lan.hosts {
|
||||||
list = append(list, t)
|
list = append(list, t)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -95,10 +112,10 @@ func (lan *LAN) Remove(ip, mac string) {
|
||||||
lan.Lock()
|
lan.Lock()
|
||||||
defer lan.Unlock()
|
defer lan.Unlock()
|
||||||
|
|
||||||
if e, found := lan.Hosts[mac]; found {
|
if e, found := lan.hosts[mac]; found {
|
||||||
lan.ttl[mac]--
|
lan.ttl[mac]--
|
||||||
if lan.ttl[mac] == 0 {
|
if lan.ttl[mac] == 0 {
|
||||||
delete(lan.Hosts, mac)
|
delete(lan.hosts, mac)
|
||||||
delete(lan.ttl, mac)
|
delete(lan.ttl, mac)
|
||||||
lan.lostCb(e)
|
lan.lostCb(e)
|
||||||
}
|
}
|
||||||
|
@ -132,7 +149,7 @@ func (lan *LAN) Has(ip string) bool {
|
||||||
lan.Lock()
|
lan.Lock()
|
||||||
defer lan.Unlock()
|
defer lan.Unlock()
|
||||||
|
|
||||||
for _, e := range lan.Hosts {
|
for _, e := range lan.hosts {
|
||||||
if e.IpAddress == ip {
|
if e.IpAddress == ip {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -145,7 +162,7 @@ func (lan *LAN) EachHost(cb func(mac string, e *Endpoint)) {
|
||||||
lan.Lock()
|
lan.Lock()
|
||||||
defer lan.Unlock()
|
defer lan.Unlock()
|
||||||
|
|
||||||
for m, h := range lan.Hosts {
|
for m, h := range lan.hosts {
|
||||||
cb(m, h)
|
cb(m, h)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -154,7 +171,7 @@ func (lan *LAN) GetByIp(ip string) *Endpoint {
|
||||||
lan.Lock()
|
lan.Lock()
|
||||||
defer lan.Unlock()
|
defer lan.Unlock()
|
||||||
|
|
||||||
for _, e := range lan.Hosts {
|
for _, e := range lan.hosts {
|
||||||
if e.IpAddress == ip {
|
if e.IpAddress == ip {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
@ -171,7 +188,7 @@ func (lan *LAN) AddIfNew(ip, mac string) *Endpoint {
|
||||||
|
|
||||||
if lan.shouldIgnore(ip, mac) {
|
if lan.shouldIgnore(ip, mac) {
|
||||||
return nil
|
return nil
|
||||||
} else if t, found := lan.Hosts[mac]; found {
|
} else if t, found := lan.hosts[mac]; found {
|
||||||
if lan.ttl[mac] < LANDefaultttl {
|
if lan.ttl[mac] < LANDefaultttl {
|
||||||
lan.ttl[mac]++
|
lan.ttl[mac]++
|
||||||
}
|
}
|
||||||
|
@ -180,7 +197,7 @@ func (lan *LAN) AddIfNew(ip, mac string) *Endpoint {
|
||||||
|
|
||||||
e := NewEndpointWithAlias(ip, mac, lan.aliases.Get(mac))
|
e := NewEndpointWithAlias(ip, mac, lan.aliases.Get(mac))
|
||||||
|
|
||||||
lan.Hosts[mac] = e
|
lan.hosts[mac] = e
|
||||||
lan.ttl[mac] = LANDefaultttl
|
lan.ttl[mac] = LANDefaultttl
|
||||||
|
|
||||||
lan.newCb(e)
|
lan.newCb(e)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package network
|
package network
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -8,33 +9,46 @@ import (
|
||||||
type StationNewCallback func(s *Station)
|
type StationNewCallback func(s *Station)
|
||||||
type StationLostCallback func(s *Station)
|
type StationLostCallback func(s *Station)
|
||||||
|
|
||||||
var Channels5Ghz = [...]int{36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 132, 134, 136, 138, 140, 142, 144, 149, 151, 153, 155, 157, 159, 161, 165, 169, 173}
|
|
||||||
|
|
||||||
type WiFi struct {
|
type WiFi struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
|
|
||||||
Stations map[string]*Station
|
stations map[string]*Station
|
||||||
|
iface *Endpoint
|
||||||
|
newCb StationNewCallback
|
||||||
|
lostCb StationLostCallback
|
||||||
|
}
|
||||||
|
|
||||||
iface *Endpoint
|
type wifiJSON struct {
|
||||||
newCb StationNewCallback
|
Stations []*Station `json:"stations"`
|
||||||
lostCb StationLostCallback
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWiFi(iface *Endpoint, newcb StationNewCallback, lostcb StationLostCallback) *WiFi {
|
func NewWiFi(iface *Endpoint, newcb StationNewCallback, lostcb StationLostCallback) *WiFi {
|
||||||
return &WiFi{
|
return &WiFi{
|
||||||
Stations: make(map[string]*Station),
|
stations: make(map[string]*Station),
|
||||||
iface: iface,
|
iface: iface,
|
||||||
newCb: newcb,
|
newCb: newcb,
|
||||||
lostCb: lostcb,
|
lostCb: lostcb,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *WiFi) MarshalJSON() ([]byte, error) {
|
||||||
|
doc := wifiJSON{
|
||||||
|
Stations: make([]*Station, 0),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range w.stations {
|
||||||
|
doc.Stations = append(doc.Stations, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Marshal(doc)
|
||||||
|
}
|
||||||
|
|
||||||
func (w *WiFi) List() (list []*Station) {
|
func (w *WiFi) List() (list []*Station) {
|
||||||
w.Lock()
|
w.Lock()
|
||||||
defer w.Unlock()
|
defer w.Unlock()
|
||||||
|
|
||||||
list = make([]*Station, 0)
|
list = make([]*Station, 0)
|
||||||
for _, t := range w.Stations {
|
for _, t := range w.stations {
|
||||||
list = append(list, t)
|
list = append(list, t)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -44,8 +58,8 @@ func (w *WiFi) Remove(mac string) {
|
||||||
w.Lock()
|
w.Lock()
|
||||||
defer w.Unlock()
|
defer w.Unlock()
|
||||||
|
|
||||||
if s, found := w.Stations[mac]; found {
|
if s, found := w.stations[mac]; found {
|
||||||
delete(w.Stations, mac)
|
delete(w.stations, mac)
|
||||||
if w.lostCb != nil {
|
if w.lostCb != nil {
|
||||||
w.lostCb(s)
|
w.lostCb(s)
|
||||||
}
|
}
|
||||||
|
@ -57,14 +71,14 @@ func (w *WiFi) AddIfNew(ssid, mac string, isAp bool, frequency int, rssi int8) *
|
||||||
defer w.Unlock()
|
defer w.Unlock()
|
||||||
|
|
||||||
mac = NormalizeMac(mac)
|
mac = NormalizeMac(mac)
|
||||||
if station, found := w.Stations[mac]; found {
|
if station, found := w.stations[mac]; found {
|
||||||
station.LastSeen = time.Now()
|
station.LastSeen = time.Now()
|
||||||
station.RSSI = rssi
|
station.RSSI = rssi
|
||||||
return station
|
return station
|
||||||
}
|
}
|
||||||
|
|
||||||
newStation := NewStation(ssid, mac, isAp, frequency, rssi)
|
newStation := NewStation(ssid, mac, isAp, frequency, rssi)
|
||||||
w.Stations[mac] = newStation
|
w.stations[mac] = newStation
|
||||||
|
|
||||||
if w.newCb != nil {
|
if w.newCb != nil {
|
||||||
w.newCb(newStation)
|
w.newCb(newStation)
|
||||||
|
@ -78,11 +92,11 @@ func (w *WiFi) Get(mac string) (*Station, bool) {
|
||||||
defer w.Unlock()
|
defer w.Unlock()
|
||||||
|
|
||||||
mac = NormalizeMac(mac)
|
mac = NormalizeMac(mac)
|
||||||
station, found := w.Stations[mac]
|
station, found := w.stations[mac]
|
||||||
return station, found
|
return station, found
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WiFi) Clear() error {
|
func (w *WiFi) Clear() error {
|
||||||
w.Stations = make(map[string]*Station)
|
w.stations = make(map[string]*Station)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue