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 -- correct ddns-scripts version
18 need_update = DDNS.ipkg_ver_compare(DDNS.ipkg_ver_installed("ddns-scripts"), "<<", CTRL.DDNS_MIN)
21 font_red = [[<font color="red">]]
22 font_off = [[</font>]]
23 bold_on = [[<strong>]]
24 bold_off = [[</strong>]]
26 -- cbi-map definition -- #######################################################
29 -- first need to close <a> from cbi map template our <a> closed by template
30 --m.title = [[</a><a href="javascript:alert(']] .. CTRL.show_versions() ..[[')">]] ..
31 -- translate("Dynamic DNS")
32 m.title = [[</a><a href="javascript:alert(']]
33 .. translate("Version Information")
34 .. [[\n\nluci-app-ddns]]
35 .. [[\n\t]] .. translate("Version") .. [[:\t]] .. DDNS.ipkg_ver_installed("luci-app-ddns")
36 .. [[\n\nddns-scripts ]] .. translate("required") .. [[:]]
37 .. [[\n\t]] .. translate("Version") .. [[:\t]] .. CTRL.DDNS_MIN .. [[ ]] .. translate("or higher")
38 .. [[\n\nddns-scripts ]] .. translate("installed") .. [[:]]
39 .. [[\n\t]] .. translate("Version") .. [[:\t]] .. DDNS.ipkg_ver_installed("ddns-scripts")
42 .. translate("Dynamic DNS")
44 m.description = translate("Dynamic DNS allows that your router can be reached with " ..
45 "a fixed hostname while having a dynamically changing " ..
48 m.on_after_commit = function(self)
49 if self.changed then -- changes ?
50 if SYS.init.enabled("ddns") then -- ddns service enabled, restart all
51 os.execute("/etc/init.d/ddns restart")
52 else -- ddns service disabled, send SIGHUP to running
53 os.execute("killall -1 dynamic_dns_updater.sh")
58 -- SimpleSection definiton -- ##################################################
59 -- with all the JavaScripts we need for "a good Show"
60 a = m:section( SimpleSection )
61 a.template = "ddns/overview_status"
63 -- SimpleSection definition -- #################################################
64 -- show Hints to optimize installation and script usage
65 -- only show if service not enabled
67 -- or not GNU Wget and not cURL (for https support)
68 -- or not GNU Wget but cURL without proxy support
70 -- or ddns-scripts package need update
71 if show_hints or need_update or not SYS.init.enabled("ddns") then
72 s = m:section( SimpleSection, translate("Hints") )
74 -- ddns_scripts needs to be updated for full functionality
76 local dv = s:option(DummyValue, "_update_needed")
77 dv.titleref = DISP.build_url("admin", "system", "packages")
79 dv.title = font_red .. bold_on ..
80 translate("Software update required") .. bold_off .. font_off
81 dv.value = translate("The currently installed 'ddns-scripts' package did not support all available settings.") ..
83 translate("Please update to the current version!")
86 -- DDNS Service disabled
87 if not SYS.init.enabled("ddns") then
88 local dv = s:option(DummyValue, "_not_enabled")
89 dv.titleref = DISP.build_url("admin", "system", "startup")
92 translate("DDNS Autostart disabled") .. bold_off
93 dv.value = translate("Currently DDNS updates are not started at boot or on interface events." .. "<br />" ..
94 "You can start/stop each configuration here. It will run until next reboot.")
97 -- Show more hints on a separate page
99 local dv = s:option(DummyValue, "_separate")
100 dv.titleref = DISP.build_url("admin", "services", "ddns", "hints")
102 dv.title = bold_on ..
103 translate("Show more") .. bold_off
104 dv.value = translate("Follow this link" .. "<br />" ..
105 "You will find more hints to optimize your system to run DDNS scripts with all options")
109 -- TableSection definition -- ##################################################
110 ts = m:section( TypedSection, "service",
111 translate("Overview"),
112 translate("Below is a list of configured DDNS configurations and their current state." .. "<br />" ..
113 "If you want to send updates for IPv4 and IPv6 you need to define two separate Configurations " ..
114 "i.e. 'myddns_ipv4' and 'myddns_ipv6'") )
115 ts.sectionhead = translate("Configuration")
116 ts.template = "cbi/tblsection"
118 ts.extedit = DISP.build_url("admin", "services", "ddns", "detail", "%s")
119 function ts.create(self, name)
120 AbstractSection.create(self, name)
121 HTTP.redirect( self.extedit:format(name) )
124 -- Domain and registered IP -- #################################################
125 dom = ts:option(DummyValue, "_domainIP",
126 translate("Hostname/Domain") .. "<br />" .. translate("Registered IP") )
127 dom.template = "ddns/overview_doubleline"
128 function dom.set_one(self, section)
129 local domain = self.map:get(section, "domain") or ""
133 return [[<em>]] .. translate("config error") .. [[</em>]]
136 function dom.set_two(self, section)
137 local domain = self.map:get(section, "domain") or ""
138 if domain == "" then return "" end
139 local dnsserver = self.map:get(section, "dnsserver") or ""
140 local use_ipv6 = tonumber(self.map:get(section, "use_ipv6") or 0)
141 local force_ipversion = tonumber(self.map:get(section, "force_ipversion") or 0)
142 local force_dnstcp = tonumber(self.map:get(section, "force_dnstcp") or 0)
143 local command = [[/usr/lib/ddns/dynamic_dns_lucihelper.sh]]
144 if not NXFS.access(command, "rwx", "rx", "rx") then
145 NXFS.chmod(command, 755)
147 command = command .. [[ get_registered_ip ]] .. domain .. [[ ]] .. use_ipv6 ..
148 [[ ]] .. force_ipversion .. [[ ]] .. force_dnstcp .. [[ ]] .. dnsserver
149 local ip = SYS.exec(command)
150 if ip == "" then ip = translate("no data") end
155 ena = ts:option( Flag, "enabled",
156 translate("Enabled"))
157 ena.template = "ddns/overview_enabled"
159 function ena.parse(self, section)
160 DDNS.flag_parse(self, section)
163 -- show PID and next update
164 upd = ts:option( DummyValue, "_update",
165 translate("Last Update") .. "<br />" .. translate("Next Update"))
166 upd.template = "ddns/overview_doubleline"
167 function upd.set_one(self, section) -- fill Last Update
168 -- get/validate last update
169 local uptime = SYS.uptime()
170 local lasttime = DDNS.get_lastupd(section)
171 if lasttime > uptime then -- /var might not be linked to /tmp and cleared on reboot
175 -- no last update happen
176 if lasttime == 0 then
177 return translate("never")
179 -- we read last update
182 -- os.epoch - sys.uptime + lastupdate(uptime)
183 local epoch = os.time() - uptime + lasttime
184 -- use linux date to convert epoch
185 return DDNS.epoch2date(epoch)
188 function upd.set_two(self, section) -- fill Next Update
190 local enabled = tonumber(self.map:get(section, "enabled") or 0)
191 local datenext = translate("unknown error") -- formatted date of next update
194 local force_interval = tonumber(self.map:get(section, "force_interval") or 72)
195 local force_unit = self.map:get(section, "force_unit") or "hours"
196 local force_seconds = DDNS.calc_seconds(force_interval, force_unit)
198 -- get last update and get/validate PID
199 local uptime = SYS.uptime()
200 local lasttime = DDNS.get_lastupd(section)
201 if lasttime > uptime then -- /var might not be linked to /tmp and cleared on reboot
204 local pid = DDNS.get_pid(section)
208 local epoch = os.time() - uptime + lasttime + force_seconds
209 -- use linux date to convert epoch
210 datelast = DDNS.epoch2date(epoch)
213 -- process running but update needs to happen
214 if pid > 0 and ( lasttime + force_seconds - uptime ) < 0 then
215 datenext = translate("Verify")
218 elseif force_seconds == 0 then
219 datenext = translate("Run once")
221 -- no process running and NOT enabled
222 elseif pid == 0 and enabled == 0 then
223 datenext = translate("Disabled")
225 -- no process running and NOT
226 elseif pid == 0 and enabled ~= 0 then
227 datenext = translate("Stopped")
234 btn = ts:option( Button, "_startstop",
235 translate("Process ID") .. "<br />" .. translate("Start / Stop") )
236 btn.template = "ddns/overview_startstop"
237 function btn.cfgvalue(self, section)
238 local pid = DDNS.get_pid(section)
240 btn.inputtitle = "PID: " .. pid
241 btn.inputstyle = "reset"
243 elseif (self.map:get(section, "enabled") or "0") ~= "0" then
244 btn.inputtitle = translate("Start")
245 btn.inputstyle = "apply"
248 btn.inputtitle = "----------"
249 btn.inputstyle = "button"