RouterOS基础设置参考https://gitee.com/callmer/routeros_toss_notes

不习惯命令行操作的也可以照着字段到winbox页面设置

2.1 将fakeip段路由至Surge网关

/ip/route
add disabled=no distance=1 dst-address=198.18.0.0/15 gateway=**your_gateway_ip** routing-table=main \\
    scope=30 suppress-hw-offload=no target-scope=10

对需要分流的IP关闭添加到地址列表

/ip/firewall/address-list
add address=*.*.*.* list=out_ipv4
add address=*.*.*.* list=out_ipv4
...
# *为你需要分流的局域网IP

2.2 创建DNS劫持

/ip/firewall/nat
add action=dst-nat chain=dstnat comment=\\
    "lanconf: redirect globe DNS query (UDP) to smartdns" dst-port=53 protocol=udp \\
    src-address-list=out_ipv4 to-addresses=**your_gateway_ip** to-ports=53
add action=dst-nat chain=dstnat comment=\\
    "lanconf: redirect globe DNS query (TCP) to smartdns" dst-port=53 protocol=tcp \\
    src-address-list=out_ipv4 to-addresses=**your_gateway_ip** to-ports=5
#为什么不是将客户端DNS服务器设置为smartdns而是使用劫持,因为我在配置过程中发现自己的设备都是在交换机下局域网间的访问不会到路由器,导致实现不了第3点功能,另外也可以避免像GoogleTV这种采用内置DNS的设备分流失败。

#如果你的nat规则已经有创建dns劫持的话,则必须将则两条规则置于已有劫持规则的上方,可以到winbox页面拖拽

2.3 利用netwatch工具对网关进行监控,网关异常时自动切回主路由dns

二选一

#该规则是对代理软件web控制台(比如surge的web dashboard,clash的yacd dashboard)进行http/https-get监测,结果更精准,避免因为软件闪退而网关设备还在线导致的误判
add disabled=no down-script="/ip/firewall/nat\\r\\
    \\nset [find comment=\\"lanconf: redirect globe DNS query (UDP) to smartdns\\"] disabled=yes\\
    \\r\\
    \\nset [find comment=\\"lanconf: redirect globe DNS query (TCP) to smartdns\\"] disabled=yes\\
    " host=**your_gateway_ip** http-codes=301 interval=30s port=6171 test-script="" type=https-get \\
    up-script="/ip/firewall/nat\\r\\
    \\nset [find comment=\\"lanconf: redirect globe DNS query (UDP) to smartdns\\"] disabled=no\\
    \\r\\
    \\nset [find comment=\\"lanconf: redirect globe DNS query (TCP) to smartdns\\"] disabled=no"
#该规则是对网关IP进行ping指令,如果掉线时则关闭第二步中创建的劫持规则,反之则开启规则
/tool netwatch
add disabled=no down-script="/ip/firewall/nat\\r\\
    \\nset [find comment=\\"lanconf: redirect globe DNS query (UDP) to smartdns\\"] disabled=yes\\
    \\r\\
    \\nset [find comment=\\"lanconf: redirect globe DNS query (TCP) to smartdns\\"] disabled=yes\\
    " host=**your_gateway_ip** http-codes="" name=SurgeWatch test-script="" type=icmp up-script="/ip/fir\\
    ewall/nat\\r\\
    \\nset [find comment=\\"lanconf: redirect globe DNS query (UDP) to smartdns\\"] disabled=no\\
    \\r\\
    \\nset [find comment=\\"lanconf: redirect globe DNS query (TCP) to smartdns\\"] disabled=no"

2.4 验证运行状态

在需要分流的ip设备上运行以下指令,检查dns返回结果:

nslookup baidu.com  #应当返回real ip
nslookup google.com #应当返回fake ip