diff --git a/clients.go b/clients.go index 94586a2..e679432 100644 --- a/clients.go +++ b/clients.go @@ -63,7 +63,7 @@ func closeClients() { } } -func clientsHandleNewtConnection(publicKey string, endpoint string) { +func clientsHandleNewtConnection(publicKey string, endpoint string, relayPort uint16) { if !ready { return } @@ -77,7 +77,7 @@ func clientsHandleNewtConnection(publicKey string, endpoint string) { endpoint = strings.Join(parts[:len(parts)-1], ":") if wgService != nil { - wgService.StartHolepunch(publicKey, endpoint) + wgService.StartHolepunch(publicKey, endpoint, relayPort) } } diff --git a/clients/clients.go b/clients/clients.go index 9b17d07..17a5398 100644 --- a/clients/clients.go +++ b/clients/clients.go @@ -268,16 +268,21 @@ func (s *WireGuardService) SetOnNetstackClose(callback func()) { } // StartHolepunch starts hole punching to a specific endpoint -func (s *WireGuardService) StartHolepunch(publicKey string, endpoint string) { +func (s *WireGuardService) StartHolepunch(publicKey string, endpoint string, relayPort uint16) { if s.holePunchManager == nil { logger.Warn("Hole punch manager not initialized") return } + if relayPort == 0 { + relayPort = 21820 + } + // Convert websocket.ExitNode to holepunch.ExitNode hpExitNodes := []holepunch.ExitNode{ { Endpoint: endpoint, + RelayPort: relayPort, PublicKey: publicKey, }, } diff --git a/holepunch/holepunch.go b/holepunch/holepunch.go index b6e0a6b..8ee8767 100644 --- a/holepunch/holepunch.go +++ b/holepunch/holepunch.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "net" + "strconv" "sync" "time" @@ -19,6 +20,7 @@ import ( // ExitNode represents a WireGuard exit node for hole punching type ExitNode struct { Endpoint string `json:"endpoint"` + RelayPort uint16 `json:"relayPort"` PublicKey string `json:"publicKey"` } @@ -202,7 +204,7 @@ func (m *Manager) TriggerHolePunch() error { continue } - serverAddr := net.JoinHostPort(host, "21820") + serverAddr := net.JoinHostPort(host, strconv.Itoa(int(exitNode.RelayPort))) remoteAddr, err := net.ResolveUDPAddr("udp", serverAddr) if err != nil { logger.Error("Failed to resolve UDP address %s: %v", serverAddr, err) @@ -313,7 +315,7 @@ func (m *Manager) runMultipleExitNodes() { continue } - serverAddr := net.JoinHostPort(host, "21820") + serverAddr := net.JoinHostPort(host, strconv.Itoa(int(exitNode.RelayPort))) remoteAddr, err := net.ResolveUDPAddr("udp", serverAddr) if err != nil { logger.Error("Failed to resolve UDP address %s: %v", serverAddr, err) diff --git a/main.go b/main.go index d579357..9fdec74 100644 --- a/main.go +++ b/main.go @@ -37,6 +37,7 @@ import ( type WgData struct { Endpoint string `json:"endpoint"` + RelayPort uint16 `json:"relayPort"` PublicKey string `json:"publicKey"` ServerIP string `json:"serverIP"` TunnelIP string `json:"tunnelIP"` @@ -691,7 +692,12 @@ func runNewtMain(ctx context.Context) { return } - clientsHandleNewtConnection(wgData.PublicKey, endpoint) + relayPort := wgData.RelayPort + if relayPort == 0 { + relayPort = 21820 + } + + clientsHandleNewtConnection(wgData.PublicKey, endpoint, relayPort) // Configure WireGuard config := fmt.Sprintf(`private_key=%s