在OpenWRT上使用EasyMosdns v3.5
在OpenWRT上使用EasyMosdns v3.5
由于近期上网体验很差,怀疑是AdguardHome配置的上游公共DNS开启了限速限流措施导致的,遂计划在路由器上部署一个,刚好EasyMosdns更新了3.5版本,以下是安装记录。
安装luci-app-mosdns
由于编译在OpenWRT上mosdns太耗费时间了,这里可以使用已经编译好的luci-app-mosdns,但是卸载掉luci界面,纯用命令行运行
cd /tmp
wget https://raw.githubusercontent.com/sbwml/luci-app-mosdns/v4/install.sh
编辑install.sh
,注释掉Github Mirror
部分的代码(全部加#号就行了),或者把https://github.cooluc.com/
换成可用的GitHub mirror源
# GitHub mirror
ip_info=$(curl -sk https://ip.cooluc.com)
country_code=$(echo $ip_info | sed -r 's/.*country_code":"([^"]*).*/\1/')
if [ $country_code = "CN" ]; then
google_status=$(curl -I -4 -m 3 -o /dev/null -s -w %{http_code} http://www.google.com/generate_204)
if [ ! $google_status = "204" ];then
mirror="https://github.cooluc.com/"
fi
fi
改好之后:wq
保存,安装
sh install.sh
等待安装完成即可。
下载和配置EasyMosdns
wget https://mirror.apad.pro/dns/easymosdns.tar.gz
tar xzf easymosdns.tar.gz
mv /etc/mosdns /etc/mosdns.old
mv easymosdns /etc/mosdns
在路由器上运行还需要修改一下配置文件
cd /etc/mosdns
vi config.yaml
修改log文件存储目录到/tmp
目录,否则原装配置文件会一直在/etc
目录下存储日志信息,路由器没那么大的空间,坏处就是重启后日志会丢失
log:
file: "/tmp/mosdns.log"
level: info
拉到最下面,把dns监听端口改成8053,同时由于只是在内网运行,可以关掉TCP端口
servers:
- exec: main_sequence
timeout: 6
listeners:
- protocol: udp
addr: "127.0.0.1:8053"
#- protocol: tcp
# addr: "127.0.0.1:8053"
创建Mosdns启动项
cd /etc/init.d/
vi mosdns
把以下代码拷贝进去
#!/bin/sh /etc/rc.common
### OpenWrt init script for mosdns
### Supports start / stop / restart / status
START=98
STOP=10
USE_PROCD=1
NAME=mosdns
BIN=/usr/bin/mosdns
CONFIG_DIR=/etc/mosdns
CONFIG_FILE=config.yaml
PID_FILE=/var/run/$NAME.pid
start_service() {
procd_open_instance
procd_set_param command $BIN start --as-service -d "$CONFIG_DIR" -c "$CONFIG_FILE"
procd_set_param pidfile "$PID_FILE"
procd_set_param respawn
procd_set_param stdout 1
procd_set_param stderr 1
procd_close_instance
}
stop_service() {
[ -f "$PID_FILE" ] && rm -f "$PID_FILE"
}
status() {
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if [ -d "/proc/$PID" ]; then
echo "$NAME is running (PID $PID)"
return 0
else
echo "$NAME pid file exists but process is not running"
return 1
fi
else
echo "$NAME is not running"
return 1
fi
}
给启动文件设置可执行权限并启用
chmod +x /etc/init.d/mosdns
/etc/init.d/mosdns enable
/etc/init.d/mosdns start
修改Adguard Home启动项以检查Mosdns启动情况(可选)
由于此前在 Mosdns
中已经将启动项优先级设置为98,而AdguardHome
默认启动优先级是99,理论上来说等到AdguardHome
启动时Mosdns
肯定已经启动了,当然如果想保险一点的话,那就在AdguardHome
的启动项文件里增加检测确定Mosdns
已经启动了再启动
在 start_service()
函数靠前位置插入以下代码:
# 等待 mosdns 启动(最多等待 5 秒)
WAIT_PORT="127.0.0.1:6053"
i=0
while ! netstat -ln | grep -q "$WAIT_PORT"; do
[ $i -ge 5 ] && echo "mosdns not ready, continuing anyway" && break
echo "Waiting for mosdns to listen on $WAIT_PORT..."
sleep 1
i=$((i+1))
done
空空如也!