Custom DNS resolution rule list support

This commit is contained in:
sbwml 2022-04-10 03:15:51 +08:00
parent 08359466a7
commit 7ebb5558f4
11 changed files with 118 additions and 73 deletions

View File

@ -1,7 +1,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=luci-app-mosdns
PKG_VERSION:=1.2
PKG_VERSION:=1.3
PKG_RELEASE:=1
LUCI_TITLE:=LuCI Support for mosdns
@ -9,8 +9,9 @@ LUCI_PKGARCH:=all
LUCI_DEPENDS:=+mosdns +jsonfilter +bash +v2ray-geoip +v2ray-geosite
define Package/$(PKG_NAME)/conffiles
/etc/mosdns/config_custom.yaml
/etc/config/mosdns
/etc/mosdns/config_custom.yaml
/etc/mosdns/rule
endef
include $(TOPDIR)/feeds/luci/luci.mk

View File

@ -10,7 +10,7 @@ function index()
page.acl_depends = { "luci-app-mosdns" }
entry({"admin", "services", "mosdns", "basic"}, cbi("mosdns/basic"), _("Basic Setting"), 1).leaf = true
entry({"admin", "services", "mosdns", "whitelist"}, cbi("mosdns/whitelist"), _("ADblock whitelist"), 2).leaf = true
entry({"admin", "services", "mosdns", "rule_list"}, cbi("mosdns/rule_list"), _("Rule List"), 2).leaf = true
entry({"admin", "services", "mosdns", "update"}, cbi("mosdns/update"), _("Geodata Update"), 3).leaf = true
entry({"admin", "services", "mosdns", "log"}, cbi("mosdns/log"), _("Logs"), 4).leaf = true
entry({"admin", "services", "mosdns", "status"}, call("act_status")).leaf = true

View File

@ -11,9 +11,9 @@ s.anonymous = true
enable = s:option(Flag, "enabled", translate("Enable"))
enable.rmempty = false
configfile = s:option(ListValue, "configfile", translate("MosDNS Config File"))
configfile:value("/etc/mosdns/config.yaml", translate("Def Config"))
configfile:value("/etc/mosdns/config_custom.yaml", translate("Cus Config"))
configfile = s:option(ListValue, "configfile", translate("Config File"))
configfile:value("/etc/mosdns/config.yaml", translate("Default Config"))
configfile:value("/etc/mosdns/config_custom.yaml", translate("Custom Config"))
configfile.default = "/etc/mosdns/config.yaml"
listenport = s:option(Value, "listen_port", translate("Listen port"))
@ -30,9 +30,9 @@ loglevel:value("error", translate("Error"))
loglevel.default = "error"
loglevel:depends( "configfile", "/etc/mosdns/config.yaml")
logfile = s:option(Value, "logfile", translate("MosDNS Log File"))
logfile.placeholder = "/dev/null"
logfile.default = "/dev/null"
logfile = s:option(Value, "logfile", translate("Log File"))
logfile.placeholder = "/tmp/mosdns.log"
logfile.default = "/tmp/mosdns.log"
logfile:depends( "configfile", "/etc/mosdns/config.yaml")
dnsforward = s:option(Value, "dns_forward", translate("Remote DNS"))

View File

@ -0,0 +1,59 @@
local datatypes = require "luci.cbi.datatypes"
local white_list_file = "/etc/mosdns/rule/whitelist.txt"
local block_list_file = "/etc/mosdns/rule/blocklist.txt"
m = Map("mosdns")
s = m:section(TypedSection, "mosdns", translate("Black and White List Settings"))
s.anonymous = true
s:tab("white_list", translate("White Lists"))
s:tab("block_list", translate("Block Lists"))
o = s:taboption("white_list", TextValue, "whitelist", "", "<font color='red'>" .. translate("These domain names allow DNS resolution with the highest priority. Please input the domain names of websites, every line can input only one website domain. For example: hm.baidu.com.") .. "</font>" .. "<font color='#00bd3e'>" .. translate("<br>The list of rules only apply to 'Default Config' profiles.") .. "</font>")
o.rows = 15
o.wrap = "off"
o.cfgvalue = function(self, section) return nixio.fs.readfile(white_list_file) or "" end
o.write = function(self, section, value) nixio.fs.writefile(white_list_file , value:gsub("\r\n", "\n")) end
o.remove = function(self, section, value) nixio.fs.writefile(white_list_file , "") end
o.validate = function(self, value)
local hosts= {}
string.gsub(value, '[^' .. "\r\n" .. ']+', function(w) table.insert(hosts, w) end)
for index, host in ipairs(hosts) do
if host:find("#") and host:find("#") == 1 then
return value
end
if not datatypes.hostname(host) then
return nil, host .. " " .. translate("Not valid domain name, Please Re-enter.")
end
end
return value
end
o = s:taboption("block_list", TextValue, "blocklist", "", "<font color='red'>" .. translate("These domains are blocked from DNS resolution. Please input the domain names of websites, every line can input only one website domain. For example: baidu.com.") .. "</font>" .. "<font color='#00bd3e'>" .. translate("<br>The list of rules only apply to 'Default Config' profiles.") .. "</font>")
o.rows = 15
o.wrap = "off"
o.cfgvalue = function(self, section) return nixio.fs.readfile(block_list_file) or "" end
o.write = function(self, section, value) nixio.fs.writefile(block_list_file, value:gsub("\r\n", "\n")) end
o.remove = function(self, section, value) nixio.fs.writefile(block_list_file, "") end
o.validate = function(self, value)
local hosts= {}
string.gsub(value, '[^' .. "\r\n" .. ']+', function(w) table.insert(hosts, w) end)
for index, host in ipairs(hosts) do
if host:find("#") and host:find("#") == 1 then
return value
end
if not datatypes.hostname(host) then
return nil, host .. " " .. translate("Not valid domain name, Please Re-enter.")
end
end
return value
end
local apply = luci.http.formvalue("cbi.apply")
if apply then
luci.sys.exec("/etc/init.d/mosdns reload")
end
return m

View File

@ -1,6 +1,6 @@
m = Map("mosdns")
s = m:section(TypedSection, "mosdns", translate("Geodata Update"))
s = m:section(TypedSection, "mosdns", translate("Update GeoIP & GeoSite dat"))
s.addremove = false
s.anonymous = true
@ -18,7 +18,7 @@ o:value("6", translate("Every Saturday"))
o:value("7", translate("Every Sunday"))
o.default = "*"
update_time = s:option(ListValue, "geo_update_day_time", translate("Update Time (Every Day)"))
update_time = s:option(ListValue, "geo_update_day_time", translate("Update Time"))
for t = 0, 23 do
update_time:value(t, t..":00")
end

View File

@ -1,26 +0,0 @@
m = Map("mosdns")
s = m:section(TypedSection, "mosdns", translate("ADblock whitelist"))
s.addremove = false
s.anonymous = true
config = s:option(TextValue, "whitelist")
config.description = translate("<font color=\"ff0000\"><strong>ADblock whitelist only apply to 'Def Config' profiles.</strong></font>")
config.template = "cbi/tvalue"
config.rows = 25
function config.cfgvalue(self, section)
return nixio.fs.readfile("/etc/mosdns/whitelist.txt")
end
function config.write(self, section, value)
value = value:gsub("\r\n?", "\n")
nixio.fs.writefile("/etc/mosdns/whitelist.txt", value)
end
local apply = luci.http.formvalue("cbi.apply")
if apply then
luci.sys.exec("/etc/init.d/mosdns reload")
end
return m

View File

@ -1,9 +1,6 @@
msgid "Basic Setting"
msgstr "基本设置"
msgid "Manual Configuration"
msgstr "手动配置"
msgid "MosDNS is a 'programmable' DNS forwarder."
msgstr "MosDNS 是一个插件化的 DNS 转发/分流器。"
@ -32,19 +29,13 @@ msgid "Enable DNS ADblock"
msgstr "启用 DNS 广告过滤"
msgid "<font color=\"ff0000\"><strong>View the Custom YAML Configuration file used by this MosDNS. You can edit it as you own need.</strong></font>"
msgstr "<font color=\"ff0000\"><strong>注意!此页的更改仅当 'MosDNS 配置文件选择' 为 '自定义配置' 时才会生效</strong></font>"
msgid "<font color=\"ff0000\"><strong>ADblock whitelist only apply to 'Def Config' profiles.</strong></font>"
msgstr "<font color=\"ff0000\"><strong>注意!广告过滤白名单仅当 'MosDNS 配置文件选择' 为 '内置预设' 时才会生效</strong></font>"
msgid "ADblock whitelist"
msgstr "广告白名单"
msgstr "<font color=\"ff0000\"><strong>注意!此页的更改仅当配置文件为 “自定义” 时才会生效</strong></font>"
msgid "Geodata Update"
msgstr "数据库更新"
msgstr "更新数据库"
msgid "Update Time (Every Day)"
msgstr "更新时间(每天)"
msgid "Update Time"
msgstr "更新时间"
msgid "Update Cycle"
msgstr "更新周期"
@ -80,19 +71,19 @@ msgid "Check And Update"
msgstr "检查并更新"
msgid "Enable Auto Database Update"
msgstr "启用数据库自动更新"
msgstr "启用自动更新"
msgid "MosDNS Config File"
msgstr "MosDNS 配置文件选择"
msgid "Config File"
msgstr "配置文件"
msgid "Def Config"
msgid "Default Config"
msgstr "内置预设"
msgid "Cus Config"
msgstr "自定义配置"
msgid "Custom Config"
msgstr "自定义"
msgid "MosDNS Log File"
msgstr "MosDNS 日志文件"
msgid "Log File"
msgstr "日志文件"
msgid "Remote DNS"
msgstr "远程 DNS"
@ -111,3 +102,27 @@ msgstr "日志"
msgid "Clear logs"
msgstr "清空日志"
msgid "Rule List"
msgstr "规则列表"
msgid "Black and White List Settings"
msgstr "自定义黑白名单"
msgid "<br>The list of rules only apply to 'Default Config' profiles."
msgstr "<br>规则列表仅适用于 “内置预设” 配置文件"
msgid "White Lists"
msgstr "白名单"
msgid "These domain names allow DNS resolution with the highest priority. Please input the domain names of websites, every line can input only one website domain. For example: hm.baidu.com."
msgstr "加入的域名始终允许 DNS 解析,且优先级最高(每行只能输入一个域名)"
msgid "Block Lists"
msgstr "黑名单"
msgid "These domains are blocked from DNS resolution. Please input the domain names of websites, every line can input only one website domain. For example: baidu.com."
msgstr "加入的域名将屏蔽 DNS 解析(每行只能输入一个域名)"
msgid "Not valid domain name, Please Re-enter."
msgstr "不是有效域名,请重新输入"

View File

@ -5,12 +5,9 @@
START=91
USE_PROCD=1
##### ONLY CHANGE THIS BLOCK ######
PROG=/usr/bin/mosdns # where is mosdns
RES_DIR=/etc/mosdns/ # resource dir / working dir / the dir where you store ip/domain lists
CONF=$(uci -q get mosdns.mosdns.configfile) # where is the config file, it can be a relative path to $RES_DIR
PROG=/usr/bin/mosdns
CONF=$(uci -q get mosdns.mosdns.configfile)
CRON_FILE=/etc/crontabs/root
##### ONLY CHANGE THIS BLOCK ######
inital_conf() {
config_load "mosdns"
@ -48,7 +45,6 @@ reload_others() {
reload_service() {
stop
sleep 1
echo "MosDNS is restarted!"
start
}
@ -64,15 +60,13 @@ delcron() {
}
start_service() {
# Reading config
inital_conf
[ $enabled != 1 ] && return 1
delcron
setcron
echo '' > $(uci -q get mosdns.mosdns.logfile)
procd_open_instance mosdns
procd_set_param command $PROG -dir $RES_DIR -c "$CONF"
procd_set_param command $PROG -c "$CONF"
procd_set_param user root
procd_set_param stdout 1
procd_set_param stderr 1
@ -85,14 +79,10 @@ start_service() {
redirect_setting
reload_others
fi
echo "MosDNS turn on"
}
stop_service() {
pgrep -f /usr/bin/mosdns | xargs kill -9
echo "MosDNS turn off"
configfile=$(uci -q get mosdns.mosdns.configfile)
if [ "${configfile}" = "/etc/mosdns/config.yaml" ]; then
config_load "mosdns"
@ -101,5 +91,4 @@ stop_service() {
reload_others
fi
delcron
}

View File

@ -27,6 +27,7 @@ plugin:
- forward_local
- _return
- if:
- query_is_blocklist_domain
- query_is_ad_domain
exec:
- _block_with_nxdomain
@ -80,7 +81,13 @@ plugin:
type: query_matcher
args:
domain:
- "ext:./whitelist.txt"
- "ext:/etc/mosdns/rule/whitelist.txt"
- tag: query_is_blocklist_domain
type: query_matcher
args:
domain:
- "ext:/etc/mosdns/rule/blocklist.txt"
- tag: forward_remote
type: fast_forward

View File