new: added new Meta field to Endpoint objects (closes #66)

This commit is contained in:
evilsocket 2018-02-22 18:41:04 +01:00
commit 02d414107e
3 changed files with 48 additions and 3 deletions

View file

@ -41,7 +41,7 @@ func (s EventsStream) viewApEvent(e session.Event) {
fmt.Printf("[%s] [%s] %s\n",
e.Time.Format(eventTimeFormat),
core.Green(e.Tag),
ap)
ap.String())
}
}
@ -75,7 +75,7 @@ func (s EventsStream) viewEndpointEvent(e session.Event) {
fmt.Printf("[%s] [%s] %s\n",
e.Time.Format(eventTimeFormat),
core.Green(e.Tag),
t)
t.String())
}
}

View file

@ -28,6 +28,7 @@ type Endpoint struct {
ResolvedCallback OnHostResolvedCallback `json:"-"`
FirstSeen time.Time `json:"first_seen"`
LastSeen time.Time `json:"last_seen"`
Meta *Meta `json:"meta"`
}
func ip2int(ip net.IP) uint32 {
@ -56,6 +57,7 @@ func NewEndpointNoResolve(ip, mac, name string, bits uint32) *Endpoint {
ResolvedCallback: nil,
FirstSeen: now,
LastSeen: now,
Meta: NewMeta(),
}
_, netw, _ := net.ParseCIDR(e.CIDR())
@ -66,7 +68,6 @@ func NewEndpointNoResolve(ip, mac, name string, bits uint32) *Endpoint {
func NewEndpoint(ip, mac string) *Endpoint {
e := NewEndpointNoResolve(ip, mac, "", 0)
// start resolver goroutine
go func() {
if names, err := net.LookupAddr(e.IpAddress); err == nil && len(names) > 0 {

44
network/meta.go Normal file
View file

@ -0,0 +1,44 @@
package network
import (
"encoding/json"
"sync"
)
type Meta struct {
sync.Mutex
m map[string]interface{}
}
// we want to protect concurrent access to the Meta
// object so the m field needs to be unexported, this
// is to have it in JSON regardless.
type metaJSON struct {
Values map[string]interface{} `json:"values"`
}
func NewMeta() *Meta {
return &Meta{
m: make(map[string]interface{}),
}
}
func (m *Meta) MarshalJSON() ([]byte, error) {
return json.Marshal(metaJSON{Values: m.m})
}
func (m *Meta) Set(name string, value interface{}) {
m.Lock()
defer m.Unlock()
m.m[name] = value
}
func (m *Meta) Get(name string) interface{} {
m.Lock()
defer m.Unlock()
if v, found := m.m[name]; found == true {
return v
}
return ""
}