DIY NAS系列20 — 智能DNS管理之 SmartDNS

简介

SmartDNS 是一个为 DNS 解析服务设计的软件,其主要目标是提供更快更准确的 DNS 解析。SmartDNS 将解析请求转发给多个上游 DNS 服务器,然后从返回的结果中选择最快的一个进行响应。这种设计使其在提供 DNS 服务的同时,能有效提高解析速度和准确性。

官方架构图如下:

SmartDNS 的特点:

  1. 速度快:SmartDNS 通过并发查询多个 DNS 服务器,从中选择响应最快的结果,大大提高了 DNS 解析速度。
  2. 准确性高:SmartDNS 采用 IP 优选技术,可以根据目标服务器的实际响应速度选择最优的 DNS 服务器,提高解析的准确性。
  3. 支持 IPv 6:SmartDNS 完全支持 IPv6,可以为使用 IPv6 的网络提供服务。
  4. 支持 DNSSEC:SmartDNS 支持 DNSSEC,可以提供安全的 DNS 解析服务。
  5. 灵活的配置:SmartDNS 提供了丰富的配置选项,可以满足不同用户的需求。

打包镜像

因为官方并未提供对应 alpine SmartDNS 镜像,所以这里自己打包镜像。

FROM alpine AS builder
RUN export URL=https://api.github.com/repos/pymumu/smartdns/releases/latest \
  && export OS="linux" \
  && apk --no-cache --update add curl \
  && cd / \
  && wget --tries=3 $(curl -s $URL | grep browser_download_url | egrep -o 'http.+\.\w+' | grep -i "$(uname -m)" | grep -m 1 -i "$(echo $OS)") \
  && tar zxvf smartdns.*.tar.gz

# 指定创建的基础镜像
FROM alpine
# 作者描述信息
LABEL author=木子 [email protected] site=https://www.rockylinux.cn
COPY --from=builder /smartdns/usr/sbin/smartdns /bin/smartdns
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && apk update \
    && apk add --no-cache tzdata ca-certificates bash bash-completion busybox-extras net-tools lsof iproute2 iftop bind-tools vim \
    && chmod +x /bin/smartdns \
    && rm -rf /var/cache/apk/*
WORKDIR /
# 时区
ENV TZ Asia/Shanghai
CMD ["/bin/smartdns","-f","-x","-c","/etc/smartdns/config.conf"]

安装 SmartDNS

说明: 基于前期构建的 traefik 网关,详见: DIY NAS系列12 -- Traefik 出口网关配置
docker-compose.yaml 文件中,会对重要的配置进行说明,因个人网络环境等不同,需要根据自身实际情况调整配置。

[root@demo ~]# cat > docker-compose.yml << \EOF
version: "3"
services:
  smartdns:
    image: rockylinux/alpine-smartdns-service:v1.0
    container_name: smartdns
    restart: always
    privileged: true
#  因为我们使用 ADGuardHome DNS 解析功能做广告拦截,ADGuardHome 上游使用 SmartDNS,所以不需要放出 SmartDNS 端口,在 ADGuardHome 上配置上游  DNS  为 SmartDNS 的容器 IP 即可。
#    ports:
#      - 53:53/tcp
#      - 53:53/udp
    volumes:
      - /SSD/Dockerfile/smartdns/config.conf:/etc/smartdns/config.conf
    networks:
      - traefik_net
networks:
  traefik_net:
    external: true
EOF

# 启动服务
[root@demo ~]# docker-compose up -d

配置 SmartDNS

SmartDNS 的配置相对比较复杂,涉及的参数配置比较多,另外很多参数的配置需要结合其它一些软件或工具,这是简单介绍一下目前个人在用的配置,更多配置参数官方文档: SmartDNS

[root@demo smartdns]# cat > config.conf << \EOF
# https://github.com/pymumu/smartdns/blob/master/etc/smartdns/smartdns.conf
bind-tcp 0.0.0.0:53 #  绑定DNS解析地址
bind 0.0.0.0:53
ipv6 no
tcp-idle-time 3
cache-size 4096
prefetch-domain yes
serve-expired yes
serve-expired-ttl 0
speed-check-mode tcp:80,tcp:443,ping
rr-ttl-min 60
rr-ttl-max 86400
log-level warn
proxy-server socks5://192.168.1.1:1080 -name v2ray-proxy # 翻墙配置
address /mv.rockylinux.cn/192.168.1.1 # Host解析,内网用户访问mv.rockylinux.cn时,SmartDNS解析返回192.168.1.1,address 解析优先级最高
address /bt.rockylinux.cn/192.168.1.1
# 114 DNS 安全版
server 114.114.114.119
# 阿里
server 223.6.6.6
server 223.5.5.5
# 腾讯
server 119.29.29.29
server 119.28.28.28
# 中科大DNS
server 202.141.162.123 
# 广东电信 DNS
server 202.96.128.86

# 对于去到google获取解析的走v2ray-proxy,"-blacklist-ip" 表示启用黑名单IP过滤,"-check-edns" 表示进行EDNS检查,"-proxy v2ray-proxy" 表示通过名为 "v2ray-proxy" 的代理进行连接。这些选项可以为DNS查询提供更高的灵活性和安全性。
server-https https://dns.google/dns-query -blacklist-ip -check-edns -proxy v2ray-proxy
server-tls tls://dns.google -blacklist-ip -check-edns -proxy v2ray-proxy
server-tcp 8.8.8.8:53 -blacklist-ip -check-edns -proxy v2ray-proxy

# 对于去到cloudflare获取解析的走v2ray-proxy
server-tls 1.1.1.1:853 -blacklist-ip -check-edns -proxy v2ray-proxy
server-https https://cloudflare-dns.com/dns-query -blacklist-ip -check-edns -proxy v2ray-proxy

# 这个配置用于创建一个名为 "gfw-domain-list" 的域名集合,该集合中的域名从 "/etc/smartdns/gfw.txt" 文件中读取。这个文件一般包含了您希望通过特定方式解析(如通过特定的DNS服务器或使用特殊的网络连接)的域名列表。
domain-set -name gfw-domain-list -file /etc/smartdns/gfw.txt

# 这个配置则是设定一个域名规则。根据这个规则,所有在 "gfw-domain-list" 集合中的域名解析结果将被加入到名为 "v2ray" 的IP集合中。IP集合是一种在Linux系统中用于存储和匹配IP地址的数据结构,通常用于网络防火墙规则或路由规则的设定。在这个场景下是将这些域名的解析结果(即IP地址)加入到 "v2ray" IP集合中,然后在其他地方(如网络防火墙规则或代理服务器设置)使用这个IP集合,以实现对这些IP地址的特殊处理,比如通过v2ray代理进行访问。
domain-rules /domain-set:gfw-domain-list/ -ipset v2ray

# log-level debug
EOF

后述

至此,我们对开源项目 SmartDNS 的基本介绍就告一段落了。这个项目的灵活性和强大的功能使得它具有很高的可玩性,您可以根据自己的实际需求进行深度定制和配置。SmartDNS 不仅提供了快速准确的 DNS 解析服务,而且通过其丰富的配置选项,允许您根据特定的网络环境和需求进行优化,从而带来更好的网络体验。希望通过这次的介绍,您能对 SmartDNS 有了更深入的了解,并激发出您对它的探索和尝试的兴趣。接下来,在我们的系列文章中,我们将带您了解另一个有趣的开源项目——DDNS-Go。敬请期待,我们不见不散!

下篇预告: DIY NAS系列21 -- 动态DNS管理之 DDNS-Go

Avatar photo

关于 木子

Founder of the Rocky Linux Chinese community, MVP、VMware vExpert、TVP, advocate for cloud native technologies, with over ten years of experience in site reliability engineering (SRE) and the DevOps field. Passionate about Cloud Computing、Microservices、CI&CD、DevOps、Kubernetes, currently dedicated to promoting and implementing Rocky Linux in Chinese-speaking regions.
用一杯咖啡支持我们,每一篇 [文档] 都经过我们实操,并非从网上一味的copy,期间花费了大量的心思,希望能够帮忙到您。
暂无评论

发送评论 编辑评论


|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇