起因
博主的同桌在去年年底入手了一台 Switch,但最近他发现联网游戏的掉线次数越来越多,eShop 也经常连接不上。在博主的安利下,他决定买一台极路由 4 增强版刷 OpenWRT 做透明代理,蹭博主的 VPS 来加速游戏,顺便看看 YouTube……
情况
考虑到 Shadowsocks 的安全性,所以博主的 VPS 上并未部署这一类软件,而是部署的是更加复杂的 V2Ray。
也正因为 V2Ray 配置的复杂,博主踩了些坑。最终博主决定记下这次折腾,即如何在 OpenWRT 路由器上运行并设置透明代理,同时也希望这篇文章帮到一部分有同样需求的人。
做好准备工作
以下的操作,都是在刷好了 Breed 的前提下操作的,至于怎么刷 Breed,本文不做介绍。
固件的选用
不太喜欢 PandoraBOX 这一类的固件,太臃肿了,作为一个有心理洁癖的人,博主更加愿意自行编译固件,但这样太浪费时间,所以只好将就用用 OpenWRT 官方提供的固件,也比较纯净了。
极路由 4 增强版的型号是 HC5962,处理器是 MT7621,所以前往 downloads.openwrt.org/releases/18.06.0/targets/ramips/mt7621 下载,博主选择的是 18.06.0 版本。
由于是在 Breed 中刷入,所以对应的固件文件应该是 hc5962-squashfs-factory.bin
,而非 hc5962-squashfs-sysupgrade.bin
。
刷机操作博主也不做赘述,请自行 Google。
完成后务必为路由器设定管理密码,以便后续可以使用 vsftpd 连接上传文件。
软件和软件源
和众多 Linux 发行版一样,OpenWRT 虽小,但也提供了比较完善的软件包,位于 downloads.openwrt.org,但国内访问 downloads.openwrt.org 速度感人,所以有必要替换成镜像源,可惜的是,OpenWRT 的软件镜像源极少,在重庆地区稍微快的是加拿大的 openwrt.freemirror.org。此外,上述的固件也可以前往此镜像站下载。
在 LuCI > System > Software > Configuration > Distribution feeds 中,这样替换软件源:
1 2 3 4 5 6
| src/gz openwrt_core http://openwrt.freemirror.org/releases/18.06.0/targets/ramips/mt7621/packages src/gz openwrt_base http://openwrt.freemirror.org/releases/18.06.0/packages/mipsel_24kc/base src/gz openwrt_luci http://openwrt.freemirror.org/releases/18.06.0/packages/mipsel_24kc/luci src/gz openwrt_packages http://openwrt.freemirror.org/releases/18.06.0/packages/mipsel_24kc/packages src/gz openwrt_routing http://openwrt.freemirror.org/releases/18.06.0/packages/mipsel_24kc/routing src/gz openwrt_telephony http://openwrt.freemirror.org/releases/18.06.0/packages/mipsel_24kc/telephony
|
根据自己的网络状况,可以将 option check_signature 1
(检查签名)一行注释或是删除掉。
使用 PuTTY 连接路由器,在终端中执指令,更新软件列表。
1
| root@OpenWrt:~# opkg update
|
安装 vsftpd、iptables-mod-tproxy、kmod-ipt-tproxy、ca-certificates
1
| root@OpenWrt:~# opkg install vsftpd iptables-mod-tproxy kmod-ipt-tproxy ca-certificates
|
使用 FileZilla 的 FTP 协议与路由器建立连接,分别在 /usr/bin/
、/etc/
下新建名为 v2ray
的文件夹,上传事先下载并已经解压(v2ray-linux-mipsle.zip)的预编译 V2Ray 二进制文件(v2ray_softfloat、v2ctl_softfloat)到 /usr/bin/v2ray/
,更名为 v2ray、v2ctl。
需要注意的是,文件必须以二进制(Binary)形式上传,博主在这里被坑了半个多小时……
为了节省空间可以事先使用 upx 对文件压缩,upx 的压缩命令如下。
1
| root@yukiho:~# upx -o [compressed] [original]
|
方便起见,博主将压缩好的 V2Ray 分享出来(4.18.1 版、Linux Mipsle 架构适用、Softfloat 软件浮点)。
下载
赋予 755 权限。
1
| root@OpenWrt:~# chmod +x /usr/bin/v2ray/v2ray /usr/bin/v2ray/v2ctl
|
博主习惯使用 screen 后台运行程序,但由于 OpenWRT 没有 screen,所以博主只好自行编译了一份 ipk,也分享出来。
下载
上传 ipk 到路由器 /tmp
目录下,然后安装。
1
| root@OpenWrt:~# opkg install /tmp/screen_4.6.2-1_mipsel_24kc.ipk
|
配置文件
博主的 VPS 选用了 WebSocket + Nginx + TLS 的方案,所以配置文件示范如下,酌情更改。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| { "inbounds": [ { "port": 1080, "listen": "0.0.0.0", "protocol": "socks", "sniffing": { "enabled": true, "destOverride": ["http", "tls"] }, "settings": { "auth": "noauth", "udp": true } }, { "port": 8080, "listen": "0.0.0.0", "protocol": "http", "sniffing": { "enabled": true, "destOverride": ["http", "tls"] }, "settings": { "timeout": 300 } }, { "port": 12345, "protocol": "dokodemo-door", "sniffing": { "enabled": true, "destOverride": ["http", "tls"] }, "settings": { "network": "tcp,udp", "timeout": 0, "followRedirect": true } }, { "protocol": "dokodemo-door", "port": 5353, "listen": "127.0.0.1", "settings": { "address": "1.0.0.1", "port": 53, "network": "udp", "timeout": 0 } } ], "outbounds": [ { "protocol": "vmess", "settings": { "vnext": [ { "address": "example.org", "port": 443, "users": [ { "id": "ad335920-7932-11e9-b475-0800200c9a66", "security": "aes-128-gcm", "alterId": 64 } ] } ] }, "streamSettings": { "network": "ws", "security": "tls", "wsSettings": { "path": "/internal-proxy" } } } ] }
|
配置文件使用 UTF-8 编码、UNIX LF 换行符上传到 /etc/v2ray
。
编辑 /etc/hosts
,填入服务器 IP 地址。
加入开机启动项
编辑 /etc/rc.local
,于 exit 0
前加入以下命令。
1
| sleep 5 && screen -dmS v2ray /usr/bin/v2ray/v2ray -config=/etc/v2ray/config.json
|
保存。
DNS 转发设置
LuCI > Network > Interfaces > WAN > Advanced Settings > 取消勾选 Use DNS servers advertised by peer。其余设置不变,保存并应用(Save & Apply)。
LuCI > Interfaces > LAN > DHCP Server > 将 6,192.168.1.1
填入 DHCP-Options 项中 。192.168.1.1 是设备的管理地址。其余设置不变,保存并应用(Save & Apply)。此举是向每台连接到路由器的设备通告 DNS 地址为网关地址。
LuCI > Network > DHCP and DNS > General Settings > 将 127.0.0.1#5353
填入 DNS forwardings。其余设置不变,保存并应用(Save & Apply)。此举是将所有 DNS 解析请求全部转发到服务器,防止本地 ISP 投毒。
iptables 转发设置
LuCI > Network > Firewall > Custom Rules > 复制以下内容,其中,12345 是 dokodemo-door
的端口。另外,需要修改 12.34.56.78 为服务器的真实 IP。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| # Chain TCP iptables -t nat -N V2RAY # Reserved IP TCP 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 # VPS IP iptables -t nat -A V2RAY -d 12.34.56.78 -j RETURN # Apply Forwarding Rules TCP iptables -t nat -A V2RAY -p tcp -j REDIRECT --to-ports 12345 iptables -t nat -A PREROUTING -p tcp -j V2RAY iptables -t nat -A OUTPUT -p tcp -j V2RAY # Chain UDP iptables -t mangle -N V2RAYUDP # Reserved IP UDP iptables -t mangle -A V2RAYUDP -d 0.0.0.0/8 -j RETURN iptables -t mangle -A V2RAYUDP -d 10.0.0.0/8 -j RETURN iptables -t mangle -A V2RAYUDP -d 127.0.0.0/8 -j RETURN iptables -t mangle -A V2RAYUDP -d 169.254.0.0/16 -j RETURN iptables -t mangle -A V2RAYUDP -d 172.16.0.0/12 -j RETURN iptables -t mangle -A V2RAYUDP -d 192.168.0.0/16 -j RETURN iptables -t mangle -A V2RAYUDP -d 224.0.0.0/4 -j RETURN iptables -t mangle -A V2RAYUDP -d 240.0.0.0/4 -j RETURN # NTP IP iptables -t mangle -A V2RAYUDP -d 133.243.238.163 -j RETURN iptables -t mangle -A V2RAYUDP -d 133.243.238.243 -j RETURN iptables -t mangle -A V2RAYUDP -d 133.243.238.244 -j RETURN iptables -t mangle -A V2RAYUDP -d 133.243.238.244 -j RETURN # Apply Forwarding Rules UDP iptables -t mangle -A V2RAYUDP -p udp -j TPROXY --on-port 12345 --tproxy-mark 0x01/0x01 iptables -t mangle -A PREROUTING -p udp -j V2RAYUDP ip rule add fwmark 1 table 100 ip route add local default dev lo table 100
|
然后,点击 Restart Firewall,应用设置
这样便转发了 TCP、UDP 流量。
由于 V2Ray 需要本地与服务器的时间误差不超过三分钟,所以必须设置 NTP 服务器直连以对时。博主使用的是日本 NICT 的 NTP 服务器。所以在 LuCI > System > System > Time Synchronization 中勾选 Enable NTP client ,并更改 NTP 服务器为以下地址:
1 2 3 4
| 133.243.238.163 133.243.238.243 133.243.238.244 133.243.238.164
|
保存。
大功告成
LuCI > System > Reboot > Perform Reboot
等待路由器重启之后,试试 Google 能不能使用了呢?回头看,V2Ray 透明代理的实现其实也不是想象中那么困难。