mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-07-29 01:18:48 +02:00
fix(darwin): use scutil for provisioning hostname (#485)
macOS Sequoia with Private Wi-Fi Address enabled causes os.Hostname() to return generic names like "Mac.lan" from DHCP instead of the real computer name. The /utility provisioning endpoint sends this raw, resulting in devices named "Mac-lan" in the dashboard. Fallback chain: ComputerName → LocalHostName → os.Hostname() LocalHostName can also be affected by DHCP. ComputerName is the user-set display name from System Settings, fully immune to network state.
This commit is contained in:
@@ -10,7 +10,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -127,7 +126,7 @@ func FetchResolverUID(ctx context.Context, req *UtilityOrgRequest, version strin
|
|||||||
|
|
||||||
hostname := req.Hostname
|
hostname := req.Hostname
|
||||||
if req.Hostname == "" {
|
if req.Hostname == "" {
|
||||||
hostname, _ = os.Hostname()
|
hostname, _ = preferredHostname()
|
||||||
ctrld.Log(ctx, logger.Debug(), "Using system hostname: %s", hostname)
|
ctrld.Log(ctx, logger.Debug(), "Using system hostname: %s", hostname)
|
||||||
req.Hostname = hostname
|
req.Hostname = hostname
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package controld
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// preferredHostname returns the best available hostname on macOS.
|
||||||
|
// It prefers scutil --get ComputerName which is the user-configured name
|
||||||
|
// from System Settings → General → About → Name. This is immune to
|
||||||
|
// DHCP/network state that can cause os.Hostname() and even LocalHostName
|
||||||
|
// to return generic names like "Mac.lan" on Sequoia with Private Wi-Fi
|
||||||
|
// Address enabled.
|
||||||
|
//
|
||||||
|
// Fallback chain: ComputerName → LocalHostName → os.Hostname()
|
||||||
|
func preferredHostname() (string, error) {
|
||||||
|
for _, key := range []string{"ComputerName", "LocalHostName"} {
|
||||||
|
if out, err := exec.Command("scutil", "--get", key).Output(); err == nil {
|
||||||
|
if name := strings.TrimSpace(string(out)); name != "" {
|
||||||
|
return name, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return os.Hostname()
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
//go:build !darwin
|
||||||
|
|
||||||
|
package controld
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
|
||||||
|
// preferredHostname returns the system hostname on non-Darwin platforms.
|
||||||
|
func preferredHostname() (string, error) {
|
||||||
|
return os.Hostname()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user