在远程服务器上默认使用有效的 GRE 隧道

在远程服务器上默认使用有效的 GRE 隧道

目的是使用带有 GRE 隧道的前端 VPS (A)“保护”我的后端专用服务器 (B),并使用新建立的接口 (GRE1) 作为 (B) 上的默认接口,以免泄露服务器 (B) 的 IP 地址。这意味着后端服务器 (B) 将通过前端 VPS (A) IP 地址可见。

VPS 和专用服务器都位于远程数据中心。

有效的方法:

  1. GRE 隧道在 VPS(A)和服务器(B)之间工作
  2. VPS(A)将所有端口直接转发到服务器(B)
  3. 通过 (B) 上的新 GRE1 接口路由特定 IP 地址

服务器 (B) 上无法运行的功能

使用 GRE 隧道将所有流量重定向到新建立的 GRE1 接口

之前做过什么

在 VPS(A)上,前端;10.0.0.1

#!/bin/bash
##Front-VPS-A
apt install iptables iproute2
modprobe ip_gre
echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
sysctl -p
ip tunnel add gre1 mode gre local FRONT-IP-A remote BACKEND-IP-B ttl 255
ip addr add 10.0.0.1/30 dev gre1
ip link set gre1 up
iptables -t nat -A POSTROUTING -s 10.0.0.0/30 ! -o gre+ -j SNAT --to-source FRONT-IP
iptables -A FORWARD -d 10.0.0.2 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -s 10.0.0.2 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A PREROUTING -d FRONT-IP-A -j DNAT --to-destination 10.0.0.2

在后端服务器(B)上;10.0.0.2

#!/bin/bash
#Backend Server B
apt install iptables iproute2
echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
sysctl -p
modprobe ip_gre
ip tunnel add gre1 mode gre local BACKEND-IP-B remote FRONT-IP-A ttl 255
ip addr add 10.0.0.2/30 dev gre1
ip link set gre1 up
echo '100 GRE' >> /etc/iproute2/rt_tables
ip rule add from 10.0.0.0/30 table GRE
ip route add default via 10.0.0.1 table GRE

检查后端服务器(B)上的结果,10.0.0.2

curl http://www.cpanel.net/showip.cgi --interface=10.0.0.2

ist 从前端 VPS (A) 提供 ip 地址。但使用

curl http://www.cpanel.net/showip.cgi

仍在从后端服务器(B)传递 IP。

如何在不中断整个远程连接的情况下默认使用服务器 (B) 上的新 GRE1 接口?我知道这不是 VPN/Wireguard 连接,但我认为应该是可能的。

答案1

我发现以下适合我的解决方案

前端服务器,编辑您的前端 ip 和后端 ip 地址

#!/bin/sh
##A-Server-Front
apt install iptables iproute2
lsmod | grep gre
modprobe ip_gre
lsmod | grep gre
echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
sysctl -p
ip tunnel add gre1 mode gre local front-ip remote backend-ip ttl 255
ip addr add 10.0.0.1/30 dev gre1
ip link set gre1 up
iptables -t nat -A POSTROUTING -s 10.0.0.0/30 ! -o gre+ -j SNAT --to-source front-ip
iptables -A FORWARD -d 10.0.0.2 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -s 10.0.0.2 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A PREROUTING -d front-ip -j DNAT --to-destination 10.0.0.2
iptables -A FORWARD -d 10.0.0.2 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

后端服务器,编辑您的前端 ip 和后端 ip 地址

#!/bin/sh
##B-Server-Backend
apt install iptables iproute2
echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
sysctl -p
lsmod | grep gre
modprobe ip_gre
lsmod | grep gre
ip tunnel add gre1 mode gre local backend-ip remote front-ip ttl 255
ip addr add 10.0.0.2/30 dev gre1
ip link set gre1 up
echo '100 GRE' >> /etc/iproute2/rt_tables
ip rule add from 10.0.0.0/30 table GRE
ip route add default via 10.0.0.1 table GRE
#final IP check
curl http://www.cpanel.net/showip.cgi --interface 10.0.0.2

相关内容