diff --git a/linux.go b/linux.go index f8996e9..70918d3 100644 --- a/linux.go +++ b/linux.go @@ -66,9 +66,6 @@ func clientsHandleNewtConnectionNative(publicKey, endpoint string) { } func clientsAddProxyTargetNative(pm *proxy.ProxyManager, tunnelIp string) { - if !ready { - return - } // add a udp proxy for localost and the wgService port // TODO: make sure this port is not used in a target if wgServiceNative != nil { diff --git a/main.go b/main.go index 883f6d7..e464f31 100644 --- a/main.go +++ b/main.go @@ -240,6 +240,8 @@ func main() { if err != nil { logger.Fatal("Failed to create client: %v", err) } + endpoint = client.GetConfig().Endpoint // Update endpoint from config + id = client.GetConfig().ID // Update ID from config // output env var values if set logger.Debug("Endpoint: %v", endpoint) @@ -415,17 +417,17 @@ persistent_keepalive_interval=5`, fixKey(privateKey.String()), fixKey(wgData.Pub if len(wgData.Targets.TCP) > 0 { updateTargets(pm, "add", wgData.TunnelIP, "tcp", TargetData{Targets: wgData.Targets.TCP}) // Also update wgnetstack proxy manager - if wgService != nil { - updateTargets(wgService.GetProxyManager(), "add", wgData.TunnelIP, "tcp", TargetData{Targets: wgData.Targets.TCP}) - } + // if wgService != nil { + // updateTargets(wgService.GetProxyManager(), "add", wgData.TunnelIP, "tcp", TargetData{Targets: wgData.Targets.TCP}) + // } } if len(wgData.Targets.UDP) > 0 { updateTargets(pm, "add", wgData.TunnelIP, "udp", TargetData{Targets: wgData.Targets.UDP}) // Also update wgnetstack proxy manager - if wgService != nil { - updateTargets(wgService.GetProxyManager(), "add", wgData.TunnelIP, "udp", TargetData{Targets: wgData.Targets.UDP}) - } + // if wgService != nil { + // updateTargets(wgService.GetProxyManager(), "add", wgData.TunnelIP, "udp", TargetData{Targets: wgData.Targets.UDP}) + // } } clientsAddProxyTarget(pm, wgData.TunnelIP) diff --git a/wg/wg.go b/wg/wg.go index 13aab27..d64c56c 100644 --- a/wg/wg.go +++ b/wg/wg.go @@ -66,6 +66,7 @@ type WireGuardService struct { holePunchEndpoint string token string stopGetConfig func() + interfaceCreated bool } // Add this type definition @@ -242,11 +243,18 @@ func (s *WireGuardService) Close(rm bool) { } func (s *WireGuardService) StartHolepunch(serverPubKey string, endpoint string) { + // if the device is already created dont start a new holepunch + if s.interfaceCreated { + return + } + s.serverPubKey = serverPubKey s.holePunchEndpoint = endpoint logger.Debug("Starting UDP hole punch to %s", s.holePunchEndpoint) + s.stopHolepunch = make(chan struct{}) + // start the UDP holepunch go s.keepSendingUDPHolePunch(s.holePunchEndpoint) } @@ -310,6 +318,7 @@ func (s *WireGuardService) ensureWireguardInterface(wgconfig WgConfig) error { if err != nil { logger.Fatal("Failed to create WireGuard interface: %v", err) } + s.interfaceCreated = true logger.Info("Created WireGuard interface %s\n", s.interfaceName) } else { logger.Fatal("Error checking for WireGuard interface: %v", err) @@ -327,9 +336,16 @@ func (s *WireGuardService) ensureWireguardInterface(wgconfig WgConfig) error { s.Port = uint16(device.ListenPort) logger.Info("WireGuard interface %s already exists with port %d\n", s.interfaceName, s.Port) + s.interfaceCreated = true return nil } + // stop the holepunch its a channel + if s.stopHolepunch != nil { + close(s.stopHolepunch) + s.stopHolepunch = nil + } + logger.Info("Assigning IP address %s to interface %s\n", wgconfig.IpAddress, s.interfaceName) // Assign IP address to the interface err = s.assignIPAddress(wgconfig.IpAddress) diff --git a/wgnetstack/wgnetstack.go b/wgnetstack/wgnetstack.go index 3401e42..0dd7f0f 100644 --- a/wgnetstack/wgnetstack.go +++ b/wgnetstack/wgnetstack.go @@ -347,6 +347,11 @@ func (s *WireGuardService) Close(rm bool) { } func (s *WireGuardService) StartHolepunch(serverPubKey string, endpoint string) { + // if the device is already created dont start a new holepunch + if s.device != nil { + return + } + s.serverPubKey = serverPubKey s.holePunchEndpoint = endpoint