- A+
之前介绍过finalspeed,这类openvz架构的vps上为小伙伴加速艾斯艾斯起到了很好的效果。不过宣布FS项目暂停维护后,大家使用起来也不是那么方便了。今天无意间看到这款finalspeed的最佳替代品kcptun,涨知识了,尝试了一下结果发现加速效果很明显,而且内存占用很小。以下内容经过实践转载而来,已经在搬瓦工和sentris的便宜货上试过,720p很流畅。
嫌麻烦的同学可以直接用一键脚本了:
Kcptun介绍
Kcptun 是一个非常简单和快速的,基于 KCP 协议的 UDP 隧道,它可以将 TCP 流转换为 KCP+UDP 流。而 KCP 是一个快速可靠协议,能以比 TCP 浪费10%-20%的带宽的代价,换取平均速度提高 30%-40%,且最大延迟降低三倍的传输效果。
Kcptun 是 KCP 协议的一个简单应用,可以用于任意 TCP 网络程序的传输承载,以提高网络流畅度,降低掉线情况。由于 Kcptun 使用 Go 语言编写,内存占用低,而且适用于所有平台,甚至 Arm 平台。
Kcptun 工作示意图:
KCP 协议:https://github.com/skywind3000/kcp
Kcptun 项目地址:https://github.com/xtaci/kcptun
Kcp-server:https://github.com/clangcn/kcp-server
Kcp-server 是在 Kcptun 的基础上,添加了配置文件的支持,并简化了安装过程。本教程不探讨 Kcp-server 的使用,着重介绍 Kcptun 的配置。
部署Kcptun
2016.08.31 v20160831发布,新版支持配置文件!
2016.08.11 v20160811发布,一些优化,非关键更新。
2016.08.06 添加Kcptun可视化运行工具和Android使用说明。
2016.07.25 v20160725发布,降低了网络延迟。
2016.07.16 添加重启脚本、开机启动和端口说明。
2016.07.01 v20160701发布,限定RTO退让的最大值为8xRTT,修正假死问题。
2016.06.27 v20160627发布,新参数-nocomp,需要在两端同时使用以禁止压缩传输。
2016.06.17 添加客户端开机自启批处理,重写软件启动vbs脚本。
本教程以 CentOS 6.5 64位为例。
首先下载 Kcptun,可以到 GitHub 上获取最新版:https://github.com/xtaci/kcptun/releases
用 Xshell 或者 Putty 登陆服务器,下载 Kcptun 的预编译版:
1 2 3 4 |
mkdir /root/kcptun cd /root/kcptun wget https://github.com/xtaci/kcptun/releases/download/v20160922/kcptun-linux-amd64-20160922.tar.gz tar -zxf kcptun-linux-amd64-*.tar.gz |
注意:请根据你的系统下载对应版本。32位系统下载 kcptun-linux-386-*.tar.gz,64位系统下载 kcptun-linux-amd64-*.tar.gz
解压之后有两个文件:client_linux_amd64 和 server_linux_amd64,一个用于服务器,一个用于客户端。
请在服务端程序相同目录下新建两个文件 start.sh 和 stop.sh。
创建 start.sh,创建之前请执行一下ln -sf /bin/bash /bin/sh
vi /root/kcptun/start.sh
写入以下内容:
1 2 3 4 |
<span class="com">#!/bin/bash</span><span class="pln"> cd </span><span class="pun">/</span><span class="pln">root</span><span class="pun">/</span><span class="pln">kcptun</span><span class="pun">/</span> <span class="pun">./</span><span class="pln">server_linux_amd64 </span><span class="pun">-</span><span class="pln">c </span><span class="pun">/</span><span class="pln">root</span><span class="pun">/</span><span class="pln">kcptun</span><span class="pun">/</span><span class="pln">server</span><span class="pun">-</span><span class="pln">config</span><span class="pun">.</span><span class="pln">json </span><span class="pun">></span><span class="pln"> kcptun</span><span class="pun">.</span><span class="pln">log </span><span class="lit">2</span><span class="pun">>&</span><span class="lit">1</span> <span class="pun">&</span><span class="pln"> echo </span><span class="str">"Kcptun started."</span> |
server_linux_amd64 对应服务端文件名,请对应修改。
创建配置文件
vi /root/kcptun/server-config.json
写入以下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<span class="pun">{</span> <span class="str">"listen"</span><span class="pun">:</span> <span class="str">":29900"</span><span class="pun">,</span> <span class="str">"target"</span><span class="pun">:</span> <span class="str">"127.0.0.1:8388"</span><span class="pun">,</span> <span class="str">"key"</span><span class="pun">:</span> <span class="str">"test"</span><span class="pun">,</span> <span class="str">"crypt"</span><span class="pun">:</span> <span class="str">"salsa20"</span><span class="pun">,</span> <span class="str">"mode"</span><span class="pun">:</span> <span class="str">"fast2"</span><span class="pun">,</span> <span class="str">"mtu"</span><span class="pun">:</span> <span class="lit">1350</span><span class="pun">,</span> <span class="str">"sndwnd"</span><span class="pun">:</span> <span class="lit">1024</span><span class="pun">,</span> <span class="str">"rcvwnd"</span><span class="pun">:</span> <span class="lit">1024</span><span class="pun">,</span> <span class="str">"datashard"</span><span class="pun">:</span> <span class="lit">70</span><span class="pun">,</span> <span class="str">"parityshard"</span><span class="pun">:</span> <span class="lit">30</span><span class="pun">,</span> <span class="str">"dscp"</span><span class="pun">:</span> <span class="lit">46</span><span class="pun">,</span> <span class="str">"nocomp"</span><span class="pun">:</span> <span class="kwd">false</span><span class="pun">,</span> <span class="str">"acknodelay"</span><span class="pun">:</span> <span class="kwd">false</span><span class="pun">,</span> <span class="str">"nodelay"</span><span class="pun">:</span> <span class="lit">0</span><span class="pun">,</span> <span class="str">"interval"</span><span class="pun">:</span> <span class="lit">40</span><span class="pun">,</span> <span class="str">"resend"</span><span class="pun">:</span> <span class="lit">0</span><span class="pun">,</span> <span class="str">"nc"</span><span class="pun">:</span> <span class="lit">0</span><span class="pun">,</span> <span class="str">"sockbuf"</span><span class="pun">:</span> <span class="lit">4194304</span><span class="pun">,</span> <span class="str">"keepalive"</span><span class="pun">:</span> <span class="lit">10</span> <span class="pun">}</span> |
-l 表示 Kcptun 的服务端监听端口,用于接收外部请求和发送数据,默认 29900;
-t 表示要加速的地址,我这里设置的是我服务器的 艾斯艾斯 端口;
-key 是 Kcptun 的验证密钥,服务端和本地必须一致才能通过验证,请自行设置;
-mode 为加速模式,默认 fast,这里使用 fast2。
响应速度:
fast3 > [fast2] > fast > normal > default
有效载荷比:
default > normal > fast > [fast2] > fast3
中间mode参数比较均衡,总之就是越快越浪费带宽,推荐模式 fast2
其他参数,请使用 ./server_linux_amd64 -h 查看,更深层次的参数调整需要理解 KCP 协议。
下面是作者给的配置样例,适用大部分ADSL接入(非对称上下行)的参数(实验环境电信100M ADSL)。其它带宽请按比例调整,比如 50M ADSL,把 CLIENT 的 -sndwnd -rcvwnd 减掉一半,SERVER 不变。
SERVER: -mtu 1400 -sndwnd 2048 -rcvwnd 2048 -mode fast2
CLIENT: -mtu 1400 -sndwnd 256 -rcvwnd 2048 -mode fast2 -dscp 46
创建 stop.sh
vi /root/kcptun/stop.sh
写入以下内容:
1 2 3 4 5 6 7 8 |
<span class="com">#!/bin/bash</span><span class="pln"> echo </span><span class="str">"Stopping Kcptun..."</span><span class="pln"> PID</span><span class="pun">=</span><span class="str">`ps -ef | grep server_linux_amd64 | grep -v grep | awk '{print $2}'`</span> <span class="kwd">if</span> <span class="pun">[</span> <span class="str">""</span> <span class="pun">!=</span> <span class="str">"$PID"</span> <span class="pun">];</span> <span class="kwd">then</span><span class="pln"> echo </span><span class="str">"killing $PID"</span><span class="pln"> kill </span><span class="pun">-</span><span class="lit">9</span><span class="pln"> $PID </span><span class="kwd">fi</span><span class="pln"> echo </span><span class="str">"Kcptun stoped."</span> |
创建 restart.sh
1 2 3 4 5 |
<span class="com">#!/bin/bash</span><span class="pln"> cd </span><span class="pun">/</span><span class="pln">root</span><span class="pun">/</span><span class="pln">kcptun</span><span class="pun">/</span><span class="pln"> sh stop</span><span class="pun">.</span><span class="pln">sh echo </span><span class="str">"Restarting Kcptun..."</span><span class="pln"> sh start</span><span class="pun">.</span><span class="pln">sh</span> |
然后可以启动服务端:
sh /root/kcptun/start.sh
/root/kcptun/kcptun.log 为日志信息。
停止服务端请使用:
sh /root/kcptun/stop.sh
重启服务端:
sh /root/kcptun/restart.sh
添加开机启动:
Centos:
1 |
chmod +x /etc/rc.d/rc.local;echo "sh /root/kcptun/start.sh" >> /etc/rc.d/rc.local |
Ubuntu/Debian:
1 |
chmod +x /etc/rc.local;echo "sh /root/kcptun/start.sh" >> /etc/rc.local |
配置客户端
客户端需要新建vbs脚本,如果觉得麻烦也可以考虑使用以下图形化客户端工具。
KcptunGUI,来自“诸神的黄昏”。
源码地址:https://git.oschina.net/ragnaroks/KcptunGUI
软件下载:https://git.oschina.net/ragnaroks/KcptunGUI/releases
注意:客户端和服务端参数必须一致的有:
- datashard
- parityshard
- nocomp
- key
- crypt
以下为手动创建启动文件的方法:
以本地 Windows 64位系统为例,首先下载 Kcptun 的 Windows 版本。
我这里先新建一个文件夹,命名为 Kcptun,然后下载:
https://github.com/xtaci/kcptun/releases/download/v20160922/kcptun-windows-amd64-2016022.tar.gz
解压到文件夹下。
由于 Kcptun 是控制台程序,所以我选择使用 vbs 脚本,来达到后台运行的目的:
新建 run.vbs
Dim RunKcptun
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
'获取文件路径
currentPath = fso.GetFile(Wscript.ScriptFullName).ParentFolder.Path & "\"
'配置文件路径
configFile = currentPath & "client-config.json"
'日志文件
logFile = currentPath & "kcptun.log"
'软件运行参数
exeConfig = currentPath & "client_windows_amd64.exe -c " & configFile
'拼接命令行
cmdLine = "cmd /c " & exeConfig & " > " & logFile & " 2>&1"
'启动软件
WshShell.Run cmdLine, 0, False
'等待1秒
'WScript.Sleep 1000
'打印运行命令
'Wscript.echo cmdLine
Set WshShell = Nothing
Set fso = Nothing
'退出脚本
WScript.quit
新建 client-config.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<span class="pun">{</span> <span class="str">"localaddr"</span><span class="pun">:</span> <span class="str">":12948"</span><span class="pun">,</span> <span class="str">"remoteaddr"</span><span class="pun">:</span> <span class="str">"10.10.10.10:29900"</span><span class="pun">,</span> <span class="str">"key"</span><span class="pun">:</span> <span class="str">"test"</span><span class="pun">,</span> <span class="str">"crypt"</span><span class="pun">:</span> <span class="str">"salsa20"</span><span class="pun">,</span> <span class="str">"mode"</span><span class="pun">:</span> <span class="str">"fast2"</span><span class="pun">,</span> <span class="str">"conn"</span><span class="pun">:</span> <span class="lit">1</span><span class="pun">,</span> <span class="str">"autoexpire"</span><span class="pun">:</span> <span class="lit">60</span><span class="pun">,</span> <span class="str">"mtu"</span><span class="pun">:</span> <span class="lit">1350</span><span class="pun">,</span> <span class="str">"sndwnd"</span><span class="pun">:</span> <span class="lit">128</span><span class="pun">,</span> <span class="str">"rcvwnd"</span><span class="pun">:</span> <span class="lit">1024</span><span class="pun">,</span> <span class="str">"datashard"</span><span class="pun">:</span> <span class="lit">70</span><span class="pun">,</span> <span class="str">"parityshard"</span><span class="pun">:</span> <span class="lit">30</span><span class="pun">,</span> <span class="str">"dscp"</span><span class="pun">:</span> <span class="lit">46</span><span class="pun">,</span> <span class="str">"nocomp"</span><span class="pun">:</span> <span class="kwd">false</span><span class="pun">,</span> <span class="str">"acknodelay"</span><span class="pun">:</span> <span class="kwd">false</span><span class="pun">,</span> <span class="str">"nodelay"</span><span class="pun">:</span> <span class="lit">0</span><span class="pun">,</span> <span class="str">"interval"</span><span class="pun">:</span> <span class="lit">40</span><span class="pun">,</span> <span class="str">"resend"</span><span class="pun">:</span> <span class="lit">0</span><span class="pun">,</span> <span class="str">"nc"</span><span class="pun">:</span> <span class="lit">0</span><span class="pun">,</span> <span class="str">"sockbuf"</span><span class="pun">:</span> <span class="lit">4194304</span><span class="pun">,</span> <span class="str">"keepalive"</span><span class="pun">:</span> <span class="lit">10</span> <span class="pun">}</span> |
localaddr 为 kcptun 本地客户端运行的端口,为 12948;
remoteaddr 服务器 IP 地址 10.10.10.10,Kcptun 服务端口为 29900
key 验证密钥,和服务端一致;
conn 单线连接;
autoexpire 60 表示一分钟之后重新建立连接;
其他参数说明见上面服务端配置的说明。
新建 stop.bat
taskkill /f /im client_windows_amd64.exe
然后双击 run.vbs 运行程序,使用 stop.bat 来停止程序,kcptun.log 为输出的日志信息。
开机启动配置:
新建 startUp.bat:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<span class="lit">@echo</span><span class="pln"> off title </span><span class="typ">Kcptun</span> <span class="pun">开机启动设置</span><span class="pln"> mode con cols</span><span class="pun">=</span><span class="lit">50</span><span class="pln"> lines</span><span class="pun">=</span><span class="lit">20</span><span class="pln"> color A echo</span><span class="pun">.</span><span class="pln"> echo</span><span class="pun">.</span> <span class="typ">Kcptun</span> <span class="pun">启动项设置</span><span class="pln"> echo</span><span class="pun">.</span><span class="pln"> echo</span><span class="pun">.</span> <span class="pun">此批处理可以将</span> <span class="typ">Kcptun</span> <span class="pun">客户端添加到开机启动</span><span class="pln"> echo</span><span class="pun">.</span><span class="pln"> echo</span><span class="pun">.</span> <span class="pun">请将该文件放到</span><span class="pln"> run</span><span class="pun">.</span><span class="pln">vbs </span><span class="pun">相同目录后运行</span><span class="pln"> echo</span><span class="pun">.</span><span class="pln"> echo</span><span class="pun">.</span> <span class="pun">查看说明:</span><span class="pln">https</span><span class="pun">:</span><span class="com">//blog.kuoruan.com/102.html</span><span class="pln"> echo</span><span class="pun">.</span> <span class="kwd">set</span> <span class="pun">/</span><span class="pln">p ST</span><span class="pun">=输入</span><span class="pln"> y </span><span class="pun">添加开机启动,输入</span><span class="pln"> n </span><span class="pun">取消开机启动:</span> <span class="kwd">if</span> <span class="pun">/</span><span class="pln">I </span><span class="str">"%ST%"</span><span class="pun">==</span><span class="str">"y"</span> <span class="kwd">goto</span><span class="pln"> addStartup </span><span class="kwd">if</span> <span class="pun">/</span><span class="pln">I </span><span class="str">"%ST%"</span><span class="pun">==</span><span class="str">"n"</span> <span class="kwd">goto</span><span class="pln"> delStartup </span><span class="pun">:</span><span class="pln">addStartup reg add </span><span class="str">"HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"</span> <span class="pun">/</span><span class="pln">v </span><span class="str">"Kcptun Client"</span> <span class="pun">/</span><span class="pln">t REG_SZ </span><span class="pun">/</span><span class="pln">d </span><span class="str">"\"%~dp0run.vbs\""</span> <span class="pun">/</span><span class="pln">F</span><span class="pun">></span><span class="pln">NUL </span><span class="kwd">exit</span> <span class="pun">:</span><span class="pln">delStartup reg </span><span class="kwd">delete</span> <span class="str">"HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"</span> <span class="pun">/</span><span class="pln">v </span><span class="str">"kcptun Client"</span> <span class="pun">/</span><span class="pln">F</span><span class="pun">></span><span class="pln">NUL </span><span class="lit">2</span><span class="pun">></span><span class="pln">NUL </span><span class="kwd">exit</span> |
端口示意图
Shadow*socks 客户端配置
在客户端中新建服务器:
- 服务器 IP 填写本机:127.0.0.1
- 服务器端口填写:12948
正确填写你的 艾斯艾斯 密码,加密方式,协议和混淆方式。
切换到该服务器上,测试是否正确运行。
速度实测
本人在sentris的128M内存vps上测试了看youtube视频能稳定在2000左右,720p流畅。最主要的是vps上内存和CPU占用率非常低,这点完爆finalspeed了。
厘米总结:如果你资金有限,只能买openvz架构的vps,openvz类vps目前有效的加速方案就是kcptun、finalspeed、net_speeder这三种,前两种需要配合客户端,最后一种不需要客户端但是效果最不明显。如果你能接受5美元一月更好的vps,那么vultr、DO是你不错的选择。