mirror of
https://github.com/fosrl/newt.git
synced 2026-03-26 20:46:41 +00:00
Integrate tunnel metrics and telemetry reporting throughout main application logic
This commit is contained in:
31
main.go
31
main.go
@@ -664,7 +664,10 @@ persistent_keepalive_interval=5`, fixKey(privateKey.String()), fixKey(wgData.Pub
|
|||||||
}
|
}
|
||||||
// Use reliable ping for initial connection test
|
// Use reliable ping for initial connection test
|
||||||
logger.Debug("Testing initial connection with reliable ping...")
|
logger.Debug("Testing initial connection with reliable ping...")
|
||||||
_, err = reliablePing(tnet, wgData.ServerIP, pingTimeout, 5)
|
lat, err := reliablePing(tnet, wgData.ServerIP, pingTimeout, 5)
|
||||||
|
if err == nil && wgData.PublicKey != "" {
|
||||||
|
telemetry.ObserveTunnelLatency(context.Background(), "", wgData.PublicKey, "wireguard", lat.Seconds())
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warn("Initial reliable ping failed, but continuing: %v", err)
|
logger.Warn("Initial reliable ping failed, but continuing: %v", err)
|
||||||
} else {
|
} else {
|
||||||
@@ -677,14 +680,20 @@ persistent_keepalive_interval=5`, fixKey(privateKey.String()), fixKey(wgData.Pub
|
|||||||
// as the pings will continue in the background
|
// as the pings will continue in the background
|
||||||
if !connected {
|
if !connected {
|
||||||
logger.Debug("Starting ping check")
|
logger.Debug("Starting ping check")
|
||||||
pingStopChan = startPingCheck(tnet, wgData.ServerIP, client)
|
pingStopChan = startPingCheck(tnet, wgData.ServerIP, client, wgData.PublicKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create proxy manager
|
// Create proxy manager
|
||||||
pm = proxy.NewProxyManager(tnet)
|
pm = proxy.NewProxyManager(tnet)
|
||||||
|
pm.SetAsyncBytes(metricsAsyncBytes)
|
||||||
|
// Set tunnel_id for metrics (WireGuard peer public key)
|
||||||
|
pm.SetTunnelID(wgData.PublicKey)
|
||||||
|
|
||||||
connected = true
|
connected = true
|
||||||
|
|
||||||
|
// telemetry: record a successful site registration (omit region unless available)
|
||||||
|
telemetry.IncSiteRegistration(context.Background(), id, "", "success")
|
||||||
|
|
||||||
// add the targets if there are any
|
// add the targets if there are any
|
||||||
if len(wgData.Targets.TCP) > 0 {
|
if len(wgData.Targets.TCP) > 0 {
|
||||||
updateTargets(pm, "add", wgData.TunnelIP, "tcp", TargetData{Targets: wgData.Targets.TCP})
|
updateTargets(pm, "add", wgData.TunnelIP, "tcp", TargetData{Targets: wgData.Targets.TCP})
|
||||||
@@ -718,10 +727,25 @@ persistent_keepalive_interval=5`, fixKey(privateKey.String()), fixKey(wgData.Pub
|
|||||||
|
|
||||||
client.RegisterHandler("newt/wg/reconnect", func(msg websocket.WSMessage) {
|
client.RegisterHandler("newt/wg/reconnect", func(msg websocket.WSMessage) {
|
||||||
logger.Info("Received reconnect message")
|
logger.Info("Received reconnect message")
|
||||||
|
if wgData.PublicKey != "" {
|
||||||
|
telemetry.IncReconnect(context.Background(), "", wgData.PublicKey, "server_request")
|
||||||
|
}
|
||||||
|
|
||||||
// Close the WireGuard device and TUN
|
// Close the WireGuard device and TUN
|
||||||
closeWgTunnel()
|
closeWgTunnel()
|
||||||
|
|
||||||
|
// Clear metrics attrs and sessions for the tunnel
|
||||||
|
if pm != nil {
|
||||||
|
pm.ClearTunnelID()
|
||||||
|
state.Global().ClearTunnel(wgData.PublicKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear metrics attrs and sessions for the tunnel
|
||||||
|
if pm != nil {
|
||||||
|
pm.ClearTunnelID()
|
||||||
|
state.Global().ClearTunnel(wgData.PublicKey)
|
||||||
|
}
|
||||||
|
|
||||||
// Mark as disconnected
|
// Mark as disconnected
|
||||||
connected = false
|
connected = false
|
||||||
|
|
||||||
@@ -738,6 +762,9 @@ persistent_keepalive_interval=5`, fixKey(privateKey.String()), fixKey(wgData.Pub
|
|||||||
|
|
||||||
client.RegisterHandler("newt/wg/terminate", func(msg websocket.WSMessage) {
|
client.RegisterHandler("newt/wg/terminate", func(msg websocket.WSMessage) {
|
||||||
logger.Info("Received termination message")
|
logger.Info("Received termination message")
|
||||||
|
if wgData.PublicKey != "" {
|
||||||
|
telemetry.IncReconnect(context.Background(), "", wgData.PublicKey, "server_request")
|
||||||
|
}
|
||||||
|
|
||||||
// Close the WireGuard device and TUN
|
// Close the WireGuard device and TUN
|
||||||
closeWgTunnel()
|
closeWgTunnel()
|
||||||
|
|||||||
Reference in New Issue
Block a user