From 7a136b8874a327ee838a78f6fe49420083ec3dc0 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Wed, 12 Mar 2025 00:09:19 +0700 Subject: [PATCH] all: disable client discover on desktop platforms Since requests are mostly originated from the machine itself, so all necessary metadata is local to it. Currently, the desktop platforms are Windows desktop and darwin. --- client_info_darwin.go | 4 ++++ client_info_others.go | 6 ++++++ client_info_windows.go | 18 ++++++++++++++++++ docs/config.md | 10 ++++++++++ internal/clientinfo/client_info.go | 24 ++++++++++++++++++------ 5 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 client_info_darwin.go create mode 100644 client_info_others.go create mode 100644 client_info_windows.go diff --git a/client_info_darwin.go b/client_info_darwin.go new file mode 100644 index 0000000..4c3d10b --- /dev/null +++ b/client_info_darwin.go @@ -0,0 +1,4 @@ +package ctrld + +// SelfDiscover reports whether ctrld should only do self discover. +func SelfDiscover() bool { return true } diff --git a/client_info_others.go b/client_info_others.go new file mode 100644 index 0000000..d728913 --- /dev/null +++ b/client_info_others.go @@ -0,0 +1,6 @@ +//go:build !windows && !darwin + +package ctrld + +// SelfDiscover reports whether ctrld should only do self discover. +func SelfDiscover() bool { return false } diff --git a/client_info_windows.go b/client_info_windows.go new file mode 100644 index 0000000..f20bca7 --- /dev/null +++ b/client_info_windows.go @@ -0,0 +1,18 @@ +package ctrld + +import ( + "golang.org/x/sys/windows" +) + +// isWindowsWorkStation reports whether ctrld was run on a Windows workstation machine. +func isWindowsWorkStation() bool { + // From https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-osversioninfoexa + const VER_NT_WORKSTATION = 0x0000001 + osvi := windows.RtlGetVersion() + return osvi.ProductType == VER_NT_WORKSTATION +} + +// SelfDiscover reports whether ctrld should only do self discover. +func SelfDiscover() bool { + return isWindowsWorkStation() +} diff --git a/docs/config.md b/docs/config.md index 4f50af1..99e98c9 100644 --- a/docs/config.md +++ b/docs/config.md @@ -178,6 +178,8 @@ Perform LAN client discovery using mDNS. This will spawn a listener on port 5353 - Required: no - Default: true +This config is ignored, and always set to `false` on Windows Desktop and Macos. + ### discover_arp Perform LAN client discovery using ARP. @@ -185,6 +187,8 @@ Perform LAN client discovery using ARP. - Required: no - Default: true +This config is ignored, and always set to `false` on Windows Desktop and Macos. + ### discover_dhcp Perform LAN client discovery using DHCP leases files. Common file locations are auto-discovered. @@ -192,6 +196,8 @@ Perform LAN client discovery using DHCP leases files. Common file locations are - Required: no - Default: true +This config is ignored, and always set to `false` on Windows Desktop and Macos. + ### discover_ptr Perform LAN client discovery using PTR queries. @@ -199,6 +205,8 @@ Perform LAN client discovery using PTR queries. - Required: no - Default: true +This config is ignored, and always set to `false` on Windows Desktop and Macos. + ### discover_hosts Perform LAN client discovery using hosts file. @@ -206,6 +214,8 @@ Perform LAN client discovery using hosts file. - Required: no - Default: true +This config is ignored, and always set to `false` on Windows Desktop and Macos. + ### discover_refresh_interval Time in seconds between each discovery refresh loop to update new client information data. The default value is 120 seconds, lower this value to make the discovery process run more aggressively. diff --git a/internal/clientinfo/client_info.go b/internal/clientinfo/client_info.go index 06449e1..f69b670 100644 --- a/internal/clientinfo/client_info.go +++ b/internal/clientinfo/client_info.go @@ -177,15 +177,27 @@ func (t *Table) SetSelfIP(ip string) { t.dhcp.addSelf() } +// initSelfDiscover initializes necessary client metadata for self query. +func (t *Table) initSelfDiscover() { + t.dhcp = &dhcp{selfIP: t.selfIP} + t.dhcp.addSelf() + t.ipResolvers = append(t.ipResolvers, t.dhcp) + t.macResolvers = append(t.macResolvers, t.dhcp) + t.hostnameResolvers = append(t.hostnameResolvers, t.dhcp) +} + func (t *Table) init() { // Custom client ID presents, use it as the only source. if _, clientID := controld.ParseRawUID(t.cdUID); clientID != "" { - ctrld.ProxyLogger.Load().Debug().Msg("start self discovery") - t.dhcp = &dhcp{selfIP: t.selfIP} - t.dhcp.addSelf() - t.ipResolvers = append(t.ipResolvers, t.dhcp) - t.macResolvers = append(t.macResolvers, t.dhcp) - t.hostnameResolvers = append(t.hostnameResolvers, t.dhcp) + ctrld.ProxyLogger.Load().Debug().Msg("start self discovery with custom client id") + t.initSelfDiscover() + return + } + + // If we are running on platforms that should only do self discover, use it as the only source, too. + if ctrld.SelfDiscover() { + ctrld.ProxyLogger.Load().Debug().Msg("start self discovery on desktop platforms") + t.initSelfDiscover() return }