luci-app-mosdns: tidy up code

This commit is contained in:
qingtian110 2023-03-25 23:32:52 +08:00 committed by sbwml
parent d2a8635961
commit 47d0aae310
8 changed files with 309 additions and 310 deletions

View File

@ -1,42 +1,42 @@
module("luci.controller.mosdns", package.seeall) module("luci.controller.mosdns", package.seeall)
function index() function index()
if not nixio.fs.access("/etc/config/mosdns") then if not nixio.fs.access("/etc/config/mosdns") then
return return
end end
local page = entry({"admin", "services", "mosdns"}, alias("admin", "services", "mosdns", "basic"), _("MosDNS"), 30) local page = entry({"admin", "services", "mosdns"}, alias("admin", "services", "mosdns", "basic"), _("MosDNS"), 30)
page.dependent = true page.dependent = true
page.acl_depends = { "luci-app-mosdns" } page.acl_depends = { "luci-app-mosdns" }
entry({"admin", "services", "mosdns", "basic"}, cbi("mosdns/basic"), _("Basic Setting"), 1).leaf = true entry({"admin", "services", "mosdns", "basic"}, cbi("mosdns/basic"), _("Basic Setting"), 1).leaf = true
entry({"admin", "services", "mosdns", "rule_list"}, cbi("mosdns/rule_list"), _("Rule List"), 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", "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", "log"}, cbi("mosdns/log"), _("Logs"), 4).leaf = true
entry({"admin", "services", "mosdns", "status"}, call("act_status")).leaf = true entry({"admin", "services", "mosdns", "status"}, call("act_status")).leaf = true
entry({"admin", "services", "mosdns", "get_log"}, call("get_log")).leaf = true entry({"admin", "services", "mosdns", "get_log"}, call("get_log")).leaf = true
entry({"admin", "services", "mosdns", "clear_log"}, call("clear_log")).leaf = true entry({"admin", "services", "mosdns", "clear_log"}, call("clear_log")).leaf = true
entry({"admin", "services", "mosdns", "geo_update"}, call("geo_update")).leaf = true entry({"admin", "services", "mosdns", "geo_update"}, call("geo_update")).leaf = true
end end
function act_status() function act_status()
local e = {} local e = {}
e.running = luci.sys.call("pgrep -f mosdns >/dev/null") == 0 e.running = luci.sys.call("pgrep -f mosdns >/dev/null") == 0
luci.http.prepare_content("application/json") luci.http.prepare_content("application/json")
luci.http.write_json(e) luci.http.write_json(e)
end end
function get_log() function get_log()
luci.http.write(luci.sys.exec("cat $(/usr/share/mosdns/mosdns.sh logfile)")) luci.http.write(luci.sys.exec("cat $(/usr/share/mosdns/mosdns.sh logfile)"))
end end
function clear_log() function clear_log()
luci.sys.call("cat /dev/null > $(/usr/share/mosdns/mosdns.sh logfile)") luci.sys.call("cat /dev/null > $(/usr/share/mosdns/mosdns.sh logfile)")
end end
function geo_update() function geo_update()
local e = {} local e = {}
e.updating = luci.sys.call("/usr/share/mosdns/mosdns.sh geodata >/dev/null") == 0 e.updating = luci.sys.call("/usr/share/mosdns/mosdns.sh geodata >/dev/null") == 0
luci.http.prepare_content("application/json") luci.http.prepare_content("application/json")
luci.http.write_json(e) luci.http.write_json(e)
end end

View File

@ -1,7 +1,7 @@
if nixio.fs.access("/usr/bin/mosdns") then if nixio.fs.access("/usr/bin/mosdns") then
mosdns_version=luci.sys.exec("/usr/share/mosdns/mosdns.sh version") mosdns_version = luci.sys.exec("/usr/share/mosdns/mosdns.sh version")
else else
mosdns_version="Unknown Version" mosdns_version = "Unknown Version"
end end
m = Map("mosdns") m = Map("mosdns")
m.title = translate("MosDNS") .. " " .. mosdns_version m.title = translate("MosDNS") .. " " .. mosdns_version
@ -24,7 +24,7 @@ configfile.default = "/etc/mosdns/config.yaml"
listenport = s:option(Value, "listen_port", translate("Listen port")) listenport = s:option(Value, "listen_port", translate("Listen port"))
listenport.datatype = "and(port,min(1))" listenport.datatype = "and(port,min(1))"
listenport.default = 5335 listenport.default = 5335
listenport:depends( "configfile", "/etc/mosdns/config.yaml") listenport:depends("configfile", "/etc/mosdns/config.yaml")
loglevel = s:option(ListValue, "log_level", translate("Log Level")) loglevel = s:option(ListValue, "log_level", translate("Log Level"))
loglevel:value("debug", translate("Debug")) loglevel:value("debug", translate("Debug"))
@ -32,12 +32,12 @@ loglevel:value("info", translate("Info"))
loglevel:value("warn", translate("Warning")) loglevel:value("warn", translate("Warning"))
loglevel:value("error", translate("Error")) loglevel:value("error", translate("Error"))
loglevel.default = "info" loglevel.default = "info"
loglevel:depends( "configfile", "/etc/mosdns/config.yaml") loglevel:depends("configfile", "/etc/mosdns/config.yaml")
logfile = s:option(Value, "logfile", translate("Log File")) logfile = s:option(Value, "logfile", translate("Log File"))
logfile.placeholder = "/tmp/mosdns.log" logfile.placeholder = "/tmp/mosdns.log"
logfile.default = "/tmp/mosdns.log" logfile.default = "/tmp/mosdns.log"
logfile:depends( "configfile", "/etc/mosdns/config.yaml") logfile:depends("configfile", "/etc/mosdns/config.yaml")
redirect = s:option(Flag, "redirect", translate("DNS Forward"), translate("Forward Dnsmasq Domain Name resolution requests to MosDNS")) redirect = s:option(Flag, "redirect", translate("DNS Forward"), translate("Forward Dnsmasq Domain Name resolution requests to MosDNS"))
redirect.default = true redirect.default = true
@ -65,7 +65,7 @@ remote_dns:value("tls://149.112.112.112", "149.112.112.112 (Quad9 DNS)")
remote_dns:value("tls://45.11.45.11", "45.11.45.11 (DNS.SB)") remote_dns:value("tls://45.11.45.11", "45.11.45.11 (DNS.SB)")
remote_dns:value("tls://208.67.222.222", "208.67.222.222 (Open DNS)") remote_dns:value("tls://208.67.222.222", "208.67.222.222 (Open DNS)")
remote_dns:value("tls://208.67.220.220", "208.67.220.220 (Open DNS)") remote_dns:value("tls://208.67.220.220", "208.67.220.220 (Open DNS)")
remote_dns:depends( "configfile", "/etc/mosdns/config.yaml") remote_dns:depends("configfile", "/etc/mosdns/config.yaml")
bootstrap_dns = s:option(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")) bootstrap_dns = s:option(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"))
bootstrap_dns:value("119.29.29.29", "119.29.29.29 (DNSPod Primary)") bootstrap_dns:value("119.29.29.29", "119.29.29.29 (DNSPod Primary)")
@ -76,27 +76,27 @@ bootstrap_dns:value("114.114.114.114", "114.114.114.114 (114DNS Primary)")
bootstrap_dns:value("114.114.115.115", "114.114.115.115 (114DNS Secondary)") bootstrap_dns:value("114.114.115.115", "114.114.115.115 (114DNS Secondary)")
bootstrap_dns:value("180.76.76.76", "180.76.76.76 (Baidu DNS)") bootstrap_dns:value("180.76.76.76", "180.76.76.76 (Baidu DNS)")
bootstrap_dns.default = "119.29.29.29" bootstrap_dns.default = "119.29.29.29"
bootstrap_dns:depends( "configfile", "/etc/mosdns/config.yaml") bootstrap_dns:depends("configfile", "/etc/mosdns/config.yaml")
remote_dns_pipeline = s:option(Flag, "enable_pipeline", translate("TCP/DoT Connection Multiplexing"), translate("Enable TCP/DoT RFC 7766 new Query Pipelining connection multiplexing mode")) remote_dns_pipeline = s:option(Flag, "enable_pipeline", translate("TCP/DoT Connection Multiplexing"), translate("Enable TCP/DoT RFC 7766 new Query Pipelining connection multiplexing mode"))
remote_dns_pipeline.rmempty = false remote_dns_pipeline.rmempty = false
remote_dns_pipeline.default = false remote_dns_pipeline.default = false
remote_dns_pipeline:depends( "configfile", "/etc/mosdns/config.yaml") remote_dns_pipeline:depends("configfile", "/etc/mosdns/config.yaml")
cache_size = s:option(Value, "cache_size", translate("DNS Cache Size")) cache_size = s:option(Value, "cache_size", translate("DNS Cache Size"))
cache_size.datatype = "and(uinteger,min(0))" cache_size.datatype = "and(uinteger,min(0))"
cache_size.default = "20000" cache_size.default = "20000"
cache_size:depends( "configfile", "/etc/mosdns/config.yaml") cache_size:depends("configfile", "/etc/mosdns/config.yaml")
cache_size = s:option(Value, "cache_survival_time", translate("Cache Survival Time")) cache_size = s:option(Value, "cache_survival_time", translate("Cache Survival Time"))
cache_size.datatype = "and(uinteger,min(0))" cache_size.datatype = "and(uinteger,min(0))"
cache_size.default = "86400" cache_size.default = "86400"
cache_size:depends( "configfile", "/etc/mosdns/config.yaml") cache_size:depends("configfile", "/etc/mosdns/config.yaml")
cache_dump = s:option(Flag, "dump_file", translate("Cache Dump"), translate("Save the cache locally and reload the cache dump on the next startup")) cache_dump = s:option(Flag, "dump_file", translate("Cache Dump"), translate("Save the cache locally and reload the cache dump on the next startup"))
cache_dump.rmempty = false cache_dump.rmempty = false
cache_dump.default = false cache_dump.default = false
cache_dump:depends( "configfile", "/etc/mosdns/config.yaml") cache_dump:depends("configfile", "/etc/mosdns/config.yaml")
cache_dump = s:option(Value, "dump_interval", translate("Auto Save Cache Interval")) cache_dump = s:option(Value, "dump_interval", translate("Auto Save Cache Interval"))
cache_dump.datatype = "and(uinteger,min(0))" cache_dump.datatype = "and(uinteger,min(0))"
@ -107,15 +107,15 @@ minimal_ttl = s:option(Value, "minimal_ttl", translate("Minimum TTL"))
minimal_ttl.datatype = "and(uinteger,min(0))" minimal_ttl.datatype = "and(uinteger,min(0))"
minimal_ttl.datatype = "and(uinteger,max(3600))" minimal_ttl.datatype = "and(uinteger,max(3600))"
minimal_ttl.default = "0" minimal_ttl.default = "0"
minimal_ttl:depends( "configfile", "/etc/mosdns/config.yaml") minimal_ttl:depends("configfile", "/etc/mosdns/config.yaml")
maximum_ttl = s:option(Value, "maximum_ttl", translate("Maximum TTL")) maximum_ttl = s:option(Value, "maximum_ttl", translate("Maximum TTL"))
maximum_ttl.datatype = "and(uinteger,min(0))" maximum_ttl.datatype = "and(uinteger,min(0))"
maximum_ttl.default = "0" maximum_ttl.default = "0"
maximum_ttl:depends( "configfile", "/etc/mosdns/config.yaml") maximum_ttl:depends("configfile", "/etc/mosdns/config.yaml")
adblock = s:option(Flag, "adblock", translate("Enable DNS ADblock")) adblock = s:option(Flag, "adblock", translate("Enable DNS ADblock"))
adblock:depends( "configfile", "/etc/mosdns/config.yaml") adblock:depends("configfile", "/etc/mosdns/config.yaml")
adblock.default = false adblock.default = false
adblock = s:option(Value, "ad_source", translate("ADblock Source")) adblock = s:option(Value, "ad_source", translate("ADblock Source"))
@ -131,21 +131,21 @@ reload_service = s:option( Button, "_reload", translate("Reload Service"), trans
reload_service.write = function() reload_service.write = function()
luci.sys.exec("/etc/init.d/mosdns reload") luci.sys.exec("/etc/init.d/mosdns reload")
end end
reload_service:depends( "configfile", "/etc/mosdns/config_custom.yaml") reload_service:depends("configfile", "/etc/mosdns/config_custom.yaml")
config = s:option(TextValue, "manual-config") config = s:option(TextValue, "manual-config")
config.description = translate("<font color=\"ff0000\"><strong>View the Custom YAML Configuration file used by this MosDNS. You can edit it as you own need.</strong></font>") config.description = translate("<font color=\"ff0000\"><strong>View the Custom YAML Configuration file used by this MosDNS. You can edit it as you own need.</strong></font>")
config.template = "cbi/tvalue" config.template = "cbi/tvalue"
config.rows = 25 config.rows = 25
config:depends( "configfile", "/etc/mosdns/config_custom.yaml") config:depends("configfile", "/etc/mosdns/config_custom.yaml")
function config.cfgvalue(self, section) function config.cfgvalue(self, section)
return nixio.fs.readfile("/etc/mosdns/config_custom.yaml") return nixio.fs.readfile("/etc/mosdns/config_custom.yaml")
end end
function config.write(self, section, value) function config.write(self, section, value)
value = value:gsub("\r\n?", "\n") value = value:gsub("\r\n?", "\n")
nixio.fs.writefile("/etc/mosdns/config_custom.yaml", value) nixio.fs.writefile("/etc/mosdns/config_custom.yaml", value)
end end
return m return m

View File

@ -20,7 +20,7 @@ o.default = "*"
update_time = s:option(ListValue, "geo_update_day_time", translate("Update Time")) update_time = s:option(ListValue, "geo_update_day_time", translate("Update Time"))
for t = 0, 23 do for t = 0, 23 do
update_time:value(t, t..":00") update_time:value(t, t..":00")
end end
update_time.default = 0 update_time.default = 0

View File

@ -1,33 +1,33 @@
<%+cbi/valueheader%> <%+cbi/valueheader%>
<script type="text/javascript">//<![CDATA[ <script type="text/javascript">//<![CDATA[
function update_data(btn, dataname) function update_data(btn, dataname)
{ {
btn.disabled = true; btn.disabled = true;
btn.value = '<%:Updating...%> '; btn.value = '<%:Updating...%> ';
st=dataname; st=dataname;
XHR.get('<%=luci.dispatcher.build_url("admin", "services", "mosdns", "geo_update")%>', XHR.get('<%=luci.dispatcher.build_url("admin", "services", "mosdns", "geo_update")%>',
{ set:st }, { set:st },
function(x,data) function(x,data)
{ {
var tb = document.getElementById(dataname+'-status'); var tb = document.getElementById(dataname+'-status');
if (tb) if (tb)
{ {
switch (data.updating) switch (data.updating)
{ {
case true: case true:
tb.innerHTML = "<font color='green'>" + "<%:Update success%>" + "</font>"; tb.innerHTML = "<font color='green'>" + "<%:Update success%>" + "</font>";
break; break;
case false: case false:
tb.innerHTML = "<font color='red'>" + "<%:Update failed, Please check the network status%>" + "</font>"; tb.innerHTML = "<font color='red'>" + "<%:Update failed, Please check the network status%>" + "</font>";
break; break;
} }
} }
btn.disabled = false; btn.disabled = false;
btn.value = '<%:Check And Update%>'; btn.value = '<%:Check And Update%>';
} }
); );
return false; return false;
} }
//]]></script> //]]></script>
<input type="button" class="btn cbi-button cbi-button-reload" value="<%:Check And Update%>" onclick="return update_data(this,'<%=self.option%>')" /> <input type="button" class="btn cbi-button cbi-button-reload" value="<%:Check And Update%>" onclick="return update_data(this,'<%=self.option%>')" />
<span id="<%=self.option%>-status"><em><%=self.value%></em></span> <span id="<%=self.option%>-status"><em><%=self.value%></em></span>

View File

@ -1,29 +1,29 @@
<script type="text/javascript"> <script type="text/javascript">
//<![CDATA[ //<![CDATA[
function clear_log(btn) { function clear_log(btn) {
XHR.get('<%=url([[admin]], [[services]], [[mosdns]], [[clear_log]])%>', null, XHR.get('<%=url([[admin]], [[services]], [[mosdns]], [[clear_log]])%>', null,
function(x, data) { function(x, data) {
if(x && x.status == 200) { if(x && x.status == 200) {
var log_textarea = document.getElementById('log_textarea'); var log_textarea = document.getElementById('log_textarea');
log_textarea.innerHTML = ""; log_textarea.innerHTML = "";
log_textarea.scrollTop = log_textarea.scrollHeight; log_textarea.scrollTop = log_textarea.scrollHeight;
} }
location.reload(); location.reload();
} }
); );
} }
XHR.poll(1, '<%=url([[admin]], [[services]], [[mosdns]], [[get_log]])%>', null, XHR.poll(1, '<%=url([[admin]], [[services]], [[mosdns]], [[get_log]])%>', null,
function(x, data) { function(x, data) {
if(x && x.status == 200) { if(x && x.status == 200) {
var log_textarea = document.getElementById('log_textarea'); var log_textarea = document.getElementById('log_textarea');
log_textarea.innerHTML = x.responseText; log_textarea.innerHTML = x.responseText;
log_textarea.scrollTop = log_textarea.scrollHeight; log_textarea.scrollTop = log_textarea.scrollHeight;
} }
} }
); );
//]]> //]]>
</script> </script>
<fieldset class="cbi-section" id="_log_fieldset"> <fieldset class="cbi-section" id="_log_fieldset">
<input class="cbi-button cbi-input-remove" type="button" onclick="clear_log()" value="<%:Clear logs%>" style="margin-left: 10px; margin-top: 10px;"> <input class="cbi-button cbi-input-remove" type="button" onclick="clear_log()" value="<%:Clear logs%>" style="margin-left: 10px; margin-top: 10px;">
<textarea id="log_textarea" class="cbi-input-textarea" style="width: calc(100% - 20px); height: 600px; margin: 10px;" data-update="change" rows="5" wrap="off" readonly="readonly"></textarea> <textarea id="log_textarea" class="cbi-input-textarea" style="width: calc(100% - 20px); height: 600px; margin: 10px;" data-update="change" rows="5" wrap="off" readonly="readonly"></textarea>
</fieldset> </fieldset>

View File

@ -1,28 +1,28 @@
<script type="text/javascript"> <script type="text/javascript">
//<![CDATA[ //<![CDATA[
XHR.poll(3, '<%=url([[admin]], [[services]], [[mosdns]], [[status]])%>', null, XHR.poll(3, '<%=url([[admin]], [[services]], [[mosdns]], [[status]])%>', null,
function(x, data) { function(x, data) {
var tb = document.getElementById('mosdns_status'); var tb = document.getElementById('mosdns_status');
if (data && tb) { if (data && tb) {
if (data.running) { if (data.running) {
var links = '<em><b style=color:green>MosDNS <%:RUNNING%></b></em>'; var links = '<em><b style=color:green>MosDNS <%:RUNNING%></b></em>';
tb.innerHTML = links; tb.innerHTML = links;
} else { } else {
tb.innerHTML = '<em><b style=color:red>MosDNS <%:NOT RUNNING%></b></em>'; tb.innerHTML = '<em><b style=color:red>MosDNS <%:NOT RUNNING%></b></em>';
} }
} }
} }
); );
//]]> //]]>
</script> </script>
<style> <style>
.mar-10 { .mar-10 {
margin-left: 50px; margin-left: 50px;
margin-right: 10px; margin-right: 10px;
} }
</style> </style>
<fieldset class="cbi-section"> <fieldset class="cbi-section">
<p id="mosdns_status"> <p id="mosdns_status">
<em><%:Collecting data...%></em> <em><%:Collecting data...%></em>
</p> </p>
</fieldset> </fieldset>

View File

@ -33,142 +33,141 @@ DEFAULT_CONF=/usr/share/mosdns/default.yaml
MOSDNS_SCRIPT=/usr/share/mosdns/mosdns.sh MOSDNS_SCRIPT=/usr/share/mosdns/mosdns.sh
get_config() { get_config() {
config_get enabled $1 enabled 0 config_get enabled $1 enabled 0
config_get adblock $1 adblock 0 config_get adblock $1 adblock 0
config_get ad_source $1 ad_source "" config_get ad_source $1 ad_source ""
config_get cache_size $1 cache_size 20000 config_get cache_size $1 cache_size 20000
config_get cache_survival_time $1 cache_survival_time 86400 config_get cache_survival_time $1 cache_survival_time 86400
config_get dump_file $1 dump_file 0 config_get dump_file $1 dump_file 0
config_get dump_interval $1 dump_interval 600 config_get dump_interval $1 dump_interval 600
config_get enable_pipeline $1 enable_pipeline 0 config_get enable_pipeline $1 enable_pipeline 0
config_get geo_auto_update $1 geo_auto_update 0 config_get geo_auto_update $1 geo_auto_update 0
config_get geo_update_day_time $1 geo_update_day_time 2 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 geo_update_week_time $1 geo_update_week_time "*"
config_get listen_port $1 listen_port 5335 config_get listen_port $1 listen_port 5335
config_get log_file $1 logfile "/tmp/mosdns.log" config_get log_file $1 logfile "/tmp/mosdns.log"
config_get log_level $1 log_level "info" config_get log_level $1 log_level "info"
config_get maximum_ttl_custom $1 maximum_ttl 0 config_get maximum_ttl_custom $1 maximum_ttl 0
config_get minimal_ttl_custom $1 minimal_ttl 0 config_get minimal_ttl_custom $1 minimal_ttl 0
config_get redirect $1 redirect 0 config_get redirect $1 redirect 0
config_get remote_dns $1 remote_dns "tls://8.8.8.8 tls://1.1.1.1" config_get remote_dns $1 remote_dns "tls://8.8.8.8 tls://1.1.1.1"
config_get custom_local_dns $1 custom_local_dns 0 config_get custom_local_dns $1 custom_local_dns 0
config_get bootstrap_dns $1 bootstrap_dns "119.29.29.29" config_get bootstrap_dns $1 bootstrap_dns "119.29.29.29"
} }
init_yaml() { init_yaml() {
tmpdir=$(mktemp -d) || exit 1 TMPDIR=$(mktemp -d) || exit 1
adlist=$($MOSDNS_SCRIPT ad) adlist=$($MOSDNS_SCRIPT ad)
[ $enable_pipeline = 1 ] && enable_pipeline=true || enable_pipeline=false [ $enable_pipeline -eq 1 ] && enable_pipeline=true || enable_pipeline=false
[ "$custom_local_dns" -eq 1 ] && \ [ "$custom_local_dns" -eq 1 ] && \
local_dns=$($MOSDNS_SCRIPT dns | awk '{for(i=1;i<=NF;i++)printf "%s- addr: %s\n%s bootstrap: '${bootstrap_dns}'\n%s enable_pipeline: '${enable_pipeline}'\n",s,$i,s,s}' s=' ') || \ local_dns=$($MOSDNS_SCRIPT dns | awk '{for(i=1;i<=NF;i++)printf "%s- addr: %s\n%s bootstrap: '${bootstrap_dns}'\n%s enable_pipeline: '${enable_pipeline}'\n",s,$i,s,s}' s=' ') || \
local_dns=$($MOSDNS_SCRIPT dns | awk '{for(i=1;i<=NF;i++)printf "%s- addr: %s\n",s,$i,s}' s=' ') local_dns=$($MOSDNS_SCRIPT dns | awk '{for(i=1;i<=NF;i++)printf "%s- addr: %s\n",s,$i,s}' s=' ')
remote_dns=$(echo $remote_dns | awk '{for(i=1;i<=NF;i++)printf "%s- addr: %s\n%s bootstrap: '${bootstrap_dns}'\n%s enable_pipeline: '${enable_pipeline}'\n",s,$i,s,s}' s=' ') remote_dns=$(echo $remote_dns | awk '{for(i=1;i<=NF;i++)printf "%s- addr: %s\n%s bootstrap: '${bootstrap_dns}'\n%s enable_pipeline: '${enable_pipeline}'\n",s,$i,s,s}' s=' ')
sed "s,log_level,$log_level,g;s,log_file,$log_file,g; \ 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,listen_port,$listen_port,g;s,cache_size,$cache_size,g; \
s,cache_survival_time,$cache_survival_time,g; \ s,cache_survival_time,$cache_survival_time,g; \
s,minimal_ttl_custom,$minimal_ttl_custom,g; \ s,minimal_ttl_custom,$minimal_ttl_custom,g; \
s,maximum_ttl_custom,$maximum_ttl_custom,g; \ s,maximum_ttl_custom,$maximum_ttl_custom,g; \
s,adblock,$adlist,g;s,remote_dns_pipeline,$enable_pipeline,g" $DEFAULT_CONF > $CONF s,adblock,$adlist,g;s,remote_dns_pipeline,$enable_pipeline,g" $DEFAULT_CONF > $CONF
echo "${local_dns}" > $tmpdir/local_dns.txt echo "${local_dns}" > $TMPDIR/local_dns.txt
echo "${remote_dns}" > $tmpdir/remote_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 sed -i -e '/- addr: local_dns/{r '$TMPDIR/local_dns.txt -e';d};/- addr: remote_dns/{r '$TMPDIR/remote_dns.txt -e';d}' $CONF
[ "$dump_file" -eq 1 ] && sed -i "/lazy_cache_ttl/a\ dump_file: $DUMP_FILE\n dump_interval: $dump_interval" $CONF [ "$dump_file" -eq 1 ] && sed -i "/lazy_cache_ttl/a\ dump_file: $DUMP_FILE\n dump_interval: $dump_interval" $CONF
[ "$dump_file" -eq 1 ] && [ ! -f $DUMP_FILE ] && cp -a $DUMP_FILE_DEFAULT $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 [ "$dump_file" -eq 0 ] && \cp -a $DUMP_FILE_DEFAULT $DUMP_FILE
rm -rf $tmpdir rm -rf $TMPDIR
} }
service_triggers() { service_triggers() {
procd_add_reload_trigger "mosdns" procd_add_reload_trigger "mosdns"
} }
restore_setting() { restore_setting() {
rm -f /etc/mosdns/redirect.lock rm -f /etc/mosdns/redirect.lock
sed -i "/list server/d" /etc/config/dhcp sed -i "/list server/d" /etc/config/dhcp
uci set dhcp.@dnsmasq[0].noresolv='0' uci set dhcp.@dnsmasq[0].noresolv='0'
uci del dhcp.@dnsmasq[0].cachesize uci del dhcp.@dnsmasq[0].cachesize
uci commit dhcp uci commit dhcp
} }
redirect_setting() { redirect_setting() {
if [ "${CONF}" = "/etc/mosdns/config.yaml" ]; then if [ "${CONF}" = "/etc/mosdns/config.yaml" ]; then
sed -i "/list server/d" /etc/config/dhcp sed -i "/list server/d" /etc/config/dhcp
uci add_list dhcp.@dnsmasq[0].server="127.0.0.1#$listen_port" 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].rebind_protection='0'
uci set dhcp.@dnsmasq[0].noresolv="1" uci set dhcp.@dnsmasq[0].noresolv="1"
uci set dhcp.@dnsmasq[0].cachesize='0' uci set dhcp.@dnsmasq[0].cachesize='0'
uci commit dhcp uci commit dhcp
else else
sed -i "/list server/d" /etc/config/dhcp sed -i "/list server/d" /etc/config/dhcp
uci add_list dhcp.@dnsmasq[0].server="127.0.0.1#$(cat /etc/mosdns/config_custom.yaml | awk -F'[:" ]+' '/^\s+listen:/{for(i=1;i<=NF;i++){if($i~/^[0-9]+$/){print $i;exit}}}')" 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].rebind_protection='0'
uci set dhcp.@dnsmasq[0].noresolv="1" uci set dhcp.@dnsmasq[0].noresolv="1"
uci set dhcp.@dnsmasq[0].cachesize='0' uci set dhcp.@dnsmasq[0].cachesize='0'
uci commit dhcp uci commit dhcp
fi fi
touch /etc/mosdns/redirect.lock touch /etc/mosdns/redirect.lock
} }
reload_others() { reload_dnsmasq() {
/etc/init.d/network reload /etc/init.d/dnsmasq reload
/etc/init.d/dnsmasq reload
} }
reload_service() { reload_service() {
stop stop
sleep 1 sleep 1
start start
} }
setcron() { setcron() {
sed -i '/mosdns.sh/d' $CRON_FILE 2>/dev/null sed -i '/mosdns.sh/d' $CRON_FILE 2>/dev/null
[ "$geo_auto_update" -eq 1 ] && echo "0 $geo_update_day_time * * $geo_update_week_time $MOSDNS_SCRIPT geodata" >>$CRON_FILE [ "$geo_auto_update" -eq 1 ] && echo "0 $geo_update_day_time * * $geo_update_week_time $MOSDNS_SCRIPT geodata" >> $CRON_FILE
crontab $CRON_FILE crontab $CRON_FILE
} }
delcron() { delcron() {
sed -i '/mosdns.sh/d' $CRON_FILE 2>/dev/null sed -i '/mosdns.sh/d' $CRON_FILE 2>/dev/null
crontab $CRON_FILE crontab $CRON_FILE
} }
v2dat_dump() { v2dat_dump() {
mkdir -p /var/mosdns mkdir -p /var/mosdns
rm -f /var/mosdns/geo*.txt rm -f /var/mosdns/geo*.txt
v2dat unpack geoip -o /var/mosdns -f cn $V2DAT_DIR/geoip.dat 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 unpack geosite -o /var/mosdns -f cn -f 'geolocation-!cn' $V2DAT_DIR/geosite.dat
[ "$adblock" -eq 1 ] && [ "$ad_source" = "geosite.dat" ] && v2dat unpack geosite -o /var/mosdns -f category-ads-all $V2DAT_DIR/geosite.dat [ "$adblock" -eq 1 ] && [ "$ad_source" = "geosite.dat" ] && v2dat unpack geosite -o /var/mosdns -f category-ads-all $V2DAT_DIR/geosite.dat
} }
start_service() { start_service() {
config_load "mosdns" config_load "mosdns"
config_foreach get_config "mosdns" config_foreach get_config "mosdns"
[ $enabled != 1 ] && return 1 [ $enabled -ne 1 ] && return 1
delcron ; setcron delcron ; setcron
[ "${CONF}" = "/etc/mosdns/config.yaml" ] && init_yaml [ "${CONF}" = "/etc/mosdns/config.yaml" ] && init_yaml
:> $($MOSDNS_SCRIPT logfile) :> $($MOSDNS_SCRIPT logfile)
v2dat_dump >> $($MOSDNS_SCRIPT logfile) 2>&1 v2dat_dump >> $($MOSDNS_SCRIPT logfile) 2>&1
procd_open_instance mosdns procd_open_instance mosdns
procd_set_param command $PROG start -c "$CONF" procd_set_param command $PROG start -c "$CONF"
procd_set_param user root procd_set_param user root
procd_set_param stdout 1 procd_set_param stdout 1
procd_set_param stderr 1 procd_set_param stderr 1
procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}" procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
procd_close_instance mosdns procd_close_instance mosdns
[ "$redirect" -ne 1 ] && [ -f "/etc/mosdns/redirect.lock" ] && restore_setting [ "$redirect" -ne 1 ] && [ -f "/etc/mosdns/redirect.lock" ] && restore_setting
[ "$redirect" -eq 1 ] && redirect_setting [ "$redirect" -eq 1 ] && redirect_setting
reload_others reload_dnsmasq
update_list=0 update_list=0
[ "$adblock" -eq 1 ] && [ "$ad_source" != "geosite.dat" ] && { [ "$adblock" -eq 1 ] && [ "$ad_source" != "geosite.dat" ] && {
[ -s "/etc/mosdns/rule/adlist.txt" ] && [ -f "/etc/mosdns/rule/.ad_source" ] && url_adlist=$(cat /etc/mosdns/rule/.ad_source) || update_list=1 [ -s "/etc/mosdns/rule/adlist.txt" ] && [ -f "/etc/mosdns/rule/.ad_source" ] && url_adlist=$(cat /etc/mosdns/rule/.ad_source) || update_list=1
[ "$ad_source" != "$url_adlist" ] && update_list=1 [ "$ad_source" != "$url_adlist" ] && update_list=1
} }
[ "$update_list" -eq 1 ] && $MOSDNS_SCRIPT adlist_update &> /dev/null & [ "$update_list" -eq 1 ] && $MOSDNS_SCRIPT adlist_update &> /dev/null &
} }
stop_service() { stop_service() {
config_load "mosdns" config_load "mosdns"
config_foreach get_config "mosdns" config_foreach get_config "mosdns"
[ "$enabled" = "0" ] && [ -f "/etc/mosdns/redirect.lock" ] && restore_setting [ "$enabled" -eq "0" ] && [ -f "/etc/mosdns/redirect.lock" ] && restore_setting
reload_others reload_dnsmasq
delcron delcron
} }

View File

@ -3,107 +3,107 @@
script_action=${1} script_action=${1}
logfile_path() ( logfile_path() (
configfile=$(uci -q get mosdns.config.configfile) configfile=$(uci -q get mosdns.config.configfile)
if [ "$configfile" = "/etc/mosdns/config.yaml" ]; then if [ "$configfile" = "/etc/mosdns/config.yaml" ]; then
uci -q get mosdns.config.logfile uci -q get mosdns.config.logfile
else else
[ ! -f /etc/mosdns/config_custom.yaml ] && exit 1 [ ! -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 "/[^'\"]+" awk '/^log:/{f=1;next}f==1{if($0~/file:/){print;exit}if($0~/^[^ ]/)exit}' /etc/mosdns/config_custom.yaml | grep -Eo "/[^'\"]+"
fi fi
) )
interface_dns() ( interface_dns() (
if [ "$(uci -q get mosdns.config.custom_local_dns)" = 1 ]; then if [ "$(uci -q get mosdns.config.custom_local_dns)" = 1 ]; then
uci -q get mosdns.config.local_dns uci -q get mosdns.config.local_dns
else else
peerdns=$(uci -q get network.wan.peerdns) peerdns=$(uci -q get network.wan.peerdns)
proto=$(uci -q get network.wan.proto) proto=$(uci -q get network.wan.proto)
if [ "$peerdns" = 0 ] || [ "$proto" = "static" ]; then if [ "$peerdns" = 0 ] || [ "$proto" = "static" ]; then
uci -q get network.wan.dns uci -q get network.wan.dns
else else
interface_status=$(ubus call network.interface.wan status) interface_status=$(ubus call network.interface.wan status)
echo $interface_status | jsonfilter -e "@['dns-server'][0]" echo $interface_status | jsonfilter -e "@['dns-server'][0]"
echo $interface_status | jsonfilter -e "@['dns-server'][1]" echo $interface_status | jsonfilter -e "@['dns-server'][1]"
fi fi
[ $? -ne 0 ] && echo "119.29.29.29 223.5.5.5" [ $? -ne 0 ] && echo "119.29.29.29 223.5.5.5"
fi fi
) )
ad_block() ( ad_block() (
adblock=$(uci -q get mosdns.config.adblock) adblock=$(uci -q get mosdns.config.adblock)
if [ "$adblock" = 1 ]; then if [ "$adblock" = 1 ]; then
ad_source=$(uci -q get mosdns.config.ad_source) ad_source=$(uci -q get mosdns.config.ad_source)
if [ "$ad_source" = "geosite.dat" ]; then if [ "$ad_source" = "geosite.dat" ]; then
echo "/var/mosdns/geosite_category-ads-all.txt" echo "/var/mosdns/geosite_category-ads-all.txt"
else else
echo "/etc/mosdns/rule/adlist.txt" echo "/etc/mosdns/rule/adlist.txt"
fi fi
else else
touch /var/disable-ads.txt ; echo "/var/disable-ads.txt" touch /var/disable-ads.txt ; echo "/var/disable-ads.txt"
fi fi
) )
adlist_update() ( adlist_update() (
ad_source=$(uci -q get mosdns.config.ad_source) ad_source=$(uci -q get mosdns.config.ad_source)
[ "$ad_source" = "geosite.dat" ] || [ -z "$ad_source" ] && exit 0 [ "$ad_source" = "geosite.dat" ] || [ -z "$ad_source" ] && exit 0
AD_TMPDIR=$(mktemp -d) || exit 1 AD_TMPDIR=$(mktemp -d) || exit 1
if echo "$ad_source" | grep -Eq "^https://raw.githubusercontent.com" ; then if echo "$ad_source" | grep -Eq "^https://raw.githubusercontent.com" ; then
google_status=$(curl -I -4 -m 3 -o /dev/null -s -w %{http_code} http://www.google.com/generate_204) google_status=$(curl -I -4 -m 3 -o /dev/null -s -w %{http_code} http://www.google.com/generate_204)
[ "$google_status" -ne "204" ] && mirror="https://ghproxy.com/" [ "$google_status" -ne "204" ] && mirror="https://ghproxy.com/"
fi fi
echo -e "\e[1;32mDownloading $mirror$ad_source\e[0m" echo -e "\e[1;32mDownloading $mirror$ad_source\e[0m"
curl --connect-timeout 60 -m 90 --ipv4 -kfSLo "$AD_TMPDIR/adlist.txt" "$mirror$ad_source" curl --connect-timeout 60 -m 90 --ipv4 -kfSLo "$AD_TMPDIR/adlist.txt" "$mirror$ad_source"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
rm -rf "$AD_TMPDIR" rm -rf "$AD_TMPDIR"
exit 1 exit 1
else else
\cp "$AD_TMPDIR/adlist.txt" /etc/mosdns/rule/adlist.txt \cp "$AD_TMPDIR/adlist.txt" /etc/mosdns/rule/adlist.txt
echo "$ad_source" > /etc/mosdns/rule/.ad_source echo "$ad_source" > /etc/mosdns/rule/.ad_source
rm -rf "$AD_TMPDIR" rm -rf "$AD_TMPDIR"
fi fi
) )
geodat_update() ( geodat_update() (
geodat_download() ( geodat_download() (
google_status=$(curl -I -4 -m 3 -o /dev/null -s -w %{http_code} http://www.google.com/generate_204) google_status=$(curl -I -4 -m 3 -o /dev/null -s -w %{http_code} http://www.google.com/generate_204)
[ "$google_status" -ne "204" ] && mirror="https://ghproxy.com/" [ "$google_status" -ne "204" ] && mirror="https://ghproxy.com/"
echo -e "\e[1;32mDownloading "$mirror"https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/$1\e[0m" echo -e "\e[1;32mDownloading "$mirror"https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/$1\e[0m"
curl --connect-timeout 60 -m 900 --ipv4 -kfSLo "$TMPDIR/$1" ""$mirror"https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/$1" curl --connect-timeout 60 -m 900 --ipv4 -kfSLo "$TMPDIR/$1" ""$mirror"https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/$1"
) )
TMPDIR=$(mktemp -d) || exit 1 TMPDIR=$(mktemp -d) || exit 1
geodat_download geoip.dat && geodat_download geosite.dat geodat_download geoip.dat && geodat_download geosite.dat
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
rm -rf "$TMPDIR" rm -rf "$TMPDIR"
exit 1 exit 1
fi fi
cp -f "$TMPDIR"/* /usr/share/v2ray cp -f "$TMPDIR"/* /usr/share/v2ray
rm -rf "$TMPDIR" rm -rf "$TMPDIR"
) )
restart_service() { restart_service() {
service mosdns restart service mosdns restart
} }
case $script_action in case $script_action in
"dns") "dns")
interface_dns interface_dns
;; ;;
"ad") "ad")
ad_block ad_block
;; ;;
"geodata") "geodata")
geodat_update && adlist_update && restart_service geodat_update && adlist_update && restart_service
;; ;;
"logfile") "logfile")
logfile_path logfile_path
;; ;;
"adlist_update") "adlist_update")
adlist_update && restart_service adlist_update && restart_service
;; ;;
"version") "version")
mosdns version mosdns version
;; ;;
*) *)
exit 0 exit 0
;; ;;
esac esac