Merge pull request #680 from cshore/pull-request-dnsmasq-options
[project/luci.git] / applications / luci-app-ddns / luasrc / model / cbi / ddns / overview.lua
1 -- Copyright 2014-2016 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local NXFS = require "nixio.fs"
5 local DISP = require "luci.dispatcher"
6 local HTTP = require "luci.http"
7 local SYS  = require "luci.sys"
8 local CTRL = require "luci.controller.ddns"     -- this application's controller
9 local DDNS = require "luci.tools.ddns"          -- ddns multiused functions
10
11 local show_hints = not (DDNS.has_ipv6           -- IPv6 support
12                     and DDNS.has_ssl            -- HTTPS support
13                     and DDNS.has_proxy          -- Proxy support
14                     and DDNS.has_bindhost       -- DNS TCP support
15                     and DDNS.has_forceip        -- Force IP version
16                     and DDNS.has_dnsserver      -- DNS server support
17                     and DDNS.has_bindnet        -- Bind to network/interface
18                     and DDNS.has_cacerts        -- certificates installed at /etc/ssl/certs
19                 )
20 local not_enabled = not SYS.init.enabled("ddns")
21 local need_update = not CTRL.service_ok()
22
23 -- html constants
24 font_red = [[<font color="red">]]
25 font_off = [[</font>]]
26 bold_on  = [[<strong>]]
27 bold_off = [[</strong>]]
28
29 -- cbi-map definition -- #######################################################
30 m = Map("ddns")
31 m.title         = CTRL.app_title_main()
32 m.description   = CTRL.app_description()
33
34 m.on_after_commit = function(self)
35         if self.changed then    -- changes ?
36                 if SYS.init.enabled("ddns") then        -- ddns service enabled, restart all
37                         os.execute("/etc/init.d/ddns restart")
38                 else    -- ddns service disabled, send SIGHUP to running
39                         os.execute("killall -1 dynamic_dns_updater.sh")
40                 end
41         end
42 end
43
44 -- SimpleSection definition -- ##################################################
45 -- with all the JavaScripts we need for "a good Show"
46 a = m:section( SimpleSection )
47 a.template = "ddns/overview_status"
48
49 -- SimpleSection definition -- #################################################
50 -- show Hints to optimize installation and script usage
51 if show_hints or need_update or not_enabled then
52
53         s = m:section( SimpleSection, translate("Hints") )
54
55         -- ddns_scripts needs to be updated for full functionality
56         if need_update then
57                 local dv = s:option(DummyValue, "_update_needed")
58                 dv.titleref = DISP.build_url("admin", "system", "packages")
59                 dv.rawhtml  = true
60                 dv.title = font_red .. bold_on ..
61                         translate("Software update required") .. bold_off .. font_off
62                 dv.value = translate("The currently installed 'ddns-scripts' package did not support all available settings.") ..
63                                 "<br />" ..
64                                 translate("Please update to the current version!")
65         end
66
67         -- DDNS Service disabled
68         if not_enabled then
69                 local dv = s:option(DummyValue, "_not_enabled")
70                 dv.titleref = DISP.build_url("admin", "system", "startup")
71                 dv.rawhtml  = true
72                 dv.title = bold_on ..
73                         translate("DDNS Autostart disabled") .. bold_off
74                 dv.value = translate("Currently DDNS updates are not started at boot or on interface events." .. "<br />" ..
75                                 "You can start/stop each configuration here. It will run until next reboot.")
76         end
77
78         -- Show more hints on a separate page
79         if show_hints then
80                 local dv = s:option(DummyValue, "_separate")
81                 dv.titleref = DISP.build_url("admin", "services", "ddns", "hints")
82                 dv.rawhtml  = true
83                 dv.title = bold_on ..
84                         translate("Show more") .. bold_off
85                 dv.value = translate("Follow this link" .. "<br />" ..
86                                 "You will find more hints to optimize your system to run DDNS scripts with all options")
87         end
88 end
89
90 -- TableSection definition -- ##################################################
91 ts = m:section( TypedSection, "service",
92         translate("Overview"),
93         translate("Below is a list of configured DDNS configurations and their current state.")
94         .. "<br />"
95         .. translate("If you want to send updates for IPv4 and IPv6 you need to define two separate Configurations "
96                 .. "i.e. 'myddns_ipv4' and 'myddns_ipv6'")
97         .. "<br />"
98         .. [[<a href="]] .. DISP.build_url("admin", "services", "ddns", "global") .. [[">]]
99         .. translate("To change global settings click here") .. [[</a>]] )
100 ts.sectionhead = translate("Configuration")
101 ts.template = "cbi/tblsection"
102 ts.addremove = true
103 ts.extedit = DISP.build_url("admin", "services", "ddns", "detail", "%s")
104 function ts.create(self, name)
105         AbstractSection.create(self, name)
106         HTTP.redirect( self.extedit:format(name) )
107 end
108
109 -- Lookup_Host and registered IP -- #################################################
110 dom = ts:option(DummyValue, "_lookupIP",
111         translate("Lookup Hostname") .. "<br />" .. translate("Registered IP") )
112 dom.template = "ddns/overview_doubleline"
113 function dom.set_one(self, section)
114         local lookup = self.map:get(section, "lookup_host") or ""
115         if lookup ~= "" then
116                 return lookup
117         else
118                 return [[<em>]] .. translate("config error") .. [[</em>]]
119         end
120 end
121 function dom.set_two(self, section)
122         local lookup = self.map:get(section, "lookup_host") or ""
123         if lookup == "" then return "" end
124         local dnsserver = self.map:get(section, "dnsserver") or ""
125         local use_ipv6 = tonumber(self.map:get(section, "use_ipv6") or 0)
126         local force_ipversion = tonumber(self.map:get(section, "force_ipversion") or 0)
127         local force_dnstcp = tonumber(self.map:get(section, "force_dnstcp") or 0)
128         local command = [[/usr/lib/ddns/dynamic_dns_lucihelper.sh]]
129         if not NXFS.access(command, "rwx", "rx", "rx") then
130                 NXFS.chmod(command, 755)
131         end
132         command = command .. [[ get_registered_ip ]] .. lookup .. [[ ]] .. use_ipv6 ..
133                 [[ ]] .. force_ipversion .. [[ ]] .. force_dnstcp .. [[ ]] .. dnsserver
134         local ip = SYS.exec(command)
135         if ip == "" then ip = translate("no data") end
136         return ip
137 end
138
139 -- enabled
140 ena = ts:option( Flag, "enabled",
141         translate("Enabled"))
142 ena.template = "ddns/overview_enabled"
143 ena.rmempty = false
144
145 -- show PID and next update
146 upd = ts:option( DummyValue, "_update",
147         translate("Last Update") .. "<br />" .. translate("Next Update"))
148 upd.template = "ddns/overview_doubleline"
149 function upd.set_one(self, section)     -- fill Last Update
150         -- get/validate last update
151         local uptime   = SYS.uptime()
152         local lasttime = DDNS.get_lastupd(section)
153         if lasttime > uptime then       -- /var might not be linked to /tmp and cleared on reboot
154                 lasttime = 0
155         end
156
157         -- no last update happen
158         if lasttime == 0 then
159                 return translate("never")
160
161         -- we read last update
162         else
163                 -- calc last update
164                 --            os.epoch  - sys.uptime + lastupdate(uptime)
165                 local epoch = os.time() - uptime + lasttime
166                 -- use linux date to convert epoch
167                 return DDNS.epoch2date(epoch)
168         end
169 end
170 function upd.set_two(self, section)     -- fill Next Update
171         -- get enabled state
172         local enabled   = tonumber(self.map:get(section, "enabled") or 0)
173         local datenext  = translate("unknown error")    -- formatted date of next update
174
175         -- get force seconds
176         local force_interval = tonumber(self.map:get(section, "force_interval") or 72)
177         local force_unit = self.map:get(section, "force_unit") or "hours"
178         local force_seconds = DDNS.calc_seconds(force_interval, force_unit)
179
180         -- get last update and get/validate PID
181         local uptime   = SYS.uptime()
182         local lasttime = DDNS.get_lastupd(section)
183         if lasttime > uptime then       -- /var might not be linked to /tmp and cleared on reboot
184                 lasttime = 0
185         end
186         local pid      = DDNS.get_pid(section)
187
188         -- calc next update
189         if lasttime > 0 then
190                 local epoch = os.time() - uptime + lasttime + force_seconds
191                 -- use linux date to convert epoch
192                 datelast = DDNS.epoch2date(epoch)
193         end
194
195         -- process running but update needs to happen
196         if pid > 0 and ( lasttime + force_seconds - uptime ) < 0 then
197                 datenext = translate("Verify")
198
199         -- run once
200         elseif force_seconds == 0 then
201                 datenext = translate("Run once")
202
203         -- no process running and NOT enabled
204         elseif pid == 0 and enabled == 0 then
205                 datenext  = translate("Disabled")
206
207         -- no process running and NOT
208         elseif pid == 0 and enabled ~= 0 then
209                 datenext = translate("Stopped")
210         end
211
212         return datenext
213 end
214
215 -- start/stop button
216 btn = ts:option( Button, "_startstop",
217         translate("Process ID") .. "<br />" .. translate("Start / Stop") )
218 btn.template = "ddns/overview_startstop"
219 function btn.cfgvalue(self, section)
220         local pid = DDNS.get_pid(section)
221         if pid > 0 then
222                 btn.inputtitle  = "PID: " .. pid
223                 btn.inputstyle  = "reset"
224                 btn.disabled    = false
225         elseif (self.map:get(section, "enabled") or "0") ~= "0" then
226                 btn.inputtitle  = translate("Start")
227                 btn.inputstyle  = "apply"
228                 btn.disabled    = false
229         else
230                 btn.inputtitle  = "----------"
231                 btn.inputstyle  = "button"
232                 btn.disabled    = true
233         end
234         return true
235 end
236
237 return m