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