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.

174 lines
5.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.0" -- luci-application / openwrt Makefile compatible version
  19. version_required = "3.0.22" -- minimum required service version
  20. function index()
  21. local _service = "privoxy"
  22. local _vermin = "3.0.22"
  23. local _verinst
  24. local _util = require "luci.util"
  25. local _ipkg = require "luci.model.ipkg"
  26. local _info = _ipkg.info(_service)
  27. for _, v in pairs(_info) do
  28. if v.Package == _service and v.Status.installed then
  29. _verinst = v.Version
  30. break
  31. end
  32. end
  33. local _sver = _util.split(_verinst, "[%.%-]", nil, true)
  34. local _rver = _util.split(_vermin , "[%.%-]", nil, true)
  35. -- check config file and version
  36. if not nixio.fs.access("/etc/config/" .. _service )
  37. or (tonumber(_sver[1]) or 0) < (tonumber(_rver[1]) or 0)
  38. or (tonumber(_sver[2]) or 0) < (tonumber(_rver[2]) or 0)
  39. or (tonumber(_sver[3]) or 0) < (tonumber(_rver[3]) or 0)
  40. or (tonumber(_sver[4]) or 0) < (tonumber(_rver[4]) or 0) then
  41. entry( {"admin", "services", "privoxy"}, cbi("privoxy/apperror",
  42. {hideapplybtn=true, hidesavebtn=true, hideresetbtn=true }), _("Privoxy WEB proxy"), 59)
  43. else
  44. entry( {"admin", "services", "privoxy"}, cbi("privoxy/detail"), _("Privoxy WEB proxy"), 59)
  45. entry( {"admin", "services", "privoxy", "logview"}, call("logread") ).leaf = true
  46. entry( {"admin", "services", "privoxy", "startstop"}, call("startstop") ).leaf = true
  47. entry( {"admin", "services", "privoxy", "status"}, call("get_pid") ).leaf = true
  48. end
  49. end
  50. -- called by XHR.get from detail_logview.htm
  51. function logread()
  52. -- read application settings
  53. local uci = UCI.cursor()
  54. local logdir = uci:get("privoxy", "privoxy", "logdir") or "/var/log"
  55. local logfile = uci:get("privoxy", "privoxy", "logfile") or "privoxy.log"
  56. uci:unload("privoxy")
  57. local lfile=logdir .. "/" .. logfile
  58. local ldata=NXFS.readfile(lfile)
  59. if not ldata or #ldata == 0 then
  60. ldata="_nodata_"
  61. end
  62. HTTP.write(ldata)
  63. end
  64. -- called by XHR.get from detail_startstop.htm
  65. function startstop()
  66. local pid = get_pid(true)
  67. if pid > 0 then
  68. SYS.call("/etc/init.d/privoxy stop")
  69. NX.nanosleep(1) -- sleep a second
  70. if NX.kill(pid, 0) then -- still running
  71. NX.kill(pid, 9) -- send SIGKILL
  72. end
  73. pid = 0
  74. else
  75. SYS.call("/etc/init.d/privoxy start")
  76. NX.nanosleep(1) -- sleep a second
  77. pid = tonumber(NXFS.readfile("/var/run/privoxy.pid") or 0 )
  78. if pid > 0 and not NX.kill(pid, 0) then
  79. pid = 0 -- process did not start
  80. end
  81. end
  82. HTTP.write(tostring(pid)) -- HTTP needs string not number
  83. end
  84. -- called by XHR.poll from detail_startstop.htm
  85. -- and from lua (with parameter "true")
  86. function get_pid(from_lua)
  87. local pid = tonumber(NXFS.readfile("/var/run/privoxy.pid") or 0 )
  88. if pid > 0 and not NX.kill(pid, 0) then
  89. pid = 0
  90. end
  91. if from_lua then
  92. return pid
  93. else
  94. HTTP.write(tostring(pid)) -- HTTP needs string not number
  95. end
  96. end
  97. -- get the "name" of the current active theme
  98. function get_theme()
  99. local _uci = UCI.cursor()
  100. local _base = _uci:get("luci", "main", "mediaurlbase") -- only pathname
  101. _uci:unload("luci")
  102. for k, v in pairs(luci.config.themes) do
  103. if k:sub(1, 1) ~= "." and v == _base then
  104. return k
  105. end
  106. end
  107. return nil
  108. end
  109. -- read version information for given package if installed
  110. function ipkg_version(package)
  111. if not package then
  112. return nil
  113. end
  114. local _info = IPKG.info(package)
  115. local _data = {}
  116. local _version = ""
  117. local i = 0
  118. for k, v in pairs(_info) do
  119. if v.Package == package and v.Status.installed then
  120. _version = v.Version
  121. i = i + 1
  122. end
  123. end
  124. if i > 1 then -- more then one valid record
  125. return _data
  126. end
  127. local _sver = UTIL.split(_version, "[%.%-]", nil, true)
  128. _data = {
  129. version = _version,
  130. major = tonumber(_sver[1]) or 0,
  131. minor = tonumber(_sver[2]) or 0,
  132. patch = tonumber(_sver[3]) or 0,
  133. build = tonumber(_sver[4]) or 0
  134. }
  135. return _data
  136. end
  137. -- replacement of build-in Flag.parse of cbi.lua
  138. -- modified to mark section as changed if value changes
  139. -- current parse did not do this, but it is done AbstaractValue.parse()
  140. function flag_parse(self, section)
  141. local fexists = self.map:formvalue(
  142. luci.cbi.FEXIST_PREFIX .. self.config .. "." .. section .. "." .. self.option)
  143. if fexists then
  144. local fvalue = self:formvalue(section) and self.enabled or self.disabled
  145. local cvalue = self:cfgvalue(section)
  146. if fvalue ~= self.default or (not self.optional and not self.rmempty) then
  147. self:write(section, fvalue)
  148. else
  149. self:remove(section)
  150. end
  151. if (fvalue ~= cvalue) then self.section.changed = true end
  152. else
  153. self:remove(section)
  154. self.section.changed = true
  155. end
  156. end