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