Clean up operation

This commit is contained in:
Owen
2025-07-27 14:50:11 -07:00
parent 88f1335cff
commit ae7e2a1055
4 changed files with 29 additions and 9 deletions

View File

@@ -66,9 +66,6 @@ func clientsHandleNewtConnectionNative(publicKey, endpoint string) {
} }
func clientsAddProxyTargetNative(pm *proxy.ProxyManager, tunnelIp string) { func clientsAddProxyTargetNative(pm *proxy.ProxyManager, tunnelIp string) {
if !ready {
return
}
// add a udp proxy for localost and the wgService port // add a udp proxy for localost and the wgService port
// TODO: make sure this port is not used in a target // TODO: make sure this port is not used in a target
if wgServiceNative != nil { if wgServiceNative != nil {

14
main.go
View File

@@ -240,6 +240,8 @@ func main() {
if err != nil { if err != nil {
logger.Fatal("Failed to create client: %v", err) 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 // output env var values if set
logger.Debug("Endpoint: %v", endpoint) 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 { 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})
// Also update wgnetstack proxy manager // Also update wgnetstack proxy manager
if wgService != nil { // if wgService != nil {
updateTargets(wgService.GetProxyManager(), "add", wgData.TunnelIP, "tcp", TargetData{Targets: wgData.Targets.TCP}) // updateTargets(wgService.GetProxyManager(), "add", wgData.TunnelIP, "tcp", TargetData{Targets: wgData.Targets.TCP})
} // }
} }
if len(wgData.Targets.UDP) > 0 { if len(wgData.Targets.UDP) > 0 {
updateTargets(pm, "add", wgData.TunnelIP, "udp", TargetData{Targets: wgData.Targets.UDP}) updateTargets(pm, "add", wgData.TunnelIP, "udp", TargetData{Targets: wgData.Targets.UDP})
// Also update wgnetstack proxy manager // Also update wgnetstack proxy manager
if wgService != nil { // if wgService != nil {
updateTargets(wgService.GetProxyManager(), "add", wgData.TunnelIP, "udp", TargetData{Targets: wgData.Targets.UDP}) // updateTargets(wgService.GetProxyManager(), "add", wgData.TunnelIP, "udp", TargetData{Targets: wgData.Targets.UDP})
} // }
} }
clientsAddProxyTarget(pm, wgData.TunnelIP) clientsAddProxyTarget(pm, wgData.TunnelIP)

View File

@@ -66,6 +66,7 @@ type WireGuardService struct {
holePunchEndpoint string holePunchEndpoint string
token string token string
stopGetConfig func() stopGetConfig func()
interfaceCreated bool
} }
// Add this type definition // Add this type definition
@@ -242,11 +243,18 @@ func (s *WireGuardService) Close(rm bool) {
} }
func (s *WireGuardService) StartHolepunch(serverPubKey string, endpoint string) { 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.serverPubKey = serverPubKey
s.holePunchEndpoint = endpoint s.holePunchEndpoint = endpoint
logger.Debug("Starting UDP hole punch to %s", s.holePunchEndpoint) logger.Debug("Starting UDP hole punch to %s", s.holePunchEndpoint)
s.stopHolepunch = make(chan struct{})
// start the UDP holepunch // start the UDP holepunch
go s.keepSendingUDPHolePunch(s.holePunchEndpoint) go s.keepSendingUDPHolePunch(s.holePunchEndpoint)
} }
@@ -310,6 +318,7 @@ func (s *WireGuardService) ensureWireguardInterface(wgconfig WgConfig) error {
if err != nil { if err != nil {
logger.Fatal("Failed to create WireGuard interface: %v", err) logger.Fatal("Failed to create WireGuard interface: %v", err)
} }
s.interfaceCreated = true
logger.Info("Created WireGuard interface %s\n", s.interfaceName) logger.Info("Created WireGuard interface %s\n", s.interfaceName)
} else { } else {
logger.Fatal("Error checking for WireGuard interface: %v", err) 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) s.Port = uint16(device.ListenPort)
logger.Info("WireGuard interface %s already exists with port %d\n", s.interfaceName, s.Port) logger.Info("WireGuard interface %s already exists with port %d\n", s.interfaceName, s.Port)
s.interfaceCreated = true
return nil 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) logger.Info("Assigning IP address %s to interface %s\n", wgconfig.IpAddress, s.interfaceName)
// Assign IP address to the interface // Assign IP address to the interface
err = s.assignIPAddress(wgconfig.IpAddress) err = s.assignIPAddress(wgconfig.IpAddress)

View File

@@ -347,6 +347,11 @@ func (s *WireGuardService) Close(rm bool) {
} }
func (s *WireGuardService) StartHolepunch(serverPubKey string, endpoint string) { 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.serverPubKey = serverPubKey
s.holePunchEndpoint = endpoint s.holePunchEndpoint = endpoint