通过VPN将云服务器IP绑定到本地服务器或OpenWrt
之前看到有某某某云服务器商有售卖家庭宽带IP购买的服务,宣称是可以给家庭宽带增加一条静态IP,出于好奇,我购买了一个月进行尝试。

在客服使用 SSH 连接将 IP 链接到我本地的Linux服务器后,我通过查询系统进程和网络接口的方式发现,其实所谓增加静态 IP 不过是使用 WireGuard 将公网IP绑定到设备上,于是我产生了一个大胆奇妙的想法,能不能将自己手边吃灰的云服务器IP绑定到本地,甚至绑定到OpenWrt上,经过一番摸索和与GPT的深入交流,成功将云服务器IP绑定到了本地服务器,这种绑定和frp有所不同,所有访问到云服务器IP的请求,都将转发到本地服务器,甚至包括Ping请求。

正如上图所示,我使用的是腾讯云北京的服务器,正常在北京节点对其ip进行ping命令,得到的延迟应该在5ms以内,但是现在ping的延迟增加了从腾讯云北京到我家里的时间。并且现在通过这台服务器的IP可以直接访问到我家里的设备。
下面是详细的部署流程。
公网服务器部署WireGuard
安装WireGuard
sudo apt update
sudo apt install wireguard生成秘钥对
# 生成公网服务器的密钥对
wg genkey | tee server_privatekey | wg pubkey > server_publickey
# 生成无公网服务器的密钥对
wg genkey | tee client_privatekey | wg pubkey > client_publickey密钥对文件保存在当前命令行路径下的 server_privatekey 和 server_publickey 文件,以及 client_privatekey 和 client_publickey,其中 server_privatekey 和 server_publickey 为公网服务器的私钥与公钥,client_privatekey 和 client_publickey 为无公网服务器的私钥与公钥,这两对秘钥需要在妥善保存后删除。
下文中将以以下代称称呼
| 代称 | 秘钥文件名称 |
|---|---|
| <公网服务器的私钥> | server_privatekey |
| <公网服务器的公钥> | server_publickey |
| <无公网服务器的私钥> | client_privatekey |
| <无公网服务器的公钥> | client_publickey |
配置WireGuard
创建并编辑配置文件 /etc/wireguard/wg0.conf
sudo vi /etc/wireguard/wg0.conf添加以下内容
[Interface]
Address = 10.1.0.1/24
PrivateKey = <公网服务器的私钥>
ListenPort = 51820
[Peer]
PublicKey = <无公网服务器的公钥>
AllowedIPs = 10.1.0.2/32将 <公网服务器的私钥> 替换为你生成的 privatekey,将 <无公网服务器的公钥> 替换为无公网服务器生成的 publickey。
启动WireGuard
sudo wg-quick up wg0无公网服务器部署 WireGuard
这里先以普通Ubuntu服务器为例,如果想在OpenWrt上部署,本节主要查看配置WireGuard小节即可。
配置WireGuard
创建并编辑配置文件 /etc/wireguard/wg0.conf
sudo vi /etc/wireguard/wg0.conf添加以下内容
[Interface]
Address = 10.1.0.2/24
PrivateKey = <无公网服务器的私钥>
[Peer]
PublicKey = <公网服务器的公钥>
Endpoint = <公网服务器的IP>:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25或者填写
[Interface]
Address = 10.1.0.2/24
PrivateKey = <无公网服务器的私钥>
[Peer]
PublicKey = <公网服务器的公钥>
Endpoint = <公网服务器的IP>:51820
AllowedIPs = 10.1.0.1/32
PersistentKeepalive = 25启动 WireGuard
sudo wg-quick up wg0配置端口转发
本节内所有操作均在公网服务器上执行,并建议彻底关闭云服务器上的网络防火墙。
配置IP转发
在公网服务器上启用 IP 转发
sudo sysctl -w net.ipv4.ip_forward=1如果需要永久生效,需编辑 /etc/sysctl.conf 并取消 net.ipv4.ip_forward=1 的注释或添加 net.ipv4.ip_forward=1。
配置防火墙规则
在公网服务器上使用 iptables 将流量转发到无公网服务器
sudo iptables -t nat -I PREROUTING -p udp --dport 51820 -j ACCEPT
sudo iptables -t nat -A PREROUTING -d <公网服务器的IP> -j DNAT --to-destination 10.1.0.2
sudo iptables -t nat -A POSTROUTING -j MASQUERADE注意!!! 这里 <云服务器的IP> 有很多大厂的服务器,使用 ip addr 命令查询到的网卡绑定IP与实际公网IP不同,这里一定要使用 网卡实际绑定的 ip 而不是 公网IP 。
为了确保规则在重启后仍然有效,保存规则
sudo apt install iptables-persistent
sudo netfilter-persistent save验证配置
1、确认 WireGuard 隧道连接已建立
sudo wg show2、通过公网 IP 测试连接,确保请求能够转发到无公网服务器。
注意
确保公网服务器和无公网服务器的防火墙配置正确,允许必要的端口和协议通过。
OpenWrt部署WireGuard
1、首先确保 OpenWrt 上安装以下插件

2、插件安装完成后,必须重启路由器,否则无法再接口处查看到 WireGuard 接口。
3、网络-> 接口-> 添加新接口 输入名称 wg0 和选择 WireGuard VPN协议

4、编辑接口-> 导入配置,粘贴无公网服务器部署 WireGuard 一节中配置 WireGuard 中的配置文件即可。

5、将新建接口加入到 wan 防火墙区域
6、完成后即可把服务器 IP 当做 OpenWrt 的一个 wan 口接入使用,相当于给你的宽带绑定了一条 公网IP 。
如果遇到绑定 OpenWrt 后无效的情况,将 AllowedIPs 从 0.0.0.0/0 修改为公网服务器 ip 即可,例如:10.1.0.1/32 。
1 条评论
华纳东方明珠客服电话是多少?(▲18288362750?《?微信STS5099? 】
如何联系华纳东方明珠客服?(▲18288362750?《?微信STS5099? 】
华纳东方明珠官方客服联系方式?(▲18288362750?《?微信STS5099?
华纳东方明珠客服热线?(▲18288362750?《?微信STS5099?
华纳东方明珠24小时客服电话?(▲18288362750?《?微信STS5099? 】
华纳东方明珠官方客服在线咨询?(▲18288362750?《?微信STS5099?