manual.lua 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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("string")
  6. require("io")
  7. require("table")
  8. function gen_template_config()
  9. local b
  10. local d=""
  11. for cnt in io.lines("/tmp/resolv.conf.auto") do
  12. b=string.match (cnt,"^[^#]*nameserver%s+([^%s]+)$")
  13. if (b~=nil) then
  14. d=d.." - "..b.."\n"
  15. end
  16. end
  17. local f=io.open("/usr/share/AdGuardHome/AdGuardHome_template.yaml", "r+")
  18. local tbl = {}
  19. local a=""
  20. while (1) do
  21. a=f:read("*l")
  22. if (a=="#bootstrap_dns") then
  23. a=d
  24. elseif (a=="#upstream_dns") then
  25. a=d
  26. elseif (a==nil) then
  27. break
  28. end
  29. table.insert(tbl, a)
  30. end
  31. f:close()
  32. return table.concat(tbl, "\n")
  33. end
  34. m = Map("AdGuardHome")
  35. local escconf = uci:get("AdGuardHome","AdGuardHome","configpath")
  36. local binpath = uci:get("AdGuardHome","AdGuardHome","binpath")
  37. s = m:section(TypedSection, "AdGuardHome")
  38. s.anonymous=true
  39. s.addremove=false
  40. --- config
  41. o = s:option(TextValue, "escconf")
  42. o.rows = 66
  43. o.wrap = "off"
  44. o.rmempty = true
  45. o.cfgvalue = function(self, section)
  46. return NXFS.readfile("/tmp/AdGuardHometmpconfig.yaml") or NXFS.readfile(escconf) or gen_template_config() or ""
  47. end
  48. o.validate=function(self, value)
  49. NXFS.writefile("/tmp/AdGuardHometmpconfig.yaml", value:gsub("\r\n", "\n"))
  50. if NXFS.access(binpath) then
  51. if (sys.call(binpath.." -c /tmp/AdGuardHometmpconfig.yaml --check-config 2> /tmp/AdGuardHometest.log")==0) then
  52. return value
  53. end
  54. else
  55. return value
  56. end
  57. end
  58. o.write = function(self, section, value)
  59. NXFS.move("/tmp/AdGuardHometmpconfig.yaml",escconf)
  60. io.popen("sleep 1 ;/etc/init.d/AdGuardHome reload & ;")
  61. end
  62. o.remove = function(self, section, value)
  63. NXFS.writefile(escconf, "")
  64. end
  65. o = s:option(DummyValue, "")
  66. o.anonymous=true
  67. o.template = "AdGuardHome/yamleditor"
  68. if not NXFS.access(binpath) then
  69. o.description=translate("WARNING!!! no bin found apply config will not be test")
  70. end
  71. --- log
  72. if (NXFS.access("/tmp/AdGuardHometmpconfig.yaml")) then
  73. local c=NXFS.readfile("/tmp/AdGuardHometest.log")
  74. if (c~="") then
  75. o = s:option(TextValue, "")
  76. o.readonly=true
  77. o.rows = 5
  78. o.rmempty = true
  79. o.cfgvalue = function(self, section)
  80. return NXFS.readfile("/tmp/AdGuardHometest.log")
  81. end
  82. --- reload config
  83. o=s:option(Button,"","")
  84. o.inputtitle=translate("Reload Config")
  85. o.write=function()
  86. NXFS.remove("/tmp/AdGuardHometmpconfig.yaml")
  87. luci.http.redirect(luci.dispatcher.build_url("admin","services","AdGuardHome","manual"))
  88. end
  89. end
  90. end
  91. return m