luci-app-mosdns: use jshn to generate config file

Signed-off-by: sbwml <admin@cooluc.com>
This commit is contained in:
sbwml 2024-03-30 15:10:28 +08:00
parent 83853f6938
commit a4b8a30fb4
6 changed files with 575 additions and 388 deletions

View File

@ -1,7 +1,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=luci-app-mosdns
PKG_VERSION:=1.5.18
PKG_VERSION:=1.5.19
PKG_RELEASE:=1
LUCI_TITLE:=LuCI Support for mosdns

View File

@ -22,14 +22,14 @@ o = s:taboption("basic", Flag, "enabled", translate("Enabled"))
o.rmempty = false
o = s:taboption("basic", ListValue, "configfile", translate("Config File"))
o:value("/etc/mosdns/config.yaml", translate("Default Config"))
o:value("/var/etc/mosdns.json", translate("Default Config"))
o:value("/etc/mosdns/config_custom.yaml", translate("Custom Config"))
o.default = "/etc/mosdns/config.yaml"
o.default = "/var/etc/mosdns.json"
o = s:taboption("basic", Value, "listen_port", translate("Listen port"))
o.datatype = "and(port,min(1))"
o.default = 5335
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
o = s:taboption("basic", ListValue, "log_level", translate("Log Level"))
o:value("debug", translate("Debug"))
@ -37,22 +37,22 @@ o:value("info", translate("Info"))
o:value("warn", translate("Warning"))
o:value("error", translate("Error"))
o.default = "info"
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
o = s:taboption("basic", Value, "logfile", translate("Log File"))
o = s:taboption("basic", Value, "log_file", translate("Log File"))
o.placeholder = "/var/log/mosdns.log"
o.default = "/var/log/mosdns.log"
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
o = s:taboption("basic", Flag, "redirect", translate("DNS Forward"), translate("Forward Dnsmasq Domain Name resolution requests to MosDNS"))
o.default = true
o = s:taboption("basic", Flag, "prefer_ipv4", translate("Remote DNS prefer IPv4"), translate("IPv4 is preferred for remote DNS resolution of dual-stack addresses, and is not affected when the destination is IPv6 only"))
o:depends( "configfile", "/etc/mosdns/config.yaml")
o:depends( "configfile", "/var/etc/mosdns.json")
o.default = true
o = s:taboption("basic", Flag, "custom_local_dns", translate("Custom China DNS"), translate("Follow WAN interface DNS if not enabled"))
o:depends( "configfile", "/etc/mosdns/config.yaml")
o:depends( "configfile", "/var/etc/mosdns.json")
o.default = false
o = s:taboption("basic", Flag, "apple_optimization", translate("Apple domains optimization"), translate("For Apple domains equipped with Chinese mainland CDN, always responsive to Chinese CDN IP addresses"))
@ -83,7 +83,7 @@ o:value("tls://9.9.9.9", translate("Quad9 Public DNS (9.9.9.9)"))
o:value("tls://149.112.112.112", translate("Quad9 Public DNS (149.112.112.112)"))
o:value("tls://208.67.222.222", translate("Cisco Public DNS (208.67.222.222)"))
o:value("tls://208.67.220.220", translate("Cisco Public DNS (208.67.220.220)"))
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
o = s:taboption("basic", ListValue, "bootstrap_dns", translate("Bootstrap DNS servers"), translate("Bootstrap DNS servers are used to resolve IP addresses of the DoH/DoT resolvers you specify as upstreams"))
o:value("119.29.29.29", translate("Tencent Public DNS (119.29.29.29)"))
@ -96,34 +96,34 @@ o:value("180.76.76.76", translate("Baidu Public DNS (180.76.76.76)"))
o:value("8.8.8.8", translate("Google Public DNS (8.8.8.8)"))
o:value("1.1.1.1", translate("CloudFlare Public DNS (1.1.1.1)"))
o.default = "119.29.29.29"
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
s:tab("advanced", translate("Advanced Options"))
o = s:taboption("advanced", Value, "concurrent", translate("Concurrent"), translate("DNS query request concurrency, The number of upstream DNS servers that are allowed to initiate requests at the same time"))
o.datatype = "and(uinteger,min(1),max(3))"
o.default = "2"
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
o = s:taboption("advanced", Value, "idle_timeout", translate("Idle Timeout"), translate("DoH/TCP/DoT Connection Multiplexing idle timeout (default 30 seconds)"))
o.datatype = "and(uinteger,min(1))"
o.default = "30"
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
o = s:taboption("advanced", Flag, "enable_pipeline", translate("TCP/DoT Connection Multiplexing"), translate("Enable TCP/DoT RFC 7766 new Query Pipelining connection multiplexing mode"))
o.rmempty = false
o.default = false
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
o = s:taboption("advanced", Flag, "insecure_skip_verify", translate("Disable TLS Certificate"), translate("Disable TLS Servers certificate validation, Can be useful if system CA certificate expires or the system time is out of order"))
o.rmempty = false
o.default = false
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
o = s:taboption("advanced", Flag, "enable_ecs_remote", translate("Enable EDNS client subnet"))
o.rmempty = false
o.default = false
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
o = s:taboption("advanced", Value, "remote_ecs_ip", translate("IP Address"), translate("Please provide the IP address you use when accessing foreign websites. This IP subnet (0/24) will be used as the ECS address for Remote DNS requests") .. '<br />' .. translate("This feature is typically used when using a self-built DNS server as an Remote DNS upstream (requires support from the upstream server)"))
o.datatype = "ipaddr"
@ -132,22 +132,22 @@ o:depends("enable_ecs_remote", "1")
o = s:taboption("advanced", Flag, "dns_leak", translate("Prevent DNS Leaks"), translate("Enable this option fallback policy forces forwarding to remote DNS"))
o.rmempty = false
o.default = false
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
o = s:taboption("advanced", Value, "cache_size", translate("DNS Cache Size"), translate("DNS cache size (in piece). To disable caching, please set to 0."))
o.datatype = "and(uinteger,min(0))"
o.default = "8000"
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
o = s:taboption("advanced", Value, "cache_survival_time", translate("Lazy Cache TTL"), translate("Lazy cache survival time (in second). To disable Lazy Cache, please set to 0."))
o = s:taboption("advanced", Value, "lazy_cache_ttl", translate("Lazy Cache TTL"), translate("Lazy cache survival time (in second). To disable Lazy Cache, please set to 0."))
o.datatype = "and(uinteger,min(0))"
o.default = "86400"
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
o = s:taboption("advanced", Flag, "dump_file", translate("Cache Dump"), translate("Save the cache locally and reload the cache dump on the next startup"))
o.rmempty = false
o.default = false
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
o = s:taboption("advanced", Value, "dump_interval", translate("Auto Save Cache Interval"))
o.datatype = "and(uinteger,min(0))"
@ -157,15 +157,15 @@ o:depends("dump_file", "1")
o = s:taboption("advanced", Value, "minimal_ttl", translate("Minimum TTL"), translate("Modify the Minimum TTL value (seconds) for DNS answer results, 0 indicating no modification"))
o.datatype = "and(uinteger,min(0),max(604800))"
o.default = "0"
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
o = s:taboption("advanced", Value, "maximum_ttl", translate("Maximum TTL"), translate("Modify the Maximum TTL value (seconds) for DNS answer results, 0 indicating no modification"))
o.datatype = "and(uinteger,min(0),max(604800))"
o.default = "0"
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
o = s:taboption("advanced", Flag, "adblock", translate("Enable DNS ADblock"))
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
o.default = false
o = s:taboption("advanced", DynamicList, "ad_source", translate("ADblock Source"), translate("When using custom rule sources, please use rule types supported by MosDNS (domain lists).") .. '<br />' .. translate("Support for local files, such as: file:///var/mosdns/example.txt"))
@ -201,17 +201,17 @@ s:tab("cloudflare", translate("Cloudflare Options"))
o = s:taboption("cloudflare", Flag, "cloudflare", translate("Enabled"), translate("Match the parsing result with the Cloudflare IP ranges, and when there is a successful match, use the 'Custom IP' as the parsing result (experimental feature)"))
o.rmempty = false
o.default = false
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
o = s:taboption("cloudflare", DynamicList, "cloudflare_ip", translate("Custom IP"))
o.datatype = "ipaddr"
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
o = s:taboption("cloudflare", TextValue, "cloudflare_cidr", translate("Cloudflare IP Ranges"))
o.description = translate("IPv4 CIDR") .. [[<a href="https://www.cloudflare.com/ips-v4" target="_blank">https://www.cloudflare.com/ips-v4</a>]] .. '<br />' .. translate("IPv6 CIDR") .. [[<a href="https://www.cloudflare.com/ips-v6" target="_blank">https://www.cloudflare.com/ips-v6</a>]]
o.template = "cbi/tvalue"
o.rows = 15
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
function o.cfgvalue(self, section)
return fs.readfile("/etc/mosdns/rule/cloudflare-cidr.txt")
end
@ -225,12 +225,12 @@ s:tab("api", translate("API Options"))
o = s:taboption("api", Value, "listen_port_api", translate("API Listen port"))
o.datatype = "and(port,min(1))"
o.default = 9091
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
o = s:taboption("api", Button, "flush_cache", translate("Flush Cache"), translate("Flushing Cache will clear any IP addresses or DNS records from MosDNS cache"))
o.rawhtml = true
o.template = "mosdns/mosdns_flush_cache"
o:depends("configfile", "/etc/mosdns/config.yaml")
o:depends("configfile", "/var/etc/mosdns.json")
s:tab("geodata", translate("GeoData Export"))

View File

@ -8,11 +8,11 @@ config mosdns 'config'
option redirect '1'
option prefer_ipv4 '1'
option adblock '0'
option configfile '/etc/mosdns/config.yaml'
option configfile '/var/etc/mosdns.json'
option log_level 'info'
option logfile '/var/log/mosdns.log'
option log_file '/var/log/mosdns.log'
option cache_size '8000'
option cache_survival_time '86400'
option lazy_cache_ttl '86400'
option dump_file '0'
option concurrent '1'
option idle_timeout '30'

View File

@ -1,8 +1,7 @@
#!/bin/sh /etc/rc.common
#
# Copyright (C) 2020-2022, IrineSistiana
#
# Copyright (C) 2023, sbwml <admin@cooluc.com>
# Copyright (C) 2023-2024, sbwml <admin@cooluc.com>
#
# This file is part of mosdns.
#
@ -28,7 +27,6 @@ CONF=$(uci -q get mosdns.config.configfile)
CRON_FILE=/etc/crontabs/root
DUMP_FILE=/etc/mosdns/cache.dump
DUMP_FILE_DEFAULT=/usr/share/mosdns/cache.dump
DEFAULT_CONF=/usr/share/mosdns/default.yaml
MOSDNS_SCRIPT=/usr/share/mosdns/mosdns.sh
get_config() {
@ -36,7 +34,7 @@ get_config() {
config_get adblock $1 adblock 0
config_get ad_source $1 ad_source ""
config_get cache_size $1 cache_size 8000
config_get cache_survival_time $1 cache_survival_time 86400
config_get lazy_cache_ttl $1 lazy_cache_ttl 86400
config_get dump_file $1 dump_file 0
config_get dump_interval $1 dump_interval 3600
config_get enable_pipeline $1 enable_pipeline 0
@ -44,10 +42,10 @@ get_config() {
config_get geo_update_day_time $1 geo_update_day_time 2
config_get geo_update_week_time $1 geo_update_week_time "*"
config_get listen_port $1 listen_port 5335
config_get log_file $1 logfile "/var/log/mosdns.log"
config_get log_file $1 log_file "/var/log/mosdns.log"
config_get log_level $1 log_level "info"
config_get maximum_ttl_custom $1 maximum_ttl 0
config_get minimal_ttl_custom $1 minimal_ttl 0
config_get minimal_ttl $1 minimal_ttl 0
config_get maximum_ttl $1 maximum_ttl 0
config_get redirect $1 redirect 0
config_get prefer_ipv4 $1 prefer_ipv4 0
config_get remote_dns $1 remote_dns "tls://8.8.8.8 tls://1.1.1.1"
@ -62,84 +60,538 @@ get_config() {
config_get remote_ecs_ip $1 remote_ecs_ip "110.34.181.1"
config_get dns_leak $1 dns_leak 0
config_get cloudflare $1 cloudflare 0
config_get cloudflare_ip $1 cloudflare_ip ""
}
init_yaml() {
TMPDIR=$(mktemp -d) || exit 1
[ $enable_pipeline -eq 1 ] && enable_pipeline=true || enable_pipeline=false
[ $insecure_skip_verify -eq 1 ] && insecure_skip_verify=true || insecure_skip_verify=false
# China DNS
if [ "$custom_local_dns" -eq 1 ]; then
local_dns=$($MOSDNS_SCRIPT dns | awk -v s=' ' '{
for(i=1; i<=NF; i++) {
if ($i ~ /^h3:\/\//) {
printf "%s- addr: \"%s\"\n%s bootstrap: '${bootstrap_dns}'\n%s enable_pipeline: '${enable_pipeline}'\n%s insecure_skip_verify: '${insecure_skip_verify}'\n%s idle_timeout: '${idle_timeout}'\n%s enable_http3: true\n",s,$i,s,s,s,s,s
} else {
printf "%s- addr: \"%s\"\n%s bootstrap: '${bootstrap_dns}'\n%s enable_pipeline: '${enable_pipeline}'\n%s insecure_skip_verify: '${insecure_skip_verify}'\n%s idle_timeout: '${idle_timeout}'\n",s,$i,s,s,s,s
}
}
}')
else
local_dns=$($MOSDNS_SCRIPT dns | awk '{for(i=1;i<=NF;i++)printf "%s- addr: \"%s\"\n",s,$i,s}' s=' ')
fi
# Remote DNS
remote_dns=$(echo $remote_dns | awk -v s=' ' '{
for(i=1; i<=NF; i++) {
if ($i ~ /^h3:\/\//) {
printf "%s- addr: \"%s\"\n%s bootstrap: '${bootstrap_dns}'\n%s enable_pipeline: '${enable_pipeline}'\n%s insecure_skip_verify: '${insecure_skip_verify}'\n%s idle_timeout: '${idle_timeout}'\n%s enable_http3: true\n",s,$i,s,s,s,s,s
} else {
printf "%s- addr: \"%s\"\n%s bootstrap: '${bootstrap_dns}'\n%s enable_pipeline: '${enable_pipeline}'\n%s insecure_skip_verify: '${insecure_skip_verify}'\n%s idle_timeout: '${idle_timeout}'\n",s,$i,s,s,s,s
}
}
}')
# Write parameters
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,concurrent_num,$concurrent,g;s,api_port,$listen_port_api,g; \
s,remote_dns_pipeline,$enable_pipeline,g" $DEFAULT_CONF > $CONF
# Apple optimization
[ "$apple_optimization" != 1 ] && sed -i '/- exec: \$query_is_apple_domain/,+1d' $CONF
# Adlist
generate_config() {
# jshn shell library
. /usr/share/libubox/jshn.sh
# json data
json_init
# log
json_add_object 'log'
json_add_string "level" "$log_level"
json_add_string "file" "$log_file"
json_close_object
# api
json_add_object 'api'
json_add_string "http" "0.0.0.0:$listen_port_api"
json_close_object
# include
json_add_array "include"
json_close_array
# plugins
json_add_array "plugins"
# plugin: geosite_cn
json_add_object
json_add_string "tag" "geosite_cn"
json_add_string "type" "domain_set"
json_add_object "args"
json_add_array "files"
json_add_string "" "/var/mosdns/geosite_cn.txt"
json_close_array
json_close_object
json_close_object
# plugin: geoip_cn
json_add_object
json_add_string "tag" "geoip_cn"
json_add_string "type" "ip_set"
json_add_object "args"
json_add_array "files"
json_add_string "" "/var/mosdns/geoip_cn.txt"
json_close_array
json_close_object
json_close_object
# plugin: geosite_apple
json_add_object
json_add_string "tag" "geosite_apple"
json_add_string "type" "domain_set"
json_add_object "args"
json_add_array "files"
json_add_string "" "/var/mosdns/geosite_apple.txt"
json_close_array
json_close_object
json_close_object
# plugin: geosite_no_cn
json_add_object
json_add_string "tag" "geosite_no_cn"
json_add_string "type" "domain_set"
json_add_object "args"
json_add_array "files"
json_add_string "" "/var/mosdns/geosite_geolocation-!cn.txt"
json_close_array
json_close_object
json_close_object
# plugin: whitelist
json_add_object
json_add_string "tag" "whitelist"
json_add_string "type" "domain_set"
json_add_object "args"
json_add_array "files"
json_add_string "" "/etc/mosdns/rule/whitelist.txt"
json_close_array
json_close_object
json_close_object
# plugin: blocklist
json_add_object
json_add_string "tag" "blocklist"
json_add_string "type" "domain_set"
json_add_object "args"
json_add_array "files"
json_add_string "" "/etc/mosdns/rule/blocklist.txt"
json_close_array
json_close_object
json_close_object
# plugin: greylist
json_add_object
json_add_string "tag" "greylist"
json_add_string "type" "domain_set"
json_add_object "args"
json_add_array "files"
json_add_string "" "/etc/mosdns/rule/greylist.txt"
json_close_array
json_close_object
json_close_object
# plugin: ddnslist
json_add_object
json_add_string "tag" "ddnslist"
json_add_string "type" "domain_set"
json_add_object "args"
json_add_array "files"
json_add_string "" "/etc/mosdns/rule/ddnslist.txt"
json_close_array
json_close_object
json_close_object
# plugin: hosts
json_add_object
json_add_string "tag" "hosts"
json_add_string "type" "hosts"
json_add_object "args"
json_add_array "files"
json_add_string "" "/etc/mosdns/rule/hosts.txt"
json_close_array
json_close_object
json_close_object
# plugin: redirect
json_add_object
json_add_string "tag" "redirect"
json_add_string "type" "redirect"
json_add_object "args"
json_add_array "files"
json_add_string "" "/etc/mosdns/rule/redirect.txt"
json_close_array
json_close_object
json_close_object
# plugin: adlist
json_add_object
json_add_string "tag" "adlist"
json_add_string "type" "domain_set"
json_add_object "args"
json_add_array "files"
adlist=$($MOSDNS_SCRIPT adlist)
echo "${adlist}" > $TMPDIR/adlist_files.txt
sed -i -e '/ADBLOCK_LIST/{r '$TMPDIR/adlist_files.txt -e';d}' $CONF
# DNS
echo "${local_dns}" > $TMPDIR/local_dns.txt
echo "${remote_dns}" > $TMPDIR/remote_dns.txt
sed -i -e '/- addr: local_dns/{r '$TMPDIR/local_dns.txt -e';d};/- addr: remote_dns/{r '$TMPDIR/remote_dns.txt -e';d}' $CONF
# Convert HTTP/3 prefix format
sed -i 's/h3:\/\//https:\/\//g' $CONF
# Prefer IPv4
[ $prefer_ipv4 -ne 1 ] && sed -i "/prefer_ipv4/d" $CONF
# Cache dump
[ "$dump_file" -eq 1 ] && sed -i "/lazy_cache_ttl/a\ dump_file: $DUMP_FILE\n dump_interval: $dump_interval" $CONF
for list in $adlist; do
json_add_string "" "$list"
done
json_close_array
json_close_object
json_close_object
# plugin: local_ptr
json_add_object
json_add_string "tag" "local_ptr"
json_add_string "type" "domain_set"
json_add_object "args"
json_add_array "files"
json_add_string "" "/etc/mosdns/rule/local-ptr.txt"
json_close_array
json_close_object
json_close_object
# plugin: cloudflare_cidr
json_add_object
json_add_string "tag" "cloudflare_cidr"
json_add_string "type" "ip_set"
json_add_object "args"
json_add_array "files"
json_add_string "" "/etc/mosdns/rule/cloudflare-cidr.txt"
json_close_array
json_close_object
json_close_object
# plugin: lazy_cache
json_add_object
json_add_string "tag" "lazy_cache"
json_add_string "type" "cache"
json_add_object "args"
json_add_int "size" "$cache_size"
json_add_int "lazy_cache_ttl" "$lazy_cache_ttl"
[ "$dump_file" -eq 1 ] && {
json_add_string "dump_file" "/etc/mosdns/cache.dump"
json_add_int "dump_interval" "$dump_interval"
}
json_close_object
json_close_object
# plugin: forward_xinfeng_udp
json_add_object
json_add_string "tag" "forward_xinfeng_udp"
json_add_string "type" "forward"
json_add_object "args"
json_add_int "concurrent" 2
json_add_array "upstreams"
json_add_object
json_add_string "addr" "114.114.114.114"
json_close_object
json_add_object
json_add_string "addr" "114.114.115.115"
json_close_object
json_close_array
json_close_object
json_close_object
# plugin: forward_local
json_add_object
json_add_string "tag" "forward_local"
json_add_string "type" "forward"
json_add_object "args"
json_add_int "concurrent" "$concurrent"
json_add_array "upstreams"
local_dns=$($MOSDNS_SCRIPT dns)
for addr in $local_dns; do
enable_http3=0
if echo "$addr" | grep -q "^h3://"; then
enable_http3=1
addr=$(echo $addr | sed 's/h3:\/\//https:\/\//g')
fi
json_add_object
json_add_string "addr" "$addr"
json_add_string "bootstrap" "$bootstrap_dns"
json_add_boolean "enable_pipeline" "$enable_pipeline"
json_add_boolean "insecure_skip_verify" "$insecure_skip_verify"
json_add_int "idle_timeout" "$idle_timeout"
[ "$enable_http3" -eq 1 ] && json_add_boolean "enable_http3" "1"
json_close_object
done
json_close_array
json_close_object
json_close_object
# plugin: forward_remote
json_add_object
json_add_string "tag" "forward_remote"
json_add_string "type" "forward"
json_add_object "args"
json_add_int "concurrent" "$concurrent"
json_add_array "upstreams"
for addr in $remote_dns; do
enable_http3=0
if echo "$addr" | grep -q "^h3://"; then
enable_http3=1
addr=$(echo $addr | sed 's/h3:\/\//https:\/\//g')
fi
json_add_object
json_add_string "addr" "$addr"
json_add_string "bootstrap" "$bootstrap_dns"
json_add_boolean "enable_pipeline" "$enable_pipeline"
json_add_boolean "insecure_skip_verify" "$insecure_skip_verify"
json_add_int "idle_timeout" "$idle_timeout"
[ "$enable_http3" -eq 1 ] && json_add_boolean "enable_http3" "1"
json_close_object
done
json_close_array
json_close_object
json_close_object
# plugin: forward_remote_upstream
json_add_object
json_add_string "tag" "forward_remote_upstream"
json_add_string "type" "sequence"
json_add_array "args"
[ "$prefer_ipv4" -eq 1 ] && {
json_add_object
json_add_string "exec" "prefer_ipv4"
json_close_object
}
[ "$enable_ecs_remote" -eq 1 ] && {
json_add_object
json_add_string "exec" "ecs $remote_ecs_ip"
json_close_object
}
json_add_object
json_add_string "exec" "\$forward_remote"
json_close_object
json_close_array
json_close_object
# plugin: modify_ttl
json_add_object
json_add_string "tag" "modify_ttl"
json_add_string "type" "sequence"
json_add_array "args"
json_add_object
json_add_string "exec" "ttl $minimal_ttl-$maximum_ttl"
json_close_object
json_close_array
json_close_object
# plugin: modify_ddns_ttl
json_add_object
json_add_string "tag" "modify_ddns_ttl"
json_add_string "type" "sequence"
json_add_array "args"
json_add_object
json_add_string "exec" "ttl 5-5"
json_close_object
json_close_array
json_close_object
# plugin: has_resp_sequence
json_add_object
json_add_string "tag" "has_resp_sequence"
json_add_string "type" "sequence"
json_add_array "args"
json_add_object
json_add_string "matches" "qname \$ddnslist"
json_add_string "exec" "\$modify_ddns_ttl"
json_close_object
json_add_object
json_add_string "matches" "!qname \$ddnslist"
json_add_string "exec" "\$modify_ttl"
json_close_object
[ "$cloudflare" -eq 1 ] && {
json_add_object
json_add_string "matches" "resp_ip \$cloudflare_cidr"
json_add_string "exec" "black_hole $cloudflare_ip"
json_close_object
}
json_add_object
json_add_string "matches" "has_resp"
json_add_string "exec" "accept"
json_close_object
json_close_array
json_close_object
# plugin: query_is_non_local_ip
json_add_object
json_add_string "tag" "query_is_non_local_ip"
json_add_string "type" "sequence"
json_add_array "args"
json_add_object
json_add_string "exec" "\$forward_local"
json_close_object
json_add_object
json_add_string "matches" "!resp_ip \$geoip_cn"
json_add_string "exec" "drop_resp"
json_close_object
json_close_array
json_close_object
# plugin: fallback
json_add_object
json_add_string "tag" "fallback"
json_add_string "type" "fallback"
json_add_object "args"
[ "$dns_leak" -eq 1 ] && json_add_string "primary" "forward_remote_upstream" || json_add_string "primary" "query_is_non_local_ip"
json_add_string "secondary" "forward_remote_upstream"
json_add_int "threshold" 500
json_add_boolean "always_standby" 1
json_close_object
json_close_object
# plugin: apple_domain_fallback
json_add_object
json_add_string "tag" "apple_domain_fallback"
json_add_string "type" "fallback"
json_add_object "args"
json_add_string "primary" "query_is_non_local_ip"
json_add_string "secondary" "forward_xinfeng_udp"
json_add_int "threshold" 100
json_add_boolean "always_standby" 1
json_close_object
json_close_object
# plugin: query_is_apple_domain
json_add_object
json_add_string "tag" "query_is_apple_domain"
json_add_string "type" "sequence"
json_add_array "args"
json_add_object
json_add_string "matches" "!qname \$geosite_apple"
json_add_string "exec" "return"
json_close_object
json_add_object
json_add_string "exec" "\$apple_domain_fallback"
json_close_object
json_close_array
json_close_object
# plugin: query_is_ddns_domain
json_add_object
json_add_string "tag" "query_is_ddns_domain"
json_add_string "type" "sequence"
json_add_array "args"
json_add_object
json_add_string "matches" "qname \$ddnslist"
json_add_string "exec" "\$forward_local"
json_close_object
json_close_array
json_close_object
# plugin: query_is_local_domain
json_add_object
json_add_string "tag" "query_is_local_domain"
json_add_string "type" "sequence"
json_add_array "args"
json_add_object
json_add_string "matches" "qname \$geosite_cn"
json_add_string "exec" "\$forward_local"
json_close_object
json_close_array
json_close_object
# plugin: query_is_no_local_domain
json_add_object
json_add_string "tag" "query_is_no_local_domain"
json_add_string "type" "sequence"
json_add_array "args"
json_add_object
json_add_string "matches" "qname \$geosite_no_cn"
json_add_string "exec" "\$forward_remote_upstream"
json_close_object
json_close_array
json_close_object
# plugin: query_is_whitelist_domain
json_add_object
json_add_string "tag" "query_is_whitelist_domain"
json_add_string "type" "sequence"
json_add_array "args"
json_add_object
json_add_string "matches" "qname \$whitelist"
json_add_string "exec" "\$forward_local"
json_close_object
json_close_array
json_close_object
# plugin: query_is_greylist_domain
json_add_object
json_add_string "tag" "query_is_greylist_domain"
json_add_string "type" "sequence"
json_add_array "args"
json_add_object
json_add_string "matches" "qname \$greylist"
json_add_string "exec" "\$forward_remote_upstream"
json_close_object
json_close_array
json_close_object
# plugin: query_is_reject_domain
json_add_object
json_add_string "tag" "query_is_reject_domain"
json_add_string "type" "sequence"
json_add_array "args"
json_add_object
json_add_string "matches" "qname \$blocklist"
json_add_string "exec" "reject 3"
json_close_object
json_add_object
json_add_string "matches" "qname \$adlist"
json_add_string "exec" "reject 3"
json_close_object
json_add_object
json_add_array "matches"
json_add_string "" "qtype 12"
json_add_string "" "qname \$local_ptr"
json_close_array
json_add_string "exec" "reject 3"
json_close_object
json_add_object
json_add_string "matches" "qtype 65"
json_add_string "exec" "reject 3"
json_close_object
json_close_array
json_close_object
# plugin: main_sequence
json_add_object
json_add_string "tag" "main_sequence"
json_add_string "type" "sequence"
json_add_array "args"
json_add_object
json_add_string "exec" "\$hosts"
json_close_object
json_add_object
json_add_string "exec" "jump has_resp_sequence"
json_close_object
json_add_object
json_add_array "matches"
json_add_string "" "!qname \$ddnslist"
json_add_string "" "!qname \$blocklist"
json_add_string "" "!qname \$adlist"
json_add_string "" "!qname \$local_ptr"
json_close_array
json_add_string "exec" "\$lazy_cache"
json_close_object
json_add_object
json_add_string "exec" "\$redirect"
json_close_object
json_add_object
json_add_string "exec" "jump has_resp_sequence"
json_close_object
json_add_object
[ "$apple_optimization" -eq 1 ] && {
json_add_string "exec" "\$query_is_apple_domain"
json_close_object
json_add_object
json_add_string "exec" "jump has_resp_sequence"
json_close_object
json_add_object
}
json_add_string "exec" "\$query_is_ddns_domain"
json_close_object
json_add_object
json_add_string "exec" "jump has_resp_sequence"
json_close_object
json_add_object
json_add_string "exec" "\$query_is_whitelist_domain"
json_close_object
json_add_object
json_add_string "exec" "jump has_resp_sequence"
json_close_object
json_add_object
json_add_string "exec" "\$query_is_reject_domain"
json_close_object
json_add_object
json_add_string "exec" "jump has_resp_sequence"
json_close_object
json_add_object
json_add_string "exec" "\$query_is_greylist_domain"
json_close_object
json_add_object
json_add_string "exec" "jump has_resp_sequence"
json_close_object
json_add_object
json_add_string "exec" "\$query_is_local_domain"
json_close_object
json_add_object
json_add_string "exec" "jump has_resp_sequence"
json_close_object
json_add_object
json_add_string "exec" "\$query_is_no_local_domain"
json_close_object
json_add_object
json_add_string "exec" "jump has_resp_sequence"
json_close_object
json_add_object
json_add_string "exec" "\$fallback"
json_close_object
json_close_array
json_close_object
# plugin: udp_server
json_add_object
json_add_string "tag" "udp_server"
json_add_string "type" "udp_server"
json_add_object "args"
json_add_string "entry" "main_sequence"
json_add_string "listen" ":$listen_port"
json_close_object
json_close_object
# plugin: tcp_server
json_add_object
json_add_string "tag" "tcp_server"
json_add_string "type" "tcp_server"
json_add_object "args"
json_add_string "entry" "main_sequence"
json_add_string "listen" ":$listen_port"
json_close_object
json_close_object
# close plugins array
json_close_array
# print json
json_dump > /var/etc/mosdns.json
# init dump_file
[ "$dump_file" -eq 1 ] && [ ! -f $DUMP_FILE ] && cp -a $DUMP_FILE_DEFAULT $DUMP_FILE
[ "$dump_file" -eq 0 ] && \cp -a $DUMP_FILE_DEFAULT $DUMP_FILE
# ECS
if [ "$enable_ecs_remote" -eq 1 ]; then
sed -i "s,ecs_remote,ecs\ $remote_ecs_ip,g" $CONF
else
sed -i "/ecs_remote/d;" $CONF
fi
# DNS Leak
if [ $dns_leak -eq 1 ]; then
sed -i "s/primary: UNDEFINED/primary: forward_remote_upstream/g" $CONF
else
sed -i "s/primary: UNDEFINED/primary: query_is_non_local_ip/g" $CONF
fi
# Cloudflare IP
if [ $cloudflare -eq 1 ]; then
cloudflare_ip=$(sh $MOSDNS_SCRIPT cloudflare)
sed -i "s/CLOUDFLARE_IP/$cloudflare_ip/g" $CONF
else
sed -i '/\$cloudflare_cidr\|CLOUDFLARE_IP/d' $CONF
fi
# disable lazy_cache plugin when cache_size is 0
[ $cache_size -eq 0 ] && sed -i -E ':l;N;/exec: \$lazy_cache/s/(\n[^\n]*){6}$//;bl' $CONF
rm -rf $TMPDIR
}
service_triggers() {
@ -155,7 +607,7 @@ restore_setting() {
}
redirect_setting() {
if [ "${CONF}" = "/etc/mosdns/config.yaml" ]; then
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'
@ -203,13 +655,14 @@ start_service() {
config_foreach get_config "mosdns"
[ $enabled -ne 1 ] && return 1
delcron ; setcron
[ "${CONF}" = "/etc/mosdns/config.yaml" ] && init_yaml
:> $($MOSDNS_SCRIPT logfile)
if [ "${log_level}" = "error" ] || [ "${log_level}" = "warn" ]; then
v2dat_dump > /dev/null 2>&1
else
v2dat_dump >> $($MOSDNS_SCRIPT logfile) 2>&1
fi
[ "${CONF}" = "/var/etc/mosdns.json" ] && generate_config
procd_open_instance mosdns
procd_set_param env QUIC_GO_DISABLE_RECEIVE_BUFFER_WARNING=true
procd_set_param command $PROG start

View File

@ -1,259 +0,0 @@
log:
level: log_level
file: "log_file"
api:
http: "0.0.0.0:api_port"
include: []
plugins:
- tag: geosite_cn
type: domain_set
args:
files:
- "/var/mosdns/geosite_cn.txt"
- tag: geoip_cn
type: ip_set
args:
files:
- "/var/mosdns/geoip_cn.txt"
- tag: geosite_apple
type: domain_set
args:
files:
- "/var/mosdns/geosite_apple.txt"
- tag: geosite_no_cn
type: domain_set
args:
files:
- "/var/mosdns/geosite_geolocation-!cn.txt"
- tag: whitelist
type: domain_set
args:
files:
- "/etc/mosdns/rule/whitelist.txt"
- tag: blocklist
type: domain_set
args:
files:
- "/etc/mosdns/rule/blocklist.txt"
- tag: greylist
type: domain_set
args:
files:
- "/etc/mosdns/rule/greylist.txt"
- tag: ddnslist
type: domain_set
args:
files:
- "/etc/mosdns/rule/ddnslist.txt"
- tag: hosts
type: hosts
args:
files:
- "/etc/mosdns/rule/hosts.txt"
- tag: redirect
type: redirect
args:
files:
- "/etc/mosdns/rule/redirect.txt"
- tag: adlist
type: domain_set
args:
files:
ADBLOCK_LIST
- tag: local_ptr
type: domain_set
args:
files:
- "/etc/mosdns/rule/local-ptr.txt"
- tag: cloudflare_cidr
type: ip_set
args:
files:
- "/etc/mosdns/rule/cloudflare-cidr.txt"
- tag: lazy_cache
type: cache
args:
size: cache_size
lazy_cache_ttl: cache_survival_time
- tag: forward_xinfeng_udp
type: forward
args:
concurrent: 2
upstreams:
- addr: "114.114.114.114"
- addr: "114.114.115.115"
- tag: forward_local
type: forward
args:
concurrent: concurrent_num
upstreams:
- addr: local_dns
- tag: forward_remote
type: forward
args:
concurrent: concurrent_num
upstreams:
- addr: remote_dns
- tag: forward_remote_upstream
type: sequence
args:
- exec: prefer_ipv4
- exec: ecs_remote
- exec: $forward_remote
- tag: modify_ttl
type: sequence
args:
- exec: ttl minimal_ttl_custom-maximum_ttl_custom
- tag: modify_ddns_ttl
type: sequence
args:
- exec: ttl 5-5
- tag: has_resp_sequence
type: sequence
args:
- matches: qname $ddnslist
exec: $modify_ddns_ttl
- matches: "!qname $ddnslist"
exec: $modify_ttl
- matches: "resp_ip $cloudflare_cidr"
exec: black_hole CLOUDFLARE_IP
- matches: has_resp
exec: accept
- tag: query_is_non_local_ip
type: sequence
args:
- exec: $forward_local
- matches: "!resp_ip $geoip_cn"
exec: drop_resp
- tag: fallback
type: fallback
args:
primary: UNDEFINED
secondary: forward_remote_upstream
threshold: 500
always_standby: true
- tag: apple_domain_fallback
type: fallback
args:
primary: query_is_non_local_ip
secondary: forward_xinfeng_udp
threshold: 100
always_standby: true
- tag: query_is_apple_domain
type: sequence
args:
- matches: "!qname $geosite_apple"
exec: return
- exec: $apple_domain_fallback
- tag: query_is_ddns_domain
type: sequence
args:
- matches: qname $ddnslist
exec: $forward_local
- tag: query_is_local_domain
type: sequence
args:
- matches: qname $geosite_cn
exec: $forward_local
- tag: query_is_no_local_domain
type: sequence
args:
- matches: qname $geosite_no_cn
exec: $forward_remote_upstream
- tag: query_is_whitelist_domain
type: sequence
args:
- matches: qname $whitelist
exec: $forward_local
- tag: query_is_greylist_domain
type: sequence
args:
- matches: qname $greylist
exec: $forward_remote_upstream
- tag: query_is_reject_domain
type: sequence
args:
- matches: qname $blocklist
exec: reject 3
- matches: qname $adlist
exec: reject 3
- matches:
- qtype 12
- qname $local_ptr
exec: reject 3
- matches: qtype 65
exec: reject 3
- tag: main_sequence
type: sequence
args:
- exec: $hosts
- exec: jump has_resp_sequence
- matches:
- "!qname $ddnslist"
- "!qname $blocklist"
- "!qname $adlist"
- "!qname $local_ptr"
exec: $lazy_cache
- exec: $redirect
- exec: jump has_resp_sequence
- exec: $query_is_apple_domain
- exec: jump has_resp_sequence
- exec: $query_is_ddns_domain
- exec: jump has_resp_sequence
- exec: $query_is_whitelist_domain
- exec: jump has_resp_sequence
- exec: $query_is_reject_domain
- exec: jump has_resp_sequence
- exec: $query_is_greylist_domain
- exec: jump has_resp_sequence
- exec: $query_is_local_domain
- exec: jump has_resp_sequence
- exec: $query_is_no_local_domain
- exec: jump has_resp_sequence
- exec: $fallback
- tag: udp_server
type: udp_server
args:
entry: main_sequence
listen: ":listen_port"
- tag: tcp_server
type: tcp_server
args:
entry: main_sequence
listen: ":listen_port"

View File

@ -4,8 +4,8 @@ script_action=${1}
logfile_path() (
configfile=$(uci -q get mosdns.config.configfile)
if [ "$configfile" = "/etc/mosdns/config.yaml" ]; then
uci -q get mosdns.config.logfile
if [ "$configfile" = "/var/etc/mosdns.json" ]; then
uci -q get mosdns.config.log_file
else
[ ! -f /etc/mosdns/config_custom.yaml ] && exit 1
awk '/^log:/{f=1;next}f==1{if($0~/file:/){print;exit}if($0~/^[^ ]/)exit}' /etc/mosdns/config_custom.yaml | grep -Eo "/[^'\"]+"
@ -37,18 +37,18 @@ get_adlist() (
for url in $ad_source;
do
if [ $(echo $url) = 'geosite.dat' ]; then
echo " - \"/var/mosdns/geosite_category-ads-all.txt\""
echo "/var/mosdns/geosite_category-ads-all.txt"
elif echo "$url" | grep -Eq "^file://" ; then
echo " - \"$(echo "$url" | sed 's/file:\/\///')\""
echo "$url" | sed 's/file:\/\///'
else
echo " - \"/etc/mosdns/rule/adlist/$(basename $url)\""
echo "/etc/mosdns/rule/adlist/$(basename $url)"
[ ! -f "/etc/mosdns/rule/adlist/$(basename $url)" ] && touch /etc/mosdns/rule/adlist/$(basename $url)
fi
done
else
rm -rf /etc/mosdns/rule/adlist /etc/mosdns/rule/.ad_source
touch /var/disable-ads.txt
echo " - \"/var/disable-ads.txt\""
touch /var/mosdns/disable-ads.txt
echo "/var/mosdns/disable-ads.txt"
fi
)
@ -144,7 +144,7 @@ v2dat_dump() {
configfile=$(uci -q get mosdns.config.configfile)
mkdir -p /var/mosdns
rm -f /var/mosdns/geo*.txt
if [ "$configfile" = "/etc/mosdns/config.yaml" ]; then
if [ "$configfile" = "/var/etc/mosdns.json" ]; then
# default config
v2dat unpack geoip -o /var/mosdns -f cn $v2dat_dir/geoip.dat
v2dat unpack geosite -o /var/mosdns -f cn -f apple -f 'geolocation-!cn' $v2dat_dir/geosite.dat
@ -160,10 +160,6 @@ v2dat_dump() {
fi
}
cloudflare_ip() {
uci -q get mosdns.config.cloudflare_ip
}
case $script_action in
"dns")
interface_dns
@ -186,9 +182,6 @@ case $script_action in
"v2dat_dump")
v2dat_dump
;;
"cloudflare")
cloudflare_ip
;;
"version")
mosdns version
;;