Files
darroyo a77f84ad34 Bump version to 1.0.10
Smart WoL: TCP pre-check before sending magic packet (3s timeout).
Machine status updates via SSE: online/offline tracked in DB and
broadcast to all connected browser tabs in real-time.
- Pre-check: if machine already reachable, skip WoL and mark online
- WoL path: send 3 magic packets, wait for SSH, mark online/offline
- Backend: setMachineStatus() helper + machine_status SSE event
- Frontend: subscribeMachineStatus() SSE helper for Machines + Dashboard
- IsReachable() helper in wol package for TCP reachability checks
2026-07-08 19:56:08 -04:00

61 lines
1.3 KiB
Go

package wol
import (
"context"
"fmt"
"net"
"time"
)
var ErrTimeout = fmt.Errorf("timeout waiting for machine to respond")
func WaitUntilReady(ctx context.Context, host string, sshPort int, maxWait, checkInterval time.Duration) error {
deadline := time.Now().Add(maxWait)
interval := checkInterval
ticker := time.NewTicker(checkInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
addr := fmt.Sprintf("%s:%d", host, sshPort)
dialer := net.Dialer{Timeout: 3 * time.Second}
conn, err := dialer.DialContext(ctx, "tcp", addr)
if err == nil {
conn.Close()
return nil
}
if time.Now().After(deadline) {
return ErrTimeout
}
elapsed := time.Since(time.Now().Add(-maxWait))
if elapsed > maxWait/2 && interval < 10*time.Second {
interval = interval * 3 / 2
if interval > 10*time.Second {
interval = 10 * time.Second
}
ticker.Reset(interval)
}
}
}
func IsReachable(ctx context.Context, host string, port int, timeout time.Duration) bool {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
addr := fmt.Sprintf("%s:%d", host, port)
dialer := net.Dialer{Timeout: timeout}
conn, err := dialer.DialContext(ctx, "tcp", addr)
if err != nil {
return false
}
conn.Close()
return true
}