From 6e28517454a2ee6d640706cb10262900ed09dfd0 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Tue, 3 Oct 2023 16:51:09 +0000 Subject: [PATCH] all: generalize vpn client info VPN clients often have empty MAC address, because they come from virtual network interface. However, there's other setup/devices also create virtual interface, but is not VPN. Changing source of those clients to empty to prevent confustion in clients list command output. --- cmd/cli/cli.go | 3 +++ internal/clientinfo/client_info.go | 14 +++++++------- internal/clientinfo/{vpn.go => virtual_iface.go} | 16 ++++++++-------- 3 files changed, 18 insertions(+), 15 deletions(-) rename internal/clientinfo/{vpn.go => virtual_iface.go} (54%) diff --git a/cmd/cli/cli.go b/cmd/cli/cli.go index f548adc..d1db8fb 100644 --- a/cmd/cli/cli.go +++ b/cmd/cli/cli.go @@ -626,6 +626,9 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`, map2Slice := func(m map[string]struct{}) []string { s := make([]string, 0, len(m)) for k := range m { + if k == "" { // skip empty source from output. + continue + } s = append(s, k) } sort.Strings(s) diff --git a/internal/clientinfo/client_info.go b/internal/clientinfo/client_info.go index ea7ed05..3e92fd1 100644 --- a/internal/clientinfo/client_info.go +++ b/internal/clientinfo/client_info.go @@ -74,7 +74,7 @@ type Table struct { ptr *ptrDiscover mdns *mdns hf *hostsFile - vpn *vpn + vni *virtualNetworkIface cfg *ctrld.Config quitCh chan struct{} selfIP string @@ -202,8 +202,8 @@ func (t *Table) init() { } // VPN clients. if t.discoverDHCP() || t.discoverARP() { - t.vpn = &vpn{} - t.hostnameResolvers = append(t.hostnameResolvers, t.vpn) + t.vni = &virtualNetworkIface{} + t.hostnameResolvers = append(t.hostnameResolvers, t.vni) } } @@ -288,7 +288,7 @@ func (t *Table) ListClients() []*Client { _ = r.refresh() } ipMap := make(map[string]*Client) - il := []ipLister{t.dhcp, t.arp, t.ptr, t.mdns, t.vpn} + il := []ipLister{t.dhcp, t.arp, t.ptr, t.mdns, t.vni} for _, ir := range il { for _, ip := range ir.List() { c, ok := ipMap[ip] @@ -331,11 +331,11 @@ func (t *Table) ListClients() []*Client { // StoreVPNClient stores client info for VPN clients. func (t *Table) StoreVPNClient(ci *ctrld.ClientInfo) { - if ci == nil || t.vpn == nil { + if ci == nil || t.vni == nil { return } - t.vpn.mac.Store(ci.IP, ci.Mac) - t.vpn.ip2name.Store(ci.IP, ci.Hostname) + t.vni.mac.Store(ci.IP, ci.Mac) + t.vni.ip2name.Store(ci.IP, ci.Hostname) } func (t *Table) discoverDHCP() bool { diff --git a/internal/clientinfo/vpn.go b/internal/clientinfo/virtual_iface.go similarity index 54% rename from internal/clientinfo/vpn.go rename to internal/clientinfo/virtual_iface.go index fe62bcb..6cb018d 100644 --- a/internal/clientinfo/vpn.go +++ b/internal/clientinfo/virtual_iface.go @@ -4,14 +4,14 @@ import ( "sync" ) -// vpn is the manager for VPN clients info. -type vpn struct { +// virtualNetworkIface is the manager for clients from virtual network interface. +type virtualNetworkIface struct { ip2name sync.Map // ip => name mac sync.Map // ip => mac } // LookupHostnameByIP returns hostname of the given VPN client ip. -func (v *vpn) LookupHostnameByIP(ip string) string { +func (v *virtualNetworkIface) LookupHostnameByIP(ip string) string { val, ok := v.ip2name.Load(ip) if !ok { return "" @@ -20,17 +20,17 @@ func (v *vpn) LookupHostnameByIP(ip string) string { } // LookupHostnameByMac always returns empty string. -func (v *vpn) LookupHostnameByMac(mac string) string { +func (v *virtualNetworkIface) LookupHostnameByMac(mac string) string { return "" } -// String returns the string representation of vpn struct. -func (v *vpn) String() string { - return "vpn" +// String returns the string representation of virtualNetworkIface struct. +func (v *virtualNetworkIface) String() string { + return "" } // List lists all known VPN clients IP. -func (v *vpn) List() []string { +func (v *virtualNetworkIface) List() []string { if v == nil { return nil }