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.
This commit is contained in:
Cuong Manh Le
2023-10-03 16:51:09 +00:00
committed by Cuong Manh Le
parent 8ddbf881b3
commit 6e28517454
3 changed files with 18 additions and 15 deletions

View File

@@ -626,6 +626,9 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
map2Slice := func(m map[string]struct{}) []string { map2Slice := func(m map[string]struct{}) []string {
s := make([]string, 0, len(m)) s := make([]string, 0, len(m))
for k := range m { for k := range m {
if k == "" { // skip empty source from output.
continue
}
s = append(s, k) s = append(s, k)
} }
sort.Strings(s) sort.Strings(s)

View File

@@ -74,7 +74,7 @@ type Table struct {
ptr *ptrDiscover ptr *ptrDiscover
mdns *mdns mdns *mdns
hf *hostsFile hf *hostsFile
vpn *vpn vni *virtualNetworkIface
cfg *ctrld.Config cfg *ctrld.Config
quitCh chan struct{} quitCh chan struct{}
selfIP string selfIP string
@@ -202,8 +202,8 @@ func (t *Table) init() {
} }
// VPN clients. // VPN clients.
if t.discoverDHCP() || t.discoverARP() { if t.discoverDHCP() || t.discoverARP() {
t.vpn = &vpn{} t.vni = &virtualNetworkIface{}
t.hostnameResolvers = append(t.hostnameResolvers, t.vpn) t.hostnameResolvers = append(t.hostnameResolvers, t.vni)
} }
} }
@@ -288,7 +288,7 @@ func (t *Table) ListClients() []*Client {
_ = r.refresh() _ = r.refresh()
} }
ipMap := make(map[string]*Client) 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 _, ir := range il {
for _, ip := range ir.List() { for _, ip := range ir.List() {
c, ok := ipMap[ip] c, ok := ipMap[ip]
@@ -331,11 +331,11 @@ func (t *Table) ListClients() []*Client {
// StoreVPNClient stores client info for VPN clients. // StoreVPNClient stores client info for VPN clients.
func (t *Table) StoreVPNClient(ci *ctrld.ClientInfo) { func (t *Table) StoreVPNClient(ci *ctrld.ClientInfo) {
if ci == nil || t.vpn == nil { if ci == nil || t.vni == nil {
return return
} }
t.vpn.mac.Store(ci.IP, ci.Mac) t.vni.mac.Store(ci.IP, ci.Mac)
t.vpn.ip2name.Store(ci.IP, ci.Hostname) t.vni.ip2name.Store(ci.IP, ci.Hostname)
} }
func (t *Table) discoverDHCP() bool { func (t *Table) discoverDHCP() bool {

View File

@@ -4,14 +4,14 @@ import (
"sync" "sync"
) )
// vpn is the manager for VPN clients info. // virtualNetworkIface is the manager for clients from virtual network interface.
type vpn struct { type virtualNetworkIface struct {
ip2name sync.Map // ip => name ip2name sync.Map // ip => name
mac sync.Map // ip => mac mac sync.Map // ip => mac
} }
// LookupHostnameByIP returns hostname of the given VPN client ip. // 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) val, ok := v.ip2name.Load(ip)
if !ok { if !ok {
return "" return ""
@@ -20,17 +20,17 @@ func (v *vpn) LookupHostnameByIP(ip string) string {
} }
// LookupHostnameByMac always returns empty string. // LookupHostnameByMac always returns empty string.
func (v *vpn) LookupHostnameByMac(mac string) string { func (v *virtualNetworkIface) LookupHostnameByMac(mac string) string {
return "" return ""
} }
// String returns the string representation of vpn struct. // String returns the string representation of virtualNetworkIface struct.
func (v *vpn) String() string { func (v *virtualNetworkIface) String() string {
return "vpn" return ""
} }
// List lists all known VPN clients IP. // List lists all known VPN clients IP.
func (v *vpn) List() []string { func (v *virtualNetworkIface) List() []string {
if v == nil { if v == nil {
return nil return nil
} }