From 6ebb46c873840cb8ce934217cf58cff21d1b2716 Mon Sep 17 00:00:00 2001 From: sbwml <984419930@qq.com> Date: Wed, 29 Jun 2022 03:50:27 +0800 Subject: [PATCH] luci-app-mosdns: update mosdns v4.x support --- luci-app-mosdns/Makefile | 2 +- .../luasrc/model/cbi/mosdns/basic.lua | 15 +- luci-app-mosdns/po/zh-cn/mosdns.po | 9 +- luci-app-mosdns/root/etc/config/mosdns | 7 +- luci-app-mosdns/root/etc/init.d/mosdns | 19 +- luci-app-mosdns/root/etc/mosdns/config.yaml | 136 ------------- .../root/etc/mosdns/config_custom.yaml | 192 ++++++++++++------ .../root/usr/share/mosdns/ad_domain.sh | 2 +- .../root/usr/share/mosdns/default.yaml | 156 ++++++++++++++ 9 files changed, 322 insertions(+), 216 deletions(-) delete mode 100644 luci-app-mosdns/root/etc/mosdns/config.yaml create mode 100644 luci-app-mosdns/root/usr/share/mosdns/default.yaml diff --git a/luci-app-mosdns/Makefile b/luci-app-mosdns/Makefile index 5018876..1bc0050 100644 --- a/luci-app-mosdns/Makefile +++ b/luci-app-mosdns/Makefile @@ -1,7 +1,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=luci-app-mosdns -PKG_VERSION:=1.3 +PKG_VERSION:=1.4 PKG_RELEASE:=1 LUCI_TITLE:=LuCI Support for mosdns diff --git a/luci-app-mosdns/luasrc/model/cbi/mosdns/basic.lua b/luci-app-mosdns/luasrc/model/cbi/mosdns/basic.lua index 89253d2..cafa5ab 100644 --- a/luci-app-mosdns/luasrc/model/cbi/mosdns/basic.lua +++ b/luci-app-mosdns/luasrc/model/cbi/mosdns/basic.lua @@ -60,15 +60,20 @@ cache_size.datatype = "and(uinteger,min(0))" cache_size.default = "200000" cache_size:depends( "configfile", "/etc/mosdns/config.yaml") +cache_size = s:option(Value, "cache_survival_time", translate("Cache Survival Time")) +cache_size.datatype = "and(uinteger,min(0))" +cache_size.default = "259200" +cache_size:depends( "configfile", "/etc/mosdns/config.yaml") + minimal_ttl = s:option(Value, "minimal_ttl", translate("Minimum TTL")) -minimal_ttl.datatype = "and(uinteger,min(1))" +minimal_ttl.datatype = "and(uinteger,min(0))" minimal_ttl.datatype = "and(uinteger,max(3600))" -minimal_ttl.default = "300" +minimal_ttl.default = "0" minimal_ttl:depends( "configfile", "/etc/mosdns/config.yaml") maximum_ttl = s:option(Value, "maximum_ttl", translate("Maximum TTL")) -maximum_ttl.datatype = "and(uinteger,min(1))" -maximum_ttl.default = "3600" +maximum_ttl.datatype = "and(uinteger,min(0))" +maximum_ttl.default = "0" maximum_ttl:depends( "configfile", "/etc/mosdns/config.yaml") redirect = s:option(Flag, "redirect", translate("Enable DNS Forward"), translate("Forward Dnsmasq Domain Name resolution requests to MosDNS")) @@ -77,7 +82,7 @@ redirect.default = true adblock = s:option(Flag, "adblock", translate("Enable DNS ADblock")) adblock:depends( "configfile", "/etc/mosdns/config.yaml") -adblock.default = true +adblock.default = false config = s:option(TextValue, "manual-config") config.description = translate("View the Custom YAML Configuration file used by this MosDNS. You can edit it as you own need.") diff --git a/luci-app-mosdns/po/zh-cn/mosdns.po b/luci-app-mosdns/po/zh-cn/mosdns.po index efa4b51..ab9dba3 100644 --- a/luci-app-mosdns/po/zh-cn/mosdns.po +++ b/luci-app-mosdns/po/zh-cn/mosdns.po @@ -92,13 +92,16 @@ msgid "Remote DNS" msgstr "远程 DNS" msgid "DNS Cache Size" -msgstr "DNS 缓存大小" +msgstr "DNS 缓存大小(条)" + +msgid "Cache Survival Time" +msgstr "缓存生存时间(秒)" msgid "Minimum TTL" -msgstr "覆盖最小 TTL 值" +msgstr "覆盖最小 TTL 值(禁用:0)" msgid "Maximum TTL" -msgstr "覆盖最大 TTL 值" +msgstr "覆盖最大 TTL 值(禁用:0)" msgid "Logs" msgstr "日志" diff --git a/luci-app-mosdns/root/etc/config/mosdns b/luci-app-mosdns/root/etc/config/mosdns index e4b30df..05d7fa5 100644 --- a/luci-app-mosdns/root/etc/config/mosdns +++ b/luci-app-mosdns/root/etc/config/mosdns @@ -8,11 +8,12 @@ config mosdns 'mosdns' option redirect '1' option adblock '0' option configfile '/etc/mosdns/config.yaml' - option log_level 'error' + option log_level 'info' option logfile '/tmp/mosdns.log' option remote_dns1 'tls://8.8.8.8' option remote_dns2 'tls://1.1.1.1' option cache_size '200000' - option minimal_ttl '300' - option maximum_ttl '3600' + option cache_survival_time '259200' + option minimal_ttl '0' + option maximum_ttl '0' diff --git a/luci-app-mosdns/root/etc/init.d/mosdns b/luci-app-mosdns/root/etc/init.d/mosdns index f3d7a44..af7b8a1 100755 --- a/luci-app-mosdns/root/etc/init.d/mosdns +++ b/luci-app-mosdns/root/etc/init.d/mosdns @@ -14,6 +14,22 @@ inital_conf() { config_get "enabled" "mosdns" "enabled" "0" } +init_yaml() { + log_level=$(uci -q get mosdns.mosdns.log_level) + log_file=$(uci -q get mosdns.mosdns.logfile) + listen_port=$(uci -q get mosdns.mosdns.listen_port) + cache_size=$(uci -q get mosdns.mosdns.cache_size) + cache_survival_time=$(uci -q get mosdns.mosdns.cache_survival_time) + minimal_ttl_custom=$(uci -q get mosdns.mosdns.minimal_ttl) + maximum_ttl_custom=$(uci -q get mosdns.mosdns.maximum_ttl) + local_dns1=$(/usr/share/mosdns/dns.sh 0) + local_dns2=$(/usr/share/mosdns/dns.sh 1) + remote_dns1=$(uci -q get mosdns.mosdns.remote_dns1) + remote_dns2=$(uci -q get mosdns.mosdns.remote_dns2) + adblock=$(/usr/share/mosdns/ad_domain.sh) + sed "s,log_level,$log_level,g;s,log_file,$log_file,g;s,listen_port,$listen_port,g;s,cache_size,$cache_size,g;s,cache_survival_time,$cache_survival_time,g;s,minimal_ttl_custom,$minimal_ttl_custom,g;s,maximum_ttl_custom,$maximum_ttl_custom,g;s,local_dns1,$local_dns1,g;s,local_dns2,$local_dns2,g;s,remote_dns1,$remote_dns1,g;s,remote_dns2,$remote_dns2,g;s,adblock,$adblock,g" /usr/share/mosdns/default.yaml > /etc/mosdns/config.yaml +} + service_triggers() { procd_add_reload_trigger "mosdns" } @@ -64,9 +80,10 @@ start_service() { [ $enabled != 1 ] && return 1 delcron setcron + init_yaml cat /dev/null > $(uci -q get mosdns.mosdns.logfile) procd_open_instance mosdns - procd_set_param command $PROG -c "$CONF" + procd_set_param command $PROG start -c "$CONF" procd_set_param user root procd_set_param stdout 1 procd_set_param stderr 1 diff --git a/luci-app-mosdns/root/etc/mosdns/config.yaml b/luci-app-mosdns/root/etc/mosdns/config.yaml deleted file mode 100644 index 9fa8a8d..0000000 --- a/luci-app-mosdns/root/etc/mosdns/config.yaml +++ /dev/null @@ -1,136 +0,0 @@ -log: - level: ${{ uci -q get mosdns.mosdns.log_level }} - file: ${{ uci -q get mosdns.mosdns.logfile }} - -plugin: - - tag: main_server - type: server - args: - entry: - - _single_flight - - main_sequence - server: - - protocol: udp - addr: ":${{ uci -q get mosdns.mosdns.listen_port }}" - - protocol: tcp - addr: ":${{ uci -q get mosdns.mosdns.listen_port }}" - - - tag: main_sequence - type: sequence - args: - exec: - - query_is_hosts_domain - - query_is_redirect_domain - - if: - - query_is_whitelist_domain - exec: - - forward_local - - _return - - if: - - query_is_blocklist_domain - - query_is_ad_domain - exec: - - _block_with_nxdomain - - _return - - - mem_cache - - - if: - - query_is_local_domain - - "!_query_is_common" - exec: - - forward_local - - _return - - if: - - query_is_non_local_domain - exec: - - _prefer_ipv4 - - forward_remote - - _return - - primary: - - forward_local - - if: - - "!response_has_local_ip" - exec: - - _drop_response - secondary: - - _prefer_ipv4 - - forward_remote - fast_fallback: 200 - always_standby: true - - - modify_ttl - - - tag: mem_cache - type: cache - args: - size: ${{ uci -q get mosdns.mosdns.cache_size }} - - - tag: modify_ttl - type: ttl - args: - minimal_ttl: ${{ uci -q get mosdns.mosdns.minimal_ttl }} - maximum_ttl: ${{ uci -q get mosdns.mosdns.maximum_ttl }} - - - tag: forward_local - type: fast_forward - args: - upstream: - - addr: ${{ /usr/share/mosdns/dns.sh 0 }} - - addr: ${{ /usr/share/mosdns/dns.sh 1 }} - - - tag: forward_remote - type: fast_forward - args: - upstream: - - addr: ${{ uci -q get mosdns.mosdns.remote_dns1 }} - - addr: ${{ uci -q get mosdns.mosdns.remote_dns2 }} - - - tag: query_is_whitelist_domain - type: query_matcher - args: - domain: - - "ext:/etc/mosdns/rule/whitelist.txt" - - - tag: query_is_blocklist_domain - type: query_matcher - args: - 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: query_is_local_domain - type: query_matcher - args: - domain: - - "ext:/usr/share/v2ray/geosite.dat:cn" - - "ext:/usr/share/v2ray/geosite.dat:apple-cn" - - - tag: query_is_non_local_domain - type: query_matcher - args: - domain: - - "ext:/usr/share/v2ray/geosite.dat:geolocation-!cn" - - - tag: response_has_local_ip - type: response_matcher - args: - ip: - - "ext:/usr/share/v2ray/geoip.dat:cn" - - - tag: query_is_ad_domain - type: query_matcher - args: - domain: - - ${{ /usr/share/mosdns/ad_domain.sh }} diff --git a/luci-app-mosdns/root/etc/mosdns/config_custom.yaml b/luci-app-mosdns/root/etc/mosdns/config_custom.yaml index 19ab0c0..e382916 100644 --- a/luci-app-mosdns/root/etc/mosdns/config_custom.yaml +++ b/luci-app-mosdns/root/etc/mosdns/config_custom.yaml @@ -1,96 +1,156 @@ log: - level: error - file: "" + level: info + file: "/tmp/mosdns.log" -plugin: - - tag: main_server - type: server - args: - entry: - - _no_ecs - - lazy_cache - - _prefer_ipv4 - - _single_flight - - main_sequence - server: - - protocol: udp - addr: "127.0.0.1:5335" - - protocol: tcp - addr: "127.0.0.1:5335" +include: [] - - tag: main_sequence - type: sequence - args: - exec: - - if: - - query_is_ad_domain - exec: - - _block_with_nxdomain - - _return - - if: - - query_is_local_domain - - "!_query_is_common" - exec: - - forward_local - - _return - - if: - - query_is_non_local_domain - exec: - - forward_remote - - _return - - primary: - - forward_local - - if: - - "!response_has_local_ip" - exec: - - _drop_response - secondary: - - forward_remote - fast_fallback: 150 - always_standby: true +data_providers: + - tag: geoip + file: "/usr/share/v2ray/geoip.dat" + auto_reload: true - - tag: forward_local - type: fast_forward - args: - upstream: - - addr: ${{ /usr/share/mosdns/dns.sh 0 }} - - addr: ${{ /usr/share/mosdns/dns.sh 1 }} + - tag: geosite + file: "/usr/share/v2ray/geosite.dat" + auto_reload: true - - tag: forward_remote - type: fast_forward - args: - upstream: - - addr: "tls://8.8.4.4" - idle_timeout: 10 - - addr: "tls://9.9.9.9" - idle_timeout: 10 + - tag: whitelist + file: "/etc/mosdns/rule/whitelist.txt" + auto_reload: true + - tag: blocklist + file: "/etc/mosdns/rule/blocklist.txt" + auto_reload: true + + - tag: hosts + file: "/etc/mosdns/rule/hosts.txt" + auto_reload: true + + - tag: redirect + file: "/etc/mosdns/rule/redirect.txt" + auto_reload: true + +plugins: - tag: lazy_cache type: cache args: - size: 512000 + size: 200000 lazy_cache_ttl: 259200 + - tag: modify_ttl + type: ttl + args: + minimal_ttl: 0 + maximum_ttl: 0 + + - tag: "forward_local" + type: fast_forward + args: + upstream: + - addr: 119.29.29.29 + - addr: 114.114.114.114 + + - tag: "forward_remote" + type: fast_forward + args: + upstream: + - addr: tls://8.8.8.8 + - addr: tls://1.1.1.1 + + - tag: query_is_whitelist_domain + type: query_matcher + args: + domain: + - "provider:whitelist" + + - tag: query_is_blocklist_domain + type: query_matcher + args: + domain: + - "provider:blocklist" + + - tag: query_is_hosts_domain + type: hosts + args: + hosts: + - "provider:hosts" + + - tag: query_is_redirect_domain + type: redirect + args: + rule: + - "provider:redirect" + - tag: query_is_local_domain type: query_matcher args: domain: - - "ext:/usr/share/v2ray/geosite.dat:cn" + - "provider:geosite:cn" - tag: query_is_non_local_domain type: query_matcher args: domain: - - "ext:/usr/share/v2ray/geosite.dat:geolocation-!cn" + - "provider:geosite:geolocation-!cn" - tag: response_has_local_ip type: response_matcher args: ip: - - "ext:/usr/share/v2ray/geoip.dat:cn" + - "provider:geoip:cn" - tag: query_is_ad_domain type: query_matcher args: domain: - - "ext:/usr/share/v2ray/geosite.dat:category-ads-all" + - "provider:geosite:category-ads-all" + + - tag: "main_sequence" + type: "sequence" + args: + exec: + - query_is_hosts_domain + - query_is_redirect_domain + + - if: query_is_whitelist_domain + exec: + - forward_local + - modify_ttl + - _return + + - if: "query_is_blocklist_domain || query_is_ad_domain" + exec: + - _new_nxdomain_response + - _return + + - lazy_cache + + - if: query_is_local_domain + exec: + - forward_local + - modify_ttl + - _return + + - if: query_is_non_local_domain + exec: + - _prefer_ipv4 + - forward_remote + - modify_ttl + - _return + - primary: + - forward_local + - if: "(! response_has_local_ip) && [_response_valid_answer]" + exec: + - _drop_response + secondary: + - _prefer_ipv4 + - forward_remote + - modify_ttl + fast_fallback: 200 + +servers: + - exec: main_sequence + listeners: + - protocol: udp + addr: ":5335" + - protocol: tcp + addr: ":5335" diff --git a/luci-app-mosdns/root/usr/share/mosdns/ad_domain.sh b/luci-app-mosdns/root/usr/share/mosdns/ad_domain.sh index fe2212f..1fdbd93 100755 --- a/luci-app-mosdns/root/usr/share/mosdns/ad_domain.sh +++ b/luci-app-mosdns/root/usr/share/mosdns/ad_domain.sh @@ -2,5 +2,5 @@ adblock=$(uci -q get mosdns.mosdns.adblock) if [ "$adblock" -eq 1 ];then - echo "ext:/usr/share/v2ray/geosite.dat:category-ads-all" + echo "provider:geosite:category-ads-all" fi diff --git a/luci-app-mosdns/root/usr/share/mosdns/default.yaml b/luci-app-mosdns/root/usr/share/mosdns/default.yaml new file mode 100644 index 0000000..5d119a6 --- /dev/null +++ b/luci-app-mosdns/root/usr/share/mosdns/default.yaml @@ -0,0 +1,156 @@ +log: + level: log_level + file: "log_file" + +include: [] + +data_providers: + - tag: geoip + file: "/usr/share/v2ray/geoip.dat" + auto_reload: true + + - tag: geosite + file: "/usr/share/v2ray/geosite.dat" + auto_reload: true + + - tag: whitelist + file: "/etc/mosdns/rule/whitelist.txt" + auto_reload: true + + - tag: blocklist + file: "/etc/mosdns/rule/blocklist.txt" + auto_reload: true + + - tag: hosts + file: "/etc/mosdns/rule/hosts.txt" + auto_reload: true + + - tag: redirect + file: "/etc/mosdns/rule/redirect.txt" + auto_reload: true + +plugins: + - tag: lazy_cache + type: cache + args: + size: cache_size + lazy_cache_ttl: cache_survival_time + + - tag: modify_ttl + type: ttl + args: + minimal_ttl: minimal_ttl_custom + maximum_ttl: maximum_ttl_custom + + - tag: "forward_local" + type: fast_forward + args: + upstream: + - addr: local_dns1 + - addr: local_dns2 + + - tag: "forward_remote" + type: fast_forward + args: + upstream: + - addr: remote_dns1 + - addr: remote_dns2 + + - tag: query_is_whitelist_domain + type: query_matcher + args: + domain: + - "provider:whitelist" + + - tag: query_is_blocklist_domain + type: query_matcher + args: + domain: + - "provider:blocklist" + + - tag: query_is_hosts_domain + type: hosts + args: + hosts: + - "provider:hosts" + + - tag: query_is_redirect_domain + type: redirect + args: + rule: + - "provider:redirect" + + - tag: query_is_local_domain + type: query_matcher + args: + domain: + - "provider:geosite:cn" + + - tag: query_is_non_local_domain + type: query_matcher + args: + domain: + - "provider:geosite:geolocation-!cn" + + - tag: response_has_local_ip + type: response_matcher + args: + ip: + - "provider:geoip:cn" + + - tag: query_is_ad_domain + type: query_matcher + args: + domain: + - "adblock" + + - tag: "main_sequence" + type: "sequence" + args: + exec: + - query_is_hosts_domain + - query_is_redirect_domain + + - if: query_is_whitelist_domain + exec: + - forward_local + - modify_ttl + - _return + + - if: "query_is_blocklist_domain || query_is_ad_domain" + exec: + - _new_nxdomain_response + - _return + + - lazy_cache + + - if: query_is_local_domain + exec: + - forward_local + - modify_ttl + - _return + + - if: query_is_non_local_domain + exec: + - _prefer_ipv4 + - forward_remote + - modify_ttl + - _return + - primary: + - forward_local + - if: "(! response_has_local_ip) && [_response_valid_answer]" + exec: + - _drop_response + secondary: + - _prefer_ipv4 + - forward_remote + - modify_ttl + fast_fallback: 200 + +servers: + - exec: main_sequence + listeners: + - protocol: udp + addr: ":listen_port" + - protocol: tcp + addr: ":listen_port"