iptables 1:1 NAT,使用反射将第二个公共 IP 映射到本地 IP

iptables 1:1 NAT,使用反射将第二个公共 IP 映射到本地 IP

我想使用 iptables 设置 1:1 NAT + 反射,这样就可以将到达该公共 IP 的所有内容转发到我的内部 VM。

背景信息:我有一台专用服务器,运行 proxmox,网络配置基于 NAT(见下文)。一个 NIC 一个公共主 IP(94.x.x.A)和一个额外订购的第二个公共 IP(94.x.x.B)在同一个接口上。我还有一个虚拟化的 opnsense/pfsense VM,它使用 vmbr10 和 vmbr11 作为 WAN 和 LAN。剧透,我已经尝试通过 opnsense/pfsense 进行 1:1 Nat。我真的尝试了这里的所有方法,但无法让大蓝色按钮工作。我想,好吧,也许所有通过 vmbr10(WAN)到 vmbr11(LAN)的 nat 以及通过 opnsense/pfsense 本身会给 BBB 带来很多麻烦。因此,这个想法是让路由更容易。

auto enp0s31f6
iface enp0s31f6 inet static
  address 94.x.x.A
  netmask 255.255.255.192
  gateway 94.x.x.x
  mtu 1500
  post-up echo 1 > /proc/sys/net/ipv4/ip_forward
  post-up echo 1 > /proc/sys/net/ipv6/conf/all/forwarding
  up ip addr add 94.x.x.B/26 dev $IFACE label $IFACE:0
  down ip addr del 94.x.x.B/26 dev $IFACE label $IFACE:0
  post-up iptables -t nat -A PREROUTING -i enp0s31f6 -p tcp -d 94.x.x.A -m multiport ! --dport 22,8006,179 -j DNAT --to 10.10.10.2
  post-up iptables -t nat -A PREROUTING -i enp0s31f6 -p udp -d 94.x.x.A -m multiport ! --dport 5405:5412,4789 -j DNAT --to 10.10.10.2

现在我创建了另一个 Linux 桥 (vmbr0),并将这个带有 Big Blue Button 的 VM 移出 vmbr11,不再通过 opnsense/pfsense,我真的很想将所有内容转发给它。Iptables 绝对不应该关心任何事情,盲目地将到达第二个公共 IP 的所有内容转发94.x.x.B到我的本地 BBB VM。因此,VM 本身直接连接到互联网。它基本上拥有该 IP。

我尝试使用 1:1 NAT 和反射来实现这一点,但无法成功。我尝试了整个 www 中能找到的所有 iptables 命令。根本无法成功,有两个命令总是失败,无论我做什么。它们是在该 VM 上执行的:

docker exec -it bbb-docker-greenlight-1 bundle exec rake conf:check

Checking environment: Passed
Checking Connection: Failed
Error connecting to BigBlueButton server - Failed to open TCP connection to bbb.my-domain.com:443 (Connection refused - connect(2) for "bbb.domain-domain.com" port 443)

curl --trace-ascii - -k https://94.x.x.B:443/bigbluebutton/api立即显示:

== Info:   Trying 94.x.x.B:443...
== Info: connect to 94.x.x.B port 443 failed: Connection refused
== Info: Failed to connect to 94.x.x.B port 443: Connection refused
== Info: Closing connection 0
curl: (7) Failed to connect to 94.x.x.B port 443: Connection refused

因此,我感觉当 VM 本身尝试从本地 vmbr0 内部连接到该公共 IP 时,反射部分不起作用,出了问题。 curl 命令立即失败,就像没有任何东西监听 443 一样。但是 nginx docker 容器正在运行,当连接到我的 VPN 时,在浏览器中输入本地 VM IP 时,我可以访问 bbb 服务的主页。

有谁对 iptable 有深入了解,可以帮我将所有内容转发到那一个虚拟机并设置反射吗?我认为拆分 DNS 不起作用,因为似乎在整个 BBB docker 构造中,他们使用的是公共 IP 而不是域名。




命令集合,我尝试了大部分整个块、注释掉的块、尝试了新块等,以及每个块中的单个命令。这个主题已经让我做噩梦了,所以请有人帮助我。谢谢

auto vmbr0
iface vmbr0 inet static
        address  10.2.1.1/30
        bridge-ports none
        bridge-stp off
        bridge-fd 0

        post-up   echo 1 > /proc/sys/net/ipv4/ip_forward

        post-up iptables -t nat -A PREROUTING -i enp0s31f6 -d 94.x.x.B -j DNAT --to 10.2.1.2
        post-down iptables -t nat -D PREROUTING -i enp0s31f6 -d 94.x.x.B -j DNAT --to 10.2.1.2

        post-up   iptables -t nat -A POSTROUTING -o enp0s31f6 -s 10.2.1.2 -j SNAT --to-source 94.x.x.B
        post-down iptables -t nat -D POSTROUTING -o enp0s31f6 -s 10.2.1.2 -j SNAT --to-source 94.x.x.B
        #post-up   iptables -t nat -A PREROUTING -i enp0s31f6 -d 94.x.x.B -j DNAT --to-destination 10.2.1.2
        #post-down iptables -t nat -D PREROUTING -i enp0s31f6 -d 94.x.x.B -j DNAT --to-destination 10.2.1.2
        #post-up   iptables -t nat -A PREROUTING -i vmbr0 -s 10.2.1.0/30 -d 94.x.x.B -j DNAT --to-destination 10.2.1.2
        #post-down iptables -t nat -D PREROUTING -i vmbr0 -s 10.2.1.0/30 -d 94.x.x.B -j DNAT --to-destination 10.2.1.2
        #post-up   iptables -t nat -A POSTROUTING -o vmbr0 -s 10.2.1.0/30 -d 10.2.1.2 -j SNAT --to-source 10.2.1.1
        #post-down iptables -t nat -D POSTROUTING -o vmbr0 -s 10.2.1.0/30 -d 10.2.1.2 -j SNAT --to-source 10.2.1.1

        #post-up   iptables -t nat -A POSTROUTING -o enp0s31f6 -s 10.2.1.2 -j SNAT --to-source 94.x.x.B
        #post-down iptables -t nat -D POSTROUTING -o enp0s31f6 -s 10.2.1.2 -j SNAT --to-source 94.x.x.B
        #post-up   iptables -t nat -A PREROUTING -i enp0s31f6 -d 94.x.x.B -j DNAT --to-destination 10.2.1.2
        #post-down iptables -t nat -D PREROUTING -i enp0s31f6 -d 94.x.x.B -j DNAT --to-destination 10.2.1.2
        post-up   iptables -A FORWARD -s 94.x.x.B -j ACCEPT
        post-down iptables -D FORWARD -s 94.x.x.B -j ACCEPT
        post-up   iptables -A FORWARD -d 10.2.1.2 -j ACCEPT
        post-down iptables -D FORWARD -d 10.2.1.2 -j ACCEPT

        #post-up   iptables -P FORWARD ACCEPT
        #post-up   iptables -P OUTPUT ACCEPT
        #post-up   iptables -P INPUT ACCEPT
        #post-up   iptables -t nat -A POSTROUTING -o enp0s31f6 -s 10.2.1.0/30 ! -d 10.2.1.0/30 -j SNAT --to-source 94.x.x.B
        #post-down iptables -t nat -D POSTROUTING -o enp0s31f6 -s 10.2.1.0/30 ! -d 10.2.1.0/30 -j SNAT --to-source 94.x.x.B
        #post-up   iptables -A FORWARD -i enp0s31f6 -o vmbr0 --syn -m conntrack --ctstate NEW -j ACCEPT
        #post-down iptables -D FORWARD -i enp0s31f6 -o vmbr0 --syn -m conntrack --ctstate NEW -j ACCEPT
        #post-up   iptables -A FORWARD -i enp0s31f6 -o vmbr0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
        #post-down iptables -D FORWARD -i enp0s31f6 -o vmbr0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
        #post-up   iptables -A FORWARD -i vmbr0 -o enp0s31f6 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
        #post-down iptables -D FORWARD -i vmbr0 -o enp0s31f6 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
        #post-up   iptables -A FORWARD -d 10.2.1.2/32 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
        #post-down iptables -D FORWARD -d 10.2.1.2/32 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
        #post-up   iptables -A FORWARD -s 10.2.1.0/30 -j ACCEPT
        #post-down iptables -D FORWARD -s 10.2.1.0/30 -j ACCEPT
        #post-up   iptables -A FORWARD -d 10.2.1.0/30 -j ACCEPT
        #post-down iptables -D FORWARD -d 10.2.1.0/30 -j ACCEPT
        #post-up   iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT
        #post-down iptables -D INPUT -m state --state ESTABLISHED -j ACCEPT
        #post-up   iptables -t nat -A PREROUTING -i enp0s31f6 -d 94.x.x.B -j DNAT --to-destination 10.2.1.2
        #post-down iptables -t nat -D PREROUTING -i enp0s31f6 -d 94.x.x.B -j DNAT --to-destination 10.2.1.2
        #post-up   iptables -t nat -A PREROUTING -d 94.x.x.B -j DNAT --to-destination 10.2.1.2
        #post-down iptables -t nat -D PREROUTING -d 94.x.x.B -j DNAT --to-destination 10.2.1.2
        #post-up   iptables -t nat -A POSTROUTING -o vmbr0 -d 10.2.1.2 -j SNAT --to-source 10.2.1.1
        #post-down iptables -t nat -D POSTROUTING -o vmbr0 -d 10.2.1.2 -j SNAT --to-source 10.2.1.1

        #post-up   iptables -t nat -A PREROUTING -d 94.x.x.B -j DNAT --to 10.2.1.2
        #post-down iptables -t nat -D PREROUTING -d 94.x.x.B -j DNAT --to 10.2.1.2
        #post-up   iptables -t nat -A POSTROUTING -s 10.2.1.2 -j SNAT --to 94.x.x.B
        #post-down iptables -t nat -D POSTROUTING -s 10.2.1.2 -j SNAT --to 94.x.x.B

        #post-up   iptables -t nat -A POSTROUTING -s 10.2.1.0/30 -d 10.2.1.2 -j LOG --log-level info
        #post-up   iptables -t nat -A POSTROUTING -s 10.2.1.0/30 -d 10.2.1.2 -j SNAT --to-source 10.2.1.1
        #post-down iptables -t nat -D POSTROUTING -s 10.2.1.0/30 -d 10.2.1.2 -j SNAT --to-source 10.2.1.1
        #post-up   iptables -t nat -A POSTROUTING -o enp0s31f6 -s 10.2.1.0/30 -j LOG --log-level info
        #post-up   iptables -t nat -A POSTROUTING -o enp0s31f6 -s 10.2.1.0/30 -j SNAT --to-source 94.x.x.B
        #post-down iptables -t nat -D POSTROUTING -o enp0s31f6 -s 10.2.1.0/30 -j SNAT --to-source 94.x.x.B
        #post-up   iptables -t nat -A POSTROUTING -s 10.2.1.0/30 -j LOG --log-level info
        #post-up   iptables -t nat -A POSTROUTING -s 10.2.1.0/30 -j MASQUERADE
        #post-down iptables -t nat -D POSTROUTING -s 10.2.1.0/30 -j MASQUERADE
        post-up    iptables -t raw -I PREROUTING -i fwbr+ -j CT --zone 1
        post-down  iptables -t raw -D PREROUTING  -i fwbr+ -j CT --zone 1
        #post-up    iptables -I FORWARD -s 94.x.x.B -j ACCEPT
        #post-down  iptables -D FORWARD -s 94.x.x.B -j ACCEPT
        #post-up    iptables -I FORWARD -d 10.2.1.0/30 -j ACCEPT
        #post-down  iptables -D FORWARD -d 10.2.1.0/30 -j ACCEPT

答案1

无法让 BigBlueButton 与 1:1 NAT + Reflection 配合使用,所以我放弃了这种方法。我仍然找到了一个解决方案,使用 @djdomi 提到的方法:“你为什么不直接分配它并路由它?”我在这里记录了它:为虚拟机分配了第二个公共 IP,但外部无法访问

答案2

因为您使用的 Hetzner 与我使用的 Netcup 有类似的要求。

由于你没有发布完整的配置,我将为你完成

#default interaces
auto lo
iface lo inet loopback
iface lo inet6 loopback

#ens3 could be other named 
auto ens3
#8.8.8.8 = pub ip, 1.1.1.1 = gateway IP and PtP
iface ens3 inet static
    address 8.8.8.8/32
    gateway 1.1.1.1
    pointopoint 1.1.1.1
    #Init all Pre-Requirements for vmbr0
    post-up echo 1 > /proc/sys/net/ipv4/conf/ens3/proxy_arp
    post-up echo 1 > /proc/sys/net/ipv6/conf/all/forwarding
    #Restore persistent the IPTables
    pre-up iptables-restore < /etc/iptables.rules
    #dump that for reboot
    pre-down iptables-save > /etc/iptables.rules
    


auto vmbr0 
iface vmbr0 inet static
    address     10.0.0.1/8
        bridge_ports    none
        bridge_stp  off
        bridge_fd   0
        post-up     echo 1 > /proc/sys/net/ipv4/ip_forward
    #nating enable on vmbr0 up and wise versa when disabled
    post-up         iptables -t nat -A POSTROUTING -s '10.0.0.0/8' -o vmbr0 -j MASQUERADE
    post-down       iptables -t nat -D POSTROUTING -s '10.0.0.0/8' -o vmbr0 -j MASQUERADE
    #Route 2 ips direct over vmbr0 to the VM's where 2.2.2.* is the public ip
        post-up         route add 2.2.2.2 dev vmbr0
        post-up         route add 2.2.2.3 dev vmbr0
    #Remove that for reboot, and cleanup
        post-down       route del 2.2.2.2 dev vmbr0
        post-down       route del 2.2.2.3 dev vmbr0

基本上,此配置的作用是使用主机的主 MAC,而不是将所有 MAC 发布到网络。

为什么这对某些 Hoster 来说很重要?

由于安全站点或 ARP 表的限制,大多数用户拒绝访问网络,除非 MAC 来自已知服务器。此外,可能会发生这种情况,特别是对于 Proxmox,MAC 地址可能存在多次,特别是在数据中心的情况下,成千上万的客户做同样的事情。

这个配置还有什么其他作用?

简短回答:

使用 ENS3 Mac 并使用 NAT 代理其余 VM 我使用 10/8 因为我很懒,并且想使用子网来识别超出范围的服务,任何 VM 都需要将 10.0.0.1 作为网关来访问互联网,无论它们是否已被分配公共 IP 或两者兼而有之。

长答案:

我们需要深入挖掘这一点:

ens3

首先,在这种情况下,ENS3 是 Netcup 的默认 NIC。您必须将其替换为当前设置。网关和 PoinToPoint 必须相同。原因是,它将任何未知流量重定向到网关,并且主要消除了一些头部缓存有线错误,我无法解释,但过去由于没有连接而发生过。

其次,在启动后(启用设备)它已经启用设备作为 ARP 代理。同时转发 IPv6 流量。(需要单独配置!)

并且它还会导入现有的 iptables,以防设备已经关闭或禁用。

vmbr0

因此,大多数人都会看到没有使用桥接端口。是的,原因是我们将使用“路由”而不是“桥接”设置。由于 ARP 代理和 NAT,我们没有遇到任何问题。但会解决限制。

基本上,我们每次都会启用 ip_forward,以防它以某种方式被禁用。然后,我们在设备上动态启用 NAT 规则,并在网桥关闭时禁用它们。这很重要,因为有时会发生罕见的连接问题,只有重新启动才能解决问题。虚拟机的公共 IP 也是如此 → 而且大多数情况下你会想“WTH - 我没有改变任何东西??为什么我的虚拟机不工作?” → 此外,你没有将路由分配给网桥,而不是虚拟接口。原因是,虚拟接口在网桥之后,并且已经具有有关(互联网 ⇒ ens3 ⇒ vmbr0 ⇒ 虚拟 iface)的信息

结论

对于任何托管商来说,都没有“完美”的配置。这主要取决于要求和限制。在我看来,这种配置已经使用了很多年。

相关内容