Skip to content

Commit 48365af

Browse files
fix(network): ping multiple hosts
Instead of only pinging 1.1.1.1, nocturned can now fallback to 8.8.8.8 (same servers as nocturne firmware)
1 parent c7259b1 commit 48365af

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

main.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,27 @@ func corsMiddleware(next http.HandlerFunc) http.HandlerFunc {
5757
}
5858

5959
func networkChecker(hub *utils.WebSocketHub) {
60-
const (
61-
host = "1.1.1.1"
60+
var (
61+
hosts = []string{"1.1.1.1", "8.8.8.8"}
6262
interval = 1 // seconds
6363
failThreshold = 5
6464
)
6565

6666
failCount := 0
6767
isOnline := false
6868

69-
pingHost := func() bool {
70-
cmd := exec.Command("ping", "-c", "1", "-W", "1", host)
71-
err := cmd.Run()
72-
return err == nil
69+
pingHosts := func() bool {
70+
for _, h := range hosts {
71+
cmd := exec.Command("ping", "-c", "1", "-W", "1", h)
72+
if err := cmd.Run(); err == nil {
73+
return true
74+
}
75+
}
76+
return false
7377
}
7478

75-
if pingHost() {
79+
80+
if pingHosts() {
7681
currentNetworkStatus = "online"
7782
hub.Broadcast(utils.WebSocketEvent{
7883
Type: "network_status",
@@ -88,7 +93,7 @@ func networkChecker(hub *utils.WebSocketHub) {
8893
}
8994

9095
for {
91-
if pingHost() {
96+
if pingHosts() {
9297
failCount = 0
9398
if !isOnline {
9499
currentNetworkStatus = "online"
@@ -111,7 +116,7 @@ func networkChecker(hub *utils.WebSocketHub) {
111116
isOnline = false
112117
}
113118

114-
time.Sleep(interval * time.Second)
119+
time.Sleep(time.Duration(interval) * time.Second)
115120
}
116121
}
117122

0 commit comments

Comments
 (0)