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(']]
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")
40 .. translate("Dynamic DNS")
42 m.description = translate("Dynamic DNS allows that your router can be reached with " ..
43 "a fixed hostname while having a dynamically changing " ..
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")
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"
61 -- SimpleSection definition -- #################################################
62 -- show Hints to optimize installation and script usage
63 -- only show if service not enabled
65 -- or not GNU Wget and not cURL (for https support)
66 -- or not GNU Wget but cURL without proxy support
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") )
72 -- ddns_scripts needs to be updated for full functionality
74 local dv = s:option(DummyValue, "_update_needed")
75 dv.titleref = DISP.build_url("admin", "system", "packages")
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.") ..
81 translate("Please update to the current version!")
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")
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.")
95 -- Show more hints on a separate page
97 local dv = s:option(DummyValue, "_separate")
98 dv.titleref = DISP.build_url("admin", "services", "ddns", "hints")
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")
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.")
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'")
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"
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) )
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 ""
135 return [[<em>]] .. translate("config error") .. [[</em>]]
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)
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
157 ena = ts:option( Flag, "enabled",
158 translate("Enabled"))
159 ena.template = "ddns/overview_enabled"
161 function ena.parse(self, section)
162 DDNS.flag_parse(self, section)
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
177 -- no last update happen
178 if lasttime == 0 then
179 return translate("never")
181 -- we read 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)
190 function upd.set_two(self, section) -- fill Next Update
192 local enabled = tonumber(self.map:get(section, "enabled") or 0)
193 local datenext = translate("unknown error") -- formatted date of next update
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)
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
206 local pid = DDNS.get_pid(section)
210 local epoch = os.time() - uptime + lasttime + force_seconds
211 -- use linux date to convert epoch
212 datelast = DDNS.epoch2date(epoch)
215 -- process running but update needs to happen
216 if pid > 0 and ( lasttime + force_seconds - uptime ) < 0 then
217 datenext = translate("Verify")
220 elseif force_seconds == 0 then
221 datenext = translate("Run once")
223 -- no process running and NOT enabled
224 elseif pid == 0 and enabled == 0 then
225 datenext = translate("Disabled")
227 -- no process running and NOT
228 elseif pid == 0 and enabled ~= 0 then
229 datenext = translate("Stopped")
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)
242 btn.inputtitle = "PID: " .. pid
243 btn.inputstyle = "reset"
245 elseif (self.map:get(section, "enabled") or "0") ~= "0" then
246 btn.inputtitle = translate("Start")
247 btn.inputstyle = "apply"
250 btn.inputtitle = "----------"
251 btn.inputstyle = "button"