mirror of
https://github.com/fosrl/olm.git
synced 2026-02-08 05:56:41 +00:00
Reconnect to newt
This commit is contained in:
@@ -488,6 +488,8 @@ func ConfigurePeer(dev *device.Device, siteConfig SiteConfig, privateKey wgtypes
|
|||||||
monitorAddress := strings.Split(siteConfig.ServerIP, "/")[0]
|
monitorAddress := strings.Split(siteConfig.ServerIP, "/")[0]
|
||||||
monitorPeer := fmt.Sprintf("%s:%d", monitorAddress, siteConfig.ServerPort+1) // +1 for the monitor port
|
monitorPeer := fmt.Sprintf("%s:%d", monitorAddress, siteConfig.ServerPort+1) // +1 for the monitor port
|
||||||
|
|
||||||
|
logger.Debug("Setting up peer monitor for site %d at %s", siteConfig.SiteId, monitorPeer)
|
||||||
|
|
||||||
primaryRelay, err := resolveDomain(endpoint) // Using global endpoint variable
|
primaryRelay, err := resolveDomain(endpoint) // Using global endpoint variable
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warn("Failed to resolve primary relay endpoint: %v", err)
|
logger.Warn("Failed to resolve primary relay endpoint: %v", err)
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ func (pm *PeerMonitor) AddPeer(siteID int, endpoint string, wgConfig *WireGuardC
|
|||||||
// Check if we're already monitoring this peer
|
// Check if we're already monitoring this peer
|
||||||
if _, exists := pm.monitors[siteID]; exists {
|
if _, exists := pm.monitors[siteID]; exists {
|
||||||
// Update the endpoint instead of creating a new monitor
|
// Update the endpoint instead of creating a new monitor
|
||||||
pm.RemovePeer(siteID)
|
pm.removePeerUnlocked(siteID)
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := wgtester.NewClient(endpoint)
|
client, err := wgtester.NewClient(endpoint)
|
||||||
@@ -131,11 +131,9 @@ func (pm *PeerMonitor) AddPeer(siteID int, endpoint string, wgConfig *WireGuardC
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemovePeer stops monitoring a peer and removes it from the monitor
|
// removePeerUnlocked stops monitoring a peer and removes it from the monitor
|
||||||
func (pm *PeerMonitor) RemovePeer(siteID int) {
|
// This function assumes the mutex is already held by the caller
|
||||||
pm.mutex.Lock()
|
func (pm *PeerMonitor) removePeerUnlocked(siteID int) {
|
||||||
defer pm.mutex.Unlock()
|
|
||||||
|
|
||||||
client, exists := pm.monitors[siteID]
|
client, exists := pm.monitors[siteID]
|
||||||
if !exists {
|
if !exists {
|
||||||
return
|
return
|
||||||
@@ -147,6 +145,14 @@ func (pm *PeerMonitor) RemovePeer(siteID int) {
|
|||||||
delete(pm.configs, siteID)
|
delete(pm.configs, siteID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemovePeer stops monitoring a peer and removes it from the monitor
|
||||||
|
func (pm *PeerMonitor) RemovePeer(siteID int) {
|
||||||
|
pm.mutex.Lock()
|
||||||
|
defer pm.mutex.Unlock()
|
||||||
|
|
||||||
|
pm.removePeerUnlocked(siteID)
|
||||||
|
}
|
||||||
|
|
||||||
// Start begins monitoring all peers
|
// Start begins monitoring all peers
|
||||||
func (pm *PeerMonitor) Start() {
|
func (pm *PeerMonitor) Start() {
|
||||||
pm.mutex.Lock()
|
pm.mutex.Lock()
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ type Client struct {
|
|||||||
serverAddr string
|
serverAddr string
|
||||||
monitorRunning bool
|
monitorRunning bool
|
||||||
monitorLock sync.Mutex
|
monitorLock sync.Mutex
|
||||||
|
connLock sync.Mutex // Protects connection operations
|
||||||
shutdownCh chan struct{}
|
shutdownCh chan struct{}
|
||||||
packetInterval time.Duration
|
packetInterval time.Duration
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
@@ -71,6 +72,10 @@ func (c *Client) SetMaxAttempts(attempts int) {
|
|||||||
// Close cleans up client resources
|
// Close cleans up client resources
|
||||||
func (c *Client) Close() {
|
func (c *Client) Close() {
|
||||||
c.StopMonitor()
|
c.StopMonitor()
|
||||||
|
|
||||||
|
c.connLock.Lock()
|
||||||
|
defer c.connLock.Unlock()
|
||||||
|
|
||||||
if c.conn != nil {
|
if c.conn != nil {
|
||||||
c.conn.Close()
|
c.conn.Close()
|
||||||
c.conn = nil
|
c.conn = nil
|
||||||
@@ -79,6 +84,9 @@ func (c *Client) Close() {
|
|||||||
|
|
||||||
// ensureConnection makes sure we have an active UDP connection
|
// ensureConnection makes sure we have an active UDP connection
|
||||||
func (c *Client) ensureConnection() error {
|
func (c *Client) ensureConnection() error {
|
||||||
|
c.connLock.Lock()
|
||||||
|
defer c.connLock.Unlock()
|
||||||
|
|
||||||
if c.conn != nil {
|
if c.conn != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -119,9 +127,19 @@ func (c *Client) TestConnection(ctx context.Context) (bool, time.Duration) {
|
|||||||
timestamp := time.Now().UnixNano()
|
timestamp := time.Now().UnixNano()
|
||||||
binary.BigEndian.PutUint64(packet[5:13], uint64(timestamp))
|
binary.BigEndian.PutUint64(packet[5:13], uint64(timestamp))
|
||||||
|
|
||||||
|
// Lock the connection for the entire send/receive operation
|
||||||
|
c.connLock.Lock()
|
||||||
|
|
||||||
|
// Check if connection is still valid after acquiring lock
|
||||||
|
if c.conn == nil {
|
||||||
|
c.connLock.Unlock()
|
||||||
|
return false, 0
|
||||||
|
}
|
||||||
|
|
||||||
logger.Debug("Attempting to send monitor packet to %s", c.serverAddr)
|
logger.Debug("Attempting to send monitor packet to %s", c.serverAddr)
|
||||||
_, err := c.conn.Write(packet)
|
_, err := c.conn.Write(packet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
c.connLock.Unlock()
|
||||||
logger.Info("Error sending packet: %v", err)
|
logger.Info("Error sending packet: %v", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -133,6 +151,8 @@ func (c *Client) TestConnection(ctx context.Context) (bool, time.Duration) {
|
|||||||
// Wait for response
|
// Wait for response
|
||||||
responseBuffer := make([]byte, packetSize)
|
responseBuffer := make([]byte, packetSize)
|
||||||
n, err := c.conn.Read(responseBuffer)
|
n, err := c.conn.Read(responseBuffer)
|
||||||
|
c.connLock.Unlock()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
||||||
// Timeout, try next attempt
|
// Timeout, try next attempt
|
||||||
|
|||||||
Reference in New Issue
Block a user