mirror of
https://github.com/fosrl/olm.git
synced 2026-02-08 05:56:41 +00:00
@@ -13,7 +13,6 @@ import (
|
|||||||
"github.com/fosrl/newt/util"
|
"github.com/fosrl/newt/util"
|
||||||
middleDevice "github.com/fosrl/olm/device"
|
middleDevice "github.com/fosrl/olm/device"
|
||||||
"github.com/fosrl/olm/websocket"
|
"github.com/fosrl/olm/websocket"
|
||||||
"github.com/fosrl/olm/wgtester"
|
|
||||||
"golang.zx2c4.com/wireguard/device"
|
"golang.zx2c4.com/wireguard/device"
|
||||||
"gvisor.dev/gvisor/pkg/buffer"
|
"gvisor.dev/gvisor/pkg/buffer"
|
||||||
"gvisor.dev/gvisor/pkg/tcpip"
|
"gvisor.dev/gvisor/pkg/tcpip"
|
||||||
@@ -40,7 +39,7 @@ type WireGuardConfig struct {
|
|||||||
|
|
||||||
// PeerMonitor handles monitoring the connection status to multiple WireGuard peers
|
// PeerMonitor handles monitoring the connection status to multiple WireGuard peers
|
||||||
type PeerMonitor struct {
|
type PeerMonitor struct {
|
||||||
monitors map[int]*wgtester.Client
|
monitors map[int]*Client
|
||||||
configs map[int]*WireGuardConfig
|
configs map[int]*WireGuardConfig
|
||||||
callback PeerMonitorCallback
|
callback PeerMonitorCallback
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
@@ -69,7 +68,7 @@ type PeerMonitor struct {
|
|||||||
func NewPeerMonitor(callback PeerMonitorCallback, privateKey string, wsClient *websocket.Client, device *device.Device, handleRelaySwitch bool, middleDev *middleDevice.MiddleDevice, localIP string) *PeerMonitor {
|
func NewPeerMonitor(callback PeerMonitorCallback, privateKey string, wsClient *websocket.Client, device *device.Device, handleRelaySwitch bool, middleDev *middleDevice.MiddleDevice, localIP string) *PeerMonitor {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
pm := &PeerMonitor{
|
pm := &PeerMonitor{
|
||||||
monitors: make(map[int]*wgtester.Client),
|
monitors: make(map[int]*Client),
|
||||||
configs: make(map[int]*WireGuardConfig),
|
configs: make(map[int]*WireGuardConfig),
|
||||||
callback: callback,
|
callback: callback,
|
||||||
interval: 1 * time.Second, // Default check interval
|
interval: 1 * time.Second, // Default check interval
|
||||||
@@ -142,7 +141,7 @@ func (pm *PeerMonitor) AddPeer(siteID int, endpoint string, wgConfig *WireGuardC
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Use our custom dialer that uses netstack
|
// Use our custom dialer that uses netstack
|
||||||
client, err := wgtester.NewClient(endpoint, pm.dial)
|
client, err := NewClient(endpoint, pm.dial)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -155,7 +154,7 @@ func (pm *PeerMonitor) AddPeer(siteID int, endpoint string, wgConfig *WireGuardC
|
|||||||
pm.configs[siteID] = wgConfig
|
pm.configs[siteID] = wgConfig
|
||||||
|
|
||||||
if pm.running {
|
if pm.running {
|
||||||
if err := client.StartMonitor(func(status wgtester.ConnectionStatus) {
|
if err := client.StartMonitor(func(status ConnectionStatus) {
|
||||||
pm.handleConnectionStatusChange(siteID, status)
|
pm.handleConnectionStatusChange(siteID, status)
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -201,7 +200,7 @@ func (pm *PeerMonitor) Start() {
|
|||||||
// Start monitoring all peers
|
// Start monitoring all peers
|
||||||
for siteID, client := range pm.monitors {
|
for siteID, client := range pm.monitors {
|
||||||
siteIDCopy := siteID // Create a copy for the closure
|
siteIDCopy := siteID // Create a copy for the closure
|
||||||
err := client.StartMonitor(func(status wgtester.ConnectionStatus) {
|
err := client.StartMonitor(func(status ConnectionStatus) {
|
||||||
pm.handleConnectionStatusChange(siteIDCopy, status)
|
pm.handleConnectionStatusChange(siteIDCopy, status)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -213,7 +212,7 @@ func (pm *PeerMonitor) Start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// handleConnectionStatusChange is called when a peer's connection status changes
|
// handleConnectionStatusChange is called when a peer's connection status changes
|
||||||
func (pm *PeerMonitor) handleConnectionStatusChange(siteID int, status wgtester.ConnectionStatus) {
|
func (pm *PeerMonitor) handleConnectionStatusChange(siteID int, status ConnectionStatus) {
|
||||||
// Call the user-provided callback first
|
// Call the user-provided callback first
|
||||||
if pm.callback != nil {
|
if pm.callback != nil {
|
||||||
pm.callback(siteID, status.Connected, status.RTT)
|
pm.callback(siteID, status.Connected, status.RTT)
|
||||||
@@ -336,7 +335,7 @@ func (pm *PeerMonitor) TestAllPeers() map[int]struct {
|
|||||||
RTT time.Duration
|
RTT time.Duration
|
||||||
} {
|
} {
|
||||||
pm.mutex.Lock()
|
pm.mutex.Lock()
|
||||||
peers := make(map[int]*wgtester.Client, len(pm.monitors))
|
peers := make(map[int]*Client, len(pm.monitors))
|
||||||
for siteID, client := range pm.monitors {
|
for siteID, client := range pm.monitors {
|
||||||
peers[siteID] = client
|
peers[siteID] = client
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package wgtester
|
package peermonitor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
Reference in New Issue
Block a user