Skip to content

Commit 6f19756

Browse files
authored
fix: use route -n to avoid reverse lookup timeout (#323)
This caused the update page to render blank Bug: https://pms.uniontech.com/bug-view-351859.html
1 parent 2fd593e commit 6f19756

2 files changed

Lines changed: 50 additions & 12 deletions

File tree

src/internal/updateplatform/systeminfo_utils.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -554,28 +554,34 @@ func UpdateTokenConfigFile(includeDiskInfo bool, getHardwareIdByHelper bool) str
554554
}
555555

556556
func getDefaultMac() (string, error) {
557-
res, err := exec.Command("route").Output()
557+
res, err := exec.Command("route", "-n").Output()
558558
if err != nil {
559559
return "", err
560560
}
561-
lines := strings.Split(string(res), "\n")
562-
var dev string
563-
for _, line := range lines {
564-
if strings.Contains(line, "default") {
565-
devs := strings.Split(line, " ")
566-
dev = devs[len(devs)-1]
567-
break
568-
}
569-
}
561+
dev := getDefaultRouteIface(string(res))
570562
if len(dev) == 0 {
571563
return "", nil
572564
}
573565

574-
mac, err := os.ReadFile("/sys/class/net/" + dev + "/address")
566+
mac, err := os.ReadFile(filepath.Join("/sys/class/net", dev, "address"))
575567
if err != nil {
576568
return "", err
577569
}
578-
return string(mac), nil
570+
return strings.TrimSpace(string(mac)), nil
571+
}
572+
573+
func getDefaultRouteIface(routeOutput string) string {
574+
lines := strings.Split(routeOutput, "\n")
575+
for _, line := range lines {
576+
fields := strings.Fields(line)
577+
if len(fields) < 8 {
578+
continue
579+
}
580+
if fields[0] == "0.0.0.0" {
581+
return fields[len(fields)-1]
582+
}
583+
}
584+
return ""
579585
}
580586

581587
func getClientPackageInfo(client string) string {

src/internal/updateplatform/systeminfo_utils_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,35 @@ func TestSystemInfoUtil(t *testing.T) {
1414
sys := getSystemInfo(true, true)
1515
assert.NotEmpty(t, sys)
1616
}
17+
18+
func TestGetDefaultRouteIface(t *testing.T) {
19+
tests := []struct {
20+
name string
21+
output string
22+
want string
23+
}{
24+
{
25+
name: "default route found",
26+
output: `Kernel IP routing table
27+
Destination Gateway Genmask Flags Metric Ref Use Iface
28+
0.0.0.0 10.20.33.1 0.0.0.0 UG 100 0 0 eno1
29+
10.20.33.0 0.0.0.0 255.255.255.0 U 100 0 0 eno1
30+
`,
31+
want: "eno1",
32+
},
33+
{
34+
name: "no default route",
35+
output: `Kernel IP routing table
36+
Destination Gateway Genmask Flags Metric Ref Use Iface
37+
10.20.33.0 0.0.0.0 255.255.255.0 U 100 0 0 eno1
38+
`,
39+
want: "",
40+
},
41+
}
42+
43+
for _, tt := range tests {
44+
t.Run(tt.name, func(t *testing.T) {
45+
assert.Equal(t, tt.want, getDefaultRouteIface(tt.output))
46+
})
47+
}
48+
}

0 commit comments

Comments
 (0)