Skip to content

使用 mosdns 将所有域名解析的 Cloudflare IP 重定向至最快 IP #115

@Auska

Description

@Auska

https://github.com/IrineSistiana/mosdns
mosdns 是一个可以自由控制的 DNS 转发器,可以实现根据域名解析 IP 来修改为自定义 IP,因此适合用来将所有套了 Cloudflare CDN 的域名都给强制解析为优选 IP(包括 AWS Cloudfront、Akamai CDN 都可以这样干)。

目前 v5 大版本刚出来,改动较大,也可能不太稳定,因此我还没用上。

以下为配置文件示例,我自己用的是 v4.x 最后一个版本。

# 配置文件(此为 v4.x 版本)

# 日志
log:
  level: info # 日志级别,可选 debug 、 info 、 warn 、 error 。默认 info
  file: "/tmp/mosdns.log" # 留空代表不输出日志


# 服务器
servers:
  - exec: main_sequence # 本服务器运行插件的 tag
    listeners:
      - protocol: udp # 即可以通过 UDP 方式访问该 DNS
        addr: ":53" # 53 端口


# 插件
plugins:

# 缓存
  - tag: lazy_cache
    type: cache
    args:
      size: 1024 # 缓存条数
      lazy_cache_ttl: 86400 # 缓存时间
      lazy_cache_reply_ttl: 30 # 返回过期应答的 TTL

# 转发到上游 DNS
  - tag: forward_local
    type: fast_forward
    args:
      upstream:
        - addr: "udp://114.114.114.114" # 114 公共明文 UDP DNS
        - addr: "udp://223.5.5.5" # 阿里云的明文 UDP DNS
        - addr: "https://dns.alidns.com/dns-query" # 阿里的 DoH 加密 DNS
          idle_timeout: 30 # 连接复用空连接保持时间,单位: 秒
          trusted: true # 是否是可信服务器
        - addr: "https://doh.pub/dns-query" # 腾讯的 DoH 加密 DNS
          idle_timeout: 30
          trusted: true

# 匹配 Cloudflare CDN IP 地址
  - tag: response_IP_Cloudflare
    type: response_matcher
    args:
      ip:
        - "1.1.1.0/24"
        - "1.0.0.0/24"
        - "162.158.0.0/15"
        - "104.16.0.0/12"
        - "172.64.0.0/13"
#        - "2606:4700::/32" # 如果要匹配 IPv6 地址,则请移除行首第一个井号

# 修改 IP 为自选 IP
  - tag: blackhole_Cloudflare
    type: blackhole
    args:
      ipv4: "127.0.0.1" # 如果请求类型是 A,则生成 IP 为该地址的应答。请修改为自选 IPv4
#      ipv6: "xxxx:xxxx:xxxx:xxxx" # 如果请求类型是 AAAA,则生成 IP 为该地址的应答。请修改为自选 IPv6,如果要返回 IPv6 则请移除行首第一个井号

# 将多个插件组合成流水线
  - tag: main_sequence
    type: sequence
    args:
      exec:
        - lazy_cache # 匹配缓存
        - forward_local               # 请求转发至上游 DNS
        - if: response_IP_Cloudflare   # 如果域名解析结果为 Cloudflare CDN IP 地址
          exec:
            - blackhole_Cloudflare # 则返回指定的 自选 IP 

另外通过 mosdns 接管 dns 这种方式,是比较方便的为代理,比如 OpenClash 进行分流的。


# 配置文件(此为 v3.x 版本):

「 点击展开 查看内容 」
log:
  level: info

plugin:

  ################# 服务插件 ################

  # 启动服务器的插件
  - tag: main_server
    type: server
    args:
      entry:
        - _default_cache  # 启用缓存
        - main_sequence   # 分流逻辑
      server:       # 监听地址与协议。可按序增减。
        - protocol: udp
          addr: "127.0.0.1:53"    #请自行修改合适端口

  ################# 可执行插件 ################

  # 包含分流的逻辑的插件
  - tag: main_sequence
    type: sequence
    args:
      exec:
        - forward_local               # 先请求转发至本地服务器
        - if:
            - response_IP_Cloudflare # 如果域名解析结果为 Cloudflare CDN IP 地址
          exec:
            - blackhole_Cloudflare # 则返回指定的 自选 IP 

  # 转发请求至本地服务器的插件
  - tag: forward_local
    type: forward
    args:
      upstream:
        - addr: 114.114.114.114

  - tag: blackhole_Cloudflare
    type: blackhole
    args:
      ipv4: "127.0.0.1" # 如果请求类型是 A,则生成 IP 为该地址的应答。请修改为自选 IPv4。
#      ipv6: "xxxx:xxxx:xxxx:xxxx"     # 如果请求类型是 AAAA,则生成 IP 为该地址的应答。请修改为自选 IPv6,如果要返回 IPv6 则请移除行首第一个井号

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

  # 匹配 IP 的插件
  - tag: response_IP_Cloudflare
    type: response_matcher
    args:
      ip:
        - "1.1.1.0/24"
        - "1.0.0.0/24"
        - "162.158.0.0/15"
        - "104.16.0.0/12"
        - "172.64.0.0/13"
#        - "2606:4700::/32" # 如果要匹配 IPv6 地址,则请移除行首第一个井号

Metadata

Metadata

Assignees

No one assigned

    Labels

    工具教程辅助工具 或 技巧教程

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions