关于
在尝试白嫖了无数游戏加速器后,决定自行搭建。
在这之前,你一定碰到很多人告诫你不要尝试,他们告诉你线路是最重要的。
那么,我们就来聊一聊线路的选择。
如果你选择香港服务器,那么一定要认准CN2线路,其他则是国际线路的大杂烩,速度很慢。
以阿里云为例,开通弹性公网IP的时候,香港区域线路类型请选择“BGP(多线)_精品”,这个就是阿里云的CN2线路了,其他你可以咨询运营商。
至于日服或者其他别的,我不了解就不在这里误导人了。
加速效果如何
坐标深圳南山,老小区连不了电信,用的是天威的宽带,到香港服务器延迟在20ms左右;
玩PS4战地5,新加坡服延迟为60ms左右,日服延迟在70ms左右,时间为2021年1月底。
用公司的电信宽带试了一下,到香港服务器的延迟在10ms左右,如果用电信宽带效果应该会更好。
之前用UU和腾讯加速器的时候,最低延迟也就60ms左右,而且也不是很稳定,高峰期基本稳定在100ms以上,所以这个效果我还是比较满意的。
硬件列表
- 香港云服务器 x1
- 树莓派4B + Debian10 x1
- 路由器 x1
安装配置
- 在云服务器上安装纸飞机服务端 libev 版,请配置开启 UDP 功能;
- 在本地,把树莓派连在路由器上,分配一个固定IP并安装配置 ss-redir 客户端,配置我也贴在了最后边;
- 手动设置游戏主机IP,网关设为树莓派局域网的IP地址。
注意事项
- 关于纸飞机和 ss-redir 的教程很多这里就不赘述了,但是服务端和客户端都一定要选择 libev 版本,需要支持 UDP 转发;
- 如果没有树莓派,客户端可以安装在智能路由器或在虚拟机上;
- 游戏主机如果无法联网,请检查树莓派的系统是否开启IPv4转发,或者检查 iptables 的配置;
- 树莓派系统的内核必须支持 TPROXY,否则无法转发 UDP 流量;
如何使用
把脚本保存为 gedirect 并给执行权限后, 运行:
# 初始化加速器
./gedirect init <纸飞机服务器IP> <本地端口>
# 清理加速设置
./gedirect clear
# 启用主机加速
./gedirect enable <游戏主机IP>
# 停用主机加速
./gedirect disable <游戏主机IP>
如果觉得命令太麻烦,也可以在运行初始化和启用主机加速两个命令后,通过设置游戏主机的网关来达到启用和停用加速的效果:当需要加速的时候,把游戏主机的网关设为树莓派的IP地址;不需要加速的时候,设为路由器的IP地址。
以上同样适用于PC游戏加速,只需要替换成PC的IP地址即可。
持久化
因为脚本中有涉及到设置策略路由的地方,只保存 iptables 规则无法实现持久化,所以我选择随 ss-redir 服务启动/停止的方案。
详细步骤如下:
- 把脚本保存为
/usr/local/bin/gedirect
,并给执行权限; - 添加配置
/usr/local/etc/gedirect.conf
文件:- 在配置中,设置 ss-redir 配置的路径,服务器IP和本地端口都从中读取;
- 主机IP地址则直接配置;
- 增加
/etc/init.d/gedirectd
脚本,用于 ss-redir 启动/停止时加速器的启用、清理工作,注意给执行权限; - 修改 ss-redir 服务脚本
/lib/systemd/system/***-redir@.service
,在 [Service] 后添加两行:ExecStartPost=/etc/init.d/gedirectd start ExecStopPost=/etc/init.d/gedirectd stop
- 重启 ss-redir 服务;
上边提到的脚本,都在后边贴了出来。
脚本
#!/bin/bash
init()
{
# add fwmark
/usr/sbin/ip rule add fwmark 0x56/0x56 table 100
/usr/sbin/ip route add local 0.0.0.0/0 dev lo table 100
########### tcp ###########
# create new chain
/usr/sbin/iptables -t nat -N GEDIRECT
# ignore remote server
/usr/sbin/iptables -t nat -A GEDIRECT -d $1 -j RETURN
# ignore lan ip
/usr/sbin/iptables -t nat -A GEDIRECT -d 0.0.0.0/8 -j RETURN
/usr/sbin/iptables -t nat -A GEDIRECT -d 10.0.0.0/8 -j RETURN
/usr/sbin/iptables -t nat -A GEDIRECT -d 127.0.0.0/8 -j RETURN
/usr/sbin/iptables -t nat -A GEDIRECT -d 169.254.0.0/16 -j RETURN
/usr/sbin/iptables -t nat -A GEDIRECT -d 172.16.0.0/12 -j RETURN
/usr/sbin/iptables -t nat -A GEDIRECT -d 192.168.0.0/16 -j RETURN
/usr/sbin/iptables -t nat -A GEDIRECT -d 224.0.0.0/4 -j RETURN
/usr/sbin/iptables -t nat -A GEDIRECT -d 240.0.0.0/4 -j RETURN
# forwarding
/usr/sbin/iptables -t nat -A GEDIRECT -p tcp -j REDIRECT --to-ports $2
########### udp ###########
# create new chain
/usr/sbin/iptables -t mangle -N GEDIRECT
# ignore lan ip
/usr/sbin/iptables -t mangle -A GEDIRECT -d 0.0.0.0/8 -j RETURN
/usr/sbin/iptables -t mangle -A GEDIRECT -d 10.0.0.0/8 -j RETURN
/usr/sbin/iptables -t mangle -A GEDIRECT -d 127.0.0.0/8 -j RETURN
/usr/sbin/iptables -t mangle -A GEDIRECT -d 169.254.0.0/16 -j RETURN
/usr/sbin/iptables -t mangle -A GEDIRECT -d 172.16.0.0/12 -j RETURN
/usr/sbin/iptables -t mangle -A GEDIRECT -d 192.168.0.0/16 -j RETURN
/usr/sbin/iptables -t mangle -A GEDIRECT -d 224.0.0.0/4 -j RETURN
/usr/sbin/iptables -t mangle -A GEDIRECT -d 240.0.0.0/4 -j RETURN
# forwarding
/usr/sbin/iptables -t mangle -A GEDIRECT -p udp -j TPROXY --on-port $2 --tproxy-mark 0x56/0x56
}
clear()
{
# remove iptables rules
/usr/sbin/iptables -t nat -F GEDIRECT
/usr/sbin/iptables -t nat -X GEDIRECT 2>/dev/null
/usr/sbin/iptables -t mangle -F GEDIRECT
/usr/sbin/iptables -t mangle -X GEDIRECT 2>/dev/null
# remove fwmark
/usr/sbin/ip rule del fwmark 0x56/0x56 table 100 2>/dev/null
/usr/sbin/ip route del local 0.0.0.0/0 dev lo table 100 2>/dev/null
}
enable()
{
/usr/sbin/iptables -t nat -A PREROUTING -s $1 -p tcp -j GEDIRECT
/usr/sbin/iptables -t mangle -A PREROUTING -s $1 -p udp -j GEDIRECT
}
disable()
{
/usr/sbin/iptables -t nat -D PREROUTING -s $1 -p tcp -j GEDIRECT 2>/dev/null
/usr/sbin/iptables -t mangle -D PREROUTING -s $1 -p udp -j GEDIRECT 2>/dev/null
}
# usage
USAGE="\
Usage: $(basename $0) COMMAND [ARGS...]
Available commands:
init SERVER LOCAL_PORT Init the iptables rules.
clear Clear the iptables rules.
enable LOCAL_IP Enable client routing.
disable LOCAL_IP Disable client routing.\
"
case $1 in
init)
if [ "$2" = "" ] || [ "$3" = "" ]; then
echo "$USAGE"
else
clear
init $2 $3
fi
;;
enable)
if [ "$2" = "" ]; then
echo "$USAGE"
else
disable $2
enable $2
fi
;;
disable)
if [ "$2" = "" ]; then
echo "$USAGE"
else
disable $2
fi
;;
clear)
clear
;;
*)
echo "$USAGE"
;;
esac
客户端配置
{
"server":"<服务器IP地址>",
"server_port":<服务器端口>,
"mode":"tcp_and_udp",
"local_address": "0.0.0.0",
"local_port":1080,
"password":"<密码>",
"timeout":600,
"method":"aes-256-gcm", // 注意修改与服务一致
"nameserver":"8.8.4.4"
}
/usr/local/etc/gedirect.conf
# ss-redir config file
ss_config=/etc/***/client.json
# client ip list, like: 10.5.5.0/24 10.5.6.101
client_list="10.5.5.0/24 10.5.6.101"
/etc/init.d/gedirectd
#!/bin/bash
# settings
gedirect=/usr/local/bin/gedirect
config=/usr/local/etc/gedirect.conf
start()
{
if [ ! -e "$config" ]; then
echo "not found config file: $config"
return 1
fi
. $config
server=$(/usr/bin/grep -F '"server"' $ss_config | /usr/bin/awk -F '"' '{print $4}')
local_port=$(/usr/bin/grep -F '"local_port"' $ss_config | /usr/bin/awk -F ':' '{print $2}' | /usr/bin/awk -F ',' '{print $1}')
if [ "$server" = "" ] || [ "$local_port" = "" ]; then
echo "config error"
return 1
fi
$gedirect init $server $local_port
for cli in $client_list; do
$gedirect enable $cli
done
return 0
}
stop()
{
if [ ! -e "$config" ]; then
echo "not found config file: $config"
return 1
fi
. $config
for cli in $client_list; do
$gedirect disable $cli
done
$gedirect clear
return 0
}
# usage
USAGE="Usage: $(basename $0) start|stop|restart|reload"
case $1 in
start|restart|reload)
stop
start
;;
stop)
stop
;;
*)
echo "$USAGE"
;;
esac