Fix bandwidth check

This commit is contained in:
Owen Schwartz
2024-10-03 21:49:10 -04:00
parent 1576856abf
commit b524b97114

72
main.go
View File

@@ -10,6 +10,7 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"sync"
"time" "time"
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
@@ -20,6 +21,8 @@ import (
var ( var (
interfaceName = "wg0" interfaceName = "wg0"
listenAddr = ":3002" listenAddr = ":3002"
lastReadings = make(map[string]PeerReading)
mu sync.Mutex
) )
type WgConfig struct { type WgConfig struct {
@@ -40,6 +43,12 @@ type PeerBandwidth struct {
BytesOut float64 `json:"bytesOut"` BytesOut float64 `json:"bytesOut"`
} }
type PeerReading struct {
BytesReceived int64
BytesTransmitted int64
LastChecked time.Time
}
var ( var (
wgClient *wgctrl.Client wgClient *wgctrl.Client
) )
@@ -399,53 +408,62 @@ func periodicBandwidthCheck(endpoint string) {
} }
} }
func calculatePeerBandwidth() ([]PeerBandwidth, error) { //TODO: fix this to actually only report the change in bandwidth from the last query func calculatePeerBandwidth() ([]PeerBandwidth, error) {
device, err := wgClient.Device(interfaceName) device, err := wgClient.Device(interfaceName)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get device: %v", err) return nil, fmt.Errorf("failed to get device: %v", err)
} }
peerBandwidths := []PeerBandwidth{} peerBandwidths := []PeerBandwidth{}
now := time.Now()
mu.Lock()
defer mu.Unlock()
for _, peer := range device.Peers { for _, peer := range device.Peers {
// Store initial values publicKey := peer.PublicKey.String()
initialBytesReceived := peer.ReceiveBytes
initialBytesSent := peer.TransmitBytes
// Wait for a short period to measure change lastReading, exists := lastReadings[publicKey]
time.Sleep(5 * time.Second) currentReading := PeerReading{
BytesReceived: peer.ReceiveBytes,
// Get updated device info BytesTransmitted: peer.TransmitBytes,
updatedDevice, err := wgClient.Device(interfaceName) LastChecked: now,
if err != nil {
return nil, fmt.Errorf("failed to get updated device: %v", err)
} }
var updatedPeer *wgtypes.Peer var bytesInDiff, bytesOutDiff float64
for _, p := range updatedDevice.Peers {
if p.PublicKey == peer.PublicKey { if exists {
updatedPeer = &p timeDiff := now.Sub(lastReading.LastChecked).Seconds()
break bytesInDiff = float64(currentReading.BytesReceived-lastReading.BytesReceived) / timeDiff
} bytesOutDiff = float64(currentReading.BytesTransmitted-lastReading.BytesTransmitted) / timeDiff
} }
if updatedPeer == nil { // Convert to MB/s
continue
}
// Calculate change in bytes
bytesInDiff := float64(updatedPeer.ReceiveBytes - initialBytesReceived)
bytesOutDiff := float64(updatedPeer.TransmitBytes - initialBytesSent)
// Convert to MB
bytesInMB := bytesInDiff / (1024 * 1024) bytesInMB := bytesInDiff / (1024 * 1024)
bytesOutMB := bytesOutDiff / (1024 * 1024) bytesOutMB := bytesOutDiff / (1024 * 1024)
peerBandwidths = append(peerBandwidths, PeerBandwidth{ peerBandwidths = append(peerBandwidths, PeerBandwidth{
PublicKey: peer.PublicKey.String(), PublicKey: publicKey,
BytesIn: bytesInMB, BytesIn: bytesInMB,
BytesOut: bytesOutMB, BytesOut: bytesOutMB,
}) })
// Update the last reading
lastReadings[publicKey] = currentReading
}
// Clean up old peers
for publicKey := range lastReadings {
found := false
for _, peer := range device.Peers {
if peer.PublicKey.String() == publicKey {
found = true
break
}
}
if !found {
delete(lastReadings, publicKey)
}
} }
return peerBandwidths, nil return peerBandwidths, nil