1
0

privoxy.lua 8.4 KB

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