new: aliases are now centralized and can be used for any type of device (closes #504)

This commit is contained in:
evilsocket 2019-03-21 19:39:08 +01:00
commit 7e7f2ef645
No known key found for this signature in database
GPG key ID: 1564D7F30393A456
11 changed files with 115 additions and 51 deletions

View file

@ -2,23 +2,18 @@ package network
import (
"encoding/json"
"fmt"
"net"
"strings"
"sync"
"github.com/evilsocket/islazy/data"
"github.com/evilsocket/islazy/fs"
)
const LANDefaultttl = 10
const LANAliasesFile = "~/bettercap.aliases"
type EndpointNewCallback func(e *Endpoint)
type EndpointLostCallback func(e *Endpoint)
var aliasesFileName, _ = fs.Expand(LANAliasesFile)
type LAN struct {
sync.Mutex
hosts map[string]*Endpoint
@ -34,12 +29,7 @@ type lanJSON struct {
Hosts []*Endpoint `json:"hosts"`
}
func NewLAN(iface, gateway *Endpoint, newcb EndpointNewCallback, lostcb EndpointLostCallback) *LAN {
aliases, err := data.NewUnsortedKV(aliasesFileName, data.FlushOnEdit)
if err != nil {
fmt.Printf("error loading %s: %s", aliasesFileName, err)
}
func NewLAN(iface, gateway *Endpoint, aliases *data.UnsortedKV, newcb EndpointNewCallback, lostcb EndpointLostCallback) *LAN {
return &LAN{
iface: iface,
gateway: gateway,
@ -63,19 +53,6 @@ func (l *LAN) MarshalJSON() ([]byte, error) {
return json.Marshal(doc)
}
func (lan *LAN) SetAliasFor(mac, alias string) bool {
lan.Lock()
defer lan.Unlock()
mac = NormalizeMac(mac)
lan.aliases.Set(mac, alias)
if e, found := lan.hosts[mac]; found {
e.Alias = alias
return true
}
return false
}
func (lan *LAN) Get(mac string) (*Endpoint, bool) {
lan.Lock()
defer lan.Unlock()