From 2c3d55cdf58688f4ad23231676ff7cc4e3164ad5 Mon Sep 17 00:00:00 2001 From: sbwml Date: Sun, 10 Apr 2022 20:09:38 +0800 Subject: [PATCH] Add DNS redirect & hosts support --- luasrc/model/cbi/mosdns/rule_list.lua | 40 ++++++++++++++++++++++++++- po/zh-cn/mosdns.po | 17 +++++++++--- root/etc/mosdns/config.yaml | 14 ++++++++++ root/etc/mosdns/rule/hosts.txt | 0 root/etc/mosdns/rule/redirect.txt | 0 5 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 root/etc/mosdns/rule/hosts.txt create mode 100644 root/etc/mosdns/rule/redirect.txt diff --git a/luasrc/model/cbi/mosdns/rule_list.lua b/luasrc/model/cbi/mosdns/rule_list.lua index 6da04d7..b86e785 100644 --- a/luasrc/model/cbi/mosdns/rule_list.lua +++ b/luasrc/model/cbi/mosdns/rule_list.lua @@ -2,14 +2,18 @@ local datatypes = require "luci.cbi.datatypes" local white_list_file = "/etc/mosdns/rule/whitelist.txt" local block_list_file = "/etc/mosdns/rule/blocklist.txt" +local hosts_list_file = "/etc/mosdns/rule/hosts.txt" +local redirect_list_file = "/etc/mosdns/rule/redirect.txt" m = Map("mosdns") -s = m:section(TypedSection, "mosdns", translate("Black and White List Settings")) +s = m:section(TypedSection, "mosdns", translate("Rule Settings")) s.anonymous = true s:tab("white_list", translate("White Lists")) s:tab("block_list", translate("Block Lists")) +s:tab("hosts_list", translate("Hosts")) +s:tab("redirect_list", translate("Redirect")) o = s:taboption("white_list", TextValue, "whitelist", "", "" .. 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.") .. "" .. "" .. translate("
The list of rules only apply to 'Default Config' profiles.") .. "
") o.rows = 15 @@ -51,6 +55,40 @@ o.validate = function(self, value) return value end +o = s:taboption("hosts_list", TextValue, "hosts", "", "" .. translate("Hosts For example: baidu.com 10.0.0.1") .. "" .. "" .. translate("
The list of rules only apply to 'Default Config' profiles.") .. "
") +o.rows = 15 +o.wrap = "off" +o.cfgvalue = function(self, section) return nixio.fs.readfile(hosts_list_file) or "" end +o.write = function(self, section, value) nixio.fs.writefile(hosts_list_file, value:gsub("\r\n", "\n")) end +o.remove = function(self, section, value) nixio.fs.writefile(hosts_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 + end + return value +end + +o = s:taboption("redirect_list", TextValue, "redirect", "", "" .. translate("The domain name to redirect the request to. Requests domain A, but returns records for domain B. example: a.com b.com") .. "" .. "" .. translate("
The list of rules only apply to 'Default Config' profiles.") .. "
") +o.rows = 15 +o.wrap = "off" +o.cfgvalue = function(self, section) return nixio.fs.readfile(redirect_list_file) or "" end +o.write = function(self, section, value) nixio.fs.writefile(redirect_list_file, value:gsub("\r\n", "\n")) end +o.remove = function(self, section, value) nixio.fs.writefile(redirect_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 + end + return value +end + local apply = luci.http.formvalue("cbi.apply") if apply then luci.sys.exec("/etc/init.d/mosdns reload") diff --git a/po/zh-cn/mosdns.po b/po/zh-cn/mosdns.po index d8e1ee4..873263f 100644 --- a/po/zh-cn/mosdns.po +++ b/po/zh-cn/mosdns.po @@ -109,8 +109,8 @@ msgstr "清空日志" msgid "Rule List" msgstr "规则列表" -msgid "Black and White List Settings" -msgstr "自定义黑白名单" +msgid "Rule Settings" +msgstr "自定义规则列表" msgid "
The list of rules only apply to 'Default Config' profiles." msgstr "
规则列表仅适用于 “内置预设” 配置文件" @@ -119,13 +119,22 @@ 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 解析,且优先级最高(每行只能输入一个域名)" +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 解析(每行只能输入一个域名)" +msgstr "加入的域名将屏蔽 DNS 解析(每个域名一行)" msgid "Not valid domain name, Please Re-enter." msgstr "不是有效域名,请重新输入" + +msgid "Hosts For example: baidu.com 10.0.0.1" +msgstr "自定义 Hosts 重写,如:baidu.com 10.0.0.1(每个规则一行)" + +msgid "Redirect" +msgstr "重定向" + +msgid "The domain name to redirect the request to. Requests domain A, but returns records for domain B. example: a.com b.com" +msgstr "重定向请求的域名。请求域名 A,但返回域名 B 的记录,如:baidu.com qq.com(每个规则一行)" diff --git a/root/etc/mosdns/config.yaml b/root/etc/mosdns/config.yaml index 0d5a8b6..9ae2ff0 100644 --- a/root/etc/mosdns/config.yaml +++ b/root/etc/mosdns/config.yaml @@ -7,6 +7,8 @@ plugin: type: server args: entry: + - query_is_hosts_domain + - query_is_redirect_domain - _no_ecs - _prefer_ipv4 - _single_flight @@ -89,6 +91,18 @@ plugin: domain: - "ext:/etc/mosdns/rule/blocklist.txt" + - tag: query_is_hosts_domain + type: hosts + args: + hosts: + - "ext:/etc/mosdns/rule/hosts.txt" + + - tag: query_is_redirect_domain + type: redirect + args: + rule: + - "ext:/etc/mosdns/rule/redirect.txt" + - tag: forward_remote type: fast_forward args: diff --git a/root/etc/mosdns/rule/hosts.txt b/root/etc/mosdns/rule/hosts.txt new file mode 100644 index 0000000..e69de29 diff --git a/root/etc/mosdns/rule/redirect.txt b/root/etc/mosdns/rule/redirect.txt new file mode 100644 index 0000000..e69de29