manual.lua 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 (sys.call(binpath.." -c /tmp/AdGuardHometmpconfig.yaml --check-config 2> /tmp/AdGuardHometest.log")==0) then
  51. return value
  52. end
  53. luci.http.redirect(luci.dispatcher.build_url("admin","services","AdGuardHome","manual"))
  54. return nil
  55. end
  56. o.write = function(self, section, value)
  57. NXFS.move("/tmp/AdGuardHometmpconfig.yaml",escconf)
  58. io.popen("sleep 1 ;/etc/init.d/AdGuardHome reload & ;")
  59. end
  60. o.remove = function(self, section, value)
  61. NXFS.writefile(escconf, "")
  62. end
  63. o = s:option(DummyValue, "")
  64. o.anonymous=true
  65. o.template = "AdGuardHome/yamleditor"
  66. --- log
  67. if (NXFS.access("/tmp/AdGuardHometmpconfig.yaml")) then
  68. local c=NXFS.readfile("/tmp/AdGuardHometest.log")
  69. if (c~="") then
  70. o = s:option(TextValue, "")
  71. o.readonly=true
  72. o.rows = 5
  73. o.rmempty = true
  74. o.cfgvalue = function(self, section)
  75. return NXFS.readfile("/tmp/AdGuardHometest.log")
  76. end
  77. o=s:option(Button,"","")
  78. o.inputtitle=translate("Reload Config")
  79. o.write=function()
  80. NXFS.remove("/tmp/AdGuardHometmpconfig.yaml")
  81. luci.http.redirect(luci.dispatcher.build_url("admin","services","AdGuardHome","manual"))
  82. end
  83. end
  84. end
  85. return m