EdgeMax EdgeRouter ER-X(以下简称ER-X)曾经一度被广大网友评价为弱电箱神器。体积小、运行稳定,设置好扔到弱电箱里,基本无视它的存在。这款路由器带有POE供电、256M的Flash、256M的RAM,原生debian系统。处理器是MT7621,带硬件转发,可玩性非常高。这篇文章将为大家介绍如何在ER-X上搭建v2ray透明代理。
一、环境
ERX使用目前官方提供的最新版固件:EdgeRouter X v2.0.9-hotfix.4
二、远程服务器端
远程服务器端使用本站提供的V2ray多合一脚本安装VMESS+websocket+TLS+Nginx。
bash <(curl -sL https://raw.githubusercontent.com/hiifeng/v2ray/main/install_v2ray.sh)三、在ER-X上安装v2ray
ER-X路由比较好的就是有原生的debian,所以这里我们可以直接在ER-X安装相应的支持包,不过得先更新一下源。我目前使用的是官方最新版固件EdgeRouter X v2.0.9-hotfix.4。在设置完成可以通过PPPOE上网后,进行如下操作。
1、查看并删除路由器上的多余固件,腾点空间出来
root@ubnt:~# show system image
The system currently has the following image(s) installed:
v2.0.9-hotfix.4.5521907.220630.0657 (running image) (default boot)
v2.0.9-hotfix.2.5402463.210511.1317通过以上命令,我们可以看到这块路由器使用的是v2.0.9-hotfix.4固件,我们可以执行以下命令将v2.0.9-hotfix.2固件删除。
root@ubnt:~# delete system image
The system currently has the following image(s) installed:
v2.0.9-hotfix.4.5521907.220630.0657 (running image) (default boot)
v2.0.9-hotfix.2.5402463.210511.1317
You are about to delete image [v2.0.9-hotfix.2.5402463.210511.1317]
Are you sure you want to delete ? (Yes/No) [Yes]: y
Removing old image... Done2、更新软件源,安装unzip、daemon
sudo -i
#能走进水木清华,是我曾经一度的梦想,就用一下清华源吧。
echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free" > /etc/apt/sources.list
apt-get update
apt-get install unzip
apt-get install daemon
#路由器空间寸土寸金,我们清除掉apt缓存
apt-get clean3、在ER-X上安装v2ray
ER-X使用的是mipsle安装包,目前v2ray最新版本是v5.1.0,我们使用如下地址下载对应的安装包。
https://github.com/v2fly/v2ray-core/releases/download/v5.1.0/v2ray-linux-mips32le.zip
然后使用WinSCP将v2ray-linux-mips32le.zip上传到ER-X的/tmp目录后,执行如下命令。
cd /tmp/
unzip -d v2ray-linux-mips32le v2ray-linux-mips32le.zip
mkdir -p /usr/local/v2ray /etc/v2ray
cp v2ray-linux-mips32le/v2* /usr/local/v2ray/
cp v2ray-linux-mips32le/geo* /usr/local/v2ray/
cp v2ray-linux-mips32le/vpoint_vmess_freedom.json /etc/v2ray/config.json
cp v2ray-linux-mips32le/systemd/system/v2ray.service /lib/systemd/system/
sed -i 's/ExecStart=\/usr\/local\/bin\/v2ray run -config \/usr\/local\/etc\/v2ray\/config.json/ExecStart=\/usr\/local\/v2ray\/v2ray run -config \/etc\/v2ray\/config.json/g' /lib/systemd/system/v2ray.service
systemctl daemon-reload
systemctl enable v2ray
systemctl start v2ray4、配置ER-X上的v2ray
修改/etc/v2ray/config.json,可以参考以下配置文件。由于ER-X是阉割版的系统,没有TPROXY模块,我们只能使用REDIRECT模式的透明代理,仅支持TCP/IPv4和 UDP连接。TPROXY与REDIRECT是针对TCP而言的两种透明代理模式,两者的差异主要在于TPROXY可以透明代理IPV6,而REDIRECT不行。修改config.json配置文件一定细心,少一个}或,就会出错。
{
"inbounds": [
{
"tag":"transparent",
"port": 10250,
"protocol": "dokodemo-door",
"settings": {
"network": "tcp,udp",
"followRedirect": true
},
"sniffing": {
"enabled": true,
"destOverride": [
"http",
"tls"
]
},
"streamSettings": {
"sockopt": {
"tproxy": "redirect", // 透明代理使用 redirect 方式
"mark":255
}
}
},
{
"port": 1080,
"protocol": "socks", // 入口协议为 SOCKS 5
"sniffing": {
"enabled": true,
"destOverride": ["http", "tls"]
},
"settings": {
"auth": "noauth"
}
}
],
"outbounds": [
{
"tag": "proxy",
"protocol": "vmess", // 代理服务器
"settings": {
"vnext": [
{
"address": "example.ifeng.co",
"port": 443,
"users": [
{
"id": "5ccc541b-1260-4e75-b902-224333320487",
"alterId": 0,
"email": "[email protected]",
"security": "auto"
}
]
}
]
},
"streamSettings": {
"network": "ws",
"security": "tls",
"tlsSettings": {
"allowInsecure": false,
"serverName": "example.ifeng.co"
},
"wsSettings": {
"path": "/example",
"headers": {
"Host": "example.ifeng.co"
}
},
"sockopt": {
"mark": 255
}
},
"mux": {
"enabled": true
}
},
{
"tag": "block",
"protocol": "blackhole",
"settings": {
"response": {
"type": "http"
}
}
},
{
"tag": "direct",
"protocol": "freedom",
"settings": {
"domainStrategy": "UseIP"
},
"streamSettings": {
"sockopt": {
"mark": 255
}
}
}
],
"routing": {
"strategy": "rules",
"domainStrategy": "IPIfNonMatch",
"settings": {
"rules": [
{ // 广告拦截
"type": "field",
"domain": [
"geosite:category-ads-all"
],
"outboundTag": "block"
},
{ // BT 流量直连
"type": "field",
"protocol":["bittorrent"],
"outboundTag": "direct"
},
{// 直连中国大陆主流网站 ip 和 保留 ip
"type": "field",
"ip": [
"geoip:private",
"geoip:cn"
],
"outboundTag": "direct"
},
{// 直连中国大陆主流网站域名
"type": "field",
"domain": ["geosite:cn"],
"outboundTag": "direct"
}
]
}
}
}修改好配置文件后可以使用如下命令,检查配置文件中有无错误。
/usr/local/v2ray/v2ray test配置文件没有错误后,重新启动v2ray
systemctl restrat v2ray然后执行如下命令,检查是否可以科学上网。(命令中 socks5指inbound协议为 socks,1080指该 inbound端口是 1080)。如果执行这个命令出现了301或200这类数字的话代表可以科学上网,如果长时间没反应或者是000的话说明不行。
curl -so /dev/null -w "%{http_code}" google.com -x socks5://127.0.0.1:1080四、配置透明代理规则
执行下面的命令开启透明代理。
#新建一个nat链,排除私网地址流量
iptables -t nat -N V2RAY
iptables -t nat -A V2RAY -p tcp -j RETURN -m mark --mark 0xff
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 10250
#重定向局域网设备过来的TCP流量
iptables -t nat -A PREROUTING -p tcp -j V2RAY
#重定向ER-X路由器(本机)过来的TCP流量
iptables -t nat -A OUTPUT -p tcp -j V2RAY如果只允许局域网中的某个终端(例如电视机顶盒)科学上网,可以将第14行命令替换为如下命令,只允许某个ip的流量
iptables -t nat -A PREROUTING -s 192.168.0.5/32 -p tcp -j V2RAY五、开机自动运行透明代理规则
由于iptables有重启会失效的特性,所以当测试配置没有问题之后,需要在开机时自动配置iptables,否则每次开机的时候就要手动执行一遍。
ER-X路由器开机会自动运行/config/scripts/post-config.d/目录下的脚本文件,我们创建名为v2ray_iptables.sh的脚本,内容如下。
#!/bin/sh
#新建一个nat链,排除私网地址流量
iptables -t nat -N V2RAY
iptables -t nat -A V2RAY -p tcp -j RETURN -m mark --mark 0xff
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 10250
#重定向局域网设备过来的TCP流量
iptables -t nat -A PREROUTING -p tcp -j V2RAY
#重定向ER-X路由器(本机)过来的TCP流量
iptables -t nat -A OUTPUT -p tcp -j V2RAY最后将脚本文件修改为可执行文件。
chmod a+x /config/scripts/post-config.d/v2ray_iptables.sh
参考文献:https://guide.v2fly.org/app/tproxy.htmlhttps://www.anywlan.com/article-4758-1.htmlhttps://www.v2ray.com/chapter_02/05_transport.htmlhttps://xdays.me/V2RAY透明代理/https://www.cnblogs.com/zclzhao/p/5081590.html
本文出处:HiFeng'Blog本文链接:https://www.hicairo.com/post/39.html版权声明:本博客所有文章除特别声明外,均采用CC BY-NC-SA许可协议。转载请注明出处!