利用libreswan+V2Ray实现科学上网
实验背景
在云主机中(阿里云、腾讯云等),大陆地区的机器是无法科学上网的,并且好多优秀的网站、 工具等无法访问或者下载速度很慢,例如下载github上的一些资源,我们可以购买VPS(例如Vultr)或者香港等地区的主机,再配合一些软件就可以实现科学上网了
我们可以使用 libreswan 搭建 ipssec 隧道实现不通地域主机内网互通,然后使用 V2Ray 作为代理工具实现科学上网
实验环境
| 区域 | 公网IP | 内网IP段 | 内网IP | 系统 | 内核版本 |
|---|---|---|---|---|---|
| 北京 | 81.70.22.232 | 172.31.0.0/24 | 172.31.0.3 | CentOS7.9 | 3.10.0-1160.45.1.el7.x86_64 |
| 香港 | 129.226.167.89 | 10.0.0.0/24 | 10.0.0.17 | CentOS7.9 | 3.10.0-1160.45.1.el7.x86_64 |
1.安装配置libreswan
1.0 编辑环境变量文件
北京、香港区服务器操做
# 编辑文件
cat > /opt/wall_env << EOF
BJ_PUBLIC_IP=81.70.22.232
BJ_PRIVATE_IP=172.31.0.3
BJ_IP_SUBNET=172.31.0.0/24
HK_PUBLIC_IP=129.226.167.89
HK_PRIVATE_IP=10.0.0.17
HK_IP_SUBNET=10.0.0.0/24
EOF
# 加载环境变量
source /opt/wall_env
1.1 安装依赖包
北京、香港区服务器操做
yum -y install audit-libs-devel bison curl-devel fipscheck-devel flex gcc ldns-devel libcap-ng-devel libevent-devel libseccomp-devel libselinux-devel make nspr-devel nss-devel pam-devel pkgconfig systemd-devel unbound-devel xmlto
1.2 安装libreswan
北京、香港区服务器操做
yum -y install libreswan
1.3 配置内核参数
北京、香港区服务器操做
# 开启路由转发
sed -i '/^net.ipv4.ip_forward/d' /etc/sysctl.conf
cat >> /etc/sysctl.conf << EOF
net.ipv4.ip_forward = 1
EOF
# 使配置生效
sysctl -p
1.4 启动ipsec
systemctl start ipsec && systemctl enable ipsec
查看状态
$ systemctl status ipsec
● ipsec.service - Internet Key Exchange (IKE) Protocol Daemon for IPsec
Loaded: loaded (/usr/lib/systemd/system/ipsec.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2022-03-29 10:38:39 CST; 14s ago
Docs: man:ipsec(8)
man:pluto(8)
man:ipsec.conf(5)
Main PID: 6483 (pluto)
Status: "Startup completed."
CGroup: /system.slice/ipsec.service
└─6483 /usr/libexec/ipsec/pluto --leak-detective --config /etc/ipsec.conf --nofork
Mar 29 10:38:39 hk pluto[6483]: adding interface lo/lo 127.0.0.1:500
Mar 29 10:38:39 hk pluto[6483]: adding interface lo/lo 127.0.0.1:4500
Mar 29 10:38:39 hk pluto[6483]: adding interface lo/lo ::1:500
Mar 29 10:38:39 hk pluto[6483]: | setup callback for interface lo:500 fd 19
Mar 29 10:38:39 hk pluto[6483]: | setup callback for interface lo:4500 fd 18
Mar 29 10:38:39 hk pluto[6483]: | setup callback for interface lo:500 fd 17
Mar 29 10:38:39 hk pluto[6483]: | setup callback for interface eth0:4500 fd 16
Mar 29 10:38:39 hk pluto[6483]: | setup callback for interface eth0:500 fd 15
Mar 29 10:38:39 hk pluto[6483]: loading secrets from "/etc/ipsec.secrets"
Mar 29 10:38:39 hk pluto[6483]: no secrets filename matched "/etc/ipsec.d/*.secrets"
1.5 防火墙配置
香港区服务器操作
防火墙策略开放 udp 500 和 udp 4500 端口,允许北京区主机公网IP访问