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 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
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 = not CTRL.service_ok()
21 font_red = [[<font color="red">]]
22 font_off = [[</font>]]
23 bold_on = [[<strong>]]
24 bold_off = [[</strong>]]
26 -- cbi-map definition -- #######################################################
28 m.title = CTRL.app_title_main()
29 m.description = CTRL.app_description()
31 m.on_after_commit = function(self)
32 if self.changed then -- changes ?
33 if SYS.init.enabled("ddns") then -- ddns service enabled, restart all
34 os.execute("/etc/init.d/ddns restart")
35 else -- ddns service disabled, send SIGHUP to running
36 os.execute("killall -1 dynamic_dns_updater.sh")
41 -- SimpleSection definiton -- ##################################################
42 -- with all the JavaScripts we need for "a good Show"
43 a = m:section( SimpleSection )
44 a.template = "ddns/overview_status"
46 -- SimpleSection definition -- #################################################
47 -- show Hints to optimize installation and script usage
48 -- only show if service not enabled
50 -- or not GNU Wget and not cURL (for https support)
51 -- or not GNU Wget but cURL without proxy support
53 -- or ddns-scripts package need update
54 if show_hints or need_update or not SYS.init.enabled("ddns") then
55 s = m:section( SimpleSection, translate("Hints") )
57 -- ddns_scripts needs to be updated for full functionality
59 local dv = s:option(DummyValue, "_update_needed")
60 dv.titleref = DISP.build_url("admin", "system", "packages")
62 dv.title = font_red .. bold_on ..
63 translate("Software update required") .. bold_off .. font_off
64 dv.value = translate("The currently installed 'ddns-scripts' package did not support all available settings.") ..
66 translate("Please update to the current version!")
69 -- DDNS Service disabled
70 if not SYS.init.enabled("ddns") then
71 local dv = s:option(DummyValue, "_not_enabled")
72 dv.titleref = DISP.build_url("admin", "system", "startup")
75 translate("DDNS Autostart disabled") .. bold_off
76 dv.value = translate("Currently DDNS updates are not started at boot or on interface events." .. "<br />" ..
77 "You can start/stop each configuration here. It will run until next reboot.")
80 -- Show more hints on a separate page
82 local dv = s:option(DummyValue, "_separate")
83 dv.titleref = DISP.build_url("admin", "services", "ddns", "hints")
86 translate("Show more") .. bold_off
87 dv.value = translate("Follow this link" .. "<br />" ..
88 "You will find more hints to optimize your system to run DDNS scripts with all options")
92 -- TableSection definition -- ##################################################
93 ts = m:section( TypedSection, "service",
94 translate("Overview"),
95 translate("Below is a list of configured DDNS configurations and their current state.")
97 .. translate("If you want to send updates for IPv4 and IPv6 you need to define two separate Configurations "
98 .. "i.e. 'myddns_ipv4' and 'myddns_ipv6'")
100 .. [[<a href="]] .. DISP.build_url("admin", "services", "ddns", "global") .. [[">]]
101 .. translate("To change global settings click here") .. [[</a>]] )
102 ts.sectionhead = translate("Configuration")
103 ts.template = "cbi/tblsection"
105 ts.extedit = DISP.build_url("admin", "services", "ddns", "detail", "%s")
106 function ts.create(self, name)
107 AbstractSection.create(self, name)
108 HTTP.redirect( self.extedit:format(name) )
111 -- Lookup_Host and registered IP -- #################################################
112 dom = ts:option(DummyValue, "_lookupIP",
113 translate("Lookup Hostname") .. "<br />" .. translate("Registered IP") )
114 dom.template = "ddns/overview_doubleline"
115 function dom.set_one(self, section)
116 local lookup = self.map:get(section, "lookup_host") or ""
120 return [[<em>]] .. translate("config error") .. [[</em>]]
123 function dom.set_two(self, section)
124 local lookup = self.map:get(section, "lookup_host") or ""
125 if lookup == "" then return "" end
126 local dnsserver = self.map:get(section, "dnsserver") or ""
127 local use_ipv6 = tonumber(self.map:get(section, "use_ipv6") or 0)
128 local force_ipversion = tonumber(self.map:get(section, "force_ipversion") or 0)
129 local force_dnstcp = tonumber(self.map:get(section, "force_dnstcp") or 0)
130 local command = [[/usr/lib/ddns/dynamic_dns_lucihelper.sh]]
131 if not NXFS.access(command, "rwx", "rx", "rx") then
132 NXFS.chmod(command, 755)
134 command = command .. [[ get_registered_ip ]] .. lookup .. [[ ]] .. use_ipv6 ..
135 [[ ]] .. force_ipversion .. [[ ]] .. force_dnstcp .. [[ ]] .. dnsserver
136 local ip = SYS.exec(command)
137 if ip == "" then ip = translate("no data") end
142 ena = ts:option( Flag, "enabled",
143 translate("Enabled"))
144 ena.template = "ddns/overview_enabled"
147 -- show PID and next update
148 upd = ts:option( DummyValue, "_update",
149 translate("Last Update") .. "<br />" .. translate("Next Update"))
150 upd.template = "ddns/overview_doubleline"
151 function upd.set_one(self, section) -- fill Last Update
152 -- get/validate last update
153 local uptime = SYS.uptime()
154 local lasttime = DDNS.get_lastupd(section)
155 if lasttime > uptime then -- /var might not be linked to /tmp and cleared on reboot
159 -- no last update happen
160 if lasttime == 0 then
161 return translate("never")
163 -- we read last update
166 -- os.epoch - sys.uptime + lastupdate(uptime)
167 local epoch = os.time() - uptime + lasttime
168 -- use linux date to convert epoch
169 return DDNS.epoch2date(epoch)
172 function upd.set_two(self, section) -- fill Next Update
174 local enabled = tonumber(self.map:get(section, "enabled") or 0)
175 local datenext = translate("unknown error") -- formatted date of next update
178 local force_interval = tonumber(self.map:get(section, "force_interval") or 72)
179 local force_unit = self.map:get(section, "force_unit") or "hours"
180 local force_seconds = DDNS.calc_seconds(force_interval, force_unit)
182 -- get last update and get/validate PID
183 local uptime = SYS.uptime()
184 local lasttime = DDNS.get_lastupd(section)
185 if lasttime > uptime then -- /var might not be linked to /tmp and cleared on reboot
188 local pid = DDNS.get_pid(section)
192 local epoch = os.time() - uptime + lasttime + force_seconds
193 -- use linux date to convert epoch
194 datelast = DDNS.epoch2date(epoch)
197 -- process running but update needs to happen
198 if pid > 0 and ( lasttime + force_seconds - uptime ) < 0 then
199 datenext = translate("Verify")
202 elseif force_seconds == 0 then
203 datenext = translate("Run once")
205 -- no process running and NOT enabled
206 elseif pid == 0 and enabled == 0 then
207 datenext = translate("Disabled")
209 -- no process running and NOT
210 elseif pid == 0 and enabled ~= 0 then
211 datenext = translate("Stopped")
218 btn = ts:option( Button, "_startstop",
219 translate("Process ID") .. "<br />" .. translate("Start / Stop") )
220 btn.template = "ddns/overview_startstop"
221 function btn.cfgvalue(self, section)
222 local pid = DDNS.get_pid(section)
224 btn.inputtitle = "PID: " .. pid
225 btn.inputstyle = "reset"
227 elseif (self.map:get(section, "enabled") or "0") ~= "0" then
228 btn.inputtitle = translate("Start")
229 btn.inputstyle = "apply"
232 btn.inputtitle = "----------"
233 btn.inputstyle = "button"