2 LuCI - Lua Configuration Interface
4 shared module for luci-app-ddns-v2
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)
25 return nixio.fs.access("/proc/net/ipv6_route")
26 and nixio.fs.access("/usr/sbin/ip6tables")
30 if (luci.sys.call([[ grep -iq "\+ssl" /usr/bin/wget 2>/dev/null ]]) == 0) then
33 return nixio.fs.access("/usr/bin/curl")
37 function check_proxy()
38 -- we prefere GNU Wget for communication
39 if (luci.sys.call([[ grep -iq "\+ssl" /usr/bin/wget 2>/dev/null ]]) == 0) then
42 -- if not installed cURL must support proxy
43 elseif nixio.fs.access("/usr/bin/curl") then
44 return (luci.sys.call([[ grep -iq all_proxy /usr/lib/libcurl.so* 2>/dev/null ]]) == 0)
46 -- only BusyBox Wget is installed
48 return nixio.fs.access("/usr/bin/wget")
52 function check_bind_host()
53 return nixio.fs.access("/usr/bin/host")
56 -- function to calculate seconds from given interval and unit
57 function calc_seconds(interval, unit)
58 if not tonumber(interval) then
60 elseif unit == "days" then
61 return (tonumber(interval) * 86400) -- 60 sec * 60 min * 24 h
62 elseif unit == "hours" then
63 return (tonumber(interval) * 3600) -- 60 sec * 60 min
64 elseif unit == "minutes" then
65 return (tonumber(interval) * 60) -- 60 sec
66 elseif unit == "seconds" then
67 return tonumber(interval)
73 -- read PID from run file and verify if still running
74 function get_pid(section, run_dir)
75 local pid = tonumber(nixio.fs.readfile("%s/%s.pid" % { run_dir, section } ) or 0 )
76 if pid > 0 and not luci.sys.process.signal(pid, 0) then
82 -- replacement of build-in read of UCI option
83 -- modified AbstractValue.cfgvalue(self, section) from cbi.lua
84 -- needed to read from other option then current value definition
85 function read_value(self, section, option)
87 if self.tag_error[section] then
88 value = self:formvalue(section)
90 value = self.map:get(section, option)
95 elseif not self.cast or self.cast == type(value) then
97 elseif self.cast == "string" then
98 if type(value) == "table" then
101 elseif self.cast == "table" then
106 -----------------------------------------------------------------------------
107 -- copied from https://svn.nmap.org/nmap/nselib/url.lua
108 -- @author Diego Nehab
109 -- @author Eddie Bell <ejlbell@gmail.com>
111 URI parsing, composition and relative URL resolution
114 RCS ID: $Id: url.lua,v 1.37 2005/11/22 08:33:29 diego Exp $
115 parse_query and build_query added For nmap (Eddie Bell <ejlbell@gmail.com>)
118 -- Parses a URL and returns a table with all its parts according to RFC 2396.
120 -- The following grammar describes the names given to the URL parts.
122 -- <url> ::= <scheme>://<authority>/<path>;<params>?<query>#<fragment>
123 -- <authority> ::= <userinfo>@<host>:<port>
124 -- <userinfo> ::= <user>[:<password>]
125 -- <path> :: = {<segment>/}<segment>
128 -- The leading <code>/</code> in <code>/<path></code> is considered part of
129 -- <code><path></code>.
130 -- @param url URL of request.
131 -- @param default Table with default values for each field.
132 -- @return A table with the following fields, where RFC naming conventions have
134 -- <code>scheme</code>, <code>authority</code>, <code>userinfo</code>,
135 -- <code>user</code>, <code>password</code>, <code>host</code>,
136 -- <code>port</code>, <code>path</code>, <code>params</code>,
137 -- <code>query</code>, and <code>fragment</code>.
138 -----------------------------------------------------------------------------
139 function parse_url(url) --, default)
140 -- initialize default parameters
142 -- for i,v in base.pairs(default or parsed) do
147 -- url = string.gsub(url, "%s", "")
149 url = string.gsub(url, "#(.*)$",
154 -- get scheme. Lower-case according to RFC 3986 section 3.1.
155 url = string.gsub(url, "^([%w][%w%+%-%.]*)%:",
157 parsed.scheme = string.lower(s);
161 url = string.gsub(url, "^//([^/]*)",
166 -- get query stringing
167 url = string.gsub(url, "%?(.*)",
173 url = string.gsub(url, "%;(.*)",
178 -- path is whatever was left
181 local authority = parsed.authority
182 if not authority then
185 authority = string.gsub(authority,"^([^@]*)@",
190 authority = string.gsub(authority, ":([0-9]*)$",
197 if authority ~= "" then
198 parsed.host = authority
201 local userinfo = parsed.userinfo
205 userinfo = string.gsub(userinfo, ":([^:]*)$",
210 parsed.user = userinfo