• 人生,不能用时间去衡量

利用mosdns搭配openclash实现DNS高速分流

软路由 刚刚 12734次浏览 7个评论

Openwrt软路由常用的dns分流有smartdns和mosdns,长期使用下来个人感觉 Mosdns 的 DNS 查询转发速度相对更快,且查询条件通过自定义规则能做到更加精细地控制,在具体设置上smartdns相对有点小小复杂, 而mosdns比较简单和上手容易。

下载和安装

IPK核心包的安装颇为复杂,大概率安装不成功,最好选择博主直接编译好的Openwrt固件

最近Openwrt固件下载

下载资源文件

wget https://glare.xukecheng.tech/Loyalsoldier/geoip/cn.dat -O /etc/mosdns/cn.dat && \
wget https://glare.xukecheng.tech/Loyalsoldier/v2ray-rules-dat/geosite.dat -O /etc/mosdns/geosite.dat && \
wget https://glare.xukecheng.tech/Loyalsoldier/v2ray-rules-dat/geoip.dat -O /etc/mosdns/geoip.dat && \
touch /etc/mosdns/ecs_cn_domain.txt && \
touch /etc/mosdns/ecs_tw_domain.txt

# ecs_cn_domain 是强制本地解析域名,ecs_tw_domain 是强制非本地解析域名;格式可以参考 https://github.com/pmkol/easymosdns/blob/main/ecs_tw_domain.txt

V4版本配置(本站下载固件中的mosdns为V4版本)

log:
  level: info
  file: "/tmp/mosdns.log"
data_providers:
  - tag: geosite
    file: /etc/mosdns/geosite.dat
    auto_reload: true
  - tag: cn
    file: /etc/mosdns/cn.dat
    auto_reload: true

plugins:
  # 缓存
  - tag: mem_cache
    type: cache
    args:
      size: 1024
      lazy_cache_ttl: 17200
      # redis
      # redis: "redis://localhost:6379/0"
      # redis_timeout: 1000
  # 修改应答 ttl
  - tag: "modify_ttl"
    type: "ttl"
    args:
      minimal_ttl: 300
      maximum_ttl: 3600

  # 阿里 dns
  - tag: ali
    type: fast_forward
    args:
      upstream:
        - addr: "223.5.5.5"
        - addr: "https://dns.alidns.com/dns-query"
          dial_addr: "223.5.5.5"
          trusted: true
        - addr: "tls://dns.alidns.com"
          dial_addr: "223.5.5.5"
          trusted: true

  # dnspod
  - tag: dnspod
    type: fast_forward
    args:
      upstream:
        - addr: "119.29.29.29"
        - addr: "https://doh.pub/dns-query"
          dial_addr: "1.12.12.12"
          trusted: true
        - addr: "tls://dot.pub"
          dial_addr: "1.12.12.12"
          trusted: true

  # 本地 dns 备用
  - tag: localdns
    type: fast_forward
    args:
      upstream:
        - addr: "udp://114.114.114.114"

  # google dns
  - tag: google
    type: fast_forward
    args:
      upstream:
        - addr: "https://dns.google/dns-query"
          dial_addr: "8.8.4.4"
          trusted: true
        - addr: "tls://dns.google"
          dial_addr: "8.8.4.4"
          trusted: true

  # cloudflare
  - tag: cloudflare
    type: fast_forward
    args:
      upstream:
        - addr: "https://cloudflare-dns.com/dns-query"
          dial_addr: "1.0.0.1"
          trusted: true
        - addr: "tls://cloudflare-dns.com"
          dial_addr: "1.0.0.1"
          trusted: true

  # doq 备用
  - tag: adguard
    type: forward
    args:
      upstream:
        - addr: "quic://dns-unfiltered.adguard"
          ip_addr:
            - "94.140.14.140"
            - "94.140.14.141"
          trusted: true

  # local 序列
  - tag: forward_local
    type: sequence
    args:
      exec:
        - primary:
            - parallel: # 并行
                - - "ali" # 执行序列 #1。
                - - "dnspod" # 执行序列 #2。
          secondary:
            - localdns # 备用本地
          fast_fallback: 400 # 这里建议设置成 primary 服务器正常延时的 2~5 倍 单位: 毫秒。
          always_standby: true

  # remote 序列
  - tag: forward_remote
    type: sequence
    args:
      exec:
        - primary:
            - parallel: # 并行
                - - "google" # 执行序列 #1。
                - - "cloudflare" # 执行序列 #2。
          secondary:
            - adguard # 备用 adguard
          fast_fallback: 600 # 这里建议设置成 primary 服务器正常延时的 2~5 倍 单位: 毫秒。
          always_standby: true

  ################ 匹配器插件 #################

  # 匹配本地域名的插件
  - tag: query_is_local_domain
    type: query_matcher
    args:
      domain:
        - "provider:geosite:cn,apple-cn,steam@cn"

  # 匹配非本地域名的插件
  - tag: query_is_non_local_domain
    type: query_matcher
    args:
      domain:
        - "provider:geosite:geolocation-!cn"

  # 匹配广告域名的插件
  - tag: query_is_ad_domain
    type: query_matcher
    args:
      domain:
        - "provider:geosite:category-ads-all"

  # 匹配本地 IP 的插件
  - tag: response_has_local_ip
    type: response_matcher
    args:
      ip:
        - "provider:cn:cn"

  - tag: main_sequence
    type: sequence
    args:
      exec:
        # - hosts
        - mem_cache
        - _no_ecs

        - if: query_is_ad_domain
          exec:
            - _new_nxdomain_response
            - _return

        - if: query_is_local_domain
          exec:
            - forward_local
            - if: response_has_local_ip
              exec:
                - _return

        - if: query_is_non_local_domain
          exec:
            - _prefer_ipv4 # 优先 IPv4
            - forward_remote # 用远程服务器获取应答
            - if: "! response_has_local_ip"
              exec:
                - _return
        - primary:
            - forward_local # 本地服务器获取应答。
            - if: "(! response_has_local_ip) && [_response_valid_answer]"
              exec:
                - _drop_response # 丢掉。
          secondary:
            - _prefer_ipv4
            - forward_remote
          fast_fallback: 300
          always_standby: true
        - modify_ttl

servers:
  - exec: main_sequence
    listeners:
      # - protocol: udp
      #   addr: "[::1]:5335"
      # - protocol: tcp
      #   addr: "[::1]:5335"
      - protocol: udp
        addr: "0.0.0.0:5335"
      - protocol: tcp
        addr: "0.0.0.0:5335"

按照如下步骤,将以上配置文件复制粘贴后,点保存并应用。

利用mosdns搭配openclash实现DNS高速分流

mosdns会reload并重启, 如果提示正在运行, 表明运行成功, 你也可以使用命令进行手动操作

$ /etc/init.d/mosdns enable
$ /etc/init.d/mosdns reload
$ /etc/init.d/mosdns restart

检查是否启动成功, 如果有端口监听说明mosdns已经在正常工作了

$ netstat -a | grep 5335
tcp 0 0 localhost:5335 0.0.0.0:* LISTEN
tcp 0 0 localhost:5335 :::* LISTEN
udp 0 0 localhost:5335 0.0.0.0:*
udp 0 0 localhost:5335 :::*

配合openclash

openclash的全局设置页面, 找到DNS设置选项, 增加两个DNS服务器(只保留这两个,其余全部去掉✅), 分别是NameServer和FallBack, 都得填mosdns的地址, 既本地地址127.0.0.1和对应监听的端口5335, 协议是UDP并✅上这两个DNS服务器

具体设置参照下图

利用mosdns搭配openclash实现DNS高速分流 利用mosdns搭配openclash实现DNS高速分流

利用mosdns搭配openclash实现DNS高速分流

  • mosdns配置文件一定要复制本文内容,不然会导致无法启动
  • mosdns本身是支持IPv6的, 现在这里的配置不支持IPv6, 所以需要把openwrt的相关的IPv6设置都关掉, openclash禁止代理IPv6和禁止允许IPv6类型DNS解析
  • openclash如果想实现精准分流,那么还需要在DNS设置选项下面打开fallback选项,将规则导入进去,保存应用配置即可~配置文件:点击查看 清空原有配置,将内容直接粘贴复制即可
  • 将运行模式改为redir-tun模式
  • 全局–GEOIP订阅更新打勾

配置完成后重新启动openclash,分流效果将会更好!(如打不开网页,请重新尝试启动openclash

喜欢 (3)
[]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(7)个小伙伴在吐槽
  1. 编辑进去没用啊?
    风神2023-03-14 21:16 回复
    • 按步骤下载资源文件成功后才能启动成功
      ttxs2023-03-20 18:55 回复
  2. 我也放进去也启动不了
    老坑2023-03-19 21:45 回复
    • 应该是在用固件中mosdns版本不对应,可以下载博主编译的固件
      lxp1382023-04-03 09:45 回复
  3. 有bug 开了一天段时间 cpu 内存 全部占满了 直接卡死
    网民2023-03-25 10:14 回复
    • 此方案适合x86
      lxp1382023-04-03 09:44 回复
  4. 可以出一个mosdns v5版本的不
    12023-11-03 18:02 回复