Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
kiddin9 committed Jan 22, 2021
0 parents commit 014095c
Show file tree
Hide file tree
Showing 20 changed files with 1,038 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=luci-app-dnsfilter
PKG_VERSION:=1.0
PKG_RELEASE:=5
PKG_LICENSE:=GPLv2
PKG_MAINTAINER:=small_5 GaryPang

include $(INCLUDE_DIR)/package.mk

define Package/$(PKG_NAME)
SECTION:=LuCI
CATEGORY:=LuCI
SUBMENU:=3. Applications
TITLE:=LuCI support for DNSFilter
DEPENDS:=+curl +dnsmasq-full +ipset
PKGARCH:=all
endef

define Package/$(PKG_NAME)/description
Luci Support for DNSFilter.
endef

define Build/Prepare
$(foreach po,$(wildcard ${CURDIR}/po/zh-cn/*.po), \
po2lmo $(po) $(PKG_BUILD_DIR)/$(patsubst %.po,%.lmo,$(notdir $(po)));)
endef

define Build/Compile
endef

define Package/$(PKG_NAME)/conffiles
/etc/config/dnsfilter
/etc/dnsfilter/
endef

define Package/$(PKG_NAME)/install
$(INSTALL_DIR) $(1)/usr/lib/lua/luci
cp -pR ./luasrc/* $(1)/usr/lib/lua/luci
$(INSTALL_DIR) $(1)/
cp -pR ./root/* $(1)/
$(INSTALL_DIR) $(1)/usr/lib/lua/luci/i18n
$(INSTALL_DATA) $(PKG_BUILD_DIR)/dnsfilter.*.lmo $(1)/usr/lib/lua/luci/i18n/
endef

$(eval $(call BuildPackage,$(PKG_NAME)))
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 基于DNS的广告过滤 for OpenWrt

### 支持 AdGuardHome/Host/DNSMASQ/Domain 格式的规则订阅

### 规则自动识别, 自动去重, 定时更新

### 自定义黑白名单

### 短视频APP拦截

### 安全搜索

48 changes: 48 additions & 0 deletions luasrc/controller/dnsfilter.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module("luci.controller.dnsfilter",package.seeall)
function index()
if not nixio.fs.access("/etc/config/dnsfilter") then
return
end
local e=entry({"admin","services","dnsfilter"},firstchild(),_("DNSFilter"),1)
e.dependent=false
e.acl_depends={ "luci-app-dnsfilter" }
entry({"admin","services","dnsfilter","base"},cbi("dnsfilter/base"),_("Base Setting"),1).leaf=true
entry({"admin","services","dnsfilter","white"},form("dnsfilter/white"),_("White Domain List"),2).leaf=true
entry({"admin","services","dnsfilter","black"},form("dnsfilter/black"),_("Block Domain List"),3).leaf=true
entry({"admin","services","dnsfilter","ip"},form("dnsfilter/ip"),_("Block IP List"),4).leaf=true
entry({"admin","services","dnsfilter","log"},form("dnsfilter/log"),_("Update Log"),5).leaf=true
entry({"admin","services","dnsfilter","run"},call("act_status"))
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
luci.http.prepare_content("application/json")
luci.http.write_json(e)
end

function refresh_data()
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")
if tonumber(icount)>0 then
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)))
else
retstring=0
end
luci.sys.call("echo `date +'%Y-%m-%d %H:%M:%S'` > /tmp/dnsfilter/dnsfilter.updated")
else
retstring="-1"
end
luci.sys.exec("rm -rf /tmp/ad_tmp")

luci.http.prepare_content("application/json")
luci.http.write_json({ret=retstring,retcount=icount})
end
81 changes: 81 additions & 0 deletions luasrc/model/cbi/dnsfilter/base.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
local SYS=require "luci.sys"

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

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

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

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

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

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,"cron_mode")
o.title=translate("Enable automatic update rules")

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

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/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

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()
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"))
end
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()
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://small_5.coding.net/p/adbyby/d/adbyby/git/raw/master/adguard","AdGuard")
o:value("https://small_5.coding.net/p/adbyby/d/adbyby/git/raw/master/dnsmasq.adblock","Easylistchina+Easylist")
o:value("https://block.energized.pro/extensions/porn-lite/formats/domains.txt","Porn")

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

f=SimpleForm("custom")
t=f:field(TextValue,"conf")
t.rmempty=true
t.rows=13
t.description=translate("Will Always block these Domain")
function t.cfgvalue()
return fs.readfile(conffile) or ""
end

function f.handle(self,state,data)
if state == FORM_VALID then
if data.conf then
fs.writefile(conffile,data.conf:gsub("\r\n","\n"))
else
luci.sys.call("> /etc/dnsfilter/black.list")
end
luci.sys.exec("[ \"$(uci -q get dnsfilter.@dnsfilter[0].enable)\" = 1 ] && /etc/init.d/dnsfilter restart")
end
return true
end

return f
25 changes: 25 additions & 0 deletions luasrc/model/cbi/dnsfilter/ip.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
local fs=require "nixio.fs"
local conffile="/etc/dnsfilter/ip.list"

f=SimpleForm("custom")
t=f:field(TextValue,"conf")
t.rmempty=true
t.rows=13
t.description=translate("Will Always block these IP")
function t.cfgvalue()
return fs.readfile(conffile) or ""
end

function f.handle(self,state,data)
if state == FORM_VALID then
if data.conf then
fs.writefile(conffile,data.conf:gsub("\r\n","\n"))
else
luci.sys.call("> /etc/dnsfilter/ip.list")
end
luci.sys.exec("ipset -F blockip 2>/dev/null && for i in $(cat /etc/dnsfilter/ip.list);do ipset add blockip $i 2>/dev/null;done")
end
return true
end

return f
15 changes: 15 additions & 0 deletions luasrc/model/cbi/dnsfilter/log.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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
function t.cfgvalue()
return fs.readfile(conffile) or ""
end
t.readonly="readonly"

return f
27 changes: 27 additions & 0 deletions luasrc/model/cbi/dnsfilter/white.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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")
function t.cfgvalue()
return fs.readfile(conffile) or ""
end

function f.handle(self,state,data)
if state == FORM_VALID then
if data.conf then
fs.writefile(conffile,data.conf:gsub("\r\n","\n"))
else
luci.sys.call("> /etc/dnsfilter/white.list")
end
luci.sys.exec("for i in $(cat /etc/dnsfilter/white.list);do sed -i -e \"/\\/$i\\//d\" -e \"/\\.$i\\//d\" /tmp/dnsfilter/rules.conf 2>/dev/null;\\\
[ -s /etc/dnsfilter/rules/rules.conf ] && sed -i -e \"/\\/$i\\//d\" -e \"/\\.$i\\//d\" /etc/dnsfilter/rules/rules.conf;done;\\\
[ -s /tmp/dnsfilter/rules.conf ] && rm -f /tmp/dnsmasq.dnsfilter/rules.conf && /etc/init.d/dnsfilter start")
end
return true
end

return f
21 changes: 21 additions & 0 deletions luasrc/view/dnsfilter/dnsfilter_status.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script type="text/javascript">//<![CDATA[
XHR.poll(3,'<%=url([[admin]],[[services]],[[dnsfilter]],[[run]])%>',null,
function(x,data){
var tb=document.getElementById('dnsfilter_status');
if (data&&tb){
if (data.running){
tb.innerHTML='<em><b><font color=green>DNSFilter <%:RUNNING%></font></b></em>';
}else{
tb.innerHTML='<em><b><font color=red>DNSFilter <%:NOT RUNNING%></font></b></em>';
}
}
}
);
//]]>
</script>
<style>.mar-10{margin-left:50px;margin-right:10px;}</style>
<fieldset class="cbi-section">
<p id="dnsfilter_status">
<em><%:Collecting data...%></em>
</p>
</fieldset>
34 changes: 34 additions & 0 deletions luasrc/view/dnsfilter/refresh.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<%+cbi/valueheader%>
<script type="text/javascript">//<![CDATA[
function refresh_data(btn,dataname)
{
btn.disabled=true;
btn.value='<%:Refresh...%>';
murl=dataname;
XHR.get('<%=luci.dispatcher.build_url("admin","services","dnsfilter","refresh")%>',
{set:murl},
function(x,rv)
{
var s=document.getElementById(dataname+'-status');
if (s)
{
if (rv.ret=="0")
s.innerHTML="<font color='green'>"+"<%:No new data!%> "+"</font>";
else if(rv.ret=="-1")
{
s.innerHTML="<font color='red'>"+"<%:Refresh Error!%> "+"</font>";
}
else{
s.innerHTML="<font color='green'>"+"<%:Refresh OK!%> "+"<%:Total Records:%>"+rv.ret+"</font>";
}
}
btn.disabled=false;
btn.value='<%:Refresh Data%>';
}
);
return false;
}
//]]></script>
<input type="button" class="cbi-button cbi-input-reload" value="<%:Refresh Data%>" onclick="return refresh_data(this,'<%=self.option%>')"/>
<span id="<%=self.option%>-status"><em><%=self.value%></em></span>
<%+cbi/valuefooter%>
Loading

0 comments on commit 014095c

Please sign in to comment.