1 -- Copyright 2014-2016 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
2 -- Licensed under the Apache License, Version 2.0
4 module("luci.controller.privoxy", package.seeall)
6 local NX = require "nixio"
7 local NXFS = require "nixio.fs"
8 local DISP = require "luci.dispatcher"
9 local HTTP = require "luci.http"
10 local I18N = require "luci.i18n" -- not globally avalible here
11 local IPKG = require "luci.model.ipkg"
12 local UCI = require "luci.model.uci"
13 local UTIL = require "luci.util"
14 local SYS = require "luci.sys"
16 local srv_name = "privoxy"
17 local srv_ver_min = "3.0.23" -- minimum version of service required
18 local srv_ver_cmd = [[/usr/sbin/privoxy --version | awk {'print $3'}]]
19 local app_name = "luci-app-privoxy"
20 local app_title = "Privoxy WEB proxy"
21 local app_version = "1.0.6-1"
24 entry( {"admin", "services", "privoxy"}, cbi("privoxy"), _("Privoxy WEB proxy"), 59)
25 entry( {"admin", "services", "privoxy", "logview"}, call("logread") ).leaf = true
26 entry( {"admin", "services", "privoxy", "startstop"}, post("startstop") ).leaf = true
27 entry( {"admin", "services", "privoxy", "status"}, call("get_pid") ).leaf = true
30 -- Application specific information functions
31 function app_description()
32 return I18N.translate("Privoxy is a non-caching web proxy with advanced filtering "
33 .. "capabilities for enhancing privacy, modifying web page data and HTTP headers, "
34 .. "controlling access, and removing ads and other obnoxious Internet junk.")
36 .. I18N.translate("For help use link at the relevant option")
40 -- Standardized application/service functions
41 function app_title_main()
42 return [[<a href="javascript:alert(']]
43 .. I18N.translate("Version Information")
44 .. [[\n\n]] .. app_name
45 .. [[\n\t]] .. I18N.translate("Version") .. [[:\t]] .. app_version
46 .. [[\n\n]] .. srv_name .. [[ ]] .. I18N.translate("required") .. [[:]]
47 .. [[\n\t]] .. I18N.translate("Version") .. [[:\t]]
48 .. srv_ver_min .. [[ ]] .. I18N.translate("or higher")
49 .. [[\n\n]] .. srv_name .. [[ ]] .. I18N.translate("installed") .. [[:]]
50 .. [[\n\t]] .. I18N.translate("Version") .. [[:\t]]
51 .. (service_version() or I18N.translate("NOT installed"))
54 .. I18N.translate(app_title)
57 function service_version()
59 IPKG.list_installed(srv_name, function(n, v, d)
60 if v and (#v > 0) then ver = v end
63 if not ver or (#ver == 0) then
64 ver = UTIL.exec(srv_ver_cmd)
65 if #ver == 0 then ver = nil end
70 return IPKG.compare_versions((service_version() or "0"), ">=", srv_ver_min)
72 function service_update()
73 local url = DISP.build_url("admin", "system", "packages")
74 if not service_version() then
75 return [[<h3><strong><br /><font color="red"> ]]
76 .. I18N.translate("Software package '%s' is not installed." % srv_name)
77 .. [[</font><br /><br /> ]]
78 .. I18N.translate("required") .. [[: ]] .. srv_name .. [[ ]] .. srv_ver_min .. " " .. I18N.translate("or higher")
79 .. [[<br /><br /> ]]
80 .. [[<a href="]] .. url ..[[">]]
81 .. I18N.translate("Please install current version !")
82 .. [[</a><br /> </strong></h3>]]
84 return [[<h3><strong><br /><br /><font color="red"> ]]
85 .. I18N.translate("Software package '%s' is outdated." % srv_name)
86 .. [[</font><br /><br /> ]]
87 .. I18N.translate("installed") .. ": " .. service_version()
88 .. [[<br /><br /> ]]
89 .. I18N.translate("required") .. ": " .. srv_ver_min .. " " .. I18N.translate("or higher")
90 .. [[<br /><br /> ]]
91 .. [[<a href="]] .. url ..[[">]]
92 .. I18N.translate("Please update to the current version!")
93 .. [[</a><br /><br /> </strong></h3>]]
97 -- called by XHR.get from detail_logview.htm
99 -- read application settings
100 local uci = UCI.cursor()
101 local logdir = uci:get("privoxy", "privoxy", "logdir") or "/var/log"
102 local logfile = uci:get("privoxy", "privoxy", "logfile") or "privoxy.log"
103 uci:unload("privoxy")
105 local ldata=NXFS.readfile(logdir .. "/" .. logfile)
106 if not ldata or #ldata == 0 then
112 -- called by XHR.get from detail_startstop.htm
114 local pid = get_pid(true)
116 SYS.call("/etc/init.d/privoxy stop")
117 NX.nanosleep(1) -- sleep a second
118 if NX.kill(pid, 0) then -- still running
119 NX.kill(pid, 9) -- send SIGKILL
123 SYS.call("/etc/init.d/privoxy start")
124 NX.nanosleep(1) -- sleep a second
125 pid = tonumber(NXFS.readfile("/var/run/privoxy.pid") or 0 )
126 if pid > 0 and not NX.kill(pid, 0) then
127 pid = 0 -- process did not start
130 HTTP.write(tostring(pid)) -- HTTP needs string not number
133 -- called by XHR.poll from detail_startstop.htm
134 -- and from lua (with parameter "true")
135 function get_pid(from_lua)
136 local pid = tonumber(NXFS.readfile("/var/run/privoxy.pid") or 0 )
137 if pid > 0 and not NX.kill(pid, 0) then
143 HTTP.write(tostring(pid)) -- HTTP needs string not number
147 -- replacement of build-in parse of UCI option
148 -- modified AbstractValue.parse(self, section, novld) from cbi.lua
149 -- validate is called if rmempty/optional true or false
150 -- write is called if rmempty/optional true or false
151 function value_parse(self, section, novld)
152 local fvalue = self:formvalue(section)
153 local fexist = ( fvalue and (#fvalue > 0) ) -- not "nil" and "not empty"
154 local cvalue = self:cfgvalue(section)
155 local rm_opt = ( self.rmempty or self.optional )
156 local eq_cfg -- flag: equal cfgvalue
158 -- If favlue and cvalue are both tables and have the same content
159 -- make them identical
160 if type(fvalue) == "table" and type(cvalue) == "table" then
161 eq_cfg = (#fvalue == #cvalue)
164 if cvalue[i] ~= fvalue[i] then
174 -- removed parameter "section" from function call because used/accepted nowhere
175 -- also removed call to function "transfer"
176 local vvalue, errtxt = self:validate(fvalue)
178 -- error handling; validate return "nil"
180 if novld then -- and "novld" set
181 return -- then exit without raising an error
184 if fexist then -- and there is a formvalue
185 self:add_error(section, "invalid", errtxt or self.title .. ": invalid")
186 return -- so data are invalid
187 elseif not rm_opt then -- and empty formvalue but NOT (rmempty or optional) set
188 self:add_error(section, "missing", errtxt or self.title .. ": missing")
189 return -- so data is missing
191 self:add_error(section, "invalid", errtxt)
194 -- error ("\n option: " .. self.option ..
195 -- "\n fvalue: " .. tostring(fvalue) ..
196 -- "\n fexist: " .. tostring(fexist) ..
197 -- "\n cvalue: " .. tostring(cvalue) ..
198 -- "\n vvalue: " .. tostring(vvalue) ..
199 -- "\n vexist: " .. tostring(vexist) ..
200 -- "\n rm_opt: " .. tostring(rm_opt) ..
201 -- "\n eq_cfg: " .. tostring(eq_cfg) ..
202 -- "\n eq_def: " .. tostring(eq_def) ..
203 -- "\n novld : " .. tostring(novld) ..
204 -- "\n errtxt: " .. tostring(errtxt) )
207 -- lets continue with value returned from validate
208 eq_cfg = ( vvalue == cvalue ) -- update equal_config flag
209 local vexist = ( vvalue and (#vvalue > 0) ) and true or false -- not "nil" and "not empty"
210 local eq_def = ( vvalue == self.default ) -- equal_default flag
212 -- (rmempty or optional) and (no data or equal_default)
213 if rm_opt and (not vexist or eq_def) then
214 if self:remove(section) then -- remove data from UCI
215 self.section.changed = true -- and push events
220 -- not forcewrite and no changes, so nothing to write
221 if not self.forcewrite and eq_cfg then
225 -- we should have a valid value here
226 assert (vvalue, "\n option: " .. self.option ..
227 "\n fvalue: " .. tostring(fvalue) ..
228 "\n fexist: " .. tostring(fexist) ..
229 "\n cvalue: " .. tostring(cvalue) ..
230 "\n vvalue: " .. tostring(vvalue) ..
231 "\n vexist: " .. tostring(vexist) ..
232 "\n rm_opt: " .. tostring(rm_opt) ..
233 "\n eq_cfg: " .. tostring(eq_cfg) ..
234 "\n eq_def: " .. tostring(eq_def) ..
235 "\n errtxt: " .. tostring(errtxt) )
237 -- write data to UCI; raise event only on changes
238 if self:write(section, vvalue) and not eq_cfg then
239 self.section.changed = true