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