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)