1.1. 设置网关
- 用网线将树莓派接入路由器 LAN 口,假设分给树莓派的 IP 是 192.168.1.22。
- 树莓派开启 IP 转发(需要开启 IP 转发才能作为网关)。命令为
echo net.ipv4.ip_forward=1 >> /etc/sysctl.conf && sysctl -p
。执行后将出现 net.ipv4.ip_forward=1 的提示。 - 手动配置 PC 的网络,将默认网关指向树莓派的地址即
192.168.1.22
。此时 PC 应当能正常上网(由于还没设置代理,“正常”是指可以上国内的网站)。
1.2. 树莓派安装配置 V2Ray
- 安装 V2Ray。可以使用 V2Ray 提供的 go.sh 脚本安装,由于 GFW 会恶化对 GitHub 的访问,直接运行脚本几乎无法安装,建议先下载 V2Ray 的压缩包,然后用安装脚本通过 –local 参数进行安装。
因为是国内的网络环境,官方的install.direct一键安装脚本无法正常使用,所以这里改为手动安装,首先下载最新版本的V2Ray/解压:
mkdir -p /opt/v2ray && cd /opt/v2ray
wget https://github.com/v2ray/v2ray-core/releases/download/v4.20.0/v2ray-linux-64.zip
unzip v2ray-linux-64.zip
创建需要的目录:
mkdir -p /usr/bin/v2ray /etc/v2ray
移动文件到对应的目录:
cp v2ctl /usr/bin/v2ray
cp v2ray /usr/bin/v2ray
cp geoip.dat /usr/bin/v2ray
cp geosite.dat /usr/bin/v2ray
cp vpoint_vmess_freedom.json /etc/v2ray/config.json
cp systemd/v2ray.service /etc/systemd/system/v2ray.service
编辑systemd服务文件:
vi /etc/systemd/system/v2ray.service
在[Service]下面加一行,解决too many open files的问题:
LimitNOFILE=1048576
启动V2Ray服务:
systemctl daemon-reload
systemctl start v2ray.service
systemctl enable v2ray.service
现在编辑V2Ray的配置文件,清空里面的所有配置:
{
"inbounds": [
{
"port": 1080,
"protocol": "socks",
"settings": {
"auth": "noauth",
"udp": true
}
},
{
"port": 12315, // 透明代理开放的端口号
"protocol": "dokodemo-door",
"settings": {
"network": "tcp,udp",
"followRedirect": true // 这里要为true才能接受来自iptables的流量
},
"sniffing": {
"enabled": true,
"destOverride": ["http", "tls"]
}
}
],
"outbounds": [
{
"protocol": "vmess",
"settings": {
"vnext": [
{
"address": "lala.im", // 服务器地址,请修改为你自己的服务器IP或域名。
"port": 50000, // 服务器端口,与服务器上的配置文件要相同
"users": [
{
"id": "你的UUID", // 用户的UUID必须与服务器端配置相同
"alterId": 64 // 此处的值也应当与服务器相同
}
]
}
]
}
}
]
}
第二种对流量进行智能路由,需要的域名走代理,国内的域名/IP则直连,因为这套配置还使用了外部GEO文件,所以要想正常使用,还需要先下载外部GEO文件到V2Ray的运行目录:
wget https://github.com/ToutyRater/V2Ray-SiteDAT/raw/master/geofiles/h2y.dat -O /usr/bin/v2ray/h2y.dat
配置如下:
{
"inbounds": [
{
"port": 1080,
"protocol": "socks",
"settings": {
"auth": "noauth",
"udp": true
}
},
{
"port": 12315, // 透明代理开放的端口号
"protocol": "dokodemo-door",
"settings": {
"network": "tcp,udp",
"followRedirect": true // 这里要为true才能接受来自iptables的流量
},
"sniffing": {
"enabled": true,
"destOverride": ["http", "tls"]
}
}
],
"outbounds": [
{
"tag": "proxy", // 打一个TAG,让外部GeoFile使用此TAG处理被GFW屏蔽的域名
"protocol": "vmess",
"settings": {
"vnext": [
{
"address": "lala.im", // 服务器地址,请修改为你自己的服务器IP或域名。
"port": 50000, // 服务器端口,与服务器上的配置文件要相同
"users": [
{
"id": "你的UUID", // 用户的UUID必须与服务器端配置相同
"alterId": 64 // 此处的值也应当与服务器相同
}
]
}
]
}
},
{
"tag": "block", // 黑洞TAG,让外部GeoFile使用此TAG屏蔽广告域名
"protocol": "blackhole",
"settings": {}
},
{
"tag": "direct", // 直连TAG,处理国内域名和IP使其直连
"protocol": "freedom",
"settings": {}
}
],
"routing": {
"domainStrategy": "IPOnDemand",
"rules": [
{
"type": "field",
"outboundTag": "proxy",
"domain": ["ext:h2y.dat:gfw"] // GFWList
},
{
"type": "field",
"outboundTag": "block",
"domain": ["ext:h2y.dat:ad"] // 广告域名屏蔽
},
{
"type": "field",
"outboundTag": "direct",
"domain": ["geosite:cn"] // 中国大陆主流网站的域名
},
{
"type": "field",
"outboundTag": "direct",
"ip": [
"geoip:cn", // 中国大陆的IP
"geoip:private" // 私有地址IP,如路由器等
]
}
]
}
}
配置完成之后测试:
/usr/bin/v2ray/v2ray -config /etc/v2ray/config.json -test
有错排错没问题的话重启V2Ray:
systemctl restart v2ray.service
最后创建iptables规则,对流量进行处理:
iptables -t nat -N V2RAY
iptables -t nat -A V2RAY -d 192.168.0.0/16 -j RETURN
iptables -t nat -A V2RAY -p tcp -j REDIRECT --to-ports 12315
iptables -t nat -A PREROUTING -p tcp -j V2RAY
局域网内的其他机器,修改网关IP为这台虚拟机的IP即可:
现在只要接入你这个网络的设备,无论是电脑还是手机等,都可以实现自动翻墙/广告屏蔽等功能。
一点补充:
如果你使用第一套配置(全局代理)其实也可以实现对流量进行路由,并且据说这种路由方法比V2Ray自带的原生GeoIP方法效率更高,更适合跑在CPU性能不咋地的路由器上。
先重启机器清空所有iptables规则:
reboot
安装ipset:
apt -y install ipset
下载中国IP地址列表:
wget https://raw.githubusercontent.com/17mon/china_ip_list/master/china_ip_list.txt
创建一个ipset链:
ipset -N cn hash:net
将中国的IP都加入到ipset链:
for i in $(cat china_ip_list.txt); do ipset -A cn $i; done
执行下面的命令处理流量:
iptables -t nat -N V2RAY
iptables -t nat -A V2RAY -d 192.168.0.0/16 -j RETURN
iptables -t nat -A V2RAY -p tcp -m set --match-set cn dst -j RETURN
iptables -t nat -A V2RAY -p tcp -j REDIRECT --to-ports 12315
iptables -t nat -A PREROUTING -p tcp -j V2RAY
文章转载请说明出处:八零岁月 » V2Ray透明代理/透明网关/广告屏蔽/路由器翻墙
评论前必须登录!