cloud-control-panel/fqdn.go

38 lines
683 B
Go
Raw Normal View History

package main
import (
"net"
"os"
"strings"
)
// Get Fully Qualified Domain Name
// returns "unknown" or hostanme in case of error
func FqdnGet() string {
hostname, err := os.Hostname()
if err != nil {
return "unknown"
}
addrs, err := net.LookupIP(hostname)
if err != nil {
return hostname
}
for _, addr := range addrs {
if ipv4 := addr.To4(); ipv4 != nil {
ip, err := ipv4.MarshalText()
if err != nil {
return hostname
}
hosts, err := net.LookupAddr(string(ip))
if err != nil || len(hosts) == 0 {
return hostname
}
fqdn := hosts[0]
return strings.TrimSuffix(fqdn, ".") // return fqdn without trailing dot
}
}
return hostname
}