Skip to content

Commit 9cdc422

Browse files
committed
Replace Showmax/go-fqdn dependency with function
go-fqdn uses net.LookupCNAME without a context/timeout. This replaces the dependency with net.Resolver that uses a timeout
1 parent 9d5c01d commit 9cdc422

3 files changed

Lines changed: 39 additions & 6 deletions

File tree

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/NETWAYS/support-collector
33
go 1.25.0
44

55
require (
6-
github.com/Showmax/go-fqdn v1.0.0
76
github.com/mitchellh/go-ps v1.0.0
87
github.com/spf13/pflag v1.0.10
98
gopkg.in/yaml.v3 v3.0.1

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
github.com/Showmax/go-fqdn v1.0.0 h1:0rG5IbmVliNT5O19Mfuvna9LL7zlHyRfsSvBPZmF9tM=
2-
github.com/Showmax/go-fqdn v1.0.0/go.mod h1:SfrFBzmDCtCGrnHhoDjuvFnKsWjEQX/Q9ARZvOrJAko=
31
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
42
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
53
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=

modules/base/kernel.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package base
22

33
import (
4+
"context"
45
"fmt"
6+
"net"
7+
"os"
8+
"strings"
59
"syscall"
610
"time"
7-
8-
"github.com/Showmax/go-fqdn"
911
)
1012

1113
type KernelInfo struct {
@@ -19,6 +21,40 @@ type KernelInfo struct {
1921
CurrentTime string
2022
}
2123

24+
func getFQDN() (string, error) {
25+
hostname, err := os.Hostname()
26+
if err != nil {
27+
return "", err
28+
}
29+
30+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
31+
32+
defer cancel()
33+
34+
resolver := &net.Resolver{
35+
PreferGo: true,
36+
}
37+
38+
addrs, err := resolver.LookupHost(ctx, hostname)
39+
if err != nil {
40+
return hostname, nil
41+
}
42+
43+
for _, addr := range addrs {
44+
hosts, err := resolver.LookupAddr(ctx, addr)
45+
46+
if err != nil || len(hosts) == 0 {
47+
continue
48+
}
49+
50+
fqdn := strings.TrimSuffix(hosts[0], ".")
51+
52+
return fqdn, nil
53+
}
54+
55+
return hostname, nil
56+
}
57+
2258
func GetKernelInfo() (i KernelInfo, err error) {
2359
var uname syscall.Utsname
2460

@@ -37,7 +73,7 @@ func GetKernelInfo() (i KernelInfo, err error) {
3773
Domainname: CharsToString(uname.Domainname[:]),
3874
}
3975

40-
i.FQDN, err = fqdn.FqdnHostname()
76+
i.FQDN, err = getFQDN()
4177
// return err after setting info
4278

4379
i.CurrentTime = time.Now().String()

0 commit comments

Comments
 (0)