Skip to content

Commit

Permalink
tidy up code
Browse files Browse the repository at this point in the history
  • Loading branch information
Beginner-Go committed Nov 4, 2021
1 parent adbd3dc commit b764334
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 102 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ include $(TOPDIR)/rules.mk

PKG_NAME:=luci-app-dnsfilter
PKG_VERSION:=1.0
PKG_RELEASE:=11
PKG_RELEASE:=12

PKG_LICENSE:=GPLv2
PKG_MAINTAINER:=small_5 kiddin9
Expand Down
37 changes: 20 additions & 17 deletions luasrc/controller/dnsfilter.lua
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
module("luci.controller.dnsfilter",package.seeall)

function index()
if not nixio.fs.access("/etc/config/dnsfilter") then
return
end
local page = entry({"admin","services","dnsfilter"},alias("admin","services","dnsfilter","base"),_("DNS Filter"),9)

local page = entry({"admin", "services", "dnsfilter"}, alias("admin", "services", "dnsfilter", "base"), _("DNS Filter"), 9)
page.dependent = true
page.acl_depends = { "luci-app-dnsfilter" }
entry({"admin","services","dnsfilter","base"},cbi("dnsfilter/base"),_("Base Setting"),10).leaf=true
entry({"admin","services","dnsfilter","white"},form("dnsfilter/white"),_("White Domain List"),20).leaf=true
entry({"admin","services","dnsfilter","black"},form("dnsfilter/black"),_("Block Domain List"),30).leaf=true
entry({"admin","services","dnsfilter","ip"},form("dnsfilter/ip"),_("Block IP List"),40).leaf=true
entry({"admin","services","dnsfilter","log"},form("dnsfilter/log"),_("Update Log"),50).leaf=true
entry({"admin","services","dnsfilter","run"},call("act_status")).leaf = true
entry({"admin","services","dnsfilter","refresh"},call("refresh_data"))

entry({"admin", "services", "dnsfilter", "base"}, cbi("dnsfilter/base"), _("Base Setting"), 10).leaf = true
entry({"admin", "services", "dnsfilter", "white"}, form("dnsfilter/white"), _("White Domain List"), 20).leaf = true
entry({"admin", "services", "dnsfilter", "black"}, form("dnsfilter/black"), _("Block Domain List"), 30).leaf = true
entry({"admin", "services", "dnsfilter", "ip"}, form("dnsfilter/ip"), _("Block IP List"), 40).leaf = true
entry({"admin", "services", "dnsfilter", "log"}, form("dnsfilter/log"), _("Update Log"), 50).leaf = true
entry({"admin", "services", "dnsfilter", "run"}, call("act_status")).leaf = true
entry({"admin", "services", "dnsfilter", "refresh"}, call("refresh_data"))
end

function act_status()
local e={}
e.running=luci.sys.call("[ -s /tmp/dnsmasq.dnsfilter/rules.conf ]")==0
local e = {}
e.running = luci.sys.call("[ -s /tmp/dnsmasq.dnsfilter/rules.conf ]") == 0
luci.http.prepare_content("application/json")
luci.http.write_json(e)
end

function refresh_data()
local set=luci.http.formvalue("set")
local icount=0
local set = luci.http.formvalue("set")
local icount = 0

luci.sys.exec("/usr/share/dnsfilter/dnsfilter down")
icount=luci.sys.exec("find /tmp/ad_tmp -type f -name rules.conf -exec cat {} \\; 2>/dev/null | wc -l")
icount = luci.sys.exec("find /tmp/ad_tmp -type f -name rules.conf -exec cat {} \\; 2>/dev/null | wc -l")
if tonumber(icount)>0 then
oldcount=luci.sys.exec("find /tmp/dnsfilter -type f -name rules.conf -exec cat {} \\; 2>/dev/null | wc -l")
oldcount = luci.sys.exec("find /tmp/dnsfilter -type f -name rules.conf -exec cat {} \\; 2>/dev/null | wc -l")
if tonumber(icount) ~= tonumber(oldcount) then
luci.sys.exec("[ -h /tmp/dnsfilter/url ] && (rm -f /etc/dnsfilter/rules/*;cp -a /tmp/ad_tmp/* /etc/dnsfilter/rules) || (rm -f /tmp/dnsfilter/*;cp -a /tmp/ad_tmp/* /tmp/dnsfilter)")
luci.sys.exec("/etc/init.d/dnsfilter restart &")
retstring=tostring(math.ceil(tonumber(icount)))
retstring = tostring(math.ceil(tonumber(icount)))
else
retstring=0
retstring = 0
end
luci.sys.call("echo `date +'%Y-%m-%d %H:%M:%S'` > /tmp/dnsfilter/dnsfilter.updated")
else
retstring="-1"
retstring = "-1"
end
luci.sys.exec("rm -rf /tmp/ad_tmp")

Expand Down
99 changes: 51 additions & 48 deletions luasrc/model/cbi/dnsfilter/base.lua
Original file line number Diff line number Diff line change
@@ -1,60 +1,62 @@
local SYS=require "luci.sys"
local SYS = require "luci.sys"

m=Map("dnsfilter")
m.title=translate("DNS Filter")
m.description=translate("Support AdGuardHome/Host/DNSMASQ/Domain Rules")
m:section(SimpleSection).template="dnsfilter/dnsfilter_status"
m = Map("dnsfilter")
m.title = translate("DNS Filter")
m.description = translate("Support AdGuardHome/Host/DNSMASQ/Domain Rules")
m:section(SimpleSection).template = "dnsfilter/dnsfilter_status"

s=m:section(TypedSection,"dnsfilter")
s.anonymous=true
s = m:section(TypedSection, "dnsfilter")
s.anonymous = true

o=s:option(Flag,"enable")
o.title=translate("Enable")
o.rmempty=false
o = s:option(Flag, "enable", translate("Enable"))
o.rmempty = false

o=s:option(Flag,"block_ios")
o.title=translate("Block Apple iOS OTA update")
o = s:option(Flag, "block_ios", translate("Block Apple iOS OTA update"))
o.rmempty = false
o.default = 0

o=s:option(Flag,"block_cnshort")
o.title=translate("Block CNshort APP and Website")
o = s:option(Flag, "block_cnshort", translate("Block CNshort APP and Website"))
o.rmempty = false
o.default = 0

o=s:option(Flag,"safe_search")
o.title=translate("Safe Search")
o.description=translate("Enforcing SafeSearch for google, bing, duckduckgo, yandex and youtube.")
o = s:option(Flag, "safe_search", translate("Safe Search"))
o.description = translate("Enforcing SafeSearch for Google Bing Duckduckgo Yandex and Youtube.")
o.rmempty = false
o.default = 0

o=s:option(Flag,"cron_mode")
o.title=translate("Enable automatic update rules")
o = s:option(Flag, "cron_mode", translate("Enable automatic update rules"))
o.rmempty = false
o.default = 1

o=s:option(ListValue,"time_update")
o.title=translate("Update time (every day)")
for s=0,23 do
o = s:option(ListValue, "time_update", translate("Update time (every day)"))
for s = 0,23 do
o:value(s, s .. ':00')
end
o.default=6
o.default = 6
o:depends("cron_mode",1)

tmp_rule=0
tmp_rule = 0
if nixio.fs.access("/tmp/dnsfilter/rules.conf") then
tmp_rule=1
UD=SYS.exec("cat /tmp/dnsfilter/dnsfilter.updated 2>/dev/null")
rule_count=tonumber(SYS.exec("find /tmp/dnsfilter -exec cat {} \\; 2>/dev/null | wc -l"))
o=s:option(DummyValue,"1",translate("Subscribe Rules Data"))
o.rawhtml=true
o.template="dnsfilter/dnsfilter_refresh"
o.value=rule_count.." "..translate("Records")
o.description=string.format(translate("AdGuardHome / Host / DNSMASQ / Domain rules auto-convert").."<br/><strong>"..translate("Last Update Checked")..":</strong> %s<br/>",UD)
tmp_rule = 1
UD = SYS.exec("cat /tmp/dnsfilter/dnsfilter.updated 2>/dev/null")
rule_count = tonumber(SYS.exec("find /tmp/dnsfilter -exec cat {} \\; 2>/dev/null | wc -l"))
o = s:option(DummyValue, "1", translate("Subscribe Rules Data"))
o.rawhtml = true
o.template = "dnsfilter/dnsfilter_refresh"
o.value = rule_count.." "..translate("Records")
o.description = string.format(translate("AdGuardHome / Host / DNSMASQ / Domain rules auto-convert").."<br/><strong>"..translate("Last Update Checked")..":</strong> %s<br/>",UD)
end

o=s:option(Flag,"flash")
o.title=translate("Save rules to flash")
o.description=translate("Should be enabled when rules addresses are slow to download")
o.rmempty=false
o = s:option(Flag, "flash", translate("Save rules to flash"))
o.description = translate("Should be enabled when rules addresses are slow to download")
o.rmempty = false
o.default = 0

if tmp_rule==1 then
o=s:option(Button,"delete",translate("Delete All Subscribe Rules"))
o.inputstyle="reset"
o.description=translate("Delete rules files and delete the subscription link<br/>There is no need to click for modify the subscription link,The script will automatically replace the old rule file")
o.write=function()
o = s:option(Button, "delete", translate("Delete All Subscribe Rules"))
o.inputstyle = "reset"
o.description = translate("Delete rules files and delete the subscription link<br/>There is no need to click for modify the subscription link,The script will automatically replace the old rule file")
o.write = function()
SYS.exec("[ -d /etc/dnsfilter/rules ] && rm -rf /etc/dnsfilter/rules")
SYS.exec("grep -wq 'list url' /etc/config/dnsfilter && sed -i '/list url/d' /etc/config/dnsfilter && /etc/init.d/dnsfilter restart 2>&1 &")
luci.http.redirect(luci.dispatcher.build_url("admin","services","dnsfilter","base"))
Expand All @@ -63,19 +65,20 @@ end

if luci.sys.call("[ -h /tmp/dnsfilter/url ] || exit 9")==9 then
if nixio.fs.access("/etc/dnsfilter/rules") then
o=s:option(Button,"delete_1",translate("Delete Subscribe Rules On The Flash"))
o.inputstyle="reset"
o.write=function()
o = s:option(Button, "delete_1", translate("Delete Subscribe Rules On The Flash"))
o.inputstyle = "reset"
o.write = function()
SYS.exec("rm -rf /etc/dnsfilter/rules")
luci.http.redirect(luci.dispatcher.build_url("admin","services","dnsfilter","base"))
end
end
end

o=s:option(DynamicList,"url",translate("Anti-AD Rules Subscribe"))
o:value("https://cdn.jsdelivr.net/gh/privacy-protection-tools/anti-AD@master/adblock-for-dnsmasq.conf","anti-AD")
o:value("https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt","AdGuard")
o:value("https://easylist-downloads.adblockplus.org/easylistchina+easylist.txt","Easylistchina+Easylist")
o:value("https://block.energized.pro/extensions/porn-lite/formats/domains.txt","Anti-Porn")
o = s:option(DynamicList, "url", translate("Anti-AD Rules Subscribe"))
o:value("https://cdn.jsdelivr.net/gh/privacy-protection-tools/anti-AD@master/adblock-for-dnsmasq.conf", translate("anti-AD"))
o:value("https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt", translate("AdGuard"))
o:value("https://easylist-downloads.adblockplus.org/easylistchina+easylist.txt", translate("Easylistchina+Easylist"))
o:value("https://block.energized.pro/extensions/porn-lite/formats/domains.txt", translate("Anti-Porn"))
o.default = "https://cdn.jsdelivr.net/gh/privacy-protection-tools/anti-AD@master/adblock-for-dnsmasq.conf"

return m
13 changes: 7 additions & 6 deletions luasrc/model/cbi/dnsfilter/black.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
local fs=require "nixio.fs"
local conffile="/etc/dnsfilter/black.list"
local fs = require "nixio.fs"
local conffile = "/etc/dnsfilter/black.list"

f=SimpleForm("custom")
t=f:field(TextValue,"conf")
t.rmempty=true
f = SimpleForm("custom")
t = f:field(TextValue, "conf")
t.rmempty = true
t.rows=13
t.description=translate("Will Always block these Domain")
t.description = translate("Will Always block these Domain")

function t.cfgvalue()
return fs.readfile(conffile) or ""
end
Expand Down
13 changes: 7 additions & 6 deletions luasrc/model/cbi/dnsfilter/ip.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
local fs=require "nixio.fs"
local conffile="/etc/dnsfilter/ip.list"
local fs = require "nixio.fs"
local conffile = "/etc/dnsfilter/ip.list"

f=SimpleForm("custom")
t=f:field(TextValue,"conf")
t.rmempty=true
f = SimpleForm("custom")
t = f:field(TextValue, "conf")
t.rmempty = true
t.rows=13
t.description=translate("Will Always block these IP")
t.description = translate("Will Always block these IP")

function t.cfgvalue()
return fs.readfile(conffile) or ""
end
Expand Down
19 changes: 10 additions & 9 deletions luasrc/model/cbi/dnsfilter/log.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
local fs=require "nixio.fs"
local conffile="/tmp/adupdate.log"
local fs = require "nixio.fs"
local conffile = "/tmp/adupdate.log"

f = SimpleForm("logview")
f.reset = false
f.submit = false
t = f:field(TextValue, "conf")
t.rmempty = true
t.rows = 20

f=SimpleForm("logview")
f.reset=false
f.submit=false
t=f:field(TextValue,"conf")
t.rmempty=true
t.rows=20
function t.cfgvalue()
return fs.readfile(conffile) or ""
end
t.readonly="readonly"
t.readonly = "readonly"

return f
15 changes: 8 additions & 7 deletions luasrc/model/cbi/dnsfilter/white.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
local fs=require "nixio.fs"
local conffile="/etc/dnsfilter/white.list"
local fs = require "nixio.fs"
local conffile = "/etc/dnsfilter/white.list"

f = SimpleForm("custom")
t = f:field(TextValue, "conf")
t.rmempty = true
t.rows = 13
t.description = translate("Will Never filter these Domain")

f=SimpleForm("custom")
t=f:field(TextValue,"conf")
t.rmempty=true
t.rows=13
t.description=translate("Will Never filter these Domain")
function t.cfgvalue()
return fs.readfile(conffile) or ""
end
Expand Down
12 changes: 6 additions & 6 deletions po/zh-cn/dnsfilter.po
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ msgid "Enable"
msgstr "启用"

msgid "Block Apple iOS OTA update"
msgstr "拦截 Apple iOS 的OTA更新"
msgstr "拦截 Apple iOS 的OTA 更新"

msgid "Block CNshort APP and Website"
msgstr "拦截 短视频 APP 和网站"
Expand Down Expand Up @@ -77,7 +77,7 @@ msgid "Delete Subscribe Rules On The Flash"
msgstr "清空闪存上的订阅规则库"

msgid "Anti-AD Rules Subscribe"
msgstr "广告过滤规则订阅URL"
msgstr "广告过滤规则订阅 URL"

msgid "White Domain List"
msgstr "域名白名单"
Expand All @@ -92,16 +92,16 @@ msgid "Will Always block these Domain"
msgstr "拦截黑名单内的域名"

msgid "Block IP List"
msgstr "IP黑名单"
msgstr "IP 黑名单"

msgid "Will Always block these IP"
msgstr "拦截黑名单内的IP地址"
msgstr "拦截黑名单内的 IP 地址"

msgid "Safe Search"
msgstr "安全搜索"

msgid "Enforcing SafeSearch for google, bing, duckduckgo, yandex and youtube."
msgstr "强制为google,bing,duckduckgo,yandex,youtube开启安全搜索,过滤不健康内容"
msgid "Enforcing SafeSearch for Google Bing Duckduckgo Yandex and Youtube."
msgstr "强制为 Google Bing Duckduckgo Yandex Youtube 开启安全搜索,过滤不健康内容"

msgid "Update Log"
msgstr "更新日志"
2 changes: 0 additions & 2 deletions root/etc/config/dnsfilter
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ config dnsfilter
option time_update '6'
option flash '0'
option enable '0'
list url 'https://cdn.jsdelivr.net/gh/privacy-protection-tools/anti-AD@master/adblock-for-dnsmasq.conf'

0 comments on commit b764334

Please sign in to comment.