diff --git a/wg/wg.go b/wg/wg.go index 3cee1a9..adf8df6 100644 --- a/wg/wg.go +++ b/wg/wg.go @@ -3,6 +3,7 @@ package wg import ( + "context" "encoding/json" "errors" "fmt" @@ -13,16 +14,19 @@ import ( "sync" "time" + "math/rand" + "github.com/fosrl/newt/logger" "github.com/fosrl/newt/network" "github.com/fosrl/newt/websocket" "github.com/vishvananda/netlink" "golang.org/x/crypto/chacha20poly1305" "golang.org/x/crypto/curve25519" - "golang.org/x/exp/rand" "golang.zx2c4.com/wireguard/conn" "golang.zx2c4.com/wireguard/wgctrl" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" + + "github.com/fosrl/newt/internal/telemetry" ) type WgConfig struct { @@ -106,7 +110,7 @@ func FindAvailableUDPPort(minPort, maxPort uint16) (uint16, error) { } // Fisher-Yates shuffle to randomize the port order - rand.Seed(uint64(time.Now().UnixNano())) + rand.Seed(time.Now().UnixNano()) for i := len(portRange) - 1; i > 0; i-- { j := rand.Intn(i + 1) portRange[i], portRange[j] = portRange[j], portRange[i] @@ -298,6 +302,13 @@ func (s *WireGuardService) handleConfig(msg websocket.WSMessage) { s.stopGetConfig = nil } + // telemetry: config reload success + telemetry.IncConfigReload(context.Background(), "success") + // Optional reconnect reason mapping: config change + if s.serverPubKey != "" { + telemetry.IncReconnect(context.Background(), "", s.serverPubKey, telemetry.ReasonConfigChange) + } + // Ensure the WireGuard interface and peers are configured if err := s.ensureWireguardInterface(config); err != nil { logger.Error("Failed to ensure WireGuard interface: %v", err) diff --git a/wgnetstack/wgnetstack.go b/wgnetstack/wgnetstack.go index 6684c40..09f160e 100644 --- a/wgnetstack/wgnetstack.go +++ b/wgnetstack/wgnetstack.go @@ -1,6 +1,7 @@ package wgnetstack import ( + "context" "crypto/rand" "encoding/base64" "encoding/hex" @@ -26,6 +27,8 @@ import ( "golang.zx2c4.com/wireguard/tun" "golang.zx2c4.com/wireguard/tun/netstack" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" + + "github.com/fosrl/newt/internal/telemetry" ) type WgConfig struct { @@ -240,14 +243,20 @@ func NewWireGuardService(interfaceName string, mtu int, generateAndSaveKeyTo str return service, nil } +// ReportRTT allows reporting native RTTs to telemetry, rate-limited externally. +func (s *WireGuardService) ReportRTT(seconds float64) { + if s.serverPubKey == "" { return } + telemetry.ObserveTunnelLatency(context.Background(), "", s.serverPubKey, "wireguard", seconds) +} + func (s *WireGuardService) addTcpTarget(msg websocket.WSMessage) { logger.Debug("Received: %+v", msg) // if there is no wgData or pm, we can't add targets if s.TunnelIP == "" || s.proxyManager == nil { logger.Info("No tunnel IP or proxy manager available") - return - } + return +} targetData, err := parseTargetData(msg.Data) if err != nil {