1 -- Copyright 2014 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
2 -- Licensed to the public under the Apache License 2.0.
4 module("luci.tools.ddns", package.seeall)
6 local NX = require "nixio"
7 local NXFS = require "nixio.fs"
8 local OPKG = require "luci.model.ipkg"
9 local UCI = require "luci.model.uci"
10 local SYS = require "luci.sys"
11 local UTIL = require "luci.util"
13 -- function to calculate seconds from given interval and unit
14 function calc_seconds(interval, unit)
15 if not tonumber(interval) then
17 elseif unit == "days" then
18 return (tonumber(interval) * 86400) -- 60 sec * 60 min * 24 h
19 elseif unit == "hours" then
20 return (tonumber(interval) * 3600) -- 60 sec * 60 min
21 elseif unit == "minutes" then
22 return (tonumber(interval) * 60) -- 60 sec
23 elseif unit == "seconds" then
24 return tonumber(interval)
30 -- check if IPv6 supported by OpenWrt
32 return NXFS.access("/proc/net/ipv6_route")
33 and NXFS.access("/usr/sbin/ip6tables")
36 -- check if Wget with SSL support or cURL installed
38 if (SYS.call([[ grep -iq "\+ssl" /usr/bin/wget 2>/dev/null ]]) == 0) then
41 return NXFS.access("/usr/bin/curl")
45 -- check if Wget with SSL or cURL with proxy support installed
46 function check_proxy()
47 -- we prefere GNU Wget for communication
48 if (SYS.call([[ grep -iq "\+ssl" /usr/bin/wget 2>/dev/null ]]) == 0) then
51 -- if not installed cURL must support proxy
52 elseif NXFS.access("/usr/bin/curl") then
53 return (SYS.call([[ grep -iq all_proxy /usr/lib/libcurl.so* 2>/dev/null ]]) == 0)
55 -- only BusyBox Wget is installed
57 return NXFS.access("/usr/bin/wget")
61 -- check if BIND host installed
62 function check_bind_host()
63 return NXFS.access("/usr/bin/host")
66 -- convert epoch date to given format
67 function epoch2date(epoch, format)
68 if not format or #format < 2 then
69 local uci = UCI.cursor()
70 format = uci:get("ddns", "global", "date_format") or "%F %R"
73 format = format:gsub("%%n", "<br />") -- replace newline
74 format = format:gsub("%%t", " ") -- replace tab
75 return os.date(format, epoch)
78 -- read lastupdate from [section].update file
79 function get_lastupd(section)
80 local uci = UCI.cursor()
81 local run_dir = uci:get("ddns", "global", "run_dir") or "/var/run/ddns"
82 local etime = tonumber(NXFS.readfile("%s/%s.update" % { run_dir, section } ) or 0 )
87 -- read PID from run file and verify if still running
88 function get_pid(section)
89 local uci = UCI.cursor()
90 local run_dir = uci:get("ddns", "global", "run_dir") or "/var/run/ddns"
91 local pid = tonumber(NXFS.readfile("%s/%s.pid" % { run_dir, section } ) or 0 )
92 if pid > 0 and not NX.kill(pid, 0) then
99 -- compare versions using "<=" "<" ">" ">=" "=" "<<" ">>"
100 function ipkg_ver_compare(ver1, comp, ver2)
101 if not ver1 or not (#ver1 > 0)
102 or not ver2 or not (#ver2 > 0)
103 or not comp or not (#comp > 0) then
106 return (tonumber(SYS.call(
107 [[opkg compare-versions "]] .. ver1 .. [[" "]] .. comp .. [[" "]] .. ver2 .. [["]]
111 -- read version information for given package if installed
112 function ipkg_ver_installed(pkg)
116 -- opkg list-installed [pkg] | cut -d " " -f 3 - return version as sting
117 local ver = SYS.exec([[opkg list-installed ]] .. pkg .. [[ | cut -d " " -f 3 ]])
124 -- replacement of build-in read of UCI option
125 -- modified AbstractValue.cfgvalue(self, section) from cbi.lua
126 -- needed to read from other option then current value definition
127 function read_value(self, section, option)
129 if self.tag_error[section] then
130 value = self:formvalue(section)
132 value = self.map:get(section, option)
137 elseif not self.cast or self.cast == type(value) then
139 elseif self.cast == "string" then
140 if type(value) == "table" then
143 elseif self.cast == "table" then
148 -- replacement of build-in Flag.parse of cbi.lua
149 -- modified to mark section as changed if value changes
150 -- current parse did not do this, but it is done AbstaractValue.parse()
151 function flag_parse(self, section)
152 local fexists = self.map:formvalue(
153 luci.cbi.FEXIST_PREFIX .. self.config .. "." .. section .. "." .. self.option)
156 local fvalue = self:formvalue(section) and self.enabled or self.disabled
157 local cvalue = self:cfgvalue(section)
158 if fvalue ~= self.default or (not self.optional and not self.rmempty) then
159 self:write(section, fvalue)
163 if (fvalue ~= cvalue) then self.section.changed = true end
166 self.section.changed = true
170 -----------------------------------------------------------------------------
171 -- copied from https://svn.nmap.org/nmap/nselib/url.lua
172 -- @author Diego Nehab
173 -- @author Eddie Bell <ejlbell@gmail.com>
175 URI parsing, composition and relative URL resolution
178 RCS ID: $Id: url.lua,v 1.37 2005/11/22 08:33:29 diego Exp $
179 parse_query and build_query added For nmap (Eddie Bell <ejlbell@gmail.com>)
182 -- Parses a URL and returns a table with all its parts according to RFC 2396.
184 -- The following grammar describes the names given to the URL parts.
186 -- <url> ::= <scheme>://<authority>/<path>;<params>?<query>#<fragment>
187 -- <authority> ::= <userinfo>@<host>:<port>
188 -- <userinfo> ::= <user>[:<password>]
189 -- <path> :: = {<segment>/}<segment>
192 -- The leading <code>/</code> in <code>/<path></code> is considered part of
193 -- <code><path></code>.
194 -- @param url URL of request.
195 -- @param default Table with default values for each field.
196 -- @return A table with the following fields, where RFC naming conventions have
198 -- <code>scheme</code>, <code>authority</code>, <code>userinfo</code>,
199 -- <code>user</code>, <code>password</code>, <code>host</code>,
200 -- <code>port</code>, <code>path</code>, <code>params</code>,
201 -- <code>query</code>, and <code>fragment</code>.
202 -----------------------------------------------------------------------------
203 function parse_url(url) --, default)
204 -- initialize default parameters
206 -- for i,v in base.pairs(default or parsed) do
211 -- url = string.gsub(url, "%s", "")
213 url = string.gsub(url, "#(.*)$",
218 -- get scheme. Lower-case according to RFC 3986 section 3.1.
219 url = string.gsub(url, "^([%w][%w%+%-%.]*)%:",
221 parsed.scheme = string.lower(s);
225 url = string.gsub(url, "^//([^/]*)",
230 -- get query stringing
231 url = string.gsub(url, "%?(.*)",
237 url = string.gsub(url, "%;(.*)",
242 -- path is whatever was left
245 local authority = parsed.authority
246 if not authority then
249 authority = string.gsub(authority,"^([^@]*)@",
254 authority = string.gsub(authority, ":([0-9]*)$",
261 if authority ~= "" then
262 parsed.host = authority
265 local userinfo = parsed.userinfo
269 userinfo = string.gsub(userinfo, ":([^:]*)$",
274 parsed.user = userinfo