一、RouterOS 主路由 —— 最简透明代理配置
1. 定义 fake-ip 段(OpenClash 使用的虚假 IP)
假设你用的是:`198.18.0.0/15`/ip firewall address-list
add list=proxy_cidr address=198.18.0.0/15
如果你用 28.x.x.x,则改成:
add list=proxy_cidr address=28.0.0.0/8
2. 定义需要走代理的设备
(你可以全部设备,也可以只有某些设备)
例:所有设备
add list=proxy_device address=10.10.10.0/24
3. DNS 劫持(proxy_device的 DNS → OpenWrt 10.10.10.252)
DNS → OpenWrt → Clash (FakeIP/TUN) Fake-IP 会自动被 Clash 转到 1053,无需你设置。
/ip firewall nat
add chain=dstnat action=dst-nat dst-address=10.10.10.253 protocol=udp dst-port=53 \
src-address-list=proxy_device to-addresses=10.10.10.252 to-ports=53 \
comment=fakeip_proxy
add chain=dstnat action=dst-nat dst-address=10.10.10.253 protocol=tcp dst-port=53 \
src-address-list=proxy_device to-addresses=10.10.10.252 to-ports=53 \
comment=fakeip_proxy
4. 路由配置
所有访问 Fake-IP 段 198.18.0.0/15 的连接,都丢给 OpenWrt 处理:所有目标为 **198.18.0.0/15** 的流量都经过 **OpenWrt** 处理/ip route
add dst-address=198.18.0.0/15 gateway=10.10.10.252 comment=fakeip_proxy
5. NAT(网络地址转换)
/ip firewall nat
add chain=srcnat action=masquerade src-address=10.10.10.0/24 dst-address=10.10.10.252 \
comment=fakeip_proxy
6. 防火墙
把规则拖到前面 放行到 Fake-IP 段(本质是转发到 OpenWrt)的流量/ip firewall filter
add chain=forward dst-address-list=proxy_cidr action=accept comment=fakeip_proxy
二、Netwatch:监控 OpenWrt 状态
加入“自动保护脚本”(Netwatch),当 OpenClash 掉线时:
- 透明代理立即关闭(避免黑洞)
- 全网恢复直连,不影响国内访问
Netwatch 脚本
当 OpenClash 正常,我们需要启用 TCP 和 UDP 流量的 DNAT 规则,同时确保连接的清除。
/tool netwatch
add host=10.10.10.252 interval=5s timeout=3s type=icmp down-script="
:log warning \"Fake-IP Proxy DOWN — disabling rules\";
/ip firewall nat set [find comment=\"fakeip_proxy\"] disabled=yes;
/ip firewall filter set [find comment=\"fakeip_proxy\"] disabled=yes;
/ip route set [find comment=\"fakeip_proxy\"] disabled=yes;
/ip firewall connection remove [find dst-port=53];
" up-script="
:log warning \"Fake-IP Proxy UP — enabling rules\";
/ip firewall nat set [find comment=\"fakeip_proxy\"] disabled=no;
/ip firewall filter set [find comment=\"fakeip_proxy\"] disabled=no;
/ip route set [find comment=\"fakeip_proxy\"] disabled=no;
" comment="watch_openwrt_fakeip"
host=10.10.10.252 监控的目标设备 IP 你的 OpenWrt 代理盒子 interval=5s 每隔 5 秒检测一次 检测内容取决于 type(默认 icmp) timeout=1s Ping 超过3 秒没有回应 视为失败 也就是说: 如果 3 秒内没回复,就算“Down” 如果 has reply,则判定为“Up” type=icmp 检测方式为 ICMP(ping) 这是最适合 LAN 内检测设备是否在线的方法。
三、OpenWrt(10.10.10.252)设置要求
你需要确保:
- Clash 开启 fake-ip 模式
dns:
enable: true
enhanced-mode: fake-ip
fake-ip-range: 198.18.0.1/15
- Clash 必须开启 tun
tun:
enable: true
dns-hijack:
- any:53
- OpenWrt 的默认网关必须是 RouterOS
10.10.10.253
流量工作流程图
┌─────────────┐
│ 客户端手机、电脑 │
└───────┬─────┘
│ DNS
▼
┌──────────────────────────┐
│ RouterOS 主路由 10.10.10.253 │
│ ① DNS 劫持 → OpenWrt │
│ ② RouteList → 10.10.10.252 │
└───────┬──────────────────┘
│ 透明代理的流量
▼
┌──────────────────────────┐
│ OpenWrt + OpenClash 10.10.10.252 │
│ Fake-IP → 代理节点 → 回程到 ROS │
└───────┬──────────────────┘
│ 回程流量
▼
┌──────────────┐
│ RouterOS NAT出口 │
└──────────────┘