privoxy.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. -- Copyright 2014 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 HTTP = require "luci.http"
  7. local UCI = require "luci.model.uci"
  8. local UTIL = require "luci.util"
  9. local SYS = require "luci.sys"
  10. PRIVOXY_MIN = "3.0.22-0" -- minimum version of service required
  11. function index()
  12. entry( {"admin", "services", "privoxy"}, cbi("privoxy"), _("Privoxy WEB proxy"), 59)
  13. entry( {"admin", "services", "privoxy", "logview"}, call("logread") ).leaf = true
  14. entry( {"admin", "services", "privoxy", "startstop"}, call("startstop") ).leaf = true
  15. entry( {"admin", "services", "privoxy", "status"}, call("get_pid") ).leaf = true
  16. end
  17. -- called by XHR.get from detail_logview.htm
  18. function logread()
  19. -- read application settings
  20. local uci = UCI.cursor()
  21. local logdir = uci:get("privoxy", "privoxy", "logdir") or "/var/log"
  22. local logfile = uci:get("privoxy", "privoxy", "logfile") or "privoxy.log"
  23. uci:unload("privoxy")
  24. local lfile=logdir .. "/" .. logfile
  25. local ldata=NXFS.readfile(lfile)
  26. if not ldata or #ldata == 0 then
  27. ldata="_nodata_"
  28. end
  29. HTTP.write(ldata)
  30. end
  31. -- called by XHR.get from detail_startstop.htm
  32. function startstop()
  33. local pid = get_pid(true)
  34. if pid > 0 then
  35. SYS.call("/etc/init.d/privoxy stop")
  36. NX.nanosleep(1) -- sleep a second
  37. if NX.kill(pid, 0) then -- still running
  38. NX.kill(pid, 9) -- send SIGKILL
  39. end
  40. pid = 0
  41. else
  42. SYS.call("/etc/init.d/privoxy start")
  43. NX.nanosleep(1) -- sleep a second
  44. pid = tonumber(NXFS.readfile("/var/run/privoxy.pid") or 0 )
  45. if pid > 0 and not NX.kill(pid, 0) then
  46. pid = 0 -- process did not start
  47. end
  48. end
  49. HTTP.write(tostring(pid)) -- HTTP needs string not number
  50. end
  51. -- called by XHR.poll from detail_startstop.htm
  52. -- and from lua (with parameter "true")
  53. function get_pid(from_lua)
  54. local pid = tonumber(NXFS.readfile("/var/run/privoxy.pid") or 0 )
  55. if pid > 0 and not NX.kill(pid, 0) then
  56. pid = 0
  57. end
  58. if from_lua then
  59. return pid
  60. else
  61. HTTP.write(tostring(pid)) -- HTTP needs string not number
  62. end
  63. end
  64. -- compare versions using "<=" "<" ">" ">=" "=" "<<" ">>"
  65. function ipkg_ver_compare(ver1, comp, ver2)
  66. if not ver1 or not ver2
  67. or not comp or not (#comp > 0) then return nil end
  68. -- correct compare string
  69. if comp == "<>" or comp == "><" or comp == "!=" or comp == "~=" then comp = "~="
  70. elseif comp == "<=" or comp == "<" or comp == "=<" then comp = "<="
  71. elseif comp == ">=" or comp == ">" or comp == "=>" then comp = ">="
  72. elseif comp == "=" or comp == "==" then comp = "=="
  73. elseif comp == "<<" then comp = "<"
  74. elseif comp == ">>" then comp = ">"
  75. else return nil end
  76. local av1 = UTIL.split(ver1, "[%.%-]", nil, true)
  77. local av2 = UTIL.split(ver2, "[%.%-]", nil, true)
  78. for i = 1, math.max(table.getn(av1),table.getn(av2)), 1 do
  79. local s1 = av1[i] or ""
  80. local s2 = av2[i] or ""
  81. -- first "not equal" found return true
  82. if comp == "~=" and (s1 ~= s2) then return true end
  83. -- first "lower" found return true
  84. if (comp == "<" or comp == "<=") and (s1 < s2) then return true end
  85. -- first "greater" found return true
  86. if (comp == ">" or comp == ">=") and (s1 > s2) then return true end
  87. -- not equal then return false
  88. if (s1 ~= s2) then return false end
  89. end
  90. -- all equal and not compare greater or lower then true
  91. return not (comp == "<" or comp == ">")
  92. end
  93. -- read version information for given package if installed
  94. function ipkg_ver_installed(pkg)
  95. local version = nil
  96. local control = io.open("/usr/lib/opkg/info/%s.control" % pkg, "r")
  97. if control then
  98. local ln
  99. repeat
  100. ln = control:read("*l")
  101. if ln and ln:match("^Version: ") then
  102. version = ln:gsub("^Version: ", "")
  103. break
  104. end
  105. until not ln
  106. control:close()
  107. end
  108. return version
  109. end
  110. -- replacement of build-in Flag.parse of cbi.lua
  111. -- modified to mark section as changed if value changes
  112. -- current parse did not do this, but it is done AbstaractValue.parse()
  113. function flag_parse(self, section)
  114. local fexists = self.map:formvalue(
  115. luci.cbi.FEXIST_PREFIX .. self.config .. "." .. section .. "." .. self.option)
  116. if fexists then
  117. local fvalue = self:formvalue(section) and self.enabled or self.disabled
  118. local cvalue = self:cfgvalue(section)
  119. if fvalue ~= self.default or (not self.optional and not self.rmempty) then
  120. self:write(section, fvalue)
  121. else
  122. self:remove(section)
  123. end
  124. if (fvalue ~= cvalue) then self.section.changed = true end
  125. else
  126. self:remove(section)
  127. self.section.changed = true
  128. end
  129. end