实在受不了SS和SSR因为开大会而不停抽风的情况,一下午仔细读了V2Ray的说明,终于配置好了,其实搞清楚了也很简单。
注意
此配置是v2ray动态端口设置,并不保证绝对可用,本人在使用此配置一两个星期之后出现断流问题,估计是动态端口大流量被运营商屏蔽了,现在已经切换到了tls+WebSocket方式,具体服务器配置请参考这里,而dnsmasq和iptables设置不受影响,只要你的v2ray设置正确就肯定能使用。
V2Ray介绍
V2Ray是个最新出现的翻墙爱国工具,集合了其他前辈工具的各种优点,问题就是配置安装稍微复杂了一些,这里有个白话文上手教程,也还是挺复杂的,今天仔细读了一下,参考这两篇文章:利用 V2Ray + GFWList 实现路由器自动翻墙和v2ray+dnsmasq搭建透明代理网关,实现了用局域网上的一台linux服务器(比如树莓派)做网关,让全局域网的设备免配置国内外网站分流、自动翻墙,
墙外VPS设置
我用的是Vultr的日本节点,最低配每个月5美元,优化后在这南方某城市联通100M宽带看YoutTube在晚上高峰期都甚至可以达到60,000 Kbps的疯狂速度,看4K视频都没问题。服务器操作系统选择的是Debian 8 x64(jessie)。
VPS端V2Ray安装与设置
V2Ray官方提供了Linux下的一键安装脚本:
bash <(curl -L -s https://install.direct/go.sh)
运行完成后,编辑’/etc/v2ray/config.json’,配置如下(需要把注释去掉,不然用不了):
{
"log" : {
"access": "/var/log/v2ray/access.log",
"error": "/var/log/v2ray/error.log",
"loglevel": "warning"
},
"inbound": {
"port": 23018, //自定义监听端口
"protocol": "vmess", //v2ray自己的协议
"settings": {
"clients": [
{
"id": "28f5f778-b947-11e7-abc4-cec278b6b50a",
//UUID在这网站上随机生成:
//https://www.uuidgenerator.net/
"level": 1,
"alterId": 64 //客户id,客户端上必须有同样数字的id
}
],
"detour":{
"to":"dynamicPort" //随机端口,防止运营商封大流量端口,
//只需要在服务器上设置
}
},
"streamSettings":{
"network":"kcp", //打开mKcp,网速加速,详细设置在此配置文件末尾
}
},
"inboundDetour":[
{
"protocol": "vmess",
"port": "10000-20000", //随机端口范围
"tag": "dynamicPort",
"settings": {
"default": {
"level": 1,
"alterId": 32
}
},
"allocate": {
"strategy": "random",
"concurrency": 2,
"refresh": 3
},
"streamSettings": {
"network": "kcp" //随机端口也要使用mKcp
}
}
],
"outbound": {
"protocol": "freedom",
"settings": {}
},
"outboundDetour": [
{
"protocol": "blackhole",
"settings": {},
"tag": "blocked"
}
],
"transport":{
"tcpSettings":{
"connectionReuse":true
},
"kcpSettings":{ //mKCP设置,请看官方和其他教程设置
"mtu":1350,
"tti":20,
"uplinkCapacity":100,
"downlinkCapacity":100,
"congestion":false,
"readBufferSize":5,
"writeBufferSize":5,
"header":{
"type":"utp"
}
}
}
}
保存文件之后可以测试配置文件是否正确:
jq . /etc/v2ray/config.json
/usr/bin/v2ray/v2ray -test -config /etc/v2ray/config.json
确保这两个命令都没有出错,就可以启动服务器端的V2ray了:
service v2ray restart
本地网关V2Ray设置
我用一台运行着Debian虚拟机的家用FreeNas服务器来作为网关,也可以换成任何可以运行V2ray的设备,比如树莓派或者高性能的Open-wrt的路由器。
同样也是用脚本安装V2Ray(运行在其他架构比如说mips路由芯片的可以下载V2Ray编译好的二进制文件):
bash <(curl -L -s https://install.direct/go.sh)
安装好后编辑配置文件/etc/v2ray/config.json
,同样注意不要把注释也带进去,复制粘贴的要把注释删掉:
{
"log": {
"access": "/var/log/v2ray/access.log",
"error": "/var/log/v2ray/error.log",
"loglevel": "warning"
},
"outbound": {
"protocol": "vmess",
"settings":{
"vnext":[
{
"address":"VPS服务器ip地址",
"port": 23018, //端口号和VPS上的配置要一致
"users": [
{
"id": "28f5f778-b947-11e7-abc4-cec278b6b50a",
"level": 1,
"alterId": 64
}
]
}
]
},
"streamSettings": {
"network": "kcp"
},
"mux": {"enabled": true} //mux加速,只需在客户端设置
},
"outboundDetour": [
{
"protocol":"freedom",
"settings":{
},
"tag":"direct"
}
],
"inbound": {
"protocol": "dokodemo-door",
"port": 5354, //Dnsmasq查询代理
"settings": {
"address": "8.8.8.8",
"port": 53,
"network": "udp",
"timeout": 0,
"followRedirect": false
}
},
"inboundDetour": [
{
"domainOverride":["http", "tls"], //如果有需要直连访问某个境外域名,需要加上这句,并在routing里设置相关域名
"protocol": "dokodemo-door",
"port":1060, //所有1060 tcp端口流量走v2ray代理
"settings":{
"network": "tcp",
"timeout": 30,
"followRedirect": true
}
},
{
"protocol":"socks",
"port":8080, //8080端口socks5代理备用
"settings":{
"auth":"noauth",
"udp":false,
"ip":"127.0.0.1"
}
}
],
"routing": {//设置除了80,443端口外都走直连,这样可以避免BT等应用占用VPS带宽和触犯版权法;设置国内的域名和ip直连
"strategy":"rules",
"settings":{
"domainStrategy":"IPIfNonMatch",
"rules":[
{
"type":"field",
"port":"1-52",
"outboundTag":"direct"
},
{
"type":"field",
"port":"54-79",
"outboundTag":"direct"
},
{
"type":"field",
"port":"81-442",
"outboundTag":"direct"
},
{
"type":"field",
"port":"444-3999",
"outboundTag":"direct"
},
{
"type":"field",
"port":"4001-65535", //4000是Resilio Sync使用的端口
"outboundTag":"direct"
},
"domain":[ //设置需要直接连接的境外域名,要与上面的domainOverride配合使用
"vultr.com"
],
"type":"field",
"outboundTag":"direct"
},
{
"type":"chinasites",
"outboundTag":"direct"
},
{
"type":"field",
"ip":[
"0.0.0.0/8",
"10.0.0.0/8",
"100.64.0.0/10",
"127.0.0.0/8",
"169.254.0.0/16",
"172.16.0.0/12",
"192.0.0.0/24",
"192.0.2.0/24",
"192.168.0.0/16",
"198.18.0.0/15",
"198.51.100.0/24",
"203.0.113.0/24",
"::1/128",
"fc00::/7",
"fe80::/10"
],
"outboundTag":"direct"
},
{
"type":"chinaip",
"outboundTag":"direct"
}
]
}
},
"transport":{
"tcpSettings":{
"connectionReuse":true
},
"kcpSettings":{
"mtu":1350,
"tti":20,
"uplinkCapacity":100,
"downlinkCapacity":100,
"congestion":false,
"readBufferSize":5,
"writeBufferSize":5,
"header":{
"type":"utp"
}
}
}
}
测试/usr/bin/v2ray/v2ray -test -config /etc/v2ray/config.json
如果没问题就可以启动客户端这边的V2Ray了:
service v2ray restart
service v2ray status
到目前为止已经搭建好了一个以客户端地址为网关地址,可以根据IP和域名判断走代理还是直连的v2ray代理服务器。
网关iptables设置
需要设置iptables把所有tcp流量导入网关的1060端口:
iptables -t nat -N V2RAY
iptables -t nat -A V2RAY -d 11.22.33.44 -j RETURN //改成你的 VPS 的 IP
iptables -t nat -A V2RAY -d 0.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 10.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 127.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 169.254.0.0/16 -j RETURN
iptables -t nat -A V2RAY -d 172.16.0.0/12 -j RETURN
iptables -t nat -A V2RAY -d 192.168.0.0/16 -j RETURN
iptables -t nat -A V2RAY -d 224.0.0.0/4 -j RETURN
iptables -t nat -A V2RAY -d 240.0.0.0/4 -j RETURN
iptables -t nat -A V2RAY -p tcp -j REDIRECT --to-ports 1060
iptables -t nat -A PREROUTING -p tcp -j V2RAY
这个方法是参考利用 V2Ray + GFWList 实现路由器自动翻墙,我简化了一下,而且每次重开机都要重新设置iptables,原文有自动运行的脚本。
解决DNS污染
用V2ray转发dnsmasq的非标准端口查询GFWlist里面的域名(好像有了代理再用非标准端口查询有点多此一举?),从而避免DNS查询结果被污染。GFWlist配置文件使用gfwlist2dnmasq这个脚本来生成。
apt-get install dnsmasq
nano /etc/dnsmasq.conf
dnsmasq配置文件:
listen-address=127.0.0.1
listen-address=192.168.xx.xxx //你的网关地址
cache-size=102400
conf-dir=/etc/dnsmasq.d
墙内墙外域名分开查询
git clone https://github.com/cokebar/gfwlist2dnsmasq
cd gfwlist2dnsmasq
./gfwlist2dnsmasq.sh -d 127.0.0.1 -p 5354 -o dnsmasq_gfwlist.conf
//5354 是V2ray配置的转发dns查询端口号
mkdir /etc/dnsmasq.d
cp dnsmasq_gfwlist.conf /etc/dnsmasq.d
cd /etc/dnsmasq.d
wget https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf
很奇怪我这里114DNS突然不能解析网易云音乐的媒体文件服务器了,所以用114DNS听不了网易云音乐,看了下dnsmasq的记录居然是查询了网易ipv6的地址可是114不是ipv6/ipv4双栈的服务器,所以要在accelerated-domains.china.conf
里改一下:
把server=/126.net/114.114.114.114
和server=/163.com/114.114.114.114
改成server=/126.net/202.120.2.101
和server=/163.com/202.120.2.101
然后重启dnsmasq
service dnsmasq restart
局域网路由器网关设置
我用的是OpenWrt系统的路由器,版本号是Chaos Calmer 15.05。
设置网关和DNS服务器的地方在网络 -> 接口 -> LAN
页面下方DHCP服务器
的高级设置
里:DHCP-选项
输入3,192.168.xx.xxx
和6,192.168.xx.xxx
(你的网关设备内网ip地址)
其他类型的路由器请自行寻找设置网关和DNS服务器的方法。或者在局域网各设备上分别设置。
清除局域网设备dns缓存
windows设备在命令行输入
ipconfig /flushdns
Android和iOS设备开一下飞行模式就好。
没有Mac,请自己搜索解决方法。
完成
以上步骤都完成之后,没有意外的话应该都可以自由爱国上网了。
如果觉得本文对你有所帮助,可以通过我的邀请连接注册Vultr。