Skip to content

Commit 045870e

Browse files
committed
chore: upgrade pinger
1 parent eebbaf5 commit 045870e

File tree

4 files changed

+31
-62
lines changed

4 files changed

+31
-62
lines changed

Justfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build:
2+
GOOS=linux GOARCH=arm GOARM=7 go build -ldflags "-s -w" -o nocturned

go.mod

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,9 @@ toolchain go1.23.9
77
require (
88
github.com/godbus/dbus/v5 v5.1.0
99
github.com/gorilla/websocket v1.5.3
10-
github.com/prometheus-community/pro-bing v0.7.0
1110
github.com/vishvananda/netlink v1.3.0
1211
)
1312

14-
require (
15-
github.com/google/uuid v1.6.0 // indirect
16-
golang.org/x/net v0.38.0 // indirect
17-
golang.org/x/sync v0.13.0 // indirect
18-
)
19-
2013
require (
2114
github.com/vishvananda/netns v0.0.5 // indirect
2215
golang.org/x/sys v0.31.0 // indirect

go.sum

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
22
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
3-
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
4-
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
53
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
64
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
7-
github.com/prometheus-community/pro-bing v0.7.0 h1:KFYFbxC2f2Fp6c+TyxbCOEarf7rbnzr9Gw8eIb0RfZA=
8-
github.com/prometheus-community/pro-bing v0.7.0/go.mod h1:Moob9dvlY50Bfq6i88xIwfyw7xLFHH69LUgx9n5zqCE=
95
github.com/vishvananda/netlink v1.3.0 h1:X7l42GfcV4S6E4vHTsw48qbrV+9PVojNfIhZcwQdrZk=
106
github.com/vishvananda/netlink v1.3.0/go.mod h1:i6NetklAujEcC6fK0JPjT8qSwWyO0HLn4UKG+hGqeJs=
117
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
128
github.com/vishvananda/netns v0.0.5 h1:DfiHV+j8bA32MFM7bfEunvT8IAqQ/NzSJHtcmW5zdEY=
139
github.com/vishvananda/netns v0.0.5/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
14-
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
15-
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
16-
golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610=
17-
golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
1810
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1911
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2012
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=

main.go

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net"
99
"net/http"
1010
"os"
11+
"os/exec"
1112
"path/filepath"
1213
"strconv"
1314
"strings"
@@ -16,7 +17,6 @@ import (
1617
"github.com/gorilla/websocket"
1718
"github.com/vishvananda/netlink"
1819

19-
ping "github.com/prometheus-community/pro-bing"
2020
"github.com/usenocturne/nocturned/bluetooth"
2121
"github.com/usenocturne/nocturned/utils"
2222
)
@@ -59,64 +59,46 @@ func networkChecker(hub *utils.WebSocketHub) {
5959
const (
6060
host = "1.1.1.1"
6161
interval = 1 // seconds
62-
failThreshold = 3
62+
failThreshold = 5
6363
)
6464

6565
failCount := 0
66-
6766
isOnline := false
68-
pinger, err := ping.NewPinger(host)
69-
if err == nil {
70-
pinger.Count = 1
71-
pinger.Timeout = 1 * time.Second
72-
pinger.Interval = 1 * time.Second
73-
pinger.SetPrivileged(true)
74-
err = pinger.Run()
75-
if err == nil && pinger.Statistics().PacketsRecv > 0 {
76-
currentNetworkStatus = "online"
77-
hub.Broadcast(utils.WebSocketEvent{
78-
Type: "network_status",
79-
Payload: map[string]string{"status": "online"},
80-
})
81-
isOnline = true
82-
} else {
83-
currentNetworkStatus = "offline"
84-
hub.Broadcast(utils.WebSocketEvent{
85-
Type: "network_status",
86-
Payload: map[string]string{"status": "offline"},
87-
})
88-
}
67+
68+
pingHost := func() bool {
69+
cmd := exec.Command("ping", "-c", "1", "-W", "1", host)
70+
err := cmd.Run()
71+
return err == nil
72+
}
73+
74+
if pingHost() {
75+
currentNetworkStatus = "online"
76+
hub.Broadcast(utils.WebSocketEvent{
77+
Type: "network_status",
78+
Payload: map[string]string{"status": "online"},
79+
})
80+
isOnline = true
8981
} else {
82+
currentNetworkStatus = "offline"
9083
hub.Broadcast(utils.WebSocketEvent{
9184
Type: "network_status",
9285
Payload: map[string]string{"status": "offline"},
9386
})
9487
}
9588

9689
for {
97-
pinger, err := ping.NewPinger(host)
98-
if err != nil {
99-
log.Printf("Failed to create pinger: %v", err)
100-
failCount++
101-
} else {
102-
pinger.Count = 1
103-
pinger.Timeout = 1 * time.Second
104-
pinger.Interval = 1 * time.Second
105-
pinger.SetPrivileged(true)
106-
err = pinger.Run()
107-
if err != nil || pinger.Statistics().PacketsRecv == 0 {
108-
failCount++
109-
} else {
110-
failCount = 0
111-
if !isOnline {
112-
currentNetworkStatus = "online"
113-
hub.Broadcast(utils.WebSocketEvent{
114-
Type: "network_status",
115-
Payload: map[string]string{"status": "online"},
116-
})
117-
isOnline = true
118-
}
90+
if pingHost() {
91+
failCount = 0
92+
if !isOnline {
93+
currentNetworkStatus = "online"
94+
hub.Broadcast(utils.WebSocketEvent{
95+
Type: "network_status",
96+
Payload: map[string]string{"status": "online"},
97+
})
98+
isOnline = true
11999
}
100+
} else {
101+
failCount++
120102
}
121103

122104
if failCount >= failThreshold && isOnline {
@@ -630,7 +612,7 @@ func main() {
630612
go func() {
631613
utils.SetUpdateStatus(true, "download", "")
632614

633-
tempDir, err := os.MkdirTemp("/data/tmp", "update-*")
615+
tempDir, err := os.MkdirTemp("/var/tmp", "update-*")
634616
if err != nil {
635617
utils.SetUpdateStatus(false, "", fmt.Sprintf("Failed to create temp directory: %v", err))
636618
return

0 commit comments

Comments
 (0)