/
icmp.go
62 lines (58 loc) · 1.2 KB
/
icmp.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"os"
"runtime"
"time"
"github.com/CHTJonas/concheck/utils"
"github.com/go-ping/ping"
)
func testICMP() {
endpoints := []string{
"1.0.0.1", "2606:4700:4700::1001",
"8.8.4.4", "2001:4860:4860::8844",
"149.112.112.112", "2620:fe::fe",
}
unreachableCount := 0
for _, endpoint := range endpoints {
if (forceIPv4Flag && !utils.IsIPv4(endpoint)) || (forceIPv6Flag && utils.IsIPv4(endpoint)) {
continue
}
wg.Add(1)
go checkICMP(endpoint, &unreachableCount)
}
wg.Wait()
if unreachableCount > 3 {
os.Exit(2)
}
}
func checkICMP(endpoint string, unreachableCount *int) {
pinger, err := ping.NewPinger(endpoint)
if err != nil {
panic(err)
}
pinger.Count = 5
pinger.Interval = 100 * time.Millisecond
pinger.Timeout = 5 * time.Second
if runtime.GOOS != "darwin" {
pinger.SetPrivileged(true)
}
err = pinger.Run()
if err != nil {
if utils.IsUnreachableError(err) {
// If the user has explicitly forced IPv4 or IPv6 then don't squelch errors
if forceIPv4Flag || forceIPv6Flag {
os.Exit(2)
}
*unreachableCount++
wg.Done()
return
} else {
panic(err)
}
}
stats := pinger.Statistics()
if stats.PacketLoss > 0.5 {
os.Exit(2)
}
wg.Done()
}