如何将所有传入服务器A的流量转发到服务器B,然后B将其返回给A?

如何将所有传入服务器A的流量转发到服务器B,然后B将其返回给A?

假设我有两台服务器:A 和 B。我在两台服务器上都安装了 WireGuard。

在服务器 A 上,wg0使用 IP 进行路由10.8.0.0/24,在服务器 B 上,wg0使用 IP 进行路由10.7.0.0/24

假设服务器 A 的 WireGuard 正在监听端口 4000,而服务器 B 的 WireGuard 正在监听端口 5000。

我只想将所有传入流量转发到端口 5000 上的服务器 A。还有其他应用程序正在监听其他端口,因此我不想转发它们。

我想要的是:

  1. 客户端连接到 --> 服务器 A
  2. 假设客户端对服务器 A 说为我打开 google.com
  3. 服务器 A 连接到 --> 服务器 B
  4. 假设服务器 A 要求服务器 B 为我打开 google.com
  5. 服务器 B 到达 google.com
  6. 将从 google.com 收到的数据发送到 --> 服务器 A
  7. 服务器A将收到的数据发送给-->客户端

该怎么办?两台服务器上的操作系统都是 Ubuntu。

更新 1

SB 的/etc/wireguard/wg0.conf文件:

[Interface]
Address = 10.7.0.1/24
PrivateKey = SOME_KEY
ListenPort = PORT_NUMBER

SA 的/etc/wireguard/wg0.conf文件:

[Interface]
Address = 10.8.0.1/24
PrivateKey = SOME_KEY
ListenPort = PORT_NUMBER

我连接到 SA 的 VPN,在 cmd 8.8.8.8 上 ping,成功了。连接到 VPN 时通过 SSH 进入 SA,ping 8.8.8.8,成功了。

我对 SB 做了同样的事情并且成功了。

我可以通过 SSH 从 SA 到 SB 以及反之亦然。

SA 上的一个客户端配置文件:

[Interface]
Address = 10.8.0.2/24
DNS = 8.8.8.8
PrivateKey = PRIVATE

[Peer]
PublicKey = PUBLIC
PresharedKey = PRESHARED
AllowedIPs = 0.0.0.0/0, ::/0
Endpoint = IP:PORT
PersistentKeepalive = 25

SB上的一个客户端配置文件:

[Interface]
Address = 10.7.0.2/24
DNS = 8.8.8.8
PrivateKey = PRIVATE

[Peer]
PublicKey = PUBLIC
PresharedKey = PRESHARED
AllowedIPs = 0.0.0.0/0, ::/0
Endpoint = IP:PORT
PersistentKeepalive = 25

相关内容