在遠(yuǎn)程辦公期間,有那么一些同事總是有奇奇怪怪的想法僻焚,比如:“我怎么能從云服務(wù)器訪問(wèn)家里電腦”,“我怎么通過(guò)VPN訪問(wèn)云服務(wù)器”膝擂,“我怎么這樣虑啤,怎么那樣”;下面就分享一個(gè)奇怪的上網(wǎng)方案架馋。
背景概述
國(guó)內(nèi)某搜索引擎大家都知道狞山,有用資料基本查不出來(lái),全特么廣告叉寂,這時(shí)就需要訪問(wèn)一下海外的某搜索引擎等等铣墨,所以就購(gòu)買(mǎi)了某公司的商用專(zhuān)線產(chǎn)品,實(shí)現(xiàn)天津到新加坡網(wǎng)絡(luò)加速办绝,優(yōu)點(diǎn):價(jià)格便宜伊约,速度快;缺點(diǎn):只支持端到端的撥號(hào)連接(Forticlient)孕蝉,并且每個(gè)客戶(hù)端都單獨(dú)收費(fèi)屡律。所以就衍生出“能不能將一個(gè)客戶(hù)端通過(guò)某種方式共享給多人使用”,作為公司“專(zhuān)業(yè)網(wǎng)管”也就只能硬著頭皮嘗試一下了降淮,功夫不負(fù)有心人超埋,還是可以跑通的。
安裝流程
環(huán)境描述
- 操作系統(tǒng):Ubuntu 1204
- VPN客戶(hù)端:Openforticlient佳鳖、Wireguard
Openforticlient
github倉(cāng)庫(kù):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
啟動(dòng):
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霍殴、服務(wù)器路由表/接口轉(zhuǎn)發(fā)配置
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
查看路由表狀態(tài):
iptables -vnL -t nat
ip route show table 10
附:
有時(shí)VPN鏈接過(guò)多或者forticlient無(wú)故掉線需要重新?lián)芴?hào),所以就寫(xiě)了一個(gè)腳本監(jiān)控它系吩,其實(shí)也可以用Supervisor等服務(wù)替代以下腳本来庭。
進(jìn)程重啟腳本
#!/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
檢測(cè)openfortivpn運(yùn)行狀態(tài)腳本
#!/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啟動(dòng)文件
[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進(jìn)行用戶(hù)管理。
wiregard客戶(hù)端下載:
Windows
https://download.wireguard.com/windows-client/wireguard-amd64-0.0.30.msi
Mac(app store非中國(guó)區(qū)可安裝)
https://itunes.apple.com/us/app/wireguard/id1451685025?ls=1&mt=12