From f5a64abb4e661850c3b59b1907d6ac4e5a8e4676 Mon Sep 17 00:00:00 2001 From: Dan Fuhry Date: Tue, 27 Feb 2024 10:31:16 -0500 Subject: [PATCH] utils/hostname: fix build on darwin, support openbsd --- utils/hostname/hostname.go | 2 +- utils/hostname/hostname_macos.go | 4 +- utils/hostname/hostname_openbsd.go | 91 ++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 utils/hostname/hostname_openbsd.go diff --git a/utils/hostname/hostname.go b/utils/hostname/hostname.go index f99e32d..65431fb 100644 --- a/utils/hostname/hostname.go +++ b/utils/hostname/hostname.go @@ -1,4 +1,4 @@ -//go:build !darwin +//go:build !darwin && !openbsd package hostname diff --git a/utils/hostname/hostname_macos.go b/utils/hostname/hostname_macos.go index 75e07da..73337bd 100644 --- a/utils/hostname/hostname_macos.go +++ b/utils/hostname/hostname_macos.go @@ -16,9 +16,9 @@ import ( "howett.net/plist" ) -const ( - defaultDomainName = constants.DefaultHostDomain +var defaultDomainName = constants.DefaultHostDomain +const ( systemPreferencesPlist = "/Library/Preferences/SystemConfiguration/preferences.plist" ) diff --git a/utils/hostname/hostname_openbsd.go b/utils/hostname/hostname_openbsd.go new file mode 100644 index 0000000..caf8ebe --- /dev/null +++ b/utils/hostname/hostname_openbsd.go @@ -0,0 +1,91 @@ +// go:build openbsd + +package hostname + +import ( + "context" + "errors" + "fmt" + "net" + "os" + "strings" + "time" + + "go.fuhry.dev/runtime/constants" + "go.fuhry.dev/runtime/utils/log" +) + +var defaultDomainName = constants.DefaultHostDomain +var cachedFqdn string + +func Hostname() string { + fqdn := Fqdn() + return strings.Split(fqdn, ".")[0] +} + +func DomainName() string { + fqdn := strings.Split(Fqdn(), ".") + if len(fqdn) > 1 { + return strings.Join(fqdn[1:], ".") + } + return defaultDomainName +} + +func RegionName() string { + return strings.Split(DomainName(), ".")[0] +} + +func Fqdn() string { + if cachedFqdn != "" { + return cachedFqdn + } + cachedFqdn = fqdn() + return cachedFqdn +} + +func fqdn() string { + myName, myNameErr := os.ReadFile("/etc/myname") + if myNameErr == nil { + return maybeAppendDefaultDomainName(strings.Trim(string(myName), " \r\n")) + } + + dnsFqdn, dnsErr := fqdnFromDns() + if dnsErr == nil { + return maybeAppendDefaultDomainName(dnsFqdn) + } + + log.Panicf("failed to get fqdn: myname failed because %q, and dns failed because %q", myNameErr.Error(), dnsErr.Error()) + return "" +} + +func maybeAppendDefaultDomainName(name string) string { + parts := strings.Split(name, ".") + if len(parts) < 2 { + name = fmt.Sprintf("%s.%s", parts[0], defaultDomainName) + } + + log.Default().V(1).Noticef("detected system hostname: %q", name) + + return name +} + +func fqdnFromDns() (string, error) { + resolver := &net.Resolver{} + + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) + defer cancel() + + result, err := resolver.LookupAddr(ctx, "127.0.0.1") + if err != nil { + return "", fmt.Errorf("failed to lookup hostname for 127.0.0.1: %v", err) + } + + for _, name := range result { + parts := strings.Split(name, ".") + if parts[0] != "localhost" { + return parts[0], nil + } + } + + return "", errors.New("failed to lookup any hostname for 127.0.0.1 that does not resolve to just \"localhost\"") +} -- 2.50.1