You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.1 KiB

  1. --[[
  2. LuCI - Lua Configuration Interface
  3. Copyright 2014 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. $Id$
  9. ]]--
  10. module("luci.controller.privoxy", package.seeall)
  11. local NX = require "nixio"
  12. local NXFS = require "nixio.fs"
  13. local HTTP = require "luci.http"
  14. local IPKG = require "luci.model.ipkg"
  15. local UCI = require "luci.model.uci"
  16. local SYS = require "luci.sys"
  17. local UTIL = require "luci.util"
  18. version_luci_app = "1.0.1" -- luci-application / openwrt Makefile compatible version
  19. version_required = "3.0.22-1" -- minimum required service version
  20. function index()
  21. local _sys = require "luci.sys"
  22. local _vermin = "3.0.22-1"
  23. local _verinst = _sys.exec([[opkg list-installed ]] .. "privoxy" .. [[ | awk '{print $3}']])
  24. local _cmd = [[opkg compare-versions "]] .. _verinst .. [[" ">=" "]] .. _vermin .. [["]]
  25. local _verok = tonumber(_sys.call(_cmd))
  26. -- check config file and version
  27. if not nixio.fs.access("/etc/config/privoxy") or (_verok == 0) then
  28. entry( {"admin", "services", "privoxy"}, cbi("privoxy/apperror",
  29. {hideapplybtn=true, hidesavebtn=true, hideresetbtn=true }), _("Privoxy WEB proxy"), 59)
  30. else
  31. entry( {"admin", "services", "privoxy"}, cbi("privoxy/detail"), _("Privoxy WEB proxy"), 59)
  32. entry( {"admin", "services", "privoxy", "logview"}, call("logread") ).leaf = true
  33. entry( {"admin", "services", "privoxy", "startstop"}, call("startstop") ).leaf = true
  34. entry( {"admin", "services", "privoxy", "status"}, call("get_pid") ).leaf = true
  35. end
  36. end
  37. -- called by XHR.get from detail_logview.htm
  38. function logread()
  39. -- read application settings
  40. local uci = UCI.cursor()
  41. local logdir = uci:get("privoxy", "privoxy", "logdir") or "/var/log"
  42. local logfile = uci:get("privoxy", "privoxy", "logfile") or "privoxy.log"
  43. uci:unload("privoxy")
  44. local lfile=logdir .. "/" .. logfile
  45. local ldata=NXFS.readfile(lfile)
  46. if not ldata or #ldata == 0 then
  47. ldata="_nodata_"
  48. end
  49. HTTP.write(ldata)
  50. end
  51. -- called by XHR.get from detail_startstop.htm
  52. function startstop()
  53. local pid = get_pid(true)
  54. if pid > 0 then
  55. SYS.call("/etc/init.d/privoxy stop")
  56. NX.nanosleep(1) -- sleep a second
  57. if NX.kill(pid, 0) then -- still running
  58. NX.kill(pid, 9) -- send SIGKILL
  59. end
  60. pid = 0
  61. else
  62. SYS.call("/etc/init.d/privoxy start")
  63. NX.nanosleep(1) -- sleep a second
  64. pid = tonumber(NXFS.readfile("/var/run/privoxy.pid") or 0 )
  65. if pid > 0 and not NX.kill(pid, 0) then
  66. pid = 0 -- process did not start
  67. end
  68. end
  69. HTTP.write(tostring(pid)) -- HTTP needs string not number
  70. end
  71. -- called by XHR.poll from detail_startstop.htm
  72. -- and from lua (with parameter "true")
  73. function get_pid(from_lua)
  74. local pid = tonumber(NXFS.readfile("/var/run/privoxy.pid") or 0 )
  75. if pid > 0 and not NX.kill(pid, 0) then
  76. pid = 0
  77. end
  78. if from_lua then
  79. return pid
  80. else
  81. HTTP.write(tostring(pid)) -- HTTP needs string not number
  82. end
  83. end
  84. -- get the "name" of the current active theme
  85. function get_theme()
  86. local _uci = UCI.cursor()
  87. local _base = _uci:get("luci", "main", "mediaurlbase") -- only pathname
  88. _uci:unload("luci")
  89. for k, v in pairs(luci.config.themes) do
  90. if k:sub(1, 1) ~= "." and v == _base then
  91. return k
  92. end
  93. end
  94. return nil
  95. end
  96. -- replacement of build-in Flag.parse of cbi.lua
  97. -- modified to mark section as changed if value changes
  98. -- current parse did not do this, but it is done AbstaractValue.parse()
  99. function flag_parse(self, section)
  100. local fexists = self.map:formvalue(
  101. luci.cbi.FEXIST_PREFIX .. self.config .. "." .. section .. "." .. self.option)
  102. if fexists then
  103. local fvalue = self:formvalue(section) and self.enabled or self.disabled
  104. local cvalue = self:cfgvalue(section)
  105. if fvalue ~= self.default or (not self.optional and not self.rmempty) then
  106. self:write(section, fvalue)
  107. else
  108. self:remove(section)
  109. end
  110. if (fvalue ~= cvalue) then self.section.changed = true end
  111. else
  112. self:remove(section)
  113. self.section.changed = true
  114. end
  115. end