Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Auska opened this issue Aug 31, 2021 · 4 comments
Closed
Labels
工具教程 辅助工具 或 技巧教程

Comments

@Auska
Copy link

Auska commented Aug 31, 2021

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 地址,则请移除行首第一个井号
@XIU2 XIU2 changed the title 利用mosdns项目实现返回测速CDN 使用 mosdns 将所有域名解析的 Cloudflare IP 重定向至最快 IP Nov 18, 2021
@XIU2 XIU2 added the 工具教程 辅助工具 或 技巧教程 label Nov 18, 2021
@orency
Copy link

orency commented Nov 28, 2021

其实cloudfront和akamai也可以这么玩,把默认的ntt线路垃圾ipv4替换成香港或东京的cn2线路ipv4,可以达到起飞的效果,ipv6更佳.
唯一问题是akaimai没做全证书绑定,会显示证书错误,需要手动忽略

@120318
Copy link

120318 commented Sep 11, 2022

已合并至 1L 。

@GD2021
Copy link

GD2021 commented Dec 25, 2022

mosdns这样配置好的话,还需要下载CloudflareSpeedTest嘛?

@XIU2
Copy link
Owner

XIU2 commented Dec 26, 2022

@GD2021 mosdns 只是负责把所有套了 Cloudflare CDN 的域名解析 IP 修改为 自选 IP 罢了。
而这个自选 IP,还需要依靠 CloudflareST 来本地测速获得,并修改 mosdns 配置文件里的目标 IP。

Repository owner locked and limited conversation to collaborators Mar 1, 2023
@XIU2 XIU2 converted this issue into discussion #317 Mar 1, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
工具教程 辅助工具 或 技巧教程
Projects
None yet
Development

No branches or pull requests

5 participants