luci-app-adblock: sync with adblock 3.8.3
[oweals/luci.git] / applications / luci-app-adblock / luasrc / controller / adblock.lua
1 -- Copyright 2017-2019 Dirk Brenken (dev@brenken.org)
2 -- This is free software, licensed under the Apache License, Version 2.0
3
4 module("luci.controller.adblock", package.seeall)
5
6 local sys   = require("luci.sys")
7 local util  = require("luci.util")
8 local http  = require("luci.http")
9 local i18n  = require("luci.i18n")
10 local json  = require("luci.jsonc")
11 local uci   = require("luci.model.uci").cursor()
12
13 function index()
14         if not nixio.fs.access("/etc/config/adblock") then
15                 return
16         end
17         entry({"admin", "services", "adblock"}, firstchild(), _("Adblock"), 30).dependent = false
18         entry({"admin", "services", "adblock", "tab_from_cbi"}, cbi("adblock/overview_tab", {hideresetbtn=true, hidesavebtn=true}), _("Overview"), 10).leaf = true
19         if nixio.fs.access("/usr/sbin/tcpdump") then
20                 entry({"admin", "services", "adblock", "report"}, template("adblock/report"), _("DNS Query Report"), 20).leaf = true
21         end
22         entry({"admin", "services", "adblock", "log"}, template("adblock/logread"), _("Logfile"), 30).leaf = true
23         entry({"admin", "services", "adblock", "advanced"}, firstchild(), _("Advanced"), 100)
24         entry({"admin", "services", "adblock", "advanced", "blacklist"}, form("adblock/blacklist_tab"), _("Edit Blacklist"), 110).leaf = true
25         entry({"admin", "services", "adblock", "advanced", "whitelist"}, form("adblock/whitelist_tab"), _("Edit Whitelist"), 120).leaf = true
26         entry({"admin", "services", "adblock", "advanced", "configuration"}, form("adblock/configuration_tab"), _("Edit Configuration"), 130).leaf = true
27         entry({"admin", "services", "adblock", "advanced", "query"}, template("adblock/query"), _("Query domains"), 140).leaf = true
28         entry({"admin", "services", "adblock", "advanced", "result"}, call("queryData"), nil, 150).leaf = true
29         entry({"admin", "services", "adblock", "logread"}, call("logread"), nil).leaf = true
30         entry({"admin", "services", "adblock", "status_update"}, call("status_update"), nil).leaf = true
31         entry({"admin", "services", "adblock", "report_json"}, call("report_json"), nil).leaf = true
32         entry({"admin", "services", "adblock", "report_text"}, call("report_text"), nil).leaf = true
33         entry({"admin", "services", "adblock", "action"}, call("adb_action"), nil).leaf = true
34 end
35
36 function adb_action(name, ...)
37         local domain = select(1, ...) or ""
38         local search = select(2, ...) or "+"
39         local count  = select(3, ...) or "50"
40         local filter = select(4, ...) or "false"
41         local print  = select(5, ...) or "false"
42
43         local report_params = {
44                 search,
45                 count,
46                 filter,
47                 print
48         }
49
50         if name == "do_suspend" then
51                 luci.sys.call("/etc/init.d/adblock suspend >/dev/null 2>&1")
52         elseif name == "do_resume" then
53                 luci.sys.call("/etc/init.d/adblock resume >/dev/null 2>&1")
54         elseif name == "do_refresh" then
55                 luci.sys.call("/etc/init.d/adblock reload >/dev/null 2>&1")
56         elseif name == "do_report" then
57                 luci.sys.call("/etc/init.d/adblock report " ..table.concat(report_params, " ").. " >/dev/null 2>&1")
58                 local rep_dir  = uci:get("adblock", "extra", "adb_repdir") or "/tmp"
59                 repeat
60                         nixio.nanosleep(1)
61                 until not nixio.fs.access(rep_dir.. "/adb_report.raw")
62         elseif name == "do_filter" then
63                 luci.sys.call("/etc/init.d/adblock report " ..table.concat(report_params, " ").. " >/dev/null 2>&1")
64                 local rep_dir  = uci:get("adblock", "extra", "adb_repdir") or "/tmp"
65                 repeat
66                         nixio.nanosleep(1)
67                 until nixio.fs.access(rep_dir.. "/adb_report.final")
68         elseif name == "add_blacklist" then
69                 local file = uci:get("adblock", "blacklist", "adb_src") or "/etc/adblock/adblock.blacklist"
70                 if nixio.fs.access(file) then
71                         local blacklist = nixio.fs.readfile(file)
72                         if not string.find(blacklist, domain, 1, true)
73                         then
74                                 nixio.fs.writefile(file, blacklist.. domain.. "\n")
75                         end
76                 end
77         elseif name == "add_whitelist" then
78                 local file = uci:get("adblock", "global", "adb_whitelist") or "/etc/adblock/adblock.whitelist"
79                 if nixio.fs.access(file) then
80                         local whitelist = nixio.fs.readfile(file)
81                         if not string.find(whitelist, domain, 1, true)
82                         then
83                                 nixio.fs.writefile(file, whitelist.. domain.. "\n")
84                         end
85                 end
86         end
87         if name == "do_suspend" or name == "do_resume" or name == "do_refresh" then
88                 local pid_file = "/var/run/adblock.pid"
89                 if nixio.fs.access(pid_file) then
90                         repeat
91                                 nixio.nanosleep(1)
92                         until nixio.fs.readfile(pid_file) == ""
93                 end
94         end
95         luci.http.prepare_content("text/plain") 
96         luci.http.write("0")
97 end
98
99 function status_update()
100         local rt_file
101         local content
102
103         rt_file = uci:get("adblock", "global", "adb_rtfile") or "/tmp/adb_runtime.json"
104
105         if nixio.fs.access(rt_file) then
106                 content = json.parse(nixio.fs.readfile(rt_file) or "")
107                 http.prepare_content("application/json")
108                 http.write_json(content)
109         end
110 end
111
112 function report_json()
113         local rep_dir
114         local rep_file
115         local content
116
117         rep_dir  = uci:get("adblock", "extra", "adb_repdir") or "/tmp"
118         rep_file = rep_dir.. "/adb_report.json"
119         http.prepare_content("application/json")
120
121         if nixio.fs.access(rep_file) then
122                 content = json.parse(nixio.fs.readfile(rep_file) or "")
123                 http.write_json(content)
124         else
125                 http.write_json("{}")
126         end
127 end
128
129 function report_text()
130         local file
131         local rep_dir
132         local rep_file
133         local content
134
135         rep_dir  = uci:get("adblock", "extra", "adb_repdir") or "/tmp"
136         rep_file = rep_dir.. "/adb_report.final"
137         http.prepare_content("text/plain")
138
139         if nixio.fs.access(rep_file) then
140                 file = io.open(rep_file, "r")
141                 content = file:read("*all")
142                 file:close()
143                 http.write(content)
144         else
145                 http.write("")
146         end
147 end
148
149 function logread()
150         local content = util.trim(util.exec("logread -e 'adblock-'")) or ""
151
152         if content == "" then
153                 content = "No adblock related logs yet!"
154         end
155         http.write(content)
156 end
157
158 function queryData(domain)
159         if domain then
160                 luci.http.prepare_content("text/plain")
161                 local cmd = "/etc/init.d/adblock query %s 2>&1"
162                 local util = io.popen(cmd % util.shellquote(domain))
163                 if util then
164                         while true do
165                                 local line = util:read("*l")
166                                 if not line then
167                                         break
168                                 end
169                                 luci.http.write(line)
170                                 luci.http.write("\n")
171                         end
172                         util:close()
173                 end
174         end
175 end