mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-08-10 13:20:33 +02:00
all: spoof loopback ranges in client info
Sending them are useless, so using RFC1918 address instead.
This commit is contained in:
@@ -573,9 +573,23 @@ func (p *prog) getClientInfo(remoteIP string, msg *dns.Msg) *ctrld.ClientInfo {
|
|||||||
ci.Hostname = p.ciTable.LookupHostname(ci.IP, ci.Mac)
|
ci.Hostname = p.ciTable.LookupHostname(ci.IP, ci.Mac)
|
||||||
}
|
}
|
||||||
ci.Self = queryFromSelf(ci.IP)
|
ci.Self = queryFromSelf(ci.IP)
|
||||||
|
p.spoofLoopbackIpInClientInfo(ci)
|
||||||
return ci
|
return ci
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// spoofLoopbackIpInClientInfo replaces loopback IPs in client info.
|
||||||
|
//
|
||||||
|
// - Preference IPv4.
|
||||||
|
// - Preference RFC1918.
|
||||||
|
func (p *prog) spoofLoopbackIpInClientInfo(ci *ctrld.ClientInfo) {
|
||||||
|
if ip := net.ParseIP(ci.IP); ip == nil || !ip.IsLoopback() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if ip := p.ciTable.LookupRFC1918IPv4(ci.Mac); ip != "" {
|
||||||
|
ci.IP = ip
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// queryFromSelf reports whether the input IP is from device running ctrld.
|
// queryFromSelf reports whether the input IP is from device running ctrld.
|
||||||
func queryFromSelf(ip string) bool {
|
func queryFromSelf(ip string) bool {
|
||||||
netIP := netip.MustParseAddr(ip)
|
netIP := netip.MustParseAddr(ip)
|
||||||
|
|||||||
@@ -241,6 +241,21 @@ func (t *Table) LookupHostname(ip, mac string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LookupRFC1918IPv4 returns the RFC1918 IPv4 address for the given MAC address, if any.
|
||||||
|
func (t *Table) LookupRFC1918IPv4(mac string) string {
|
||||||
|
t.initOnce.Do(t.init)
|
||||||
|
for _, r := range t.ipResolvers {
|
||||||
|
ip, err := netip.ParseAddr(r.LookupIP(mac))
|
||||||
|
if err != nil || ip.Is6() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if ip.IsPrivate() {
|
||||||
|
return ip.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
type macEntry struct {
|
type macEntry struct {
|
||||||
mac string
|
mac string
|
||||||
src string
|
src string
|
||||||
|
|||||||
@@ -25,3 +25,22 @@ func Test_normalizeIP(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTable_LookupRFC1918IPv4(t *testing.T) {
|
||||||
|
table := &Table{
|
||||||
|
dhcp: &dhcp{},
|
||||||
|
arp: &arpDiscover{},
|
||||||
|
}
|
||||||
|
|
||||||
|
table.ipResolvers = append(table.ipResolvers, table.dhcp)
|
||||||
|
table.ipResolvers = append(table.ipResolvers, table.arp)
|
||||||
|
|
||||||
|
macAddress := "cc:19:f9:8a:49:e6"
|
||||||
|
rfc1918IPv4 := "10.0.10.245"
|
||||||
|
table.dhcp.ip.Store(macAddress, "127.0.0.1")
|
||||||
|
table.arp.ip.Store(macAddress, rfc1918IPv4)
|
||||||
|
|
||||||
|
if got := table.LookupRFC1918IPv4(macAddress); got != rfc1918IPv4 {
|
||||||
|
t.Fatalf("unexpected result, want: %s, got: %s", rfc1918IPv4, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user