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 local srv_name = "ddns-scripts"
21 local srv_ver_min = "2.6.0" -- minimum version of service required
22 local srv_ver_cmd = [[/usr/lib/ddns/dynamic_dns_updater.sh --version | awk {'print $2'}]]
23 local app_name = "luci-app-ddns"
24 local app_title = "Dynamic DNS"
25 local app_version = "2.4.1-1"
28 local nxfs = require "nixio.fs" -- global definitions not available
29 local sys = require "luci.sys" -- in function index()
30 local ddns = require "luci.tools.ddns" -- ddns multiused functions
31 local muci = require "luci.model.uci"
33 -- no config create an empty one
34 if not nxfs.access("/etc/config/ddns") then
35 nxfs.writefile("/etc/config/ddns", "")
38 -- preset new option "lookup_host" if not already defined
39 local uci = muci.cursor()
41 uci:foreach("ddns", "service", function (s)
42 if not s["lookup_host"] and s["domain"] then
43 uci:set("ddns", s[".name"], "lookup_host", s["domain"])
47 if commit then uci:commit("ddns") end
50 entry( {"admin", "services", "ddns"}, cbi("ddns/overview"), _("Dynamic DNS"), 59)
51 entry( {"admin", "services", "ddns", "detail"}, cbi("ddns/detail"), nil ).leaf = true
52 entry( {"admin", "services", "ddns", "hints"}, cbi("ddns/hints",
53 {hideapplybtn=true, hidesavebtn=true, hideresetbtn=true}), nil ).leaf = true
54 entry( {"admin", "services", "ddns", "global"}, cbi("ddns/global"), nil ).leaf = true
55 entry( {"admin", "services", "ddns", "logview"}, call("logread") ).leaf = true
56 entry( {"admin", "services", "ddns", "startstop"}, post("startstop") ).leaf = true
57 entry( {"admin", "services", "ddns", "status"}, call("status") ).leaf = true
60 -- Application specific information functions
61 function app_description()
62 return I18N.translate("Dynamic DNS allows that your router can be reached with " ..
63 "a fixed hostname while having a dynamically changing IP address.")
65 .. I18N.translate("OpenWrt Wiki") .. ": "
66 .. [[<a href="http://wiki.openwrt.org/doc/howto/ddns.client" target="_blank">]]
67 .. I18N.translate("DDNS Client Documentation") .. [[</a>]]
69 .. [[<a href="http://wiki.openwrt.org/doc/uci/ddns" target="_blank">]]
70 .. I18N.translate("DDNS Client Configuration") .. [[</a>]]
72 function app_title_back()
74 .. DISP.build_url("admin", "services", "ddns")
76 .. I18N.translate(app_title)
80 -- Standardized application/service functions
81 function app_title_main()
82 return [[<a href="javascript:alert(']]
83 .. I18N.translate("Version Information")
84 .. [[\n\n]] .. app_name
85 .. [[\n\t]] .. I18N.translate("Version") .. [[:\t]] .. app_version
86 .. [[\n\n]] .. srv_name .. [[ ]] .. I18N.translate("required") .. [[:]]
87 .. [[\n\t]] .. I18N.translate("Version") .. [[:\t]]
88 .. srv_ver_min .. [[ ]] .. I18N.translate("or higher")
89 .. [[\n\n]] .. srv_name .. [[ ]] .. I18N.translate("installed") .. [[:]]
90 .. [[\n\t]] .. I18N.translate("Version") .. [[:\t]]
91 .. (service_version() or I18N.translate("NOT installed"))
94 .. I18N.translate(app_title)
97 function service_version()
99 IPKG.list_installed(srv_name, function(n, v, d)
100 if v and (#v > 0) then ver = v end
103 if not ver or (#ver == 0) then
104 ver = UTIL.exec(srv_ver_cmd)
105 if #ver == 0 then ver = nil end
109 function service_ok()
110 return IPKG.compare_versions((service_version() or "0"), ">=", srv_ver_min)
113 -- internal function to read all sections status and return data array
114 local function _get_status()
115 local uci = UCI.cursor()
116 local service = SYS.init.enabled("ddns") and 1 or 0
117 local url_start = DISP.build_url("admin", "system", "startup")
118 local data = {} -- Array to transfer data to javascript
121 enabled = service, -- service enabled
122 url_up = url_start, -- link to enable DDS (System-Startup)
125 uci:foreach("ddns", "service", function (s)
127 -- Get section we are looking at
129 local section = s[".name"]
130 local enabled = tonumber(s["enabled"]) or 0
131 local datelast = "_empty_" -- formatted date of last update
132 local datenext = "_empty_" -- formatted date of next update
135 local force_seconds = DDNS.calc_seconds(
136 tonumber(s["force_interval"]) or 72 ,
137 s["force_unit"] or "hours" )
138 -- get/validate pid and last update
139 local pid = DDNS.get_pid(section)
140 local uptime = SYS.uptime()
141 local lasttime = DDNS.get_lastupd(section)
142 if lasttime > uptime then -- /var might not be linked to /tmp
143 lasttime = 0 -- and/or not cleared on reboot
146 -- no last update happen
147 if lasttime == 0 then
150 -- we read last update
153 -- sys.epoch - sys uptime + lastupdate(uptime)
154 local epoch = os.time() - uptime + lasttime
155 -- use linux date to convert epoch
156 datelast = DDNS.epoch2date(epoch)
157 -- calc and fill next update
158 datenext = DDNS.epoch2date(epoch + force_seconds)
161 -- process running but update needs to happen
162 -- problems if force_seconds > uptime
163 force_seconds = (force_seconds > uptime) and uptime or force_seconds
164 if pid > 0 and ( lasttime + force_seconds - uptime ) <= 0 then
165 datenext = "_verify_"
168 elseif force_seconds == 0 then
169 datenext = "_runonce_"
171 -- no process running and NOT enabled
172 elseif pid == 0 and enabled == 0 then
173 datenext = "_disabled_"
175 -- no process running and enabled
176 elseif pid == 0 and enabled ~= 0 then
177 datenext = "_stopped_"
180 -- get/set monitored interface and IP version
181 local iface = s["interface"] or "_nonet_"
182 local use_ipv6 = tonumber(s["use_ipv6"]) or 0
183 if iface ~= "_nonet_" then
184 local ipv = (use_ipv6 == 1) and "IPv6" or "IPv4"
185 iface = ipv .. " / " .. iface
188 -- try to get registered IP
189 local lookup_host = s["lookup_host"] or "_nolookup_"
190 local dnsserver = s["dns_server"] or ""
191 local force_ipversion = tonumber(s["force_ipversion"] or 0)
192 local force_dnstcp = tonumber(s["force_dnstcp"] or 0)
193 local command = [[/usr/lib/ddns/dynamic_dns_lucihelper.sh]]
194 command = command .. [[ get_registered_ip ]] .. lookup_host .. [[ ]] .. use_ipv6 ..
195 [[ ]] .. force_ipversion .. [[ ]] .. force_dnstcp .. [[ ]] .. dnsserver
196 local reg_ip = SYS.exec(command)
201 -- fill transfer array
206 lookup = lookup_host,
218 -- called by XHR.get from detail_logview.htm
219 function logread(section)
220 -- read application settings
221 local uci = UCI.cursor()
222 local log_dir = uci:get("ddns", "global", "log_dir") or "/var/log/ddns"
223 local lfile = log_dir .. "/" .. section .. ".log"
224 local ldata = NXFS.readfile(lfile)
226 if not ldata or #ldata == 0 then
233 -- called by XHR.get from overview_status.htm
234 function startstop(section, enabled)
235 local uci = UCI.cursor()
236 local pid = DDNS.get_pid(section)
237 local data = {} -- Array to transfer data to javascript
239 -- if process running we want to stop and return
241 local tmp = NX.kill(pid, 15) -- terminate
242 NX.nanosleep(2) -- 2 second "show time"
243 -- status changed so return full status
245 HTTP.prepare_content("application/json")
246 HTTP.write_json(data)
250 -- read uncommitted changes
251 -- we don't save and commit data from other section or other options
252 -- only enabled will be done
254 local changed = uci:changes("ddns")
255 for k_config, v_section in pairs(changed) do
256 -- security check because uci.changes only gets our config
257 if k_config ~= "ddns" then
261 for k_section, v_option in pairs(v_section) do
262 -- check if only section of button was changed
263 if k_section ~= section then
267 for k_option, v_value in pairs(v_option) do
268 -- check if only enabled was changed
269 if k_option ~= "enabled" then
277 -- we can not execute because other
278 -- uncommitted changes pending, so exit here
280 HTTP.write("_uncommitted_")
285 uci:set("ddns", section, "enabled", ( (enabled == "true") and "1" or "0") )
290 -- start dynamic_dns_updater.sh script
291 os.execute ([[/usr/lib/ddns/dynamic_dns_updater.sh %s 0 > /dev/null 2>&1 &]] % section)
292 NX.nanosleep(3) -- 3 seconds "show time"
294 -- status changed so return full status
296 HTTP.prepare_content("application/json")
297 HTTP.write_json(data)
300 -- called by XHR.poll from overview_status.htm
302 local data = _get_status()
303 HTTP.prepare_content("application/json")
304 HTTP.write_json(data)