manual.lua 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. local m, s, o
  2. local fs = 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 configpath = 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 fs.readfile("/tmp/AdGuardHometmpconfig.yaml") or fs.readfile(configpath) or gen_template_config() or ""
  47. end
  48. o.validate=function(self, value)
  49. fs.writefile("/tmp/AdGuardHometmpconfig.yaml", value:gsub("\r\n", "\n"))
  50. if fs.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. luci.http.redirect(luci.dispatcher.build_url("admin","services","AdGuardHome","manual"))
  58. return nil
  59. end
  60. o.write = function(self, section, value)
  61. fs.move("/tmp/AdGuardHometmpconfig.yaml",configpath)
  62. end
  63. o.remove = function(self, section, value)
  64. fs.writefile(configpath, "")
  65. end
  66. --- js and reload button
  67. o = s:option(DummyValue, "")
  68. o.anonymous=true
  69. o.template = "AdGuardHome/yamleditor"
  70. if not fs.access(binpath) then
  71. o.description=translate("WARNING!!! no bin found apply config will not be test")
  72. end
  73. --- log
  74. if (fs.access("/tmp/AdGuardHometmpconfig.yaml")) then
  75. local c=fs.readfile("/tmp/AdGuardHometest.log")
  76. if (c~="") then
  77. o = s:option(TextValue, "")
  78. o.readonly=true
  79. o.rows = 5
  80. o.rmempty = true
  81. o.name=""
  82. o.cfgvalue = function(self, section)
  83. return fs.readfile("/tmp/AdGuardHometest.log")
  84. end
  85. end
  86. end
  87. function m.on_commit(map)
  88. local ucitracktest=uci:get("AdGuardHome","AdGuardHome","ucitracktest")
  89. if ucitracktest=="1" then
  90. return
  91. elseif ucitracktest=="0" then
  92. io.popen("/etc/init.d/AdGuardHome reload &")
  93. else
  94. fs.writefile("/var/run/AdGlucitest","")
  95. end
  96. end
  97. return m