internal/clientinfo: check hostname mapping for both ipv4/ipv6

This commit is contained in:
Cuong Manh Le
2024-04-19 14:32:21 +07:00
committed by Cuong Manh Le
parent dd9f2465be
commit da01a146d2
2 changed files with 8 additions and 7 deletions
+2 -3
View File
@@ -794,7 +794,6 @@ func (p *prog) getClientInfo(remoteIP string, msg *dns.Msg) *ctrld.ClientInfo {
ci.Mac = p.ciTable.LookupMac(ci.IP) ci.Mac = p.ciTable.LookupMac(ci.IP)
} }
isV6 := ctrldnet.IsIPv6(ci.IP)
// If MAC is still empty here, that mean the requests are made from virtual interface, // If MAC is still empty here, that mean the requests are made from virtual interface,
// like VPN/Wireguard clients, so we use ci.IP as hostname to distinguish those clients. // like VPN/Wireguard clients, so we use ci.IP as hostname to distinguish those clients.
if ci.Mac == "" { if ci.Mac == "" {
@@ -808,7 +807,7 @@ func (p *prog) getClientInfo(remoteIP string, msg *dns.Msg) *ctrld.ClientInfo {
// IDs created for the same device, which is pointless. // IDs created for the same device, which is pointless.
// //
// TODO(cuonglm): investigate whether this can be a false positive for other clients? // TODO(cuonglm): investigate whether this can be a false positive for other clients?
if !isV6 { if !ctrldnet.IsIPv6(ci.IP) {
ci.Hostname = ci.IP ci.Hostname = ci.IP
p.ciTable.StoreVPNClient(ci) p.ciTable.StoreVPNClient(ci)
} }
@@ -820,7 +819,7 @@ func (p *prog) getClientInfo(remoteIP string, msg *dns.Msg) *ctrld.ClientInfo {
// If this is a query from self, but ci.IP is not loopback IP, // If this is a query from self, but ci.IP is not loopback IP,
// try using hostname mapping for lookback IP if presents. // try using hostname mapping for lookback IP if presents.
if ci.Self { if ci.Self {
if name := p.ciTable.LocalHostname(isV6); name != "" { if name := p.ciTable.LocalHostname(); name != "" {
ci.Hostname = name ci.Hostname = name
} }
} }
+6 -4
View File
@@ -328,11 +328,13 @@ func (t *Table) LookupRFC1918IPv4(mac string) string {
} }
// LocalHostname returns the localhost hostname associated with loopback IP. // LocalHostname returns the localhost hostname associated with loopback IP.
func (t *Table) LocalHostname(v6 bool) string { func (t *Table) LocalHostname() string {
if v6 { for _, ip := range []string{ipV4Loopback, ipv6Loopback} {
return t.LookupHostname(ipv6Loopback, "") if name := t.LookupHostname(ip, ""); name != "" {
return name
}
} }
return t.LookupHostname(ipV4Loopback, "") return ""
} }
type macEntry struct { type macEntry struct {