LoginSignup
4
3

More than 3 years have passed since last update.

在AlibabaCloud上搭建VPN服务器翻墙回国

Last updated at Posted at 2019-12-17

目的

飘在海外,国内三大视频网站腾讯视频,优酷,爱奇艺越来越多的视频变得无法访问,购买会员也无济于事;
小米的小爱同学也因为海外IP的问题有些服务比如音乐播放不了。
在阿里云上搭个VPN服务器来解决这一切吧。

VPN协议

可选项有PPTP,L2TP,OpenVPN等,先装个配置上最简单的PPTP,
不确定是不是真的是最简单的,因为没试过其它两种,等有时间再试。
注意一下PPTP有个缺点就是苹果已经不支持了,也就是iphone,ipad啥的直接连接不了。
家里用的是小米的路由器,支持这个协议,设置后接入路由器的所有设备也都走VPN,所以也就无所谓了。

准备

在阿里云上购买国内节点的弹性计算(ECS)和通用流量包;
第一次购买国内节点的服务器需要身份证明,用的是日本的驾照;
服务器位置选了离日本最近的上海;
配置没要求,选最便宜的;
系统选的CentOS的最新版7.7;

ECS初始化设置

第一次以SSH连接ECS前,需要重置密码,在阿里云的实例列表-更多-密码/密匙中有这个选项;
重置密码后重启实例就可以通过SSH以root用户访问;

安全组规则

阿里云默认开启防火墙,而且入方向默认全部屏蔽,所以需要在阿里云网页上的安全组规则中添加放行以下的数据;
协议类型:全部GRE 端口范围: -1/-1 授权对象:0.0.0.0/0
协议类型:自定义TCP 端口范围: 1723/1723 授权对象:0.0.0.0/0

连上SSH开搞

事前准备

先看看系统信息

$ lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.7.1908 (Core)
Release:    7.7.1908
Codename:   Core

安装必要的软件

$ yum install ppp pptpd iptables iptables-services

安装成功后检查一下

$ pptpd --version
pptpd v1.4.0
$ iptables --version
iptables v1.4.21

环境设置

允许ip_forward
在文件/etc/sysctl.conf最后添加以下设置保存
net.ipv4.ip_forward = 1
为了让该配置生效,运行

$ sysctl -p

ppp设置

设置DNS

$ vi /etc/ppp/options.pptpd


'#ms-dns 10.0.0.1
'#ms-dns 10.0.0.2
替换成Google的DNS服务器
ms-dns 8.8.8.8
ms-dns 8.8.4.4

设置用户名密码

$ vi /etc/ppp/chap-secrets

添加一行设置如下

# Secrets for authentication using CHAP
# client    server  secret          IP addresses
username pptpd password *

设置pptp

修改pptp的配置文件

$ vi /etc/pptpd.conf

去掉以下两行的开头注释符#

localip 192.168.0.1
remoteip 192.168.0.234-238,192.168.0.245

设置iptables

iptables是个防火墙,这里用到的只是它的NAT功能,
因为阿里云自带防火墙,所以这部分的设置可以非常精简,懒得试了,就这样吧。

# Reset/Flush iptables
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
# Flush end

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

iptables -A INPUT -i eth0 -p tcp --dport 1723 -j ACCEPT
iptables -A INPUT -i eth0 -p gre -j ACCEPT

# Allow localhost traffic
iptables -A INPUT -i lo   -m state --state NEW  -j ACCEPT
iptables -A OUTPUT -o lo   -m state --state NEW  -j ACCEPT

# Allow server and internal network to go anyway
iptables -A INPUT  -s 10.0.0.0/24   -m state --state NEW  -j ACCEPT
iptables -A INPUT  -s 199.101.100.10   -m state --state NEW  -j ACCEPT
iptables -A OUTPUT  -m state --state NEW  -j ACCEPT

# Allow ssh
iptables -A INPUT -p tcp --dport ssh -j ACCEPT

# change policy of INPUT to DROP
iptables -P INPUT DROP

# Save and restart service
service iptables save
service iptables restart

重启pptpd

$ service pptpd restart

设置iptables和pptpd自启动

$ chkconfig iptables on
$ chkconfig pptpd on

搞定

到这里VPN服务器已经设置完毕并且应该已经开始运行,
在客户端填上服务器ip,用户名,密码应该就可以连接了。
偶尔会连接不上,原因不明,多试几次,不行就换个时间再连接看看。
Good Luck!!!

Debug

因为阿里云服务器的机房物理位置,路由路径,用到的各种软件的版本等等诸多原因,很容易导致设置完后根本连不上,或者连上了却上不了网,或者网速极慢等种种问题。别问我是怎么知道的。
再写点Debug相关的东西。

Speedtest

这个软件是用来测试服务器的上传下载速度的,戳这里到官网查看如何安装,安装后执行speedtest命令即可,就这么简单。
结果长这样:(对,你没看错,阿里爸爸给的比标称还高了3Mbps,莫名的感动)

[root@iZuf69cvmhmo22uspsg9]# speedtest
   Speedtest by Ookla
     Server: China Telecom - Shanghai (id = 3633)
        ISP: Hangzhou Alibaba Advertising Co.,Ltd.
    Latency:     1.72 ms   (0.03 ms jitter)
   Download:   103.25 Mbps (data used: 162.6 MB)                               
     Upload:    33.15 Mbps (data used: 51.3 MB)                               
Packet Loss: Not available.
 Result URL: https://www.speedtest.net/result/c/aaaf71e2-fa21-43de-a5d4-951b847945ff

iperf

测试完了服务器端的上传下载速度,再来看看服务器和海外的客户端,也就是自己的电脑之间的带宽吧,这才是我们实际能使用的带宽,毕竟数据是需要走海底光缆,而海底光缆的带宽又是那么珍贵。
这里用到个工具叫iperf,服务器端和自己的电脑都需要进行安装,服务器作为被动端,自己的电脑去主动连接来模拟和VPN一样的场景。
服务器端的系统是CentOS,这么装,装完后启动监听

yum install iperf
iperf -s

客户端用的是MAC,这么装,装完后连接服务器,记得换成你的服务器ip

brew install iperf
iperf -c 192.168.0.1

这里需要注意一下的是,iperf默认用的是TCP的5001端口,记得去阿里云网站的安全组策略放行,服务器上安装的防火墙iptables也需要放行这个端口。
结果长这样

w0811617m:~ XXXX$ iperf -c 147.100.96.6
------------------------------------------------------------
Client connecting to 147.100.96.6, TCP port 5001
TCP window size:  129 KByte (default)
------------------------------------------------------------
[  4] local 192.168.10.163 port 49712 connected with 147.100.96.6 port 5001
[ ID] Interval       Transfer     Bandwidth
[  4]  0.0-11.2 sec   512 KBytes   375 Kbits/sec

对,你没看错,带宽才375Kb/s

查看服务器日志

$ less /var/log/messages

查看接收到的数据包

用工具tcpdump来查看服务器端收到的数据包,看不懂,在连接不上的情况下可以用来确认是不是真的有数据进来。
功能很多,可以指定源服务器IP,端口等,像这样:

$ tcpdump port 1723

连上了但没速度

原因不明,能搜到的原因大概只有MTU设置不对,但是照着改了之后干脆就连不上了。
以为是阿里云服务器对海外带宽的问题,实际上并不是,因为换成L2TP协议后速度快的很。
教程戳这里

4
3
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3