路由

ADG+OPC+MOSDNS 实现DNS分流、防泄漏、兼容 IPV6

波比AI · 5月2日 · 2025年本文共19679个字 · 预计阅读66分钟6次已读

[!quote] 背景
在恩山尝试过几个主、旁路由的 OPT 配置方案,以期实现 DNS 分流及防泄漏。经半年使用,总会遇到各种问题,如:1. 浏览器偶尔会提示“访问的链接不是私密链接”,然过一段时间又能访问。2. 使用油猴各类破解插件时,访问速度很慢且频繁卡顿。

我的设备:
– 光猫:已改设为桥连
– 主路由:红米 AX 6 S 官方固件(尝试过刷 Openwrt,因性能问题放弃 – 安装 OPC 使用 Fake-ip,会不断重启 OPC)
– Esxi OPT 旁路由

目标:
1. 油猴 VIP 类视频解析不卡顿
2. 去广告
3. 虚拟机 socks 5 访问亚马逊等跨境电商平台不频繁掉线

一、实现效果

  • 完美保留了 AdGuardHome 的各种功能
  • 基于 DNS 的流量 分流,国内流量绕过 Clash 核心
  • 用 Fake-IP 模式来解决 DNS 污染的问题,但限制 Fake-IP 的范围,不需要代理的域名仍返回正常 IP
  • 不用再费心找无污染的 DNS 服务器,使用运营商提供的 DNS 也没问题
  • 因为彻底解决了 DNS 污染,可以放心缓存 DNS 请求结果,开启 AdGuardHome 的乐观缓存后,DNS 平均处理时间降到 3ms
  • 完美兼容 IPv6。国内流量可正常使用 IPv6 服务。只要代理有 IPv6 出口,那国外也可正常使用。(使用 IPv6 居然还有意料之外的好处,后悔没早开)
  • 兼容 BT/PT 应用,无需特殊配置也不会消耗代理流量
  • 可以通过 AdGuardHome 的 Web 管理页面轻松切换内网设备是否走代理

1. DNS 分流

ADG+OPC+MOSDNS 实现DNS分流、防泄漏、兼容 IPV6

经过 DNS 分流后,所有需要代理的域名都分配到了 Fake IP,无需代理的域名都是由运营商 DNS 返回的最优结果。Clash 的 DNS 在 Fake IP 模式下可以无需请求网络直接返回结果,所以整体的 DNS 响应速度非常快。在处理 Fake IP 的流量时,Clash 会把 hostname 发送到远端进行 DNS 解析,也就自然不存在 DNS 污染的问题了。

2. 流量代理分流

经过 DNS 分流以后,我们只需要一条防火墙规则,把所有目的地址是 Fake IP 的流量都转发到 Clash 核心,所有其他流量都不经转发正常通行。

OpenClash 在 Fake IP 模式下会自动帮我们添加对应的防火墙规则。但它为了防止小白误操作把其它 IP 的流量也转发到 Clash 核心了,这是没必要的,我们在自定义防火墙规则里把这条删掉就可以了。

同时由于只有 Fake IP 流量会经过代理,那么无需 DNS 解析的 IP 直连流量自然就不会经过代理了,这样就不用再担心 BT/PT 跑代理的流量了。

3. 解决个别 IP 的代理问题

有的需要代理的 App 是直连 IP,不经过 DNS 域名解析的步骤的,目前我用到的只有一个,就是 telegram。好在 telegram 提供了它所使用的 ip-cidr 列表,我们只需要为这些 IP 单独配置防火墙规则,给它们转发到 Clash 核心。

4. IPV6

在这种分流方案下,可以放心开启路由器的 IPv6 支持了。因为我们在 DNS 请求阶段,把可能需要代理才能访问的 IPv6 请求已经都过滤掉了,也就不用担心代理软件对 IPv6 支持不好的问题了。

同时因为 Clash 会将 hostname 传递到远端进行解析,那么如果你的代理落地机支持 IPv6 的话,经过代理的流量就也可以走 IPv6 出口了。

5. 内网设备分流控制

通过 AdGuardHome 的 Web 管理界面可以为局域网内不同的设备指定不同的上游 DNS 服务器。

对于需要代理的设备,把上游 DNS 服务器指定到 mosdns。对于不需代理的设备,把上游 DNS 服务器指定到 dnsmasq。这样就能做到基于设备的分流控制了。

如果内网大部分设备都需要走代理,那就把 mosdns 作为 AdGuardHome 的默认上游 DNS 服务器,然后对个别不需要走代理的设备单独配置 dnsmasq 上游。反过来也是一样的。

6. 基于域名的黑白名单

通过 mosdns 的 query_matcher 功能,可以构建基于域名的黑白名单,保证一些你选择的域名一定通过或不通过 Clash 核心的代理。

比如开启了 IPv6 功能之后,我们就可以把 test-ipv6.com 加入到域名白名单里,这样我们就能测试自己对 IPv6 配置的怎么样了。

7. AdGuardHome 乐观缓存

Clash 可以在重启过程中对 Fake IP 列表进行保存,运营商 DNS 的返回bobyai结果也基本稳定,所以我们可以放心的开启 AdGuardHome 的乐观缓存功能。这时 AdGuardHome 会优先返回已过期的缓存结果然后再自己慢慢去更新缓存内容,可以大大提高内网客户端的 DNS 请求响应速度。如果不使用 Adguardhome,在 MosDNS 中实现去广告的话,注意要开启 MosDNS 的自动缓存。

二、旁路由设置

1. 接口 >> LAN

(1)不启用 IPV6

设置主旁路由为山一网段,将旁路由网关指向主路由,“IPV 6 分配长度”设置为“已禁用”,“DHCP 服务器”>> “常规设置” >> 勾选“忽略接口”,“DHCP 服务器”>>”IPv6 设置” >> 禁用“RA 服务”、“DHCPv6 服务”、“NDP 代理”,”DHCP 服务器” >> “高级设置” >> “DHCP 选项” >> 添加“6,192.168.1.2”、“3,192.168.1.2”。如图:
ADG+OPC+MOSDNS 实现DNS分流、防泄漏、兼容 IPV6
ADG+OPC+MOSDNS 实现DNS分流、防泄漏、兼容 IPV6
ADG+OPC+MOSDNS 实现DNS分流、防泄漏、兼容 IPV6
ADG+OPC+MOSDNS 实现DNS分流、防泄漏、兼容 IPV6
ADG+OPC+MOSDNS 实现DNS分流、防泄漏、兼容 IPV6

(2)启用 IPV6

如果需要启用 IPV6 的话,接 [[ADG+OPC+MOSDNS 实现DNS分流、防泄漏、兼容 IPV6#(1)不启用 IPV6]] 设置后,添加新接口,协议选择 “DHCPv 6 客户端”,防火墙选择 “@LAN”, 创建新接口即可。
ADG+OPC+MOSDNS 实现DNS分流、防泄漏、兼容 IPV6

注意: 启用或禁用 IPV6,同步设置 “网络”>> “DHCP/DNS” >> “过滤器”>>”过滤 IPV6 AAAA 记录”,如下(我是禁用 IPV6 的):
ADG+OPC+MOSDNS 实现DNS分流、防泄漏、兼容 IPV6

2. 防火墙

设置如下(照抄):
ADG+OPC+MOSDNS 实现DNS分流、防泄漏、兼容 IPV6

三、软件安装

先把对应的包都安装好:luci-app-adguardhomeluci-app-mosdnsOpenClash。然后我们来一个一个的配置。

安装各插件过程中,raw.githubusercontent.gom 可能无法访问,导致上述软件无法正常下载资源,添加如下:

vim /etc/hosts

# 将下述内容添加至hosts
127.0.0.1 localhost

::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

添加:
185.199.110.133 raw.githubusercontent.com

1. Adguardhome 安装

(1)更新 core,提示 a task is already running

手动把 /usr/share/AdGuardHome/update_core.sh 下的相关代码注释掉
ADG+OPC+MOSDNS 实现DNS分流、防泄漏、兼容 IPV6
或者

check_if_already_running(){  
    sleep 1  # 增加此项
    running_tasks="$(ps |grep 'AdGuardHome' |grep 'update_core' |grep -v 'grep' |  
    [ "${running_tasks}" -gt "2" ] && echo -e "\nA task is already running." &&  
}

(2)更新 core,提示 wget-ssl: not found

ADG+OPC+MOSDNS 实现DNS分流、防泄漏、兼容 IPV6

opkg update  
opkg install wget-ssl

(3)端口冲突

在 Linux 设备上运行 AdGuard Home,通常会出现 53(本地 DNS 服务器)、68(DHCP 客户端)、80(Http)、443(Https) 端口冲突的问题,可以通过 netstat -tunlp | grep 端口号查询占用进程。有两种解决方案:使用不同端口、停用冲突进程。
如果是通过 Docker 方式运行 bobyaiAdGuard Home,出现 listen udp 0.0.0.0:53: bind: address already in use 的提示,需要手动处理,方法如下:

#停止 DNSStubListener
systemctl stop systemd-resolved

#创建文件夹(如果不存在)
mkdir /etc/systemd/resolved.conf.d/

#使用 Nano 创建配置文件2021-04-29 16:16:00 星期四
nano /etc/systemd/resolved.conf.d/adguardhome.conf

在编辑器中粘贴以下内容:

[Resolve]
DNS=127.0.0.1
DNSStubListener=no

保存后执行以下命令。

#创建备份
sudo mv /etc/resolv.conf /etc/resolv.conf.backup

#将 /etc/resolv.conf 链接至 /run/systemd/resolve/resolv.conf
ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf

#重启 DNSStubListener
systemctl restart systemd-resolved

完成后使用 netstat -tunlp | grep 53 命令检查是否依旧有进程占用 53 端口,如无冲突,重启 AdGuard Home 容器即可。

(4)开机启动

用 ssh 登录 openwrt 后,输入下面的代码回车即可,无需重启 openwrt

chmod 755 /etc/init.d/AdGuardHome 
service AdGuardHome restart

/etc/init.d/adguardhome start
如果上述无法启动,执行
/etc/init.d/AdGuardHome enable

(5)平均处理时间过长

以下几个因素会使 AdGuard Home 的处理时间过长:

  • 本地到上游 DNS 的速度:如果本地运营商的 DNS 没有 DNS 劫持、投毒的问题,建议使用运营商 DNS + 公共 DNS 的方案,DNS 速度可以通过 Ping 值比较。并在 AdGuard Home 中选择以「并行请求」的方式处理 DNS 请求
  • 浏览安全、家庭控制与强制安全搜索服务:以上三个功能,在 DNS 请求时不会经过 DNS 缓存,直接向上游 DNS 服务器请求,从而减慢 AdGuard Home 的处理速度
  • IPv 6:如果使用的宽带、校园网不支持 IPv 6,可以禁用解析 IPv 6,提高响应速度
  • 过滤规则:过多的过滤规则会影响响应速度,宁缺毋滥,选择最合适自己的规则,一般保留 3 – 4 个广告过滤规则即可。
  • 统计周期: 在完成以上优化后,发现平均处理时间并没有太大改变,使用体验上也并不慢,有可能是因为统计周期过长,将优化前的结果计入,导致优化前后的结果无太大差异。不妨将仪表盘的统计周期缩短为 24 小时再看看。
  • 完成以上步骤后使用体验比没有使用 AdGuard Home 还要糟糕,问题有亿点严重了。这个时候需要查找 AdGuard Home 的日志,寻找问题的原因。

(6)登陆跳转无反应

路径:服务-AdGuard Home-手动设置第六行,ttl 没改的话是 0 s,改成 3600 s,然后保存/应用,重新用账号密码登录就可以了

2. MosDNS 安装

opkg update
opkg install curl

sh -c "$(curl -ksS https://raw.githubusercontent.com/sbwml/luci-app-mosdns/v5/install.sh)"

四、配置

上面讲了实现的思路、旁路由设置、相关软件的安装及可能出现的问题,现在讲软件的具体配置。

1. OpenClash

OpenClash 配置繁多,初始配置请自行参考 OpenClash 的 wiki,下面只说换成本文方案所需的配置。

  • 插件设置模式设置运行模式: 切换到Fake-IP(增强)模式 我用的是 Fake-IP(TUN-混合)模式「UDP-TUN, TCP-转发」
  • 插件设置DNS 设置本地 DNS 劫持选择禁用
  • 插件设置流量控制绕过中国大陆 IP取消勾选
  • 插件设置流量控制仅允许内网开启
  • 插件设置IPv6 设置这页的选项全都关闭就行了
  • 覆写设置常规设置这里都不用改,只需要记住 DNS 监听,后面配置 mosdns 要用
  • 覆写设置DNS 设置自定义上游 DNS 服务器勾选
  • 覆写设置DNS 设置追加上游 DNS勾选
  • 覆写设置DNS 设置追加默认 DNS勾选
  • 覆写设置DNS 设置Fake-IP 持久化勾选
  • 覆写设置DNS 设置页面下方NameServerFallBackDefault-NameServer里的 DNS 服务器全都取消勾选,我们只用运营商提供的 DNS 服务器就够了,一般运营商 DNS 都是最快的,也是 CDN 最优化的。
  • 插件设置GEO 数据库订阅GeoIP DatGeoSite这两个库的自动更新打开,都选 Loyalsoldier 的版本,这个是用来给 mosdns 用的。
  • 插件设置开发者选项里,我们自定义一下防火墙规则,增加如下这些行。
if [ "$en_mode" == "fake-ip" ]; then
  LOG_OUT "limit route to only fake ips with proxy port $proxy_port"

  /etc/mosdns/rule/geoip2ipset.sh /etc/openclash/GeoIP.dat telegram

  if [ -n "$FW4" ]; then
    handle=$(nft -a list chain inet fw4 openclash | grep 'ip protocol tcp counter' | awk '{print $NF}')
    LOG_OUT "deleting nft rule handle $handle"
    nft delete rule inet fw4 openclash handle $handle
    nft add rule inet fw4 openclash ip protocol tcp ip daddr @telegram counter redirect to $proxy_port
  else
    iptables -t nat -D openclash -p tcp -j REDIRECT --to-ports $proxy_port
    iptables -t nat -A openclash -m set --match-set telegram dst -p tcp -j REDIRECT --to-ports $proxy_port
  fi
fi

LOG_OUT "restart adguardhome"
/etc/init.d/AdGuardHome restart # 如果去广告功能放在 MosDNS中,此行可注释

其中/etc/mosdns/rule/geoip2ipset.sh这个脚本可以根据 GeoIP 数据库来生成对应的 ipset。内容如下,这个文件放到路由器上后,记得要执行chmod a+x /etc/mosdns/rule/geoip2ipset.sh给它赋予可执行权限。

#!/bin/bash



geoipfile="$1"

tag="$2"

tmpdir="/tmp/v2dat"

FW4=$(command -v fw4)



cd $(cd $(dirname $BASH_SOURCE) && pwd)



mkdir -p "$tmpdir"

filename=$(basename -- "$geoipfile")

filename="${filename%.*}"

filename="$tmpdir/${filename}_$tag.txt"



if [ "$tag" == "telegram" ]; then

  wget -4 --timeout 5 -O "$filename" 'https://mirror.ghproxy.com/https://raw.githubusercontent.com/fernvenue/telegram-cidr-list/master/CIDR.txt'

  if [ "$?" != "0" ]; then

    /usr/bin/v2dat unpack geoip -o "$tmpdir" -f "$tag" "$geoipfile"

  fi

else

  /usr/bin/v2dat unpack geoip -o "$tmpdir" -f "$tag" "$geoipfile"

fi



if test -f "$filename"; then

  if [ -n "$FW4" ]; then

    nft add set inet fw4 "$tag" { type ipv4_addr\; flags interval\; auto-merge\; }

    nft add set inet fw4 "${tag}6" { type ipv6_addr\; flags interval\; auto-merge\; }

    nft flush set inet fw4 "$tag"

    nft flush set inet fw4 "${tag}6"

  fi

  ipset create "$tag" hash:net -!

  ipset create "${tag}6" hash:net family inet6 -!

  ipset flush "$tag"

  ipset flush "${tag}6"

  while read p; do

    if ! grep -q ":" <<< "$p"; then

      if [ -n "$FW4" ]; then

        nft add element inet fw4 "$tag" { "$p" }

      fi

      ipset add "$tag" "$p"

    else

      if [ -n "$FW4" ]; then

        nft add element inet fw4 "${tag}6" { "$p" }

      fi

      ipset add "${tag}6" "$p"

    fi

  done <"$filename"

else

  echo "$filename missing."

fi



rm -rf "$tmpdir"

2. MosDNS

MosDNS 可以只做分流,配合Adguardhome 去广告使用,也可以在MosDNS 中实现广告屏蔽的功能。两种方法的配置文件如下。

(1)只分流,不去广告

选自定义配置文件,取消DNS 转发 的勾,然后我就直接贴配置了,注意 Clash DNS 端口要改成你自己在 OpenClash 里的配置,LAN IP-CIDR 也要改成你自己的内网配置,这里 mosdns 监听了 5335 端口。并且要在GeoData 数据导出里配置上我们用到的标签:GeoSite: cn, apple-cn, category-games@cn, geolocation-!cn,GeoIP: cn。需要对Adguardhome 进行相应配置。

[!info] 注意
mosdns v 5.3.4 之后,apple-cn 无法获取,需要尝试删除

log:
  level: info
  file: "/tmp/mosdns.log"

plugins:
  # 缓存插件
  - tag: cache
    type: cache
    args:
      size: 20480
      lazy_cache_ttl: 86400

  # 国内域名
  - tag: geosite_cn
    type: domain_set
    args:
      exps:
        - "lan"
        - "local"
        - "arpa"
      files:
        - "/etc/mosdns/rule/whitelist.txt"
        - "/var/mosdns/geosite_cn.txt"
        - "/var/mosdns/geosite_apple-cn.txt"
        - "/var/mosdns/[email protected]"

  # 国内 IP
  - tag: geoip_cn
    type: ip_set
    args:
      files:
        - "/var/mosdns/geoip_cn.txt"

  # 国外域名
  - tag: geosite_no_cn
    type: domain_set
    args:
      files:
        - "/etc/mosdns/rule/greylist.txt"
        - "/var/mosdns/geosite_geolocation-!cn.txt"

  # 国内 DNS
  - tag: forward_local
    type: forward
    args:
      upstreams:
        - addr: "quic://223.5.5.5"      
        - addr: "tls://1.12.12.12"
          enable_pipeline: true      
        - addr: "tls://120.53.53.53"
          enable_pipeline: true

  # 国外 DNS
  # 自己查找低延迟 DNS
  - tag: forward_remote
    type: forward
    args:
      upstreams:
        # 101DNS
        - addr: "tls://101.101.101.101"
          enable_pipeline: true
        # NextDNS HK 节点
        - addr: "https://45.11.104.186"
          insecure_skip_verify: true
        # OpenDNS SANDNOX
        - addr: "tls://208.67.222.2"
          enable_pipeline: true
        # OpenDNS 
        - addr: "tls://208.67.222.222"
          enable_pipeline: true                    

  # 私人分流 dns         
  - tag: forward_private
    type: forward
    args:
      upstreams: 
        - addr: "https://doh.apad.pro/dns-query"
          bootstrap: "223.5.5.5" 


  # 国内解析
  - tag: local_sequence
    type: sequence
    args:
      - exec: $forward_local

  # 国外解析
  - tag: remote_sequence
    type: sequence
    args:
      - exec: prefer_ipv4
      - exec: $forward_remote

  # 分流解析    
  - tag: private_sequence
    type: sequence
    args:
      - exec: $forward_private

  # clash 解析
  - tag: clash_sequence
    type: sequence
    args:
      - exec: forward 127.0.0.1:7874
      - exec: ttl 0

  # 有响应终止返回
  - tag: has_resp_sequence
    type: sequence
    args:
      - matches: has_resp
        exec: accept

  # fallback 用分流 sequence
  # 返回国外 IP 则用 clash 解析得出 fakeip
  - tag: fallback_sequence
    type: sequence
    args:
      - exec: $private_sequence
      - matches: "!resp_ip $geoip_cn"
        exec: goto clash_sequence
      - exec: accept

  # 国外 sequence
  - tag: fallback2_sequence
    type: sequence
    args:
      - exec: $remote_sequence
      - matches: "resp_ip $geoip_cn"
        exec: goto local_sequence
      - exec: goto clash_sequence      

  # fallback 用分流 DNS ,超时后用国外 DNS 解析    
  - tag: fallback
    type: fallback
    args:
      primary: fallback_sequence
      secondary: fallback2_sequence
      threshold: 2000
      always_standby: true

  # 主要的运行逻辑插件
  # sequence 插件中调用的插件 tag 必须在 sequence 前定义,
  # 否则 sequence 找不到对应插件。
  - tag: main_sequence
    type: sequence
    args:
      # drop https query type
      - matches:
        - qtype 65
        exec: reject 3

      # handle local ptr
      - matches:
        - qtype 12
        exec: $local_sequence
      - exec: jump has_resp_sequence

      #FakeIP 不缓存
      - matches:
        - qname $geosite_no_cn
        exec: $clash_sequence
      - exec: jump has_resp_sequence

      # 缓存
      - exec: $cache
      - exec: jump has_resp_sequence

      - matches:
        - qname $geosite_cn
        exec: $local_sequence
      - exec: jump has_resp_sequence

      - exec: $fallback

  - tag: udp_server
    type: udp_server
    args:
      entry: main_sequence
      listen: ":5335" #mosdns 监听端口

(2)去广告

在 MosDNS 就行实现了广告屏蔽功能,所以不需要安装配置 Adguardhome.

log:
  level: info
  file: "/tmp/mosdns.log"

include: []

plugins:
  # 国内域名
  - tag: geosite_cn
    type: domain_set
    args:
      exps:
        - "lan"
        - "local"
        - "arpa"
      files:
        - "/etc/mosdns/rule/whitelist.txt"
        - "/var/mosdns/geosite_cn.txt"
        - "/var/mosdns/geosite_apple-cn.txt"
        - "/var/mosdns/[email protected]"

  # 国内 IP
  - tag: geoip_cn
    type: ip_set
    args:
      files:
        - "/var/mosdns/geoip_cn.txt"

  # 国外域名
  - tag: geosite_no_cn
    type: domain_set
    args:
      files:
        - "/etc/mosdns/rule/greylist.txt"
        - "/var/mosdns/geosite_geolocation-!cn.txt"

  # hosts
  - tag: hosts
    type: hosts
    args:
      files:
        - "/etc/mosdns/rule/hosts.txt"

  # 缓存插件
  - tag: cache
    type: cache
    args:
      size: 20480
      lazy_cache_ttl: 86400

  - tag: ad_sequence
    type: sequence
    args:
      - exec: concurrent 3
      - matches:             # 如果
          - qname "/etc/mosdns/rule/adlist/anti-ad-domains.txt" # 如果请求的域名在广告列表内。
        exec: reject 3       # 执行 直接返回 NXDOMAIN(3) 屏蔽。
      - exec: cache 1024     # 然后。查找 cache。
      - matches:             # 如果
          - has_resp         # 有应答了(上一步 cache 找到应答)
        exec: accept         # 结束。

      # 上一步没有找到缓存,就会到这一步,转发至 本地 获取应答。
      - exec: forward 127.0.0.1
      # sequence 结束(被 reject,accept,或者运行到末尾)后就会返回调用者。在这个配置文件中
      # 调用者是下面的服务器。服务器就会返回应答至客户端。

  # 国内解析
  - tag: local_sequence
    type: sequence
    args:
      - exec: concurrent 3
      - exec: forward 192.168.1.1
      - matches:
        - has_resp
        - resp_ip 192.168.1.0/24 # LAN IPCIDR
        exec: ttl 1800-0

  # 国外解析
  - tag: remote_sequence
    type: sequence
    args:
      - exec: concurrent 3
      - matches:
        - qtype 28
        exec: reject 0
      - exec: forward 127.0.0.1:7874
      - exec: ttl 1800-0

  # 有响应终止返回
  - tag: has_resp_sequence
    type: sequence
    args:
      - matches: has_resp
        exec: accept

  # fallback 用本地服务器 sequence
  # 返回非国内 ip 则 drop_resp
  - tag: fallback_local
    type: sequence
    args:
      - exec: $local_sequence
      - matches: "!resp_ip $geoip_cn"
        exec: drop_resp

  # fallback 用远程服务器 sequence
  - tag: fallback
    type: fallback
    args:
      primary: fallback_local
      secondary: remote_sequence
      threshold: 200
      always_standby: true

  # 主要的运行逻辑插件
  # sequence 插件中调用的插件 tag 必须在 sequence 前定义,
  # 否则 sequence 找不到对应插件。
  - tag: main_sequence
    type: sequence
    args:

      # hosts
      - exec: $hosts
      - exec: jump has_resp_sequence

      # drop https query type
      - matches:
        - qtype 65
        exec: reject 3

      # handle local ptr
      - matches:
        - qtype 12
        exec: $local_sequence
      - exec: jump has_resp_sequence

      - matches:
        - qname $geosite_cn
        exec: $local_sequence
      - exec: jump has_resp_sequence

      - matches:
        - qname $geosite_no_cn
        exec: $remote_sequence
      - exec: jump has_resp_sequence

      - exec: $fallback

  - tag: udp_server
    type: udp_server
    args:
      entry: main_sequence
      listen: :5335
  - tag: server_tcp
    type: tcp_server
    args:
      entry: main_sequence
      listen: :5335

3. AdGuardHome

在 luci 页面上,开启端口重定向,选择重定向53端口到AdGuardHome,这里注意 AdGuardHome 本身不要监听 53 端口,把 53 端口留给 dnsmasq,AdGuardHome 设置一个其它的端口就可以了。

在 Web 管理页面上,设置DNS 设置中,上游 DNS 服务器内只填写一个 mosdns 的地址127.0.0.1:5335 #mosdns,私人反向 DNS 服务器写上127.0.0.1 #dnsmasq。DNS 缓存配置里面,缓存大小看你内存大小填写,乐观缓存勾上。

对于不想走代理的设备,可以在 设置客户端设置 中添加,并且把上游 DNS 服务器设置成127.0.0.1

解决谷歌 Play 商店不能更新:

ADG – 过滤器 – 添加 DNS 重写,[services.googleapis.cn](http://services.googleapis.cn/)198.18.1.50

如果无法使用游戏加速器:OpenClash – 插件设置 – 模式设置,取消勾选 UDP 流量转发

参考教程:https://blog.openwrtcn.eu.org/dnsling-wu-ran/https://github.com/IrineSistiana/mosdns/discussions/796

其他设置,请查看 [[Adguardhome 配置、优化教程]]

配置如下:

http:
  pprof:
    port: 6060
    enabled: false
  address: 0.0.0.0:3000
  session_ttl: 720h
users:
  - name: yanghaixiong
    password: $2y$10$t4WZhKFulyEy245kV4zitOAmyHGGBASFezaASPefwXRGDuPZB6z72
auth_attempts: 5
block_auth_min: 15
http_proxy: ""
language: ""
theme: auto
dns:
  bind_hosts:
    - 0.0.0.0
  port: 6060
  anonymize_client_ip: false
  ratelimit: 0
  ratelimit_subnet_len_ipv4: 24
  ratelimit_subnet_len_ipv6: 56
  ratelimit_whitelist: []
  refuse_any: false
  upstream_dns:
    - 127.0.0.1:5335
  upstream_dns_file: ""
  bootstrap_dns:
    - 8.8.8.8
  fallback_dns: []
  upstream_mode: fastest_addr
  fastest_timeout: 1s
  allowed_clients: []
  disallowed_clients: []
  blocked_hosts:
    - version.bind
    - id.server
    - hostname.bind
  trusted_proxies:
    - 127.0.0.0/8
    - ::1/128
  cache_size: 0
  cache_ttl_min: 0
  cache_ttl_max: 0
  cache_optimistic: false
  bogus_nxdomain: []
  aaaa_disabled: true
  enable_dnssec: false
  edns_client_subnet:
    custom_ip: ""
    enabled: true
    use_custom: false
  max_goroutines: 300
  handle_ddr: true
  ipset: []
  ipset_file: ""
  bootstrap_prefer_ipv6: false
  upstream_timeout: 10s
  private_netwolaoxiongb2crks: []
  use_private_ptr_resolvers: false
  local_ptr_upstreams:
    - 127.0.0.1
  use_dns64: false
  dns64_prefixes: []
  serve_http3: false
  use_http3_upstreams: false
  serve_plain_dns: true
  hostsfile_enabled: true
tls:
  enabled: false
  server_name: ""
  force_https: false
  port_https: 443
  port_dns_over_tls: 853
  port_dns_over_quic: 853
  port_dnscrypt: 0
  dnscrypt_config_file: ""
  allow_unencrypted_doh: false
  certificate_chain: ""
  private_key: ""
  certificate_path: ""
  private_key_path: ""
  strict_sni_check: false
querylog:
  dir_path: ""
  ignored: []
  interval: 6h
  size_memory: 1000
  enabled: true
  file_enabled: true
statistics:
  dir_path: ""
  ignored: []
  interval: 6h
  enabled: true
filters:
  - enabled: true
    url: https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt
    name: AdGuard Simplified Domain Names filter
    id: 1
  - enabled: true
    url: https://adaway.org/hosts.txt
    name: AdAway
    id: 2
  - enabled: true
    url: https://raw.githubusercontent.com/vokins/yhosts/master/data/tvbox.txt
    name: tvbox
    id: 1575018007
  - enabled: true
    url: http://sbc.io/hosts/hosts
    name: StevenBlack host basic
    id: 1575618242
  - enabled: true
    url: http://sbc.io/hosts/alternates/fakenews-gambling-porn-social/hosts
    name: StevenBlack host+fakenews + gambling + porn + social
    id: 1575618243
  - enabled: true
    url: https://cdn.jsdelivr.net/gh/privacy-protection-tools/anti-AD/anti-ad-easylist.txt
    name: anti-AD(Adblock+neohosts+yhosts+cjxlist+adhlist)
    id: 1577113202
  - enabled: true
    url: https://raw.githubusercontent.com/o0HalfLife0o/list/master/ad.txt
    name: halflife
    id: 1734811795
  - enabled: true
    url: https://anti-ad.net/easylist.txt
    name: anti-AD
    id: 1734811796
  - enabled: true
    url: https://cdn.jsdelivr.net/gh/neoFelhz/neohosts@gh-pages/full/hosts.txt
    name: neoHosts
    id: 1734811797
  - enabled: true
    url: https://raw.githubusercontent.com/jdlingyu/ad-wars/master/hosts
    name: 大圣净化
    id: 1734811798
  - enabled: true
    url: https://raw.githubusercontent.com/banbendalao/ADgk/master/ADgk.txt
    name: adgk手机去广告规则
    id: 1734811799
  - enabled: true
    url: http://sub.adtchrome.com/adt-chinalist-easylist.txt
    name: 广告终结者
    id: 1734811800
  - enabled: true
    url: https://easylist-downloads.adblockplus.org/easylistchina+easylist.txt
    name: EasyList China+EasyList
    id: 1734811801
  - enabled: true
    url: https://easylist-downloads.adblockplus.org/easyprivacy.txt
    name: EasyPrivacy
    id: 1734811802
  - enabled: true
    url: https://raw.githubusercontent.com/cjx82630/cjxlist/master/cjx-annoyance.txt
    name: EasyList China
    id: 1734811803
  - enabled: true
    url: https://raw.githubusercontent.com/o0HalfLife0o/list/master/ad-pc.txt
    name: ad-pc
    id: 1734811804
  - enabled: true
    url: https://raw.githubusercontent.com/o0HalfLife0o/list/master/ad-mo.txt
    name: ad-mo
    id: 1734811805
  - enabled: true
    url: https://adguardteam.github.io/HostlistsRegistry/assets/filter_21.txt
    name: 'CHN: anti-AD'
    id: 1734811811
  - enabled: true
    url: https://adrules.top/dns.txt
    name: Adlist
    id: 1734811812
whitelist_filters:
  - enabled: true
    url: https://raw.githubusercontent.com/privacy-protection-tools/dead-horse/master/anti-ad-white-list.txt
    name: anti-ad白名单
    id: 1734811806
  - enabled: true
    url: https://raw.githubusercontent.com/hl2guide/Filterlist-for-AdGuard/master/filter_whitelist.txt
    name: filter_whitelist
    id: 1734811807
  - enabled: true
    url: https://raw.githubusercontent.com/liwenjie119/adg-rules/master/white.txt
    name: LWJ's white list
    id: 1734811808
  - enabled: true
    url: https://raw.githubusercontent.com/ChengJi-e/AFDNS/master/QD.txt
    name: DNS允许白名单
    id: 1734811809
  - enabled: true
    url: https://gist.githubusercontent.com/YiDaCP/8f51ee10c770e5726ca4b8aff890de92/raw/dns.txt
    name: 自用
    id: 1734811810
user_rules:
  - '@@||xhamster.com^'
  - '@@||xhamsterlive.com^'
dhcp:
  enabled: false
  interface_name: ""
  local_domain_name: lan
  dhcpv4:
    gateway_ip: ""
    subnet_mask: ""
    range_start: ""
    range_end: ""
    lease_duration: 86400
    icmp_timeout_msec: 1000
    options: []
  dhcpv6:
    range_start: ""
    lease_duration: 86400
    ra_slaac_only: false
    ra_allow_slaac: false
filtering:
  blocking_ipv4: ""
  blocking_ipv6: ""
  blocked_services:
    schedule:
      time_zone: UTC
    ids: []
  protection_disabled_until: null
  safe_search:
    enabled: false
    bing: true
    duckduckgo: true
    ecosia: true
    google: true
    pixabay: true
    yandex: true
    youtube: true
  blocking_mode: nxdomain
  parental_block_host: family-block.dns.adguard.com
  safebrowsing_block_host: standard-block.dns.adguard.com
  rewrites:
    - domain: services.googleapis.cn
      answer: services.googleapis.com
    - domain: developers.google.cn
      answer: developers.google.com
  safe_fs_patterns:
    - /usr/bin/AdGuardHome/data/userfilters/*
  safebrowsing_cache_size: 1048576
  safesearch_cache_size: 1048576
  parental_cache_size: 1048576
  cache_time: 30
  filters_update_interval: 24
  blocked_response_ttl: 10
  filtering_enabled: true
  parental_enabled: false
  safebrowsing_enabled: false
  protection_enabled: true
clients:
  runtime_sources:
    whois: true
    arp: true
    rdns: false
    dhcp: true
    hosts: true
  persistent: []
log:
  enabled: true
  file: ""
  max_backups: 0
  max_size: 100
  max_age: 3
  compress: false
  local_time: false
  verbose: false
os:
  group: ""
  user: ""
  rlimit_nofile: 0
schema_version: 29


4. 防火墙

添加如下规则,重启防火墙:

#5333 是 AdGuardHome 的 DNS 监听端口,两个端口必须一致 iptables -t nat -A PREROUTING -d 198.18.0.0/16 -p tcp -j REDIRECT --to-ports 7892 iptables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 5333 iptables -t nat -A PREROUTING -p tcp --dport 53 -j REDIRECT --to-ports 5333 ip6tables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 5333 ip6tables -t nat -A PREROUTING -p tcp --dport 53 -j REDIRECT --to-ports 5333

firewall4 nftables 的补充说明:
/etc/nftables.d/10-custom-filter-chains.nft 添加以下规则,注意每行前面都要有空格

chain adg_redirect {
    type nat hook prerouting priority dstnat - 10; policy accept;
    meta nfproto { ipv4, ipv6 } udp dport 53 counter packets 0 bytes 0 redirect to :5333 comment "ADG"
}

五、验证

ADG+OPC+MOSDNS 实现DNS分流、防泄漏、兼容 IPV6

ADG+OPC+MOSDNS 实现DNS分流、防泄漏、兼容 IPV6

Test Ad Block – Toolz
AdBlock Tester: test your AdBlock extensions
IP/DNS Detect – What is your IP, what is your DNS, what informations you send to websites.
DNS leak test
DNS Leak Test – BrowserLeaks

总结

23 年,从红米 AX6S 陆续接触软件路由、旁路由等知识,上述是比较满意的集成方案:光猫(桥连)+ 主路由(拔号)+旁路由(Opclash + Mosdns 实现 DNS 分流、防泄漏,DDNS「Lucky」,WireGuard「异地组网」)。之所以没在主路由(红米 AX6S)部署 Openclash、Mosdns、Adguardhome 等,主要原因是红米 AX6S 内存、空间等硬件性能不够,全怼上去后,openclash 或路由频繁宕机重启。个人更偏向Openclash + Mosdns,实现 DNS 分流、防泄漏、去广告,原因Adguardhome 一个是规则设置相对繁琐,且可能会触发 DNS 的二次查询,导致浏览器偶尔出现“您访问的链接不是私密链接”(过段时间又莫名能访问),且使用油猴各类 VIP 视频解析时,会很卡顿,之前用的配置方法:[[Openwrt 旁路由 + MoDNS + Openclash + Adguard Home 配置教程]]。个人采用的是 Openclash + Mosdns(没有使用Adguardhome),解决上述所有问题(1. dns 多次查询 2. Adg 规则频繁调整 3. 油猴 VIP 视频解析卡顿 4. 浏览器提示“不是私密链接” 5. Socks5 频繁掉线等),目前一切正常。

参考

[Total: 0 Average: 0]
0 条回应

必须 注册 为本站用户, 登录 后才可以发表评论!