1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
3 -- Copyright 2013 Manuel Munz <freifunk at somakoma dot de>
4 -- Copyright 2014-2016 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
5 -- Licensed to the public under the Apache License 2.0.
7 module("luci.controller.ddns", package.seeall)
9 local NX = require "nixio"
10 local NXFS = require "nixio.fs"
11 local DISP = require "luci.dispatcher"
12 local HTTP = require "luci.http"
13 local I18N = require "luci.i18n" -- not globally avalible here
14 local IPKG = require "luci.model.ipkg"
15 local SYS = require "luci.sys"
16 local UCI = require "luci.model.uci"
17 local UTIL = require "luci.util"
18 local DDNS = require "luci.tools.ddns" -- ddns multiused functions
20 luci_helper = "/usr/lib/ddns/dynamic_dns_lucihelper.sh"
22 local srv_name = "ddns-scripts"
23 local srv_ver_min = "2.7.6" -- minimum version of service required
24 local srv_ver_cmd = luci_helper .. [[ -V | awk {'print $2'}]]
25 local app_name = "luci-app-ddns"
26 local app_title = "Dynamic DNS"
27 local app_version = "2.4.8-1"
30 local nxfs = require "nixio.fs" -- global definitions not available
31 local sys = require "luci.sys" -- in function index()
32 local ddns = require "luci.tools.ddns" -- ddns multiused functions
33 local muci = require "luci.model.uci"
35 -- no config create an empty one
36 if not nxfs.access("/etc/config/ddns") then
37 nxfs.writefile("/etc/config/ddns", "")
40 -- preset new option "lookup_host" if not already defined
41 local uci = muci.cursor()
43 uci:foreach("ddns", "service", function (s)
44 if not s["lookup_host"] and s["domain"] then
45 uci:set("ddns", s[".name"], "lookup_host", s["domain"])
49 if commit then uci:commit("ddns") end
52 entry( {"admin", "services", "ddns"}, cbi("ddns/overview"), _("Dynamic DNS"), 59)
53 entry( {"admin", "services", "ddns", "detail"}, cbi("ddns/detail"), nil ).leaf = true
54 entry( {"admin", "services", "ddns", "hints"}, cbi("ddns/hints",
55 {hideapplybtn=true, hidesavebtn=true, hideresetbtn=true}), nil ).leaf = true
56 entry( {"admin", "services", "ddns", "global"}, cbi("ddns/global"), nil ).leaf = true
57 entry( {"admin", "services", "ddns", "logview"}, call("logread") ).leaf = true
58 entry( {"admin", "services", "ddns", "startstop"}, post("startstop") ).leaf = true
59 entry( {"admin", "services", "ddns", "status"}, call("status") ).leaf = true
62 -- Application specific information functions
63 function app_description()
64 return I18N.translate("Dynamic DNS allows that your router can be reached with " ..
65 "a fixed hostname while having a dynamically changing IP address.")
67 .. I18N.translate("OpenWrt Wiki") .. ": "
68 .. [[<a href="http://wiki.openwrt.org/doc/howto/ddns.client" target="_blank">]]
69 .. I18N.translate("DDNS Client Documentation") .. [[</a>]]
71 .. [[<a href="http://wiki.openwrt.org/doc/uci/ddns" target="_blank">]]
72 .. I18N.translate("DDNS Client Configuration") .. [[</a>]]
74 function app_title_back()
76 .. DISP.build_url("admin", "services", "ddns")
78 .. I18N.translate(app_title)
82 -- Standardized application/service functions
83 function app_title_main()
84 return [[<a href="javascript:alert(']]
85 .. I18N.translate("Version Information")
86 .. [[\n\n]] .. app_name
87 .. [[\n\t]] .. I18N.translate("Version") .. [[:\t]] .. app_version
88 .. [[\n\n]] .. srv_name .. [[ ]] .. I18N.translate("required") .. [[:]]
89 .. [[\n\t]] .. I18N.translate("Version") .. [[:\t]]
90 .. srv_ver_min .. [[ ]] .. I18N.translate("or higher")
91 .. [[\n\n]] .. srv_name .. [[ ]] .. I18N.translate("installed") .. [[:]]
92 .. [[\n\t]] .. I18N.translate("Version") .. [[:\t]]
93 .. (service_version() or I18N.translate("NOT installed"))
96 .. I18N.translate(app_title)
99 function service_version()
102 ver = UTIL.exec(srv_ver_cmd)
103 if #ver > 0 then return ver end
105 IPKG.list_installed(srv_name, function(n, v, d)
106 if v and (#v > 0) then ver = v end
111 function service_ok()
112 return IPKG.compare_versions((service_version() or "0"), ">=", srv_ver_min)
115 -- internal function to read all sections status and return data array
116 local function _get_status()
117 local uci = UCI.cursor()
118 local service = SYS.init.enabled("ddns") and 1 or 0
119 local url_start = DISP.build_url("admin", "system", "startup")
120 local data = {} -- Array to transfer data to javascript
123 enabled = service, -- service enabled
124 url_up = url_start, -- link to enable DDS (System-Startup)
127 uci:foreach("ddns", "service", function (s)
129 -- Get section we are looking at
131 local section = s[".name"]
132 local enabled = tonumber(s["enabled"]) or 0
133 local datelast = "_empty_" -- formatted date of last update
134 local datenext = "_empty_" -- formatted date of next update
137 local force_seconds = DDNS.calc_seconds(
138 tonumber(s["force_interval"]) or 72 ,
139 s["force_unit"] or "hours" )
140 -- get/validate pid and last update
141 local pid = DDNS.get_pid(section)
142 local uptime = SYS.uptime()
143 local lasttime = DDNS.get_lastupd(section)
144 if lasttime > uptime then -- /var might not be linked to /tmp
145 lasttime = 0 -- and/or not cleared on reboot
148 -- no last update happen
149 if lasttime == 0 then
152 -- we read last update
155 -- sys.epoch - sys uptime + lastupdate(uptime)
156 local epoch = os.time() - uptime + lasttime
157 -- use linux date to convert epoch
158 datelast = DDNS.epoch2date(epoch)
159 -- calc and fill next update
160 datenext = DDNS.epoch2date(epoch + force_seconds)
163 -- process running but update needs to happen
164 -- problems if force_seconds > uptime
165 force_seconds = (force_seconds > uptime) and uptime or force_seconds
166 if pid > 0 and ( lasttime + force_seconds - uptime ) <= 0 then
167 datenext = "_verify_"
170 elseif force_seconds == 0 then
171 datenext = "_runonce_"
173 -- no process running and NOT enabled
174 elseif pid == 0 and enabled == 0 then
175 datenext = "_disabled_"
177 -- no process running and enabled
178 elseif pid == 0 and enabled ~= 0 then
179 datenext = "_stopped_"
182 -- get/set monitored interface and IP version
183 local iface = s["interface"] or "_nonet_"
184 local use_ipv6 = tonumber(s["use_ipv6"]) or 0
185 if iface ~= "_nonet_" then
186 local ipv = (use_ipv6 == 1) and "IPv6" or "IPv4"
187 iface = ipv .. " / " .. iface
190 -- try to get registered IP
191 local lookup_host = s["lookup_host"] or "_nolookup_"
192 local dnsserver = s["dns_server"] or ""
193 local force_ipversion = tonumber(s["force_ipversion"] or 0)
194 local force_dnstcp = tonumber(s["force_dnstcp"] or 0)
195 local is_glue = tonumber(s["is_glue"] or 0)
196 local command = luci_helper .. [[ -]]
197 if (use_ipv6 == 1) then command = command .. [[6]] end
198 if (force_ipversion == 1) then command = command .. [[f]] end
199 if (force_dnstcp == 1) then command = command .. [[t]] end
200 if (is_glue == 1) then command = command .. [[g]] end
201 command = command .. [[l ]] .. lookup_host
202 if (#dnsserver > 0) then command = command .. [[ -d ]] .. dnsserver end
203 command = command .. [[ -- get_registered_ip]]
204 local reg_ip = SYS.exec(command)
209 -- fill transfer array
214 lookup = lookup_host,
226 -- called by XHR.get from detail_logview.htm
227 function logread(section)
228 -- read application settings
229 local uci = UCI.cursor()
230 local ldir = uci:get("ddns", "global", "ddns_logdir") or "/var/log/ddns"
231 local lfile = ldir .. "/" .. section .. ".log"
232 local ldata = NXFS.readfile(lfile)
234 if not ldata or #ldata == 0 then
241 -- called by XHR.get from overview_status.htm
242 function startstop(section, enabled)
243 local uci = UCI.cursor()
244 local pid = DDNS.get_pid(section)
245 local data = {} -- Array to transfer data to javascript
247 -- if process running we want to stop and return
249 local tmp = NX.kill(pid, 15) -- terminate
250 NX.nanosleep(2) -- 2 second "show time"
251 -- status changed so return full status
253 HTTP.prepare_content("application/json")
254 HTTP.write_json(data)
258 -- read uncommitted changes
259 -- we don't save and commit data from other section or other options
260 -- only enabled will be done
262 local changed = uci:changes("ddns")
263 for k_config, v_section in pairs(changed) do
264 -- security check because uci.changes only gets our config
265 if k_config ~= "ddns" then
269 for k_section, v_option in pairs(v_section) do
270 -- check if only section of button was changed
271 if k_section ~= section then
275 for k_option, v_value in pairs(v_option) do
276 -- check if only enabled was changed
277 if k_option ~= "enabled" then
285 -- we can not execute because other
286 -- uncommitted changes pending, so exit here
288 HTTP.write("_uncommitted_")
293 uci:set("ddns", section, "enabled", ( (enabled == "true") and "1" or "0") )
298 -- start ddns-updater for section
299 local command = luci_helper .. [[ -S ]] .. section .. [[ -- start]]
301 NX.nanosleep(3) -- 3 seconds "show time"
303 -- status changed so return full status
305 HTTP.prepare_content("application/json")
306 HTTP.write_json(data)
309 -- called by XHR.poll from overview_status.htm
311 local data = _get_status()
312 HTTP.prepare_content("application/json")
313 HTTP.write_json(data)