2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6 Copyright 2014 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
8 Licensed under the Apache License, Version 2.0 (the "License");
9 you may not use this file except in compliance with the License.
10 You may obtain a copy of the License at
12 http://www.apache.org/licenses/LICENSE-2.0
17 module("luci.controller.ddns", package.seeall)
19 local NX = require "nixio"
20 local NXFS = require "nixio.fs"
21 local DISP = require "luci.dispatcher"
22 local HTTP = require "luci.http"
23 local UCI = require "luci.model.uci"
24 local SYS = require "luci.sys"
25 local DDNS = require "luci.tools.ddns" -- ddns multiused functions
26 local UTIL = require "luci.util"
28 local luci_ddns_version = "2.1.0-3" -- luci-app-ddns / openwrt Makefile compatible version
29 local ddns_scripts_min = "2.1.0-2" -- minimum version of ddns-scripts required
32 -- no services_ipv6 file or no dynamic_dns_lucihelper.sh
34 if not nixio.fs.access("/usr/lib/ddns/services_ipv6")
35 or not nixio.fs.access("/usr/lib/ddns/dynamic_dns_lucihelper.sh") then
38 -- no config create an empty one
39 if not nixio.fs.access("/etc/config/ddns") then
40 nixio.fs.writefile("/etc/config/ddns", "")
43 entry( {"admin", "services", "ddns"}, cbi("ddns/overview"), _("Dynamic DNS"), 59)
44 entry( {"admin", "services", "ddns", "detail"}, cbi("ddns/detail"), nil ).leaf = true
45 entry( {"admin", "services", "ddns", "hints"}, cbi("ddns/hints",
46 {hideapplybtn=true, hidesavebtn=true, hideresetbtn=true}), nil ).leaf = true
47 entry( {"admin", "services", "ddns", "logview"}, call("logread") ).leaf = true
48 entry( {"admin", "services", "ddns", "startstop"}, call("startstop") ).leaf = true
49 entry( {"admin", "services", "ddns", "status"}, call("status") ).leaf = true
52 -- function to read all sections status and return data array
53 local function _get_status()
54 local uci = UCI.cursor()
55 local service = SYS.init.enabled("ddns") and 1 or 0
56 local url_start = DISP.build_url("admin", "system", "startup")
57 local luci_build = DDNS.ipkg_version("luci-app-ddns").version
58 local ddns_act = DDNS.ipkg_version("ddns-scripts").version
59 local data = {} -- Array to transfer data to javascript
62 enabled = service, -- service enabled
63 url_up = url_start, -- link to enable DDS (System-Startup)
64 luci_ver = luci_ddns_version, -- luci-app-ddns / openwrt Makefile compatible version
65 luci_build = luci_build, -- installed luci build
66 script_min = ddns_scripts_min, -- minimum version of ddns-scripts needed
67 script_ver = ddns_act -- installed ddns-scripts
70 uci:foreach("ddns", "service", function (s)
72 -- Get section we are looking at
74 local section = s[".name"]
75 local enabled = tonumber(s["enabled"]) or 0
76 local datelast = "_empty_" -- formated date of last update
77 local datenext = "_empty_" -- formated date of next update
80 local force_seconds = DDNS.calc_seconds(
81 tonumber(s["force_interval"]) or 72 ,
82 s["force_unit"] or "hours" )
83 -- get/validate pid and last update
84 local pid = DDNS.get_pid(section)
85 local uptime = SYS.uptime()
86 local lasttime = DDNS.get_lastupd(section)
87 if lasttime > uptime then -- /var might not be linked to /tmp
88 lasttime = 0 -- and/or not cleared on reboot
91 -- no last update happen
95 -- we read last update
98 -- sys.epoch - sys uptime + lastupdate(uptime)
99 local epoch = os.time() - uptime + lasttime
100 -- use linux date to convert epoch
101 datelast = DDNS.epoch2date(epoch)
102 -- calc and fill next update
103 datenext = DDNS.epoch2date(epoch + force_seconds)
106 -- process running but update needs to happen
107 -- problems it force_seconds > uptime
108 force_seconds = (force_seconds > uptime) and uptime or force_seconds
109 if pid > 0 and ( lasttime + force_seconds - uptime ) <= 0 then
110 datenext = "_verify_"
113 elseif force_seconds == 0 then
114 datenext = "_runonce_"
116 -- no process running and NOT enabled
117 elseif pid == 0 and enabled == 0 then
118 datenext = "_disabled_"
120 -- no process running and NOT
121 elseif pid == 0 and enabled ~= 0 then
122 datenext = "_stopped_"
125 -- get/set monitored interface and IP version
126 local iface = s["interface"] or "_nonet_"
127 local use_ipv6 = tonumber(s["use_ipv6"]) or 0
128 if iface ~= "_nonet_" then
129 local ipv = (use_ipv6 == 1) and "IPv6" or "IPv4"
130 iface = ipv .. " / " .. iface
133 -- try to get registered IP
134 local domain = s["domain"] or "_nodomain_"
135 local dnsserver = s["dns_server"] or ""
136 local force_ipversion = tonumber(s["force_ipversion"] or 0)
137 local force_dnstcp = tonumber(s["force_dnstcp"] or 0)
138 local command = [[/usr/lib/ddns/dynamic_dns_lucihelper.sh]]
139 command = command .. [[ get_registered_ip ]] .. domain .. [[ ]] .. use_ipv6 ..
140 [[ ]] .. force_ipversion .. [[ ]] .. force_dnstcp .. [[ ]] .. dnsserver
141 local reg_ip = SYS.exec(command)
146 -- fill transfer array
163 -- called by XHR.get from detail_logview.htm
164 function logread(section)
165 -- read application settings
166 local uci = UCI.cursor()
167 local log_dir = uci:get("ddns", "global", "log_dir") or "/var/log/ddns"
168 local lfile=log_dir .. "/" .. section .. ".log"
170 local ldata=NXFS.readfile(lfile)
171 if not ldata or #ldata == 0 then
178 -- called by XHR.get from overview_status.htm
179 function startstop(section, enabled)
180 local uci = UCI.cursor()
181 local data = {} -- Array to transfer data to javascript
183 -- if process running we want to stop and return
184 local pid = DDNS.get_pid(section)
186 local tmp = NX.kill(pid, 15) -- terminate
187 NX.nanosleep(2) -- 2 second "show time"
188 -- status changed so return full status
190 HTTP.prepare_content("application/json")
191 HTTP.write_json(data)
195 -- read uncommited changes
196 -- we don't save and commit data from other section or other options
197 -- only enabled will be done
199 local changed = uci:changes("ddns")
200 for k_config, v_section in pairs(changed) do
201 -- security check because uci.changes only gets our config
202 if k_config ~= "ddns" then
206 for k_section, v_option in pairs(v_section) do
207 -- check if only section of button was changed
208 if k_section ~= section then
212 for k_option, v_value in pairs(v_option) do
213 -- check if only enabled was changed
214 if k_option ~= "enabled" then
222 -- we can not execute because other
223 -- uncommited changes pending, so exit here
225 HTTP.write("_uncommited_")
230 uci:set("ddns", section, "enabled", ( (enabled == "true") and "1" or "0") )
235 -- start dynamic_dns_updater.sh script
236 os.execute ([[/usr/lib/ddns/dynamic_dns_updater.sh %s 0 > /dev/null 2>&1 &]] % section)
237 NX.nanosleep(3) -- 3 seconds "show time"
239 -- status changed so return full status
241 HTTP.prepare_content("application/json")
242 HTTP.write_json(data)
245 -- called by XHR.poll from overview_status.htm
247 local data = _get_status()
248 HTTP.prepare_content("application/json")
249 HTTP.write_json(data)
252 -- check if installed ddns-scripts version < required version
253 function update_needed()
254 local sver = DDNS.ipkg_version("ddns-scripts")
255 local rver = UTIL.split(ddns_scripts_min, "[%.%-]", nil, true)
256 return (sver.major < (tonumber(rver[1]) or 0))
257 or (sver.minor < (tonumber(rver[2]) or 0))
258 or (sver.patch < (tonumber(rver[3]) or 0))
259 or (sver.build < (tonumber(rver[4]) or 0))