From 823982aa7f75edcfea799fbe5e1fe81f2af80191 Mon Sep 17 00:00:00 2001 From: gengzhanfei Date: Mon, 1 Apr 2024 17:41:35 +0800 Subject: [PATCH] Feature: Edge gateway automatically obtains public IP MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 边缘节点在无法连接公网环境下,是没办法通过 https://api.ipify.org 这些地址获取公网ip的 而每个边缘的gw写上public太过麻烦,wiregurd与libreswan都支持自动获取建立vpn隧道的endpoint --- .../vpndriver/wireguard/wireguard.go | 12 ++++++---- pkg/tunnelengine/tunnelagent.go | 22 +++++++------------ 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/pkg/networkengine/vpndriver/wireguard/wireguard.go b/pkg/networkengine/vpndriver/wireguard/wireguard.go index e17b24c..08ca70f 100644 --- a/pkg/networkengine/vpndriver/wireguard/wireguard.go +++ b/pkg/networkengine/vpndriver/wireguard/wireguard.go @@ -275,15 +275,19 @@ func (w *wireguard) createEdgeConnections(desiredEdgeConns map[string]*vpndriver } else { remotePort = newConn.RemoteEndpoint.PublicPort } + var endpoint *net.UDPAddr + if newConn.RemoteEndpoint.PublicIP != "" { + endpoint = &net.UDPAddr{ + IP: net.ParseIP(newConn.RemoteEndpoint.PublicIP), + Port: remotePort, + } + } peerConfigs = append(peerConfigs, wgtypes.PeerConfig{ PublicKey: *newKey, Remove: false, UpdateOnly: false, PresharedKey: &w.psk, - Endpoint: &net.UDPAddr{ - IP: net.ParseIP(newConn.RemoteEndpoint.PublicIP), - Port: remotePort, - }, + Endpoint: endpoint, PersistentKeepaliveInterval: &ka, ReplaceAllowedIPs: true, diff --git a/pkg/tunnelengine/tunnelagent.go b/pkg/tunnelengine/tunnelagent.go index c99f9af..1d30a79 100644 --- a/pkg/tunnelengine/tunnelagent.go +++ b/pkg/tunnelengine/tunnelagent.go @@ -89,18 +89,16 @@ func (c *TunnelHandler) Handler() error { // try to update public IP if empty. gw := &gws.Items[i] if ep := getTunnelActiveEndpoints(gw); ep != nil { - if ep.PublicIP == "" || c.natTraversal && (ep.NATType == "" || ep.PublicPort == 0 && ep.NATType != utils.NATSymmetric) { - if ep.PublicIP == "" { - if err := c.configGatewayPublicIP(gw); err != nil { - klog.ErrorS(err, "error config gateway public ip", "gateway", klog.KObj(gw)) - } + if ep.PublicIP == "" { + if err := c.configGatewayPublicIP(gw); err != nil { + // output only error messages, without skipping + klog.ErrorS(err, "error config gateway public ip", "gateway", klog.KObj(gw)) } - if c.natTraversal && (ep.NATType == "" || ep.PublicPort == 0 && ep.NATType != utils.NATSymmetric) { - if err := c.configGatewayStunInfo(gw); err != nil { - klog.ErrorS(err, "error config gateway stun info", "gateway", klog.KObj(gw)) - } + } + if c.natTraversal && (ep.NATType == "" || ep.PublicPort == 0 && ep.NATType != utils.NATSymmetric) { + if err := c.configGatewayStunInfo(gw); err != nil { + klog.ErrorS(err, "error config gateway stun info", "gateway", klog.KObj(gw)) } - continue } } if !c.shouldHandleGateway(gw) { @@ -212,10 +210,6 @@ func (c *TunnelHandler) shouldHandleGateway(gateway *v1beta1.Gateway) bool { klog.InfoS("no active endpoint , waiting for sync", "gateway", klog.KObj(gateway)) return false } - if ep.PublicIP == "" { - klog.InfoS("no public IP for gateway, waiting for sync", "gateway", klog.KObj(gateway)) - return false - } if c.natTraversal { if ep.NATType == "" { klog.InfoS("no nat type for gateway, waiting for sync", "gateway", klog.KObj(gateway))