1 -- Copyright 2014 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
2 -- Licensed to the public under the Apache License 2.0.
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
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
17 need_update = CTRL.update_needed() -- correct ddns-scripts version
20 font_red = [[<font color="red">]]
21 font_off = [[</font>]]
22 bold_on = [[<strong>]]
23 bold_off = [[</strong>]]
25 -- cbi-map definition -- #######################################################
28 -- first need to close <a> from cbi map template our <a> closed by template
29 --m.title = [[</a><a href="javascript:alert(']] .. CTRL.show_versions() ..[[')">]] ..
30 -- translate("Dynamic DNS")
31 m.title = [[</a><a href="#" onclick="onclick_maptitle();">]] ..
32 translate("Dynamic DNS")
34 m.description = translate("Dynamic DNS allows that your router can be reached with " ..
35 "a fixed hostname while having a dynamically changing " ..
38 m.on_after_commit = function(self)
39 if self.changed then -- changes ?
40 if SYS.init.enabled("ddns") then -- ddns service enabled, restart all
41 os.execute("/etc/init.d/ddns restart")
42 else -- ddns service disabled, send SIGHUP to running
43 os.execute("killall -1 dynamic_dns_updater.sh")
48 -- SimpleSection definiton -- ##################################################
49 -- with all the JavaScripts we need for "a good Show"
50 a = m:section( SimpleSection )
51 a.template = "ddns/overview_status"
53 -- SimpleSection definition -- #################################################
54 -- show Hints to optimize installation and script usage
55 -- only show if service not enabled
57 -- or not GNU Wget and not cURL (for https support)
58 -- or not GNU Wget but cURL without proxy support
60 -- or ddns-scripts package need update
61 if show_hints or need_update or not SYS.init.enabled("ddns") then
62 s = m:section( SimpleSection, translate("Hints") )
64 -- ddns_scripts needs to be updated for full functionality
66 local dv = s:option(DummyValue, "_update_needed")
67 dv.titleref = DISP.build_url("admin", "system", "packages")
69 dv.title = font_red .. bold_on ..
70 translate("Software update required") .. bold_off .. font_off
71 dv.value = translate("The currently installed 'ddns-scripts' package did not support all available settings.") ..
73 translate("Please update to the current version!")
76 -- DDNS Service disabled
77 if not SYS.init.enabled("ddns") then
78 local dv = s:option(DummyValue, "_not_enabled")
79 dv.titleref = DISP.build_url("admin", "system", "startup")
82 translate("DDNS Autostart disabled") .. bold_off
83 dv.value = translate("Currently DDNS updates are not started at boot or on interface events." .. "<br />" ..
84 "You can start/stop each configuration here. It will run until next reboot.")
87 -- Show more hints on a separate page
89 local dv = s:option(DummyValue, "_separate")
90 dv.titleref = DISP.build_url("admin", "services", "ddns", "hints")
93 translate("Show more") .. bold_off
94 dv.value = translate("Follow this link" .. "<br />" ..
95 "You will find more hints to optimize your system to run DDNS scripts with all options")
99 -- TableSection definition -- ##################################################
100 ts = m:section( TypedSection, "service",
101 translate("Overview"),
102 translate("Below is a list of configured DDNS configurations and their current state." .. "<br />" ..
103 "If you want to send updates for IPv4 and IPv6 you need to define two separate Configurations " ..
104 "i.e. 'myddns_ipv4' and 'myddns_ipv6'") )
105 ts.sectionhead = translate("Configuration")
106 ts.template = "cbi/tblsection"
108 ts.extedit = DISP.build_url("admin", "services", "ddns", "detail", "%s")
109 function ts.create(self, name)
110 AbstractSection.create(self, name)
111 HTTP.redirect( self.extedit:format(name) )
114 -- Domain and registered IP -- #################################################
115 dom = ts:option(DummyValue, "_domainIP",
116 translate("Hostname/Domain") .. "<br />" .. translate("Registered IP") )
117 dom.template = "ddns/overview_doubleline"
118 function dom.set_one(self, section)
119 local domain = self.map:get(section, "domain") or ""
123 return [[<em>]] .. translate("config error") .. [[</em>]]
126 function dom.set_two(self, section)
127 local domain = self.map:get(section, "domain") or ""
128 if domain == "" then return "" end
129 local dnsserver = self.map:get(section, "dnsserver") or ""
130 local use_ipv6 = tonumber(self.map:get(section, "use_ipv6") or 0)
131 local force_ipversion = tonumber(self.map:get(section, "force_ipversion") or 0)
132 local force_dnstcp = tonumber(self.map:get(section, "force_dnstcp") or 0)
133 local command = [[/usr/lib/ddns/dynamic_dns_lucihelper.sh]]
134 if not NXFS.access(command, "rwx", "rx", "rx") then
135 NXFS.chmod(command, 755)
137 command = command .. [[ get_registered_ip ]] .. domain .. [[ ]] .. use_ipv6 ..
138 [[ ]] .. force_ipversion .. [[ ]] .. force_dnstcp .. [[ ]] .. dnsserver
139 local ip = SYS.exec(command)
140 if ip == "" then ip = translate("no data") end
145 ena = ts:option( Flag, "enabled",
146 translate("Enabled"))
147 ena.template = "ddns/overview_enabled"
149 function ena.parse(self, section)
150 DDNS.flag_parse(self, section)
153 -- show PID and next update
154 upd = ts:option( DummyValue, "_update",
155 translate("Last Update") .. "<br />" .. translate("Next Update"))
156 upd.template = "ddns/overview_doubleline"
157 function upd.set_one(self, section) -- fill Last Update
158 -- get/validate last update
159 local uptime = SYS.uptime()
160 local lasttime = DDNS.get_lastupd(section)
161 if lasttime > uptime then -- /var might not be linked to /tmp and cleared on reboot
165 -- no last update happen
166 if lasttime == 0 then
167 return translate("never")
169 -- we read last update
172 -- os.epoch - sys.uptime + lastupdate(uptime)
173 local epoch = os.time() - uptime + lasttime
174 -- use linux date to convert epoch
175 return DDNS.epoch2date(epoch)
178 function upd.set_two(self, section) -- fill Next Update
180 local enabled = tonumber(self.map:get(section, "enabled") or 0)
181 local datenext = translate("unknown error") -- formatted date of next update
184 local force_interval = tonumber(self.map:get(section, "force_interval") or 72)
185 local force_unit = self.map:get(section, "force_unit") or "hours"
186 local force_seconds = DDNS.calc_seconds(force_interval, force_unit)
188 -- get last update and get/validate PID
189 local uptime = SYS.uptime()
190 local lasttime = DDNS.get_lastupd(section)
191 if lasttime > uptime then -- /var might not be linked to /tmp and cleared on reboot
194 local pid = DDNS.get_pid(section)
198 local epoch = os.time() - uptime + lasttime + force_seconds
199 -- use linux date to convert epoch
200 datelast = DDNS.epoch2date(epoch)
203 -- process running but update needs to happen
204 if pid > 0 and ( lasttime + force_seconds - uptime ) < 0 then
205 datenext = translate("Verify")
208 elseif force_seconds == 0 then
209 datenext = translate("Run once")
211 -- no process running and NOT enabled
212 elseif pid == 0 and enabled == 0 then
213 datenext = translate("Disabled")
215 -- no process running and NOT
216 elseif pid == 0 and enabled ~= 0 then
217 datenext = translate("Stopped")
224 btn = ts:option( Button, "_startstop",
225 translate("Process ID") .. "<br />" .. translate("Start / Stop") )
226 btn.template = "ddns/overview_startstop"
227 function btn.cfgvalue(self, section)
228 local pid = DDNS.get_pid(section)
230 btn.inputtitle = "PID: " .. pid
231 btn.inputstyle = "reset"
233 elseif (self.map:get(section, "enabled") or "0") ~= "0" then
234 btn.inputtitle = translate("Start")
235 btn.inputstyle = "apply"
238 btn.inputtitle = "----------"
239 btn.inputstyle = "button"