-
Notifications
You must be signed in to change notification settings - Fork 308
Description
1.看了一下初始化文件/etc/init.d/mosdns,现在是直接设置值,并且停止服务时重绑定保护rebind_protection没有恢复。
restore_setting() {
rm -f /etc/mosdns/redirect.lock
sed -i "/list server/d" /etc/config/dhcp
uci set dhcp.@dnsmasq[0].noresolv='0'
uci del dhcp.@dnsmasq[0].cachesize
uci commit dhcp
}
redirect_setting() {
if [ "${CONF}" = "/var/etc/mosdns.json" ]; then
sed -i "/list server/d" /etc/config/dhcp
uci add_list dhcp.@dnsmasq[0].server="127.0.0.1#$listen_port"
uci set dhcp.@dnsmasq[0].rebind_protection='0'
uci set dhcp.@dnsmasq[0].noresolv="1"
uci set dhcp.@dnsmasq[0].cachesize='0'
uci commit dhcp
else
sed -i "/list server/d" /etc/config/dhcp
uci add_list dhcp.@dnsmasq[0].server="127.0.0.1#$(awk -F'[:" ]+' '/^\s+listen:/{for(i=1;i<=NF;i++){if($i~/^[0-9]+$/){print $i;exit}}}' $CONF)"
uci set dhcp.@dnsmasq[0].rebind_protection='0'
uci set dhcp.@dnsmasq[0].noresolv="1"
uci set dhcp.@dnsmasq[0].cachesize='0'
uci commit dhcp
fi
touch /etc/mosdns/redirect.lock
}
2.不同的openwrt的dnsmasq默认值也不同,想要停止mosdns时恢复默认值就需要先将需要修改的默认值保存在配置文件中。新增两个函数save_dnsmasq_server、set_dnsmasq_server修改了restore_setting和redirect_setting两个函数中的逻辑,其余无修改。
save_dnsmasq_server() {
if [ -z "$1" ] || [ "$1" = "127.0.0.1#$listen_port" ]; then
return 0
fi
uci -q add_list mosdns.config.dnsmasq_server="$1"
}
set_dnsmasq_server() {
if [ -z "$1" ] || [ "$1" = "127.0.0.1#$listen_port" ]; then
return 0
fi
uci -q add_list dhcp.@dnsmasq[0].server="$1"
}
restore_setting() {
rm -f /etc/mosdns/redirect.lock
uci -q del dhcp.@dnsmasq[-1].server
config_load "mosdns"
config_list_foreach "config" "dnsmasq_server" set_dnsmasq_server
if [ -z "$(uci -q get mosdns.config.dnsmasq_rebind_protection)" ]; then
uci -q del dhcp.@dnsmasq[0].rebind_protection
else
uci -q set dhcp.@dnsmasq[0].rebind_protection="$(uci -q get mosdns.config.dnsmasq_rebind_protection)"
fi
if [ -z "$(uci -q get mosdns.config.dnsmasq_noresolv)" ]; then
uci -q del dhcp.@dnsmasq[0].noresolv
else
uci -q set dhcp.@dnsmasq[0].noresolv="$(uci -q get mosdns.config.dnsmasq_noresolv)"
fi
if [ -z "$(uci -q get mosdns.config.dnsmasq_cachesize)" ]; then
uci -q del dhcp.@dnsmasq[0].cachesize
else
uci -q set dhcp.@dnsmasq[0].cachesize="$(uci -q get mosdns.config.dnsmasq_cachesize)"
fi
uci -q del mosdns.config.dnsmasq_server
uci -q del mosdns.config.dnsmasq_rebind_protection
uci -q del mosdns.config.dnsmasq_noresolv
uci -q del mosdns.config.dnsmasq_cachesize
uci -q commit dhcp && uci -q commit mosdns
}
redirect_setting() {
if [ "${CONF}" = "/var/etc/mosdns.json" ]; then
config_load "dhcp"
config_list_foreach "$(uci -q show dhcp.@dnsmasq[0].server |awk -F '.' '{print $2}')" "server" save_dnsmasq_server
uci -q set mosdns.config.dnsmasq_rebind_protection="$(uci -q get dhcp.@dnsmasq[0].rebind_protection)"
uci -q set mosdns.config.dnsmasq_noresolv="$(uci -q get dhcp.@dnsmasq[0].noresolv)"
uci -q set mosdns.config.dnsmasq_cachesize="$(uci -q get dhcp.@dnsmasq[0].cachesize)"
uci -q del dhcp.@dnsmasq[-1].server
uci -q add_list dhcp.@dnsmasq[0].server="127.0.0.1#$listen_port"
uci -q set dhcp.@dnsmasq[0].rebind_protection='0'
uci -q set dhcp.@dnsmasq[0].noresolv="1"
uci -q set dhcp.@dnsmasq[0].cachesize='0'
uci -q commit dhcp && uci -q commit mosdns
else
config_load "dhcp"
config_list_foreach "$(uci -q show dhcp.@dnsmasq[0].server |awk -F '.' '{print $2}')" "server" save_dnsmasq_server
uci -q set mosdns.config.dnsmasq_rebind_protection="$(uci -q get dhcp.@dnsmasq[0].rebind_protection)"
uci -q set mosdns.config.dnsmasq_noresolv="$(uci -q get dhcp.@dnsmasq[0].noresolv)"
uci -q set mosdns.config.dnsmasq_cachesize="$(uci -q get dhcp.@dnsmasq[0].cachesize)"
uci -q del dhcp.@dnsmasq[-1].server
uci -q add_list dhcp.@dnsmasq[0].server="127.0.0.1#$(awk -F'[:" ]+' '/^\s+listen:/{for(i=1;i<=NF;i++){if($i~/^[0-9]+$/){print $i;exit}}}' $CONF)"
uci -q set dhcp.@dnsmasq[0].rebind_protection='0'
uci -q set dhcp.@dnsmasq[0].noresolv="1"
uci -q set dhcp.@dnsmasq[0].cachesize='0'
uci -q commit dhcp && uci -q commit mosdns
fi
touch /etc/mosdns/redirect.lock
}
3.这样修改后dnsmasq被修改的默认值在停止mosdns服务时可以恢复。这是参考了openclash中对dnsmasq的处理,这样可以适配更多不同的openwrt版本。
4.处理逻辑时发现了一个问题。如果设备突然断电重启后会执行redirect_setting,但是这时dnsmasq中的server处于被修改的状态,redirect_setting函数中会将被修改的dnsmasq存储到mosdns配置文件中,这就会导致原本dnsmasq的设置会丢失。为了解决这个问题在有执行redirect_setting函数的地方前先执行restore_setting。查找发现现在只有start_service函数22行中有执行一次redirect_setting
[ "$redirect" -eq 1 ] && redirect_setting
将其修改为
if [ "$redirect" -eq 1 ]; then
if [ -f "/etc/mosdns/redirect.lock" ]; then
restore_setting && redirect_setting
else
redirect_setting
fi
fi
这里使用if判断[ -f "/etc/mosdns/redirect.lock" ]而不直接使用[ "$redirect" -eq 1 ] && restore_setting && redirect_setting的原因是防止手动停止后再启动时执行restore_setting ,这样会导致修改的dnsmasq会被还原成mosdns配置文件保存的值。
5.我自己使用时每次都会修改此文件,达到想使用的效果。这次分享出来是希望作者可以并入此项目中,代码如果有可以改进的地方还请指正。
6.题外,懒得再提交一个issue。文件/usr/share/mosdns/mosdns.sh,此文件中的v2dat_dump函数18和19行
# custom config
v2dat unpack geoip -o /var/mosdns -f cn $v2dat_dir/geoip.dat
v2dat unpack geosite -o /var/mosdns -f cn -f 'geolocation-!cn' $v2dat_dir/geosite.dat
使用自定义配置的时候v2dat还是默认生成geoip_cn、geosite_cn、geosite_geolocation-!cn这三个文件,感觉不是很合理,可以删除这两行。因为此项目有geodata导出功能,使用自定义配置时需要哪些导出哪些就行了,再默认生成有可能用不到,有点损失性能。