luci-app-unbound: clean up presentation of advanced LuCI tabs
[project/luci.git] / applications / luci-app-unbound / luasrc / controller / unbound.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
3 -- Copyright 2017 Eric Luehrsen <ericluehrsen@hotmail.com>
4 -- Licensed to the public under the Apache License 2.0.
5
6 module("luci.controller.unbound", package.seeall)
7
8
9 function index()
10   local ucl = luci.model.uci.cursor()
11   local valexp = ucl:get_first("unbound", "unbound", "extended_luci")
12   local valman = ucl:get_first("unbound", "unbound", "manual_conf")
13
14
15   if not nixio.fs.access("/etc/config/unbound") then
16     return
17   end
18
19
20   if valexp == "1" then
21     -- Expanded View
22     entry({"admin", "services", "unbound"}, firstchild(), _("Recursive DNS")).dependent = false
23
24     -- UCI Tab(s)
25     entry({"admin", "services", "unbound", "configure"}, cbi("unbound/configure"), _("Settings"), 10)
26
27     -- Status Tab(s)
28     entry({"admin", "services", "unbound", "status"}, firstchild(), _("Status"), 20)
29     entry({"admin", "services", "unbound", "status", "syslog"}, call("QuerySysLog"), _("Log"), 50).leaf = true
30
31
32     if nixio.fs.access("/usr/sbin/unbound-control") then
33       -- Require unbound-control to execute
34       entry({"admin", "services", "unbound", "status", "statistics"}, call("QueryStatistics"), _("Statistics"), 10).leaf = true
35       entry({"admin", "services", "unbound", "status", "localdata"}, call("QueryLocalData"), _("Local Data"), 20).leaf = true
36       entry({"admin", "services", "unbound", "status", "localzone"}, call("QueryLocalZone"), _("Local Zones"), 30).leaf = true
37
38     else
39       entry({"admin", "services", "unbound", "status", "statistics"}, call("ShowEmpty"), _("Statistics"), 10).leaf = true
40     end
41
42
43     -- Raw File Tab(s)
44     entry({"admin", "services", "unbound", "files"}, firstchild(), _("Files"), 30)
45
46
47     if valman ~= "1" then
48       entry({"admin", "services", "unbound", "files", "base"}, call("ShowUnboundConf"), _("UCI: Unbound"), 10).leaf = true
49     else
50       entry({"admin", "services", "unbound", "files", "base"}, cbi("unbound/manual"), _("Edit: Unbound"), 10).leaf = true
51     end
52
53
54     entry({"admin", "services", "unbound", "files", "server"}, cbi("unbound/server"), _("Edit: Server"), 20).leaf = true
55     entry({"admin", "services", "unbound", "files", "extended"}, cbi("unbound/extended"), _("Edit: Extended"), 30).leaf = true
56
57
58     if nixio.fs.access("/var/lib/unbound/unbound_dhcp.conf") then
59       entry({"admin", "services", "unbound", "files", "dhcp"}, call("ShowDHCPConf"), _("Include: DHCP"), 40).leaf = true
60     end
61
62
63     if nixio.fs.access("/var/lib/unbound/adb_list.overall") then
64       entry({"admin", "services", "unbound", "files", "adblock"}, call("ShowAdblock"), _("Include: Adblock"), 50).leaf = true
65     end
66
67   else
68     -- Simple View to UCI only
69     entry({"admin", "services", "unbound"}, cbi("unbound/configure"), _("Recursive DNS")).dependent = false
70   end
71 end
72
73
74 function ShowEmpty()
75   local lclhead = "Unbound Control"
76   local lcldesc = luci.i18n.translate("This could display more statistics with the unbound-control package.")
77   luci.template.render("unbound/show-empty", {heading = lclhead, description = lcldesc})
78 end
79
80
81 function QuerySysLog()
82   local lclhead = "System Log"
83   local lcldata = luci.util.exec("logread | grep -i unbound")
84   local lcldesc = luci.i18n.translate("This shows syslog filtered for events involving Unbound.")
85   luci.template.render("unbound/show-textbox", {heading = lclhead, description = lcldesc, content = lcldata})
86 end
87
88
89 function QueryStatistics()
90   local lclhead = "Unbound Control Stats"
91   local lcldata = luci.util.exec("unbound-control -c /var/lib/unbound/unbound.conf stats_noreset")
92   local lcldesc = luci.i18n.translate("This shows some performance statistics tracked by Unbound.")
93   luci.template.render("unbound/show-textbox", {heading = lclhead, description = lcldesc, content = lcldata})
94 end
95
96
97 function QueryLocalData()
98   local lclhead = "Unbound Control Local Data"
99   local lcldata = luci.util.exec("unbound-control -c /var/lib/unbound/unbound.conf list_local_data")
100   local lcldesc = luci.i18n.translate("This shows local host records that shortcut recursion.")
101   luci.template.render("unbound/show-textbox", {heading = lclhead, description = lcldesc, content = lcldata})
102 end
103
104
105 function QueryLocalZone()
106   local lclhead = "Unbound Control Local Zones"
107   local lcldata = luci.util.exec("unbound-control -c /var/lib/unbound/unbound.conf list_local_zones")
108   local lcldesc = luci.i18n.translate("This shows local zone definitions that affect recursion routing or processing. ")
109   luci.template.render("unbound/show-textbox", {heading = lclhead, description = lcldesc, content = lcldata})
110 end
111
112
113 function ShowUnboundConf()
114   local unboundfile = "/var/lib/unbound/unbound.conf"
115   local lclhead = "Unbound Conf"
116   local lcldata = nixio.fs.readfile(unboundfile)
117   local lcldesc = luci.i18n.translate("This shows configuration generated by UCI:")
118   lcldesc = lcldesc .. " (" .. unboundfile .. ")"
119   luci.template.render("unbound/show-textbox", {heading = lclhead, description = lcldesc, content = lcldata})
120 end
121
122
123 function ShowDHCPConf()
124   local dhcpfile = "/var/lib/unbound/unbound_dhcp.conf"
125   local lclhead = "DHCP Conf"
126   local lcldata = nixio.fs.readfile(dhcpfile)
127   local lcldesc = luci.i18n.translate("This shows LAN hosts added by DHCP hook scripts:")
128   lcldesc = lcldesc .. " (" .. dhcpfile .. ")"
129   luci.template.render("unbound/show-textbox", {heading = lclhead, description = lcldesc, content = lcldata})
130 end
131
132
133 function ShowAdblock()
134   local adblockfile = "/var/lib/unbound/adb_list.overall"
135   local lclhead = "Adblock Conf"
136   local lcldata, lcldesc
137
138
139   if nixio.fs.stat(adblockfile).size > 262144 then
140     lcldesc = luci.i18n.translate("Adblock domain list is too large for LuCI:")
141     lcldesc = lcldesc .. " (" .. adblockfile .. ")"
142     luci.template.render("unbound/show-empty", {heading = lclhead, description = lcldesc})
143
144   else
145     lcldata = nixio.fs.readfile(adblockfile)
146     lcldesc = luci.i18n.translate("This shows blocked domains provided by Adblock scripts:")
147     lcldesc = lcldesc .. " (" .. adblockfile .. ")"
148     luci.template.render("unbound/show-textbox", {heading = lclhead, description = lcldesc, content = lcldata})
149   end
150 end
151