1 -- Copyright 2014-2018 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 local command = CTRL.luci_helper
37 if SYS.init.enabled("ddns") then -- ddns service enabled, restart all
38 command = command .. " -- restart"
40 else -- ddns service disabled, send SIGHUP to running
41 command = command .. " -- reload"
47 -- SimpleSection definition -- ##################################################
48 -- with all the JavaScripts we need for "a good Show"
49 a = m:section( SimpleSection )
50 a.template = "ddns/overview_status"
52 -- SimpleSection definition -- #################################################
53 -- show Hints to optimize installation and script usage
54 if show_hints or need_update or not_enabled then
56 s = m:section( SimpleSection, translate("Hints") )
58 -- ddns-scripts needs to be updated for full functionality
60 local dv = s:option(DummyValue, "_update_needed")
61 dv.titleref = DISP.build_url("admin", "system", "packages")
63 dv.title = font_red .. bold_on ..
64 translate("Software update required") .. bold_off .. font_off
65 dv.value = translate("The currently installed 'ddns-scripts' package did not support all available settings.") ..
67 translate("Please update to the current version!")
70 -- DDNS Service disabled
72 local dv = s:option(DummyValue, "_not_enabled")
73 dv.titleref = DISP.build_url("admin", "system", "startup")
76 translate("DDNS Autostart disabled") .. bold_off
77 dv.value = translate("Currently DDNS updates are not started at boot or on interface events." .. "<br />" ..
78 "You can start/stop each configuration here. It will run until next reboot.")
81 -- Show more hints on a separate page
83 local dv = s:option(DummyValue, "_separate")
84 dv.titleref = DISP.build_url("admin", "services", "ddns", "hints")
87 translate("Show more") .. bold_off
88 dv.value = translate("Follow this link" .. "<br />" ..
89 "You will find more hints to optimize your system to run DDNS scripts with all options")
93 -- TableSection definition -- ##################################################
94 ts = m:section( TypedSection, "service",
95 translate("Overview"),
96 translate("Below is a list of configured DDNS configurations and their current state.")
98 .. translate("If you want to send updates for IPv4 and IPv6 you need to define two separate Configurations "
99 .. "i.e. 'myddns_ipv4' and 'myddns_ipv6'")
101 .. [[<a href="]] .. DISP.build_url("admin", "services", "ddns", "global") .. [[">]]
102 .. translate("To change global settings click here") .. [[</a>]] )
103 ts.sectionhead = translate("Configuration")
104 ts.template = "cbi/tblsection"
106 ts.extedit = DISP.build_url("admin", "services", "ddns", "detail", "%s")
107 function ts.create(self, name)
108 AbstractSection.create(self, name)
109 HTTP.redirect( self.extedit:format(name) )
112 -- Lookup_Host and registered IP -- #################################################
113 dom = ts:option(DummyValue, "_lookupIP",
114 translate("Lookup Hostname") .. "<br />" .. translate("Registered IP") )
115 dom.template = "ddns/overview_doubleline"
116 function dom.set_one(self, section)
117 local lookup = self.map:get(section, "lookup_host") or ""
121 return [[<em>]] .. translate("config error") .. [[</em>]]
124 function dom.set_two(self, section)
125 local chk_sec = DDNS.calc_seconds(
126 tonumber(self.map:get(section, "check_interval")) or 10,
127 self.map:get(section, "check_unit") or "minutes" )
128 local ip = DDNS.get_regip(section, chk_sec)
129 if ip == "NOFILE" then
130 local lookup_host = self.map:get(section, "lookup_host") or ""
131 if lookup_host == "" then return "" end
132 local dnsserver = self.map:get(section, "dnsserver") or ""
133 local use_ipv6 = tonumber(self.map:get(section, "use_ipv6") or 0)
134 local force_ipversion = tonumber(self.map:get(section, "force_ipversion") or 0)
135 local force_dnstcp = tonumber(self.map:get(section, "force_dnstcp") or 0)
136 local is_glue = tonumber(self.map:get(section, "is_glue") or 0)
137 local command = CTRL.luci_helper .. [[ -]]
138 if (use_ipv6 == 1) then command = command .. [[6]] end
139 if (force_ipversion == 1) then command = command .. [[f]] end
140 if (force_dnstcp == 1) then command = command .. [[t]] end
141 if (is_glue == 1) then command = command .. [[g]] end
142 command = command .. [[l ]] .. lookup_host
143 command = command .. [[ -S ]] .. section
144 if (#dnsserver > 0) then command = command .. [[ -d ]] .. dnsserver end
145 command = command .. [[ -- get_registered_ip]]
146 ip = SYS.exec(command)
148 if ip == "" then ip = translate("no data") end
153 ena = ts:option( Flag, "enabled",
154 translate("Enabled"))
155 ena.template = "ddns/overview_enabled"
158 -- show PID and next update
159 upd = ts:option( DummyValue, "_update",
160 translate("Last Update") .. "<br />" .. translate("Next Update"))
161 upd.template = "ddns/overview_doubleline"
162 function upd.set_one(self, section) -- fill Last Update
163 -- get/validate last update
164 local uptime = SYS.uptime()
165 local lasttime = DDNS.get_lastupd(section)
166 if lasttime > uptime then -- /var might not be linked to /tmp and cleared on reboot
170 -- no last update happen
171 if lasttime == 0 then
172 return translate("never")
174 -- we read last update
177 -- os.epoch - sys.uptime + lastupdate(uptime)
178 local epoch = os.time() - uptime + lasttime
179 -- use linux date to convert epoch
180 return DDNS.epoch2date(epoch)
183 function upd.set_two(self, section) -- fill Next Update
185 local enabled = tonumber(self.map:get(section, "enabled") or 0)
186 local datenext = translate("unknown error") -- formatted date of next update
189 local force_interval = tonumber(self.map:get(section, "force_interval") or 72)
190 local force_unit = self.map:get(section, "force_unit") or "hours"
191 local force_seconds = DDNS.calc_seconds(force_interval, force_unit)
193 -- get last update and get/validate PID
194 local uptime = SYS.uptime()
195 local lasttime = DDNS.get_lastupd(section)
196 if lasttime > uptime then -- /var might not be linked to /tmp and cleared on reboot
199 local pid = DDNS.get_pid(section)
203 local epoch = os.time() - uptime + lasttime + force_seconds
204 -- use linux date to convert epoch
205 datelast = DDNS.epoch2date(epoch)
208 -- process running but update needs to happen
209 if pid > 0 and ( lasttime + force_seconds - uptime ) < 0 then
210 datenext = translate("Verify")
213 elseif force_seconds == 0 then
214 datenext = translate("Run once")
216 -- no process running and NOT enabled
217 elseif pid == 0 and enabled == 0 then
218 datenext = translate("Disabled")
220 -- no process running and NOT
221 elseif pid == 0 and enabled ~= 0 then
222 datenext = translate("Stopped")
229 btn = ts:option( Button, "_startstop",
230 translate("Process ID") .. "<br />" .. translate("Start / Stop") )
231 btn.template = "ddns/overview_startstop"
232 function btn.cfgvalue(self, section)
233 local pid = DDNS.get_pid(section)
235 btn.inputtitle = "PID: " .. pid
236 btn.inputstyle = "reset"
238 elseif (self.map:get(section, "enabled") or "0") ~= "0" then
239 btn.inputtitle = translate("Start")
240 btn.inputstyle = "apply"
243 btn.inputtitle = "----------"
244 btn.inputstyle = "button"