manual.lua 1.7 KB

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