OpenWrt之中应用SmartDNS

OpenWrt之中应用SmartDNS










Converted Image



Converted Image



Converted Image




Converted Image




设置之后, /tmp/etc/smartdns/smartdns.conf

dualstack-ip-selection no
serve-expired yes
cache-size 8192
dnsmasq-lease-file /tmp/dhcp.leases
odhcpd-lease-file /tmp/odhcpd.leases
rr-ttl-min 600
log-size 64K
log-num 1
log-level info
log-syslog yes
audit-size 64K
audit-num 1
response-mode first-ping
cache-persist yes
cache-file /etc/smartdns/smartdns.cache
resolv-file /tmp/resolv.conf.d/resolv.conf.auto
bind :5353@br-lan
bind :5353@lo
bind-tcp :5353@br-lan
bind-tcp :5353@lo
bind-https :843@br-lan
bind-https :843@lo
bind-cert-generate yes
bind-cert-root-key-file /etc/smartdns/smartdns-root-key.pem
bind-cert-file /etc/smartdns/smartdns-cert.pem
bind-cert-key-file /etc/smartdns/smartdns-key.pem
server-https https://ram.kicsm.kdns.fr/ramdo 
server-h3 https://wan.legenws.kdns.fr/disco 
server-h3 https://tem.tech.kdns.fr/impro 
server-https https://dbase.txtdb.kdns.fr/tisks 
domain-set -name domain-block-list -file /etc/smartdns/domain-block.list
domain-rules /domain-set:domain-block-list/ -address #
conf-file /etc/smartdns/address.conf
conf-file /etc/smartdns/blacklist-ip.conf
conf-file /etc/smartdns/custom.conf




_______________________________________________________________________________________________________________________________________________________________________________________________________________



如果需要高级设置,比如将上游DOH服务器分成5组,每隔3小时轮换 一组,可以自定义配置文件:

1.首先在LUCI面板中将上游服务器取消勾选,/tmp/etc/smartdns/smartdns.conf 文件如下:


dualstack-ip-selection no
serve-expired yes
cache-size 8192
dnsmasq-lease-file /tmp/dhcp.leases
odhcpd-lease-file /tmp/odhcpd.leases
rr-ttl-min 600
log-size 64K
log-num 1
log-level info
log-syslog yes
audit-size 64K
audit-num 1
response-mode first-ping
cache-persist yes
cache-file /etc/smartdns/smartdns.cache
resolv-file /tmp/resolv.conf.d/resolv.conf.auto
bind :5353@br-lan
bind :5353@lo
bind-tcp :5353@br-lan
bind-tcp :5353@lo
bind-https :843@br-lan
bind-https :843@lo
bind-cert-generate yes
bind-cert-root-key-file /etc/smartdns/smartdns-root-key.pem
bind-cert-file /etc/smartdns/smartdns-cert.pem
bind-cert-key-file /etc/smartdns/smartdns-key.pem
domain-set -name domain-block-list -file /etc/smartdns/domain-block.list
domain-rules /domain-set:domain-block-list/ -address #
conf-file /etc/smartdns/address.conf
conf-file /etc/smartdns/blacklist-ip.conf
conf-file /etc/smartdns/custom.conf


SmartDNS 上游每 3 小时轮换完整教程

一、 前提说明


二、 创建目录和批次文件


mkdir -p /etc/smartdns/upstreams

第 一组 /etc/smartdns/upstreams/batch1.conf

# bootstrap(解析 DoH 域名用,不要删)
server 223.5.5.5 -bootstrap-dns
server 119.29.29.29 -bootstrap-dns

server-https https://ram.kicsm.kdns.fr/ramdo
server-https https://trems.chem.kdns.fr/plat
server-https https://bin.factory.kdns.fr/link
server-https https://12.footeam.kdns.fr/teamai

第 二组 /etc/smartdns/upstreams/batch2.

server 223.5.5.5 -bootstrap-dns
server 119.29.29.29 -bootstrap-dns

server-https https://cook.cakemu.kdns.fr/technol
server-https https://bar2000.foobarl.kdns.fr/teamma
server-https https://paper.dinner.kdns.fr/linkto
server-https https://toos.reflash.kdns.fr/addon

第 三组 /etc/smartdns/upstreams/batch3.

server 223.5.5.5 -bootstrap-dns
server 119.29.29.29 -bootstrap-dns

server-https https://quess.kato.kdns.fr/temp
server-https https://mus.musiclyric.kdns.fr/lyrics
server-https https://testadd.igure.kdns.fr/cookies
server-https https://cacl.shoes.kdns.fr/for

第 四组 /etc/smartdns/upstreams/batch4.

server 223.5.5.5 -bootstrap-dns
server 119.29.29.29 -bootstrap-dns

server-https https://account.temple.kdns.fr/andaa
server-https https://name.protech.kdns.fr/nomane
server-https https://example.winnis.kdns.fr/mem
server-https https://tis.forten.kdns.fr/moddi

第 五组 /etc/smartdns/upstreams/batch5.

server 223.5.5.5 -bootstrap-dns
server 119.29.29.29 -bootstrap-dns

server-https https://no.fourth.kdns.fr/four
server-https https://for.istwo.kdns.fr/fine
server-https https://tree.transtwo.kdns.fr/four
server-https https://tradedb01.20162000.xyz/saledb

三、 初始化激活文件

cp /etc/smartdns/upstreams/batch1. /etc/smartdns/active_upstream.
echo 0 > /etc/smartdns/rotate_state

四、 修改 custom.

编 辑 /etc/smartdns/custom.,只保留或追加这一行(不 要重复写上游):

-file /etc/smartdns/active_upstream.

如 果 custom. 里已有其它自定义规则,把上面这行加在文件末尾即可。


五、 确认主配置干净

打 开你生成的主配置(通常在 /var/etc/smartdns/smartdns. 或 LuCI 对应位置),确认没有类似下面的行:

server-https https://....
server-h3 https://....

你 原来的监听、缓存、日志等保持不动,例如:

bind :5353@br-lan
bind :5353@lo
bind-tcp :5353@br-lan
bind-tcp :5353@lo
...
-file /etc/smartdns/address.
-file /etc/smartdns/blacklist-ip.
-file /etc/smartdns/custom.

六、 轮换脚本

创 建 /etc/smartdns/rotate_upstream.sh:

#!/bin/sh

UPSTREAM_DIR="/etc/smartdns/upstreams"
ACTIVE_="/etc/smartdns/active_upstream."
STATE_FILE="/etc/smartdns/rotate_state"
LOCK_FILE="/var/run/smartdns_rotate.lock"

# 防止并发
[ -f "$LOCK_FILE" ] && exit 0
touch "$LOCK_FILE"
trap "rm -f $LOCK_FILE" EXIT

BATCHES=$(ls "$UPSTREAM_DIR"/batch*. 2>/dev/null | sort)
if [ -z "$BATCHES" ]; then
logger -t smartdns-rotate "ERROR: no batch*. found in $UPSTREAM_DIR"
exit 1
fi

INDEX=0
[ -f "$STATE_FILE" ] && INDEX=$(cat "$STATE_FILE")

COUNT=$(echo "$BATCHES" | wc -l)
NEXT=$(( (INDEX + 1) % COUNT ))
TARGET=$(echo "$BATCHES" | sed -n "$((NEXT + 1))p")

cp "$TARGET" "$ACTIVE_"
echo "$NEXT" > "$STATE_FILE"

# 优先用 reload,失败再 restart
/etc/init.d/smartdns reload 2>/dev/null || /etc/init.d/smartdns restart

logger -t smartdns-rotate "Switched to $(basename "$TARGET") (index $NEXT / total $COUNT)"

赋 予执行权限:

chmod +x /etc/smartdns/rotate_upstream.sh

七、 设置 cron(每 3 小时)

crontab -e

加 入:

0 */3 * * * /etc/smartdns/rotate_upstream.sh

保 存后:

/etc/init.d/cron restart

说 明:0 */3 * * * 表示每天 0 点、3 点、6 点、9 点、12 点、15 点、18 点、21 点执行。


八、 立即生效并验证

# 1. 重新加载 SmartDNS
/etc/init.d/smartdns restart

# 2. 看当前用的是哪一组
cat /etc/smartdns/active_upstream.

# 3. 手动切换一次测试
/etc/smartdns/rotate_upstream.sh
cat /etc/smartdns/active_upstream.
logread | grep smartdns-rotate

# 4. 测试解析(按你实际监听端口)
nslookup www.baidu.com 127.0.0.1#5353
# 或
dig @127.0.0.1 -p 5353 www.baidu.com

九、 目录结构最终应是这样

/etc/smartdns/
├── address.
├── blacklist-ip.
├── custom. ← 里面有 -file active_upstream.
├── active_upstream. ← 当前生效的那一组(脚本会覆盖)
├── rotate_state ← 记录当前是第几组(0~4)
├── rotate_upstream.sh ← 轮换脚本
└── upstreams/
├── batch1.
├── batch2.
├── batch3.
├── batch4.
└── batch5.


十、 可选优化

  1. 想改用 HTTP/3
    把对应行的 server-https 改成 server-h3 即可(前提是你的 SmartDNS 版本支持且 OpenSSL 够新)。

  2. bootstrap 想换
    可以把批次文件里的 223.5.5.5 / 119.29.29.29 换成你更信任的,例如:

    server 1.1.1.1 -bootstrap-dns
    server 8.8.8.8 -bootstrap-dns
  3. 临时固定某一组

    cp /etc/smartdns/upstreams/batch3.conf /etc/smartdns/active_upstream.conf
    echo 2 > /etc/smartdns/rotate_state
    /etc/init.d/smartdns reload
  4. 暂停轮换
    注释掉 crontab 那一行,或删掉即可。


按 上面做完后,SmartDNS 会在每 3 小时自动在 5 组上游之间循环切换。