-
Notifications
You must be signed in to change notification settings - Fork 1
/
fqdn.go
42 lines (36 loc) · 795 Bytes
/
fqdn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright (c) 2017-2019 The Fonero developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package util
import (
"net"
"os"
"strings"
)
// FQDN returns the fully qualified domain name of the machine it is running
// on.
func FQDN() string {
hostname, err := os.Hostname()
if err != nil {
return "localhost"
}
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 hostname
}