在远程办公期间,有那么一些同事总是有奇奇怪怪的想法,比如:“我怎么能从云服务器访问家里电脑”,“我怎么通过VPN访问云服务器”,“我怎么这样,怎么那样”;下面就分享一个奇怪的上网方案。
背景概述
国内某搜索引擎大家都知道,有用资料基本查不出来,全特么广告,这时就需要访问一下海外的某搜索引擎等等,所以就购买了某公司的商用专线产品,实现天津到新加坡网络加速,优点:价格便宜,速度快;缺点:只支持端到端的拨号连接(Forticlient),并且每个客户端都单独收费。所以就衍生出“能不能将一个客户端通过某种方式共享给多人使用”,作为公司“专业网管”也就只能硬着头皮尝试一下了,功夫不负有心人,还是可以跑通的。
安装流程
环境描述
- 操作系统:Ubuntu 1204
- VPN客户端:Openforticlient、Wireguard
Openforticlient
github仓库:https://github.com/adrienverge/openfortivpn.git
安装
apt install openfortivpn
配置
openforticlient配置文件
# cat /etc/openfortivpn/config.conf
# config file for openfortivpn, see man openfortivpn(1)
host = <vpn提供商提供>
port = <vpn提供商提供>
username = <vpn提供商提供>
password = <vpn提供商提供>
set-routes = 0
#set-dns = 0
#pppd-use-peerdns = 0
# X509 certificate sha256 sum, trust only this one!
trusted-cert = 25a0d500f10d6bbe06f0761bcc7a023d6174b82ddab4e4c2ccf9763f1b44748f
openforticlient systemd文件
$cat /etc/systemd/system/openfortivpn.service
[Unit]
Description=OpenfortiVPN daemon
Documentation=OpenfortiVPN daemon
After=network.target
[Service]
Type=simple
ExecStart= /usr/local/bin/openfortivpn -c /etc/openfortivpn/config.conf
Restart=on-failure
[Install]
WantedBy=multi-user.target
启动:
systemctl start openfortivpn.service
wireguard
安装
apt install wireguard
wiregard配置文件
$ cat /etc/wireguard/wg0.conf
[Interface]
Address = 192.168.8.1/24
SaveConfig = true
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o ppp0 -j MASQUERADE
ListenPort = 51821
PrivateKey = kOQgWsV9CJ6gob/VOt8+t/Yba9rFmHMwbZp8St5c8kU=
[Peer]
PublicKey = VC4H1P273gsHu6ebyL2gS0j3JDrewM85vcRWld5OuG8=
AllowedIPs = 192.168.8.10/32
3、服务器路由表/接口转发配置
sudo ip rule add from 192.168.8.0/24 table 10
sudo ip route add default dev ppp0 table 10
sudo ip route add 172.16.8.0/22 dev eth0 table 10
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
查看路由表状态:
iptables -vnL -t nat
ip route show table 10
附:
有时VPN链接过多或者forticlient无故掉线需要重新拨号,所以就写了一个脚本监控它,其实也可以用Supervisor等服务替代以下脚本。
进程重启脚本
#!/bin/bash
systemctl restart openfortivpn.service
if test $? -eq 0; then
sleep 5
sudo ip rule del from 192.168.8.0/24 table 10
sudo ip route del default dev ppp0 table 10
sudo ip route del 172.16.8.0/22 dev eth0 table 10
sudo iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
sudo ip rule add from 192.168.8.0/24 table 10
sudo ip route add default dev ppp0 table 10
sudo ip route add 172.16.8.0/22 dev eth0 table 10
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
fi
systemctl restart wg-quick@wg0.service
检测openfortivpn运行状态脚本
#!/bin/bash
check() {
fortivpn_status=`systemctl is-active openfortivpn.service > /dev/null 2>&1 && echo 0 || echo 1`
}
while true; do
check
if [ $fortivpn_status -ne 0 ]; then
systemctl restart openfortivpn.service
if test $? -eq 0; then
sleep 5
sudo ip rule del from 192.168.8.0/24 table 10
sudo ip route del default dev ppp0 table 10
sudo ip route del 172.16.8.0/22 dev eth0 table 10
sudo iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
sudo ip rule add from 192.168.8.0/24 table 10
sudo ip route add default dev ppp0 table 10
sudo ip route add 172.16.8.0/22 dev eth0 table 10
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
fi
systemctl restart wg-quick@wg0.service
fi
sleep 30
done
systemd启动文件
[root@vpn01 system]# cat check_openfortivpn.service
[Unit]
Description=rinetd check
[Service]
WorkingDirectory=/opt
ExecStart=/bin/bash restartVPN.sh
Restart=always
[Install]
WantedBy=multi-user.target
wiregard可以使用wg-portal进行用户管理。
wiregard客户端下载:
Windows
https://download.wireguard.com/windows-client/wireguard-amd64-0.0.30.msi
Mac(app store非中国区可安装)
https://itunes.apple.com/us/app/wireguard/id1451685025?ls=1&mt=12