2 LuCI - Lua Configuration Interface
4 Copyright 2014 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
15 local NXFS = require "nixio.fs"
16 local CTRL = require "luci.controller.ddns" -- this application's controller
17 local DISP = require "luci.dispatcher"
18 local HTTP = require "luci.http"
19 local SYS = require "luci.sys"
20 local DDNS = require "luci.tools.ddns" -- ddns multiused functions
23 show_hints = not (DDNS.check_ipv6() -- IPv6 support
24 and DDNS.check_ssl() -- HTTPS support
25 and DDNS.check_proxy() -- Proxy support
26 and DDNS.check_bind_host() -- DNS TCP support
28 need_update = CTRL.update_needed() -- correct ddns-scripts version
31 font_red = [[<font color="red">]]
32 font_off = [[</font>]]
33 bold_on = [[<strong>]]
34 bold_off = [[</strong>]]
36 -- cbi-map definition -- #######################################################
39 -- first need to close <a> from cbi map template our <a> closed by template
40 --m.title = [[</a><a href="javascript:alert(']] .. CTRL.show_versions() ..[[')">]] ..
41 -- translate("Dynamic DNS")
42 m.title = [[</a><a href="#" onclick="onclick_maptitle();">]] ..
43 translate("Dynamic DNS")
45 m.description = translate("Dynamic DNS allows that your router can be reached with " ..
46 "a fixed hostname while having a dynamically changing " ..
49 m.on_after_commit = function(self)
50 if self.changed then -- changes ?
51 if SYS.init.enabled("ddns") then -- ddns service enabled, restart all
52 os.execute("/etc/init.d/ddns restart")
53 else -- ddns service disabled, send SIGHUP to running
54 os.execute("killall -1 dynamic_dns_updater.sh")
59 -- SimpleSection definiton -- ##################################################
60 -- with all the JavaScripts we need for "a good Show"
61 a = m:section( SimpleSection )
62 a.template = "ddns/overview_status"
64 -- SimpleSection definition -- #################################################
65 -- show Hints to optimize installation and script usage
66 -- only show if service not enabled
68 -- or not GNU Wget and not cURL (for https support)
69 -- or not GNU Wget but cURL without proxy support
71 -- or ddns-scripts package need update
72 if show_hints or need_update or not SYS.init.enabled("ddns") then
73 s = m:section( SimpleSection, translate("Hints") )
75 -- ddns_scripts needs to be updated for full functionality
77 local dv = s:option(DummyValue, "_update_needed")
78 dv.titleref = DISP.build_url("admin", "system", "packages")
80 dv.title = font_red .. bold_on ..
81 translate("Software update required") .. bold_off .. font_off
82 dv.value = translate("The currently installed 'ddns-scripts' package did not support all available settings.") ..
84 translate("Please update to the current version!")
87 -- DDNS Service disabled
88 if not SYS.init.enabled("ddns") then
89 local dv = s:option(DummyValue, "_not_enabled")
90 dv.titleref = DISP.build_url("admin", "system", "startup")
93 translate("DDNS Autostart disabled") .. bold_off
94 dv.value = translate("Currently DDNS updates are not started at boot or on interface events." .. "<br />" ..
95 "You can start/stop each configuration here. It will run until next reboot.")
98 -- Show more hints on a separate page
100 local dv = s:option(DummyValue, "_separate")
101 dv.titleref = DISP.build_url("admin", "services", "ddns", "hints")
103 dv.title = bold_on ..
104 translate("Show more") .. bold_off
105 dv.value = translate("Follow this link" .. "<br />" ..
106 "You will find more hints to optimize your system to run DDNS scripts with all options")
110 -- TableSection definition -- ##################################################
111 ts = m:section( TypedSection, "service",
112 translate("Overview"),
113 translate("Below is a list of configured DDNS configurations and their current state." .. "<br />" ..
114 "If you want to send updates for IPv4 and IPv6 you need to define two separate Configurations " ..
115 "i.e. 'myddns_ipv4' and 'myddns_ipv6'") )
116 ts.sectionhead = translate("Configuration")
117 ts.template = "cbi/tblsection"
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) )
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 ""
134 return [[<em>]] .. translate("config error") .. [[</em>]]
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)
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
156 ena = ts:option( Flag, "enabled",
157 translate("Enabled"))
158 ena.template = "ddns/overview_enabled"
160 function ena.parse(self, section)
161 DDNS.flag_parse(self, section)
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
176 -- no last update happen
177 if lasttime == 0 then
178 return translate("never")
180 -- we read 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)
189 function upd.set_two(self, section) -- fill Next Update
191 local enabled = tonumber(self.map:get(section, "enabled") or 0)
192 local datenext = translate("unknown error") -- formatted date of next update
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)
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
205 local pid = DDNS.get_pid(section)
209 local epoch = os.time() - uptime + lasttime + force_seconds
210 -- use linux date to convert epoch
211 datelast = DDNS.epoch2date(epoch)
214 -- process running but update needs to happen
215 if pid > 0 and ( lasttime + force_seconds - uptime ) < 0 then
216 datenext = translate("Verify")
219 elseif force_seconds == 0 then
220 datenext = translate("Run once")
222 -- no process running and NOT enabled
223 elseif pid == 0 and enabled == 0 then
224 datenext = translate("Disabled")
226 -- no process running and NOT
227 elseif pid == 0 and enabled ~= 0 then
228 datenext = translate("Stopped")
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)
241 btn.inputtitle = "PID: " .. pid
242 btn.inputstyle = "reset"
244 elseif (self.map:get(section, "enabled") or "0") ~= "0" then
245 btn.inputtitle = translate("Start")
246 btn.inputstyle = "apply"
249 btn.inputtitle = "----------"
250 btn.inputstyle = "button"