1 -- Copyright 2014-2016 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 DISP = require "luci.dispatcher"
6 local HTTP = require "luci.http"
7 local SYS = require "luci.sys"
8 local CTRL = require "luci.controller.ddns" -- this application's controller
9 local DDNS = require "luci.tools.ddns" -- ddns multiused functions
11 local show_hints = not (DDNS.has_ipv6 -- IPv6 support
12 and DDNS.has_ssl -- HTTPS support
13 and DDNS.has_proxy -- Proxy support
14 and DDNS.has_bindhost -- DNS TCP support
15 and DDNS.has_forceip -- Force IP version
16 and DDNS.has_dnsserver -- DNS server support
17 and DDNS.has_bindnet -- Bind to network/interface
18 and DDNS.has_cacerts -- certificates installed at /etc/ssl/certs
20 local not_enabled = not SYS.init.enabled("ddns")
21 local need_update = not CTRL.service_ok()
24 font_red = [[<font color="red">]]
25 font_off = [[</font>]]
26 bold_on = [[<strong>]]
27 bold_off = [[</strong>]]
29 -- cbi-map definition -- #######################################################
31 m.title = CTRL.app_title_main()
32 m.description = CTRL.app_description()
34 m.on_after_commit = function(self)
35 if self.changed then -- changes ?
36 if SYS.init.enabled("ddns") then -- ddns service enabled, restart all
37 os.execute("/etc/init.d/ddns restart")
38 else -- ddns service disabled, send SIGHUP to running
39 os.execute("killall -1 dynamic_dns_updater.sh")
44 -- SimpleSection definiton -- ##################################################
45 -- with all the JavaScripts we need for "a good Show"
46 a = m:section( SimpleSection )
47 a.template = "ddns/overview_status"
49 -- SimpleSection definition -- #################################################
50 -- show Hints to optimize installation and script usage
51 if show_hints or need_update or not_enabled then
53 s = m:section( SimpleSection, translate("Hints") )
55 -- ddns_scripts needs to be updated for full functionality
57 local dv = s:option(DummyValue, "_update_needed")
58 dv.titleref = DISP.build_url("admin", "system", "packages")
60 dv.title = font_red .. bold_on ..
61 translate("Software update required") .. bold_off .. font_off
62 dv.value = translate("The currently installed 'ddns-scripts' package did not support all available settings.") ..
64 translate("Please update to the current version!")
67 -- DDNS Service disabled
69 local dv = s:option(DummyValue, "_not_enabled")
70 dv.titleref = DISP.build_url("admin", "system", "startup")
73 translate("DDNS Autostart disabled") .. bold_off
74 dv.value = translate("Currently DDNS updates are not started at boot or on interface events." .. "<br />" ..
75 "You can start/stop each configuration here. It will run until next reboot.")
78 -- Show more hints on a separate page
80 local dv = s:option(DummyValue, "_separate")
81 dv.titleref = DISP.build_url("admin", "services", "ddns", "hints")
84 translate("Show more") .. bold_off
85 dv.value = translate("Follow this link" .. "<br />" ..
86 "You will find more hints to optimize your system to run DDNS scripts with all options")
90 -- TableSection definition -- ##################################################
91 ts = m:section( TypedSection, "service",
92 translate("Overview"),
93 translate("Below is a list of configured DDNS configurations and their current state.")
95 .. translate("If you want to send updates for IPv4 and IPv6 you need to define two separate Configurations "
96 .. "i.e. 'myddns_ipv4' and 'myddns_ipv6'")
98 .. [[<a href="]] .. DISP.build_url("admin", "services", "ddns", "global") .. [[">]]
99 .. translate("To change global settings click here") .. [[</a>]] )
100 ts.sectionhead = translate("Configuration")
101 ts.template = "cbi/tblsection"
103 ts.extedit = DISP.build_url("admin", "services", "ddns", "detail", "%s")
104 function ts.create(self, name)
105 AbstractSection.create(self, name)
106 HTTP.redirect( self.extedit:format(name) )
109 -- Lookup_Host and registered IP -- #################################################
110 dom = ts:option(DummyValue, "_lookupIP",
111 translate("Lookup Hostname") .. "<br />" .. translate("Registered IP") )
112 dom.template = "ddns/overview_doubleline"
113 function dom.set_one(self, section)
114 local lookup = self.map:get(section, "lookup_host") or ""
118 return [[<em>]] .. translate("config error") .. [[</em>]]
121 function dom.set_two(self, section)
122 local lookup = self.map:get(section, "lookup_host") or ""
123 if lookup == "" then return "" end
124 local dnsserver = self.map:get(section, "dnsserver") or ""
125 local use_ipv6 = tonumber(self.map:get(section, "use_ipv6") or 0)
126 local force_ipversion = tonumber(self.map:get(section, "force_ipversion") or 0)
127 local force_dnstcp = tonumber(self.map:get(section, "force_dnstcp") or 0)
128 local command = [[/usr/lib/ddns/dynamic_dns_lucihelper.sh]]
129 if not NXFS.access(command, "rwx", "rx", "rx") then
130 NXFS.chmod(command, 755)
132 command = command .. [[ get_registered_ip ]] .. lookup .. [[ ]] .. use_ipv6 ..
133 [[ ]] .. force_ipversion .. [[ ]] .. force_dnstcp .. [[ ]] .. dnsserver
134 local ip = SYS.exec(command)
135 if ip == "" then ip = translate("no data") end
140 ena = ts:option( Flag, "enabled",
141 translate("Enabled"))
142 ena.template = "ddns/overview_enabled"
145 -- show PID and next update
146 upd = ts:option( DummyValue, "_update",
147 translate("Last Update") .. "<br />" .. translate("Next Update"))
148 upd.template = "ddns/overview_doubleline"
149 function upd.set_one(self, section) -- fill Last Update
150 -- get/validate last update
151 local uptime = SYS.uptime()
152 local lasttime = DDNS.get_lastupd(section)
153 if lasttime > uptime then -- /var might not be linked to /tmp and cleared on reboot
157 -- no last update happen
158 if lasttime == 0 then
159 return translate("never")
161 -- we read last update
164 -- os.epoch - sys.uptime + lastupdate(uptime)
165 local epoch = os.time() - uptime + lasttime
166 -- use linux date to convert epoch
167 return DDNS.epoch2date(epoch)
170 function upd.set_two(self, section) -- fill Next Update
172 local enabled = tonumber(self.map:get(section, "enabled") or 0)
173 local datenext = translate("unknown error") -- formatted date of next update
176 local force_interval = tonumber(self.map:get(section, "force_interval") or 72)
177 local force_unit = self.map:get(section, "force_unit") or "hours"
178 local force_seconds = DDNS.calc_seconds(force_interval, force_unit)
180 -- get last update and get/validate PID
181 local uptime = SYS.uptime()
182 local lasttime = DDNS.get_lastupd(section)
183 if lasttime > uptime then -- /var might not be linked to /tmp and cleared on reboot
186 local pid = DDNS.get_pid(section)
190 local epoch = os.time() - uptime + lasttime + force_seconds
191 -- use linux date to convert epoch
192 datelast = DDNS.epoch2date(epoch)
195 -- process running but update needs to happen
196 if pid > 0 and ( lasttime + force_seconds - uptime ) < 0 then
197 datenext = translate("Verify")
200 elseif force_seconds == 0 then
201 datenext = translate("Run once")
203 -- no process running and NOT enabled
204 elseif pid == 0 and enabled == 0 then
205 datenext = translate("Disabled")
207 -- no process running and NOT
208 elseif pid == 0 and enabled ~= 0 then
209 datenext = translate("Stopped")
216 btn = ts:option( Button, "_startstop",
217 translate("Process ID") .. "<br />" .. translate("Start / Stop") )
218 btn.template = "ddns/overview_startstop"
219 function btn.cfgvalue(self, section)
220 local pid = DDNS.get_pid(section)
222 btn.inputtitle = "PID: " .. pid
223 btn.inputstyle = "reset"
225 elseif (self.map:get(section, "enabled") or "0") ~= "0" then
226 btn.inputtitle = translate("Start")
227 btn.inputstyle = "apply"
230 btn.inputtitle = "----------"
231 btn.inputstyle = "button"