Skip to content

Commit 7e8a0b8

Browse files
author
gengzhanfei
committed
Feature: Edge gateway automatically obtains public IP
边缘节点在无法连接公网环境下,是没办法通过 https://api.ipify.org 这些地址获取公网ip的 而每个边缘的gw写上public太过麻烦,wiregurd与libreswan都支持自动获取建立vpn隧道的endpoint
1 parent a826b83 commit 7e8a0b8

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

pkg/networkengine/vpndriver/wireguard/wireguard.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,15 +275,19 @@ func (w *wireguard) createEdgeConnections(desiredEdgeConns map[string]*vpndriver
275275
} else {
276276
remotePort = newConn.RemoteEndpoint.PublicPort
277277
}
278+
var endpoint *net.UDPAddr
279+
if newConn.RemoteEndpoint.PublicIP != "" {
280+
endpoint = &net.UDPAddr{
281+
IP: net.ParseIP(newConn.RemoteEndpoint.PublicIP),
282+
Port: remotePort,
283+
}
284+
}
278285
peerConfigs = append(peerConfigs, wgtypes.PeerConfig{
279286
PublicKey: *newKey,
280287
Remove: false,
281288
UpdateOnly: false,
282289
PresharedKey: &w.psk,
283-
Endpoint: &net.UDPAddr{
284-
IP: net.ParseIP(newConn.RemoteEndpoint.PublicIP),
285-
Port: remotePort,
286-
},
290+
Endpoint: endpoint,
287291

288292
PersistentKeepaliveInterval: &ka,
289293
ReplaceAllowedIPs: true,

pkg/tunnelengine/tunnelagent.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,16 @@ func (c *TunnelHandler) Handler() error {
8989
// try to update public IP if empty.
9090
gw := &gws.Items[i]
9191
if ep := getTunnelActiveEndpoints(gw); ep != nil {
92-
if ep.PublicIP == "" || c.natTraversal && (ep.NATType == "" || ep.PublicPort == 0 && ep.NATType != utils.NATSymmetric) {
93-
if ep.PublicIP == "" {
94-
if err := c.configGatewayPublicIP(gw); err != nil {
95-
klog.ErrorS(err, "error config gateway public ip", "gateway", klog.KObj(gw))
96-
}
92+
if ep.PublicIP == "" {
93+
if err := c.configGatewayPublicIP(gw); err != nil {
94+
// output only error messages, without skipping
95+
klog.ErrorS(err, "error config gateway public ip", "gateway", klog.KObj(gw))
9796
}
98-
if c.natTraversal && (ep.NATType == "" || ep.PublicPort == 0 && ep.NATType != utils.NATSymmetric) {
99-
if err := c.configGatewayStunInfo(gw); err != nil {
100-
klog.ErrorS(err, "error config gateway stun info", "gateway", klog.KObj(gw))
101-
}
97+
}
98+
if c.natTraversal && (ep.NATType == "" || ep.PublicPort == 0 && ep.NATType != utils.NATSymmetric) {
99+
if err := c.configGatewayStunInfo(gw); err != nil {
100+
klog.ErrorS(err, "error config gateway stun info", "gateway", klog.KObj(gw))
102101
}
103-
continue
104102
}
105103
}
106104
if !c.shouldHandleGateway(gw) {
@@ -212,10 +210,6 @@ func (c *TunnelHandler) shouldHandleGateway(gateway *v1beta1.Gateway) bool {
212210
klog.InfoS("no active endpoint , waiting for sync", "gateway", klog.KObj(gateway))
213211
return false
214212
}
215-
if ep.PublicIP == "" {
216-
klog.InfoS("no public IP for gateway, waiting for sync", "gateway", klog.KObj(gateway))
217-
return false
218-
}
219213
if c.natTraversal {
220214
if ep.NATType == "" {
221215
klog.InfoS("no nat type for gateway, waiting for sync", "gateway", klog.KObj(gateway))

0 commit comments

Comments
 (0)