Skip to content

Commit b6609e0

Browse files
committed
nit
1 parent 1655c1f commit b6609e0

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

internal/list/list.go

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,28 +79,7 @@ func Devices(ctx context.Context) ([]Device, error) {
7979
if err != nil || device == nil {
8080
return
8181
}
82-
83-
p := goping.NewPinger()
84-
_, _ = p.Network("udp")
85-
86-
netProto := icmpIpv4
87-
if strings.Contains(ip, ":") {
88-
netProto = icmpIpv6
89-
}
90-
91-
addr, err := net.ResolveIPAddr(netProto, ip)
92-
if err != nil {
93-
return
94-
}
95-
96-
p.AddIPAddr(addr)
97-
p.MaxRTT = time.Second
98-
p.OnRecv = func(addr *net.IPAddr, t time.Duration) {
99-
device.Up = true
100-
}
101-
if err := p.Run(); err != nil {
102-
return
103-
}
82+
device.Up = ping(ip)
10483

10584
mutex.Lock()
10685
devices = append(devices, *device)
@@ -247,3 +226,28 @@ func removeIP(ip string, hosts []string) []string {
247226
}
248227
return filteredHosts
249228
}
229+
230+
func ping(ip string) bool {
231+
p := goping.NewPinger()
232+
p.Network("udp")
233+
234+
netProto := icmpIpv4
235+
if strings.Contains(ip, ":") {
236+
netProto = icmpIpv6
237+
}
238+
239+
addr, err := net.ResolveIPAddr(netProto, ip)
240+
if err != nil {
241+
return false
242+
}
243+
244+
p.AddIPAddr(addr)
245+
p.MaxRTT = time.Second
246+
247+
var up bool
248+
p.OnRecv = func(addr *net.IPAddr, t time.Duration) {
249+
up = true
250+
}
251+
p.Run()
252+
return up
253+
}

0 commit comments

Comments
 (0)