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