2 LuCI - Lua Configuration Interface
4 shared module for luci-app-ddns
5 Copyright 2014 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
7 function parse_url copied from https://svn.nmap.org/nmap/nselib/url.lua
8 Parses a URL and returns a table with all its parts according to RFC 2396.
9 @author Diego Nehab @author Eddie Bell <ejlbell@gmail.com>
11 Licensed under the Apache License, Version 2.0 (the "License");
12 you may not use this file except in compliance with the License.
13 You may obtain a copy of the License at
15 http://www.apache.org/licenses/LICENSE-2.0
19 module("luci.tools.ddns", package.seeall)
21 local NX = require "nixio"
22 local NXFS = require "nixio.fs"
23 local OPKG = require "luci.model.ipkg"
24 local UCI = require "luci.model.uci"
25 local SYS = require "luci.sys"
26 local UTIL = require "luci.util"
28 -- function to calculate seconds from given interval and unit
29 function calc_seconds(interval, unit)
30 if not tonumber(interval) then
32 elseif unit == "days" then
33 return (tonumber(interval) * 86400) -- 60 sec * 60 min * 24 h
34 elseif unit == "hours" then
35 return (tonumber(interval) * 3600) -- 60 sec * 60 min
36 elseif unit == "minutes" then
37 return (tonumber(interval) * 60) -- 60 sec
38 elseif unit == "seconds" then
39 return tonumber(interval)
45 -- check if IPv6 supported by OpenWrt
47 return NXFS.access("/proc/net/ipv6_route")
48 and NXFS.access("/usr/sbin/ip6tables")
51 -- check if Wget with SSL support or cURL installed
53 if (SYS.call([[ grep -iq "\+ssl" /usr/bin/wget 2>/dev/null ]]) == 0) then
56 return NXFS.access("/usr/bin/curl")
60 -- check if Wget with SSL or cURL with proxy support installed
61 function check_proxy()
62 -- we prefere GNU Wget for communication
63 if (SYS.call([[ grep -iq "\+ssl" /usr/bin/wget 2>/dev/null ]]) == 0) then
66 -- if not installed cURL must support proxy
67 elseif NXFS.access("/usr/bin/curl") then
68 return (SYS.call([[ grep -iq all_proxy /usr/lib/libcurl.so* 2>/dev/null ]]) == 0)
70 -- only BusyBox Wget is installed
72 return NXFS.access("/usr/bin/wget")
76 -- check if BIND host installed
77 function check_bind_host()
78 return NXFS.access("/usr/bin/host")
81 -- convert epoch date to given format
82 function epoch2date(epoch, format)
83 if not format or #format < 2 then
84 local uci = UCI.cursor()
85 format = uci:get("ddns", "global", "date_format") or "%F %R"
88 format = format:gsub("%%n", "<br />") -- replace newline
89 format = format:gsub("%%t", " ") -- replace tab
90 return os.date(format, epoch)
93 -- read lastupdate from [section].update file
94 function get_lastupd(section)
95 local uci = UCI.cursor()
96 local run_dir = uci:get("ddns", "global", "run_dir") or "/var/run/ddns"
97 local etime = tonumber(NXFS.readfile("%s/%s.update" % { run_dir, section } ) or 0 )
102 -- read PID from run file and verify if still running
103 function get_pid(section)
104 local uci = UCI.cursor()
105 local run_dir = uci:get("ddns", "global", "run_dir") or "/var/run/ddns"
106 local pid = tonumber(NXFS.readfile("%s/%s.pid" % { run_dir, section } ) or 0 )
107 if pid > 0 and not NX.kill(pid, 0) then
114 -- read version information for given package if installed
115 function ipkg_version(package)
119 local info = OPKG.info(package)
123 for k, v in pairs(info) do
124 if v.Package == package and v.Status.installed then
129 if i > 1 then -- more then one valid record
132 local sver = UTIL.split(version, "[%.%-]", nil, true)
135 major = tonumber(sver[1]) or 0,
136 minor = tonumber(sver[2]) or 0,
137 patch = tonumber(sver[3]) or 0,
138 build = tonumber(sver[4]) or 0
143 -- replacement of build-in read of UCI option
144 -- modified AbstractValue.cfgvalue(self, section) from cbi.lua
145 -- needed to read from other option then current value definition
146 function read_value(self, section, option)
148 if self.tag_error[section] then
149 value = self:formvalue(section)
151 value = self.map:get(section, option)
156 elseif not self.cast or self.cast == type(value) then
158 elseif self.cast == "string" then
159 if type(value) == "table" then
162 elseif self.cast == "table" then
167 -- replacement of build-in Flag.parse of cbi.lua
168 -- modified to mark section as changed if value changes
169 -- current parse did not do this, but it is done AbstaractValue.parse()
170 function flag_parse(self, section)
171 local fexists = self.map:formvalue(
172 luci.cbi.FEXIST_PREFIX .. self.config .. "." .. section .. "." .. self.option)
175 local fvalue = self:formvalue(section) and self.enabled or self.disabled
176 local cvalue = self:cfgvalue(section)
177 if fvalue ~= self.default or (not self.optional and not self.rmempty) then
178 self:write(section, fvalue)
182 if (fvalue ~= cvalue) then self.section.changed = true end
185 self.section.changed = true
189 -----------------------------------------------------------------------------
190 -- copied from https://svn.nmap.org/nmap/nselib/url.lua
191 -- @author Diego Nehab
192 -- @author Eddie Bell <ejlbell@gmail.com>
194 URI parsing, composition and relative URL resolution
197 RCS ID: $Id: url.lua,v 1.37 2005/11/22 08:33:29 diego Exp $
198 parse_query and build_query added For nmap (Eddie Bell <ejlbell@gmail.com>)
201 -- Parses a URL and returns a table with all its parts according to RFC 2396.
203 -- The following grammar describes the names given to the URL parts.
205 -- <url> ::= <scheme>://<authority>/<path>;<params>?<query>#<fragment>
206 -- <authority> ::= <userinfo>@<host>:<port>
207 -- <userinfo> ::= <user>[:<password>]
208 -- <path> :: = {<segment>/}<segment>
211 -- The leading <code>/</code> in <code>/<path></code> is considered part of
212 -- <code><path></code>.
213 -- @param url URL of request.
214 -- @param default Table with default values for each field.
215 -- @return A table with the following fields, where RFC naming conventions have
217 -- <code>scheme</code>, <code>authority</code>, <code>userinfo</code>,
218 -- <code>user</code>, <code>password</code>, <code>host</code>,
219 -- <code>port</code>, <code>path</code>, <code>params</code>,
220 -- <code>query</code>, and <code>fragment</code>.
221 -----------------------------------------------------------------------------
222 function parse_url(url) --, default)
223 -- initialize default parameters
225 -- for i,v in base.pairs(default or parsed) do
230 -- url = string.gsub(url, "%s", "")
232 url = string.gsub(url, "#(.*)$",
237 -- get scheme. Lower-case according to RFC 3986 section 3.1.
238 url = string.gsub(url, "^([%w][%w%+%-%.]*)%:",
240 parsed.scheme = string.lower(s);
244 url = string.gsub(url, "^//([^/]*)",
249 -- get query stringing
250 url = string.gsub(url, "%?(.*)",
256 url = string.gsub(url, "%;(.*)",
261 -- path is whatever was left
264 local authority = parsed.authority
265 if not authority then
268 authority = string.gsub(authority,"^([^@]*)@",
273 authority = string.gsub(authority, ":([0-9]*)$",
280 if authority ~= "" then
281 parsed.host = authority
284 local userinfo = parsed.userinfo
288 userinfo = string.gsub(userinfo, ":([^:]*)$",
293 parsed.user = userinfo