Merge pull request #2384 from davidjb/patch-1
[oweals/luci.git] / applications / luci-app-adblock / luasrc / controller / adblock.lua
1 -- Copyright 2017-2018 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, domain)
35         if name == "do_suspend" then
36                 luci.sys.call("/etc/init.d/adblock suspend >/dev/null 2>&1")
37         elseif name == "do_resume" then
38                 luci.sys.call("/etc/init.d/adblock resume >/dev/null 2>&1")
39         elseif name == "do_refresh" then
40                 luci.sys.call("/etc/init.d/adblock reload >/dev/null 2>&1")
41                 local pid_file = "/var/run/adblock.pid"
42                 if nixio.fs.access(pid_file) then
43                         repeat
44                                 nixio.nanosleep(1)
45                         until nixio.fs.readfile(pid_file) == ""
46                 end
47         elseif name == "do_report" then
48                 luci.sys.call("/etc/init.d/adblock report false >/dev/null 2>&1")
49                 local rep_dir  = uci:get("adblock", "extra", "adb_repdir") or "/tmp"
50                 repeat
51                         nixio.nanosleep(1)
52                 until not nixio.fs.access(rep_dir.. "/adb_report.raw")
53         elseif name == "add_blacklist" then
54                 local file = uci:get("adblock", "blacklist", "adb_src") or "/etc/adblock/adblock.blacklist"
55                 if nixio.fs.access(file) then
56                         local blacklist = nixio.fs.readfile(file)
57                         if not string.find(blacklist, domain, 1, true)
58                         then
59                                 nixio.fs.writefile(file, blacklist.. domain.. "\n")
60                         end
61                 end
62         elseif name == "add_whitelist" then
63                 local file = uci:get("adblock", "global", "adb_whitelist") or "/etc/adblock/adblock.whitelist"
64                 if nixio.fs.access(file) then
65                         local whitelist = nixio.fs.readfile(file)
66                         if not string.find(whitelist, domain, 1, true)
67                         then
68                                 nixio.fs.writefile(file, whitelist.. domain.. "\n")
69                         end
70                 end
71         end
72         luci.http.prepare_content("text/plain") 
73         luci.http.write("0")
74 end
75
76 function status_update()
77         local rt_file
78         local content
79
80         rt_file = uci:get("adblock", "global", "adb_rtfile") or "/tmp/adb_runtime.json"
81
82         if nixio.fs.access(rt_file) then
83                 content = json.parse(nixio.fs.readfile(rt_file) or "")
84                 http.prepare_content("application/json")
85                 http.write_json(content)
86         end
87 end
88
89 function report_json()
90         local rep_dir
91         local rep_file
92         local content
93
94         rep_dir  = uci:get("adblock", "extra", "adb_repdir") or "/tmp"
95         rep_file = rep_dir.. "/adb_report.json"
96         http.prepare_content("application/json")
97
98         if nixio.fs.access(rep_file) then
99                 content = json.parse(nixio.fs.readfile(rep_file) or "")
100                 http.write_json(content)
101         else
102                 http.write_json("{}")
103         end
104 end
105
106 function report_text()
107         local file
108         local rep_dir
109         local rep_file
110         local content
111
112         rep_dir  = uci:get("adblock", "extra", "adb_repdir") or "/tmp"
113         rep_file = rep_dir.. "/adb_report"
114         http.prepare_content("text/plain")
115
116         if nixio.fs.access(rep_file) then
117                 file = io.open(rep_file, "r")
118                 content = file:read("*all")
119                 file:close()
120                 http.write(content)
121         else
122                 http.write("")
123         end
124 end
125
126 function logread()
127         local content
128
129         if nixio.fs.access("/var/log/messages") then
130                 content = util.trim(util.exec("grep -F 'adblock-' /var/log/messages"))
131         else
132                 content = util.trim(util.exec("logread -e 'adblock-'"))
133         end
134         
135         if content == "" then
136                 content = "No adblock related logs yet!"
137         end
138         http.write(content)
139 end
140
141 function queryData(domain)
142         if domain then
143                 luci.http.prepare_content("text/plain")
144                 local cmd = "/etc/init.d/adblock query %s 2>&1"
145                 local util = io.popen(cmd % util.shellquote(domain))
146                 if util then
147                         while true do
148                                 local line = util:read("*l")
149                                 if not line then
150                                         break
151                                 end
152                                 luci.http.write(line)
153                                 luci.http.write("\n")
154                         end
155                         util:close()
156                 end
157         end
158 end