qjfy
1
一、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出口 │
└──────────────┘
qjfy
4
openclash dns配置示例(优化)
dns:
enable: true
listen: 0.0.0.0:1053 # 监听所有接口的 DNS 请求
ipv6: false # 禁用 IPv6 支持
respect-rules: true # 启用规则模式进行 DNS 分流
enhanced-mode: fake-ip # 启用 fake-ip 模式
fake-ip-range: 198.18.0.1/15 # 设置 fake-ip
fake-ip-filter-mode: blacklist
fake-ip-filter:
- "rule-set:private_domain,cn_domain" # 对国内域名进行过滤
- "+.msftconnecttest.com"
- "+.msftncsi.com"
- "time.*.com"
# 设置 DNS 解析服务(使用本地 DNS 或公共 DNS)
nameserver:
- 223.5.5.5 # 使用阿里云 DNS
- 223.6.6.6 # 使用阿里云 DNS
- 10.10.10.253 # 使用 RouterOS 提供的本地 DNS
# 设置代理服务器的 DNS 配置(Cloudflare / Google DoH 用于解析国外域名)
proxy-server-nameserver:
- https://1.1.1.1/dns-query # 使用 Cloudflare DoH 来解析国外的域名
- https://8.8.8.8/dns-query # 使用 Google DoH 来解析国外的域名
qjfy
5
Telegram透明代理不能连接的问题解决
一、在 RouterOS 上创建 Telegram 地址列表
这些地址列表包含了 Telegram 的 IP 地址范围,我们继续使用这些地址列表来标记 Telegram 流量,这些 IP 地址范围确保 Telegram 流量会被正确标记并进行处理。
/ip firewall address-list
add address=91.108.56.0/22 list=telegram_addr
add address=91.108.4.0/22 list=telegram_addr
add address=91.108.8.0/22 list=telegram_addr
add address=91.108.16.0/22 list=telegram_addr
add address=91.108.12.0/22 list=telegram_addr
add address=149.154.160.0/20 list=telegram_addr
add address=91.105.192.0/23 list=telegram_addr
add address=91.108.20.0/22 list=telegram_addr
add address=185.76.151.0/24 list=telegram_addr
add address=149.154.168.0/22 list=telegram_addr
add address=149.154.172.0/22 list=telegram_addr
add address=5.28.192.0/18 list=telegram_addr
add address=95.161.64.0/20 list=telegram_addr
add address=109.239.140.0/24 list=telegram_addr
add address=91.108.0.0/16 list=telegram_addr
二、创建并配置路由表
你已经配置了一个名为 telegram_proxy 的路由表,并指向 OpenWrt 的网关(10.10.10.252)。这条配置是正确的,确保 Telegram 流量通过 OpenWrt 转发。
1.创建路由表
/routing/table/add name=telegram_proxy fib
2.将路由条目添加到新路由表
/ip/route/add gateway=10.10.10.252 routing-table=telegram_proxy comment=telegram_proxy_route
这条命令的作用是将标记为 telegram_proxy 的流量通过 OpenWrt 转发。确保 OpenWrt 的代理服务能够处理该流量。
三、在 RouterOS 上设置 mangle 规则
你已经正确地为 Telegram 流量添加了 mangle 规则,目的是根据目的地址(即 Telegram 的 IP 地址范围)来标记流量,标记后的流量将通过 telegram_proxy 路由表转发到 OpenWrt。
/ip/firewall/mangle/add chain=prerouting dst-address-list=telegram_addr action=mark-routing new-routing-mark=telegram_proxy passthrough=yes
这条规则会在路由表的预处理阶段(prerouting)对目的地址属于 telegram_addr 地址列表的流量进行标记,并将其路由到 telegram_proxy 路由表。
四、防火墙
这条规则允许 目的地址在 telegram_addr 地址列表中的流量(即 Telegram 的流量)通过转发链(forward),不指定协议和端口。因此,所有目的地是 Telegram 服务器的流量都会被接受,无论是 TCP 还是 UDP,或者是 443 端口或其它端口。
/ip firewall filter add chain=forward dst-address-list=telegram_addr action=accept comment="Allow Telegram traffic"
qjfy
6
批量修改网关脚本
脚本
:local oldIP "10.10.10.252"
:local newIP "10.10.10.7"
# 1. NAT 规则里改 to-addresses / dst-address
/ip firewall nat
:foreach i in=[find where comment="fakeip_proxy"] do={
:if ([:len [get $i to-addresses]] > 0) do={
:if ([get $i to-addresses] = $oldIP) do={
set $i to-addresses=$newIP
}
}
:if ([:len [get $i dst-address]] > 0) do={
:if ([get $i dst-address] = $oldIP) do={
set $i dst-address=$newIP
}
}
}
# 2. 路由里改 gateway
/ip route
:foreach i in=[find where comment="fakeip_proxy"] do={
:if ([:len [get $i gateway]] > 0) do={
:if ([get $i gateway] = $oldIP) do={
set $i gateway=$newIP
}
}
}
以后 IP要改,只要改脚本最上面两行
:local oldIP "原来的IP"
:local newIP "新的IP"
/ip firewall nat print where comment=fakeip_proxy
/ip route print where comment=fakeip_proxy
/ip firewall nat set [find comment=fakeip_proxy && to-addresses=10.10.10.252] to-addresses=10.10.10.7
/ip firewall nat set [find comment=fakeip_proxy && dst-address=10.10.10.252] dst-address=10.10.10.7
/ip route set [find comment=fakeip_proxy && gateway=10.10.10.252] gateway=10.10.10.7