From 56b3ee19c1904756755e1e761297a9b12c65bfb9 Mon Sep 17 00:00:00 2001 From: Codescribe Date: Thu, 12 Feb 2026 12:41:25 -0500 Subject: [PATCH] fix: include hostname hints in metadata for API-side fallback Send all available hostname sources (ComputerName, LocalHostName, HostName, os.Hostname) in the metadata map when provisioning. This allows the API to detect and repair generic hostnames like 'Mac' by picking the best available source server-side. Belt and suspenders: preferredHostname() picks the right one client-side, but metadata gives the API a second chance. --- internal/controld/config.go | 9 +++++++++ internal/controld/hostname_darwin.go | 18 ++++++++++++++++++ internal/controld/hostname_others.go | 9 +++++++++ 3 files changed, 36 insertions(+) diff --git a/internal/controld/config.go b/internal/controld/config.go index d2451eb..765706e 100644 --- a/internal/controld/config.go +++ b/internal/controld/config.go @@ -133,6 +133,15 @@ func FetchResolverUID(ctx context.Context, req *UtilityOrgRequest, version strin ctrld.Log(ctx, logger.Debug(), "Using provided hostname: %s", hostname) } + // Include all hostname sources in metadata so the API can pick the + // best one if the primary looks generic (e.g., "Mac", "Mac.lan"). + if req.Metadata == nil { + req.Metadata = make(map[string]string) + } + for k, v := range hostnameHints() { + req.Metadata["hostname_"+k] = v + } + ctrld.Log(ctx, logger.Debug(), "Sending UID request to ControlD API") body, _ := json.Marshal(req) return postUtilityAPI(ctx, version, cdDev, false, bytes.NewReader(body)) diff --git a/internal/controld/hostname_darwin.go b/internal/controld/hostname_darwin.go index 107b4cd..0b8eb52 100644 --- a/internal/controld/hostname_darwin.go +++ b/internal/controld/hostname_darwin.go @@ -24,3 +24,21 @@ func preferredHostname() (string, error) { } return os.Hostname() } + +// hostnameHints returns all available hostname sources on macOS for +// diagnostic/fallback purposes. The API can use these to pick the +// best hostname if the primary one looks generic (e.g., "Mac"). +func hostnameHints() map[string]string { + hints := make(map[string]string) + for _, key := range []string{"ComputerName", "LocalHostName", "HostName"} { + if out, err := exec.Command("scutil", "--get", key).Output(); err == nil { + if name := strings.TrimSpace(string(out)); name != "" { + hints[key] = name + } + } + } + if h, err := os.Hostname(); err == nil { + hints["os.Hostname"] = h + } + return hints +} diff --git a/internal/controld/hostname_others.go b/internal/controld/hostname_others.go index 9ae1026..8aa03bc 100644 --- a/internal/controld/hostname_others.go +++ b/internal/controld/hostname_others.go @@ -8,3 +8,12 @@ import "os" func preferredHostname() (string, error) { return os.Hostname() } + +// hostnameHints returns available hostname sources for diagnostic purposes. +func hostnameHints() map[string]string { + hints := make(map[string]string) + if h, err := os.Hostname(); err == nil { + hints["os.Hostname"] = h + } + return hints +}