fix: using custom json marshalling in order to export in json unexported fields

This commit is contained in:
evilsocket 2018-02-20 15:58:45 +01:00
commit d0d3ce3ec7
2 changed files with 57 additions and 26 deletions

View file

@ -1,6 +1,7 @@
package network
import (
"encoding/json"
"sync"
"time"
)
@ -8,33 +9,46 @@ import (
type StationNewCallback 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 {
sync.Mutex
Stations map[string]*Station
stations map[string]*Station
iface *Endpoint
newCb StationNewCallback
lostCb StationLostCallback
}
iface *Endpoint
newCb StationNewCallback
lostCb StationLostCallback
type wifiJSON struct {
Stations []*Station `json:"stations"`
}
func NewWiFi(iface *Endpoint, newcb StationNewCallback, lostcb StationLostCallback) *WiFi {
return &WiFi{
Stations: make(map[string]*Station),
stations: make(map[string]*Station),
iface: iface,
newCb: newcb,
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) {
w.Lock()
defer w.Unlock()
list = make([]*Station, 0)
for _, t := range w.Stations {
for _, t := range w.stations {
list = append(list, t)
}
return
@ -44,8 +58,8 @@ func (w *WiFi) Remove(mac string) {
w.Lock()
defer w.Unlock()
if s, found := w.Stations[mac]; found {
delete(w.Stations, mac)
if s, found := w.stations[mac]; found {
delete(w.stations, mac)
if w.lostCb != nil {
w.lostCb(s)
}
@ -57,14 +71,14 @@ func (w *WiFi) AddIfNew(ssid, mac string, isAp bool, frequency int, rssi int8) *
defer w.Unlock()
mac = NormalizeMac(mac)
if station, found := w.Stations[mac]; found {
if station, found := w.stations[mac]; found {
station.LastSeen = time.Now()
station.RSSI = rssi
return station
}
newStation := NewStation(ssid, mac, isAp, frequency, rssi)
w.Stations[mac] = newStation
w.stations[mac] = newStation
if w.newCb != nil {
w.newCb(newStation)
@ -78,11 +92,11 @@ func (w *WiFi) Get(mac string) (*Station, bool) {
defer w.Unlock()
mac = NormalizeMac(mac)
station, found := w.Stations[mac]
station, found := w.stations[mac]
return station, found
}
func (w *WiFi) Clear() error {
w.Stations = make(map[string]*Station)
w.stations = make(map[string]*Station)
return nil
}