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 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 lookup_host = self.map:get(section, "lookup_host") or ""
126 if lookup_host == "" then return "" end
127 local dnsserver = self.map:get(section, "dnsserver") or ""
128 local use_ipv6 = tonumber(self.map:get(section, "use_ipv6") or 0)
129 local force_ipversion = tonumber(self.map:get(section, "force_ipversion") or 0)
130 local force_dnstcp = tonumber(self.map:get(section, "force_dnstcp") or 0)
131 local is_glue = tonumber(self.map:get(section, "is_glue") or 0)
132 local command = CTRL.luci_helper .. [[ -]]
133 if (use_ipv6 == 1) then command = command .. [[6]] end
134 if (force_ipversion == 1) then command = command .. [[f]] end
135 if (force_dnstcp == 1) then command = command .. [[t]] end
136 if (is_glue == 1) then command = command .. [[g]] end
137 command = command .. [[l ]] .. lookup_host
138 if (#dnsserver > 0) then command = command .. [[ -d ]] .. dnsserver end
139 command = command .. [[ -- get_registered_ip]]
140 local ip = SYS.exec(command)
141 if ip == "" then ip = translate("no data") end
146 ena = ts:option( Flag, "enabled",
147 translate("Enabled"))
148 ena.template = "ddns/overview_enabled"
151 -- show PID and next update
152 upd = ts:option( DummyValue, "_update",
153 translate("Last Update") .. "<br />" .. translate("Next Update"))
154 upd.template = "ddns/overview_doubleline"
155 function upd.set_one(self, section) -- fill Last Update
156 -- get/validate last update
157 local uptime = SYS.uptime()
158 local lasttime = DDNS.get_lastupd(section)
159 if lasttime > uptime then -- /var might not be linked to /tmp and cleared on reboot
163 -- no last update happen
164 if lasttime == 0 then
165 return translate("never")
167 -- we read last update
170 -- os.epoch - sys.uptime + lastupdate(uptime)
171 local epoch = os.time() - uptime + lasttime
172 -- use linux date to convert epoch
173 return DDNS.epoch2date(epoch)
176 function upd.set_two(self, section) -- fill Next Update
178 local enabled = tonumber(self.map:get(section, "enabled") or 0)
179 local datenext = translate("unknown error") -- formatted date of next update
182 local force_interval = tonumber(self.map:get(section, "force_interval") or 72)
183 local force_unit = self.map:get(section, "force_unit") or "hours"
184 local force_seconds = DDNS.calc_seconds(force_interval, force_unit)
186 -- get last update and get/validate PID
187 local uptime = SYS.uptime()
188 local lasttime = DDNS.get_lastupd(section)
189 if lasttime > uptime then -- /var might not be linked to /tmp and cleared on reboot
192 local pid = DDNS.get_pid(section)
196 local epoch = os.time() - uptime + lasttime + force_seconds
197 -- use linux date to convert epoch
198 datelast = DDNS.epoch2date(epoch)
201 -- process running but update needs to happen
202 if pid > 0 and ( lasttime + force_seconds - uptime ) < 0 then
203 datenext = translate("Verify")
206 elseif force_seconds == 0 then
207 datenext = translate("Run once")
209 -- no process running and NOT enabled
210 elseif pid == 0 and enabled == 0 then
211 datenext = translate("Disabled")
213 -- no process running and NOT
214 elseif pid == 0 and enabled ~= 0 then
215 datenext = translate("Stopped")
222 btn = ts:option( Button, "_startstop",
223 translate("Process ID") .. "<br />" .. translate("Start / Stop") )
224 btn.template = "ddns/overview_startstop"
225 function btn.cfgvalue(self, section)
226 local pid = DDNS.get_pid(section)
228 btn.inputtitle = "PID: " .. pid
229 btn.inputstyle = "reset"
231 elseif (self.map:get(section, "enabled") or "0") ~= "0" then
232 btn.inputtitle = translate("Start")
233 btn.inputstyle = "apply"
236 btn.inputtitle = "----------"
237 btn.inputstyle = "button"