manual.lua 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. local m, s, o
  2. local NXFS = require "nixio.fs"
  3. local uci=require"luci.model.uci".cursor()
  4. local sys=require"luci.sys"
  5. require("io")
  6. m = Map("AdGuardHome")
  7. local escconf = uci:get("AdGuardHome","AdGuardHome","configpath")
  8. local binpath = uci:get("AdGuardHome","AdGuardHome","binpath")
  9. s = m:section(TypedSection, "AdGuardHome")
  10. s.anonymous=true
  11. s.addremove=false
  12. --- config
  13. o = s:option(TextValue, "escconf")
  14. o.rows = 66
  15. o.wrap = "off"
  16. o.rmempty = true
  17. o.cfgvalue = function(self, section)
  18. return NXFS.readfile("/tmp/AdGuardHometmpconfig.yaml") or NXFS.readfile(escconf) or ""
  19. end
  20. o.validate=function(self, value)
  21. NXFS.writefile("/tmp/AdGuardHometmpconfig.yaml", value:gsub("\r\n", "\n"))
  22. if (sys.call(binpath.." -c /tmp/AdGuardHometmpconfig.yaml --check-config 2> /tmp/AdGuardHometest.log")==0) then
  23. return value
  24. end
  25. luci.http.redirect(luci.dispatcher.build_url("admin","services","AdGuardHome","manual"))
  26. return nil
  27. end
  28. o.write = function(self, section, value)
  29. NXFS.move("/tmp/AdGuardHometmpconfig.yaml",escconf)
  30. end
  31. o.remove = function(self, section, value)
  32. NXFS.writefile(escconf, "")
  33. end
  34. o = s:option(DummyValue, "")
  35. o.anonymous=true
  36. o.template = "AdGuardHome/yamleditor"
  37. --- log
  38. if (NXFS.access("/tmp/AdGuardHometmpconfig.yaml")) then
  39. local c=NXFS.readfile("/tmp/AdGuardHometest.log")
  40. if (c~="") then
  41. o = s:option(TextValue, "")
  42. o.readonly=true
  43. o.rows = 5
  44. o.rmempty = true
  45. o.cfgvalue = function(self, section)
  46. return NXFS.readfile("/tmp/AdGuardHometest.log")
  47. end
  48. o=s:option(Button,"","")
  49. o.inputtitle=translate("Reload Config")
  50. o.write=function()
  51. NXFS.remove("/tmp/AdGuardHometmpconfig.yaml")
  52. luci.http.redirect(luci.dispatcher.build_url("admin","services","AdGuardHome","manual"))
  53. end
  54. end
  55. end
  56. local apply = luci.http.formvalue("cbi.apply")
  57. if apply then
  58. io.popen("/etc/init.d/AdGuardHome reload &")
  59. end
  60. return m