openVPN的服务端还可以起到代理的作用,将网络进行转发

一、搭建openVPN

具体搭建过程见之前的文章:openVPN搭建 | Hawaii (hua-yiwei.github.io)

二、使用iptables代替防火墙

2.1 安装iptables服务

1
yum -y install iptables-service  

2.2 配置系统转发,并使配置立即生效

1
sysctl -w net.ipv4.ip_forward=1

2.3 关闭firewall

1
2
systemctl stop firewalld  
systemctl disable firewalld

2.4 启动iptables

1
2
systemctl enable iptables  
systemctl start iptables

2.5 配置iptables转发流量,代理主要以iptables转发实现

1
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE 

2.6 允许tcp/udp 1194通过防火墙

1
2
iptables -I INPUT -p tcp --dport 1194 -j ACCEPT
iptables -I INPUT -p udp --dport 1194 -j ACCEPT

1194端口是openVPN服务端的端口

2.7 保存规则并重启

1
2
iptables-save > /etc/sysconfig/iptables
systemctl restart iptables

三、修改openVPN服务端

3.1 修改openVPN的配置文件

1
vim /etc/openvpn/server.conf

注释掉原先的push,并在配置文件中加上以下两行设置

1
2
3
4
#自动推送客户端上的网关及DHCP,此项开启了流量转发,有这项才能使用服务器代理上 网  
push "redirect-gateway def1 bypass-dhcp"
#OpenVPN的DHCP功能为客户端提供指定的 DNS、WINS 等
push "dhcp-option DNS 114.114.114.114"

如下:

image-20240110213039733

3.2 重启openVPN服务

1
systemctl restart openvpn@server

3.3 验证代理

连上openVPN后,查看本机的公网ip,可以看到公网ip变成了服务器的ip,说明代理成功

image-20240110213743666