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
parent d8ec87036e
commit d0d3ce3ec7
2 changed files with 57 additions and 26 deletions

View file

@ -1,6 +1,7 @@
package network
import (
"encoding/json"
"fmt"
"net"
"strings"
@ -16,7 +17,7 @@ type EndpointLostCallback func(e *Endpoint)
type LAN struct {
sync.Mutex
Hosts map[string]*Endpoint `json:"hosts"`
hosts map[string]*Endpoint
iface *Endpoint
gateway *Endpoint
ttl map[string]uint
@ -26,6 +27,10 @@ type LAN struct {
aliasesFileName string
}
type lanJSON struct {
Hosts []*Endpoint `json:"hosts"`
}
func NewLAN(iface, gateway *Endpoint, newcb EndpointNewCallback, lostcb EndpointLostCallback) *LAN {
err, aliases := LoadAliases()
if err != nil {
@ -35,7 +40,7 @@ func NewLAN(iface, gateway *Endpoint, newcb EndpointNewCallback, lostcb Endpoint
return &LAN{
iface: iface,
gateway: gateway,
Hosts: make(map[string]*Endpoint),
hosts: make(map[string]*Endpoint),
ttl: make(map[string]uint),
aliases: aliases,
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 {
lan.Lock()
defer lan.Unlock()
mac = NormalizeMac(mac)
if e, found := lan.Hosts[mac]; found {
if e, found := lan.hosts[mac]; found {
lan.aliases.Set(mac, alias)
e.Alias = alias
return true
@ -60,7 +77,7 @@ func (lan *LAN) Get(mac string) (*Endpoint, bool) {
lan.Lock()
defer lan.Unlock()
if e, found := lan.Hosts[mac]; found == true {
if e, found := lan.hosts[mac]; found == true {
return e, true
}
return nil, false
@ -71,7 +88,7 @@ func (lan *LAN) List() (list []*Endpoint) {
defer lan.Unlock()
list = make([]*Endpoint, 0)
for _, t := range lan.Hosts {
for _, t := range lan.hosts {
list = append(list, t)
}
return
@ -95,10 +112,10 @@ func (lan *LAN) Remove(ip, mac string) {
lan.Lock()
defer lan.Unlock()
if e, found := lan.Hosts[mac]; found {
if e, found := lan.hosts[mac]; found {
lan.ttl[mac]--
if lan.ttl[mac] == 0 {
delete(lan.Hosts, mac)
delete(lan.hosts, mac)
delete(lan.ttl, mac)
lan.lostCb(e)
}
@ -132,7 +149,7 @@ func (lan *LAN) Has(ip string) bool {
lan.Lock()
defer lan.Unlock()
for _, e := range lan.Hosts {
for _, e := range lan.hosts {
if e.IpAddress == ip {
return true
}
@ -145,7 +162,7 @@ func (lan *LAN) EachHost(cb func(mac string, e *Endpoint)) {
lan.Lock()
defer lan.Unlock()
for m, h := range lan.Hosts {
for m, h := range lan.hosts {
cb(m, h)
}
}
@ -154,7 +171,7 @@ func (lan *LAN) GetByIp(ip string) *Endpoint {
lan.Lock()
defer lan.Unlock()
for _, e := range lan.Hosts {
for _, e := range lan.hosts {
if e.IpAddress == ip {
return e
}
@ -171,7 +188,7 @@ func (lan *LAN) AddIfNew(ip, mac string) *Endpoint {
if lan.shouldIgnore(ip, mac) {
return nil
} else if t, found := lan.Hosts[mac]; found {
} else if t, found := lan.hosts[mac]; found {
if lan.ttl[mac] < LANDefaultttl {
lan.ttl[mac]++
}
@ -180,7 +197,7 @@ func (lan *LAN) AddIfNew(ip, mac string) *Endpoint {
e := NewEndpointWithAlias(ip, mac, lan.aliases.Get(mac))
lan.Hosts[mac] = e
lan.hosts[mac] = e
lan.ttl[mac] = LANDefaultttl
lan.newCb(e)

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
}