ddns.lua 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. -- Copyright 2014 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
  2. -- Licensed to the public under the Apache License 2.0.
  3. module("luci.tools.ddns", package.seeall)
  4. local NX = require "nixio"
  5. local NXFS = require "nixio.fs"
  6. local OPKG = require "luci.model.ipkg"
  7. local UCI = require "luci.model.uci"
  8. local SYS = require "luci.sys"
  9. local UTIL = require "luci.util"
  10. -- function to calculate seconds from given interval and unit
  11. function calc_seconds(interval, unit)
  12. if not tonumber(interval) then
  13. return nil
  14. elseif unit == "days" then
  15. return (tonumber(interval) * 86400) -- 60 sec * 60 min * 24 h
  16. elseif unit == "hours" then
  17. return (tonumber(interval) * 3600) -- 60 sec * 60 min
  18. elseif unit == "minutes" then
  19. return (tonumber(interval) * 60) -- 60 sec
  20. elseif unit == "seconds" then
  21. return tonumber(interval)
  22. else
  23. return nil
  24. end
  25. end
  26. -- check if IPv6 supported by OpenWrt
  27. function check_ipv6()
  28. return NXFS.access("/proc/net/ipv6_route")
  29. and NXFS.access("/usr/sbin/ip6tables")
  30. end
  31. -- check if Wget with SSL support or cURL installed
  32. function check_ssl()
  33. if (SYS.call([[ grep -i "\+ssl" /usr/bin/wget >/dev/null 2>&1 ]]) == 0) then
  34. return true
  35. else
  36. return NXFS.access("/usr/bin/curl")
  37. end
  38. end
  39. -- check if Wget with SSL or cURL with proxy support installed
  40. function check_proxy()
  41. -- we prefere GNU Wget for communication
  42. if (SYS.call([[ grep -i "\+ssl" /usr/bin/wget >/dev/null 2>&1 ]]) == 0) then
  43. return true
  44. -- if not installed cURL must support proxy
  45. elseif NXFS.access("/usr/bin/curl") then
  46. return (SYS.call([[ grep -i all_proxy /usr/lib/libcurl.so* >/dev/null 2>&1 ]]) == 0)
  47. -- only BusyBox Wget is installed
  48. else
  49. return NXFS.access("/usr/bin/wget")
  50. end
  51. end
  52. -- check if BIND host installed
  53. function check_bind_host()
  54. return NXFS.access("/usr/bin/host")
  55. end
  56. -- convert epoch date to given format
  57. function epoch2date(epoch, format)
  58. if not format or #format < 2 then
  59. local uci = UCI.cursor()
  60. format = uci:get("ddns", "global", "date_format") or "%F %R"
  61. uci:unload("ddns")
  62. end
  63. format = format:gsub("%%n", "<br />") -- replace newline
  64. format = format:gsub("%%t", " ") -- replace tab
  65. return os.date(format, epoch)
  66. end
  67. -- read lastupdate from [section].update file
  68. function get_lastupd(section)
  69. local uci = UCI.cursor()
  70. local run_dir = uci:get("ddns", "global", "run_dir") or "/var/run/ddns"
  71. local etime = tonumber(NXFS.readfile("%s/%s.update" % { run_dir, section } ) or 0 )
  72. uci:unload("ddns")
  73. return etime
  74. end
  75. -- read PID from run file and verify if still running
  76. function get_pid(section)
  77. local uci = UCI.cursor()
  78. local run_dir = uci:get("ddns", "global", "run_dir") or "/var/run/ddns"
  79. local pid = tonumber(NXFS.readfile("%s/%s.pid" % { run_dir, section } ) or 0 )
  80. if pid > 0 and not NX.kill(pid, 0) then
  81. pid = 0
  82. end
  83. uci:unload("ddns")
  84. return pid
  85. end
  86. -- compare versions using "<=" "<" ">" ">=" "=" "<<" ">>"
  87. function ipkg_ver_compare(ver1, comp, ver2)
  88. if not ver1 or not ver2
  89. or not comp or not (#comp > 0) then return nil end
  90. -- correct compare string
  91. if comp == "<>" or comp == "><" or comp == "!=" or comp == "~=" then comp = "~="
  92. elseif comp == "<=" or comp == "<" or comp == "=<" then comp = "<="
  93. elseif comp == ">=" or comp == ">" or comp == "=>" then comp = ">="
  94. elseif comp == "=" or comp == "==" then comp = "=="
  95. elseif comp == "<<" then comp = "<"
  96. elseif comp == ">>" then comp = ">"
  97. else return nil end
  98. local av1 = UTIL.split(ver1, "[%.%-]", nil, true)
  99. local av2 = UTIL.split(ver2, "[%.%-]", nil, true)
  100. for i = 1, math.max(table.getn(av1),table.getn(av2)), 1 do
  101. local s1 = av1[i] or ""
  102. local s2 = av2[i] or ""
  103. -- first "not equal" found return true
  104. if comp == "~=" and (s1 ~= s2) then return true end
  105. -- first "lower" found return true
  106. if (comp == "<" or comp == "<=") and (s1 < s2) then return true end
  107. -- first "greater" found return true
  108. if (comp == ">" or comp == ">=") and (s1 > s2) then return true end
  109. -- not equal then return false
  110. if (s1 ~= s2) then return false end
  111. end
  112. -- all equal and not compare greater or lower then true
  113. return not (comp == "<" or comp == ">")
  114. end
  115. -- read version information for given package if installed
  116. function ipkg_ver_installed(pkg)
  117. local version = nil
  118. local control = io.open("/usr/lib/opkg/info/%s.control" % pkg, "r")
  119. if control then
  120. local ln
  121. repeat
  122. ln = control:read("*l")
  123. if ln and ln:match("^Version: ") then
  124. version = ln:gsub("^Version: ", "")
  125. break
  126. end
  127. until not ln
  128. control:close()
  129. end
  130. return version
  131. end
  132. -- replacement of build-in read of UCI option
  133. -- modified AbstractValue.cfgvalue(self, section) from cbi.lua
  134. -- needed to read from other option then current value definition
  135. function read_value(self, section, option)
  136. local value
  137. if self.tag_error[section] then
  138. value = self:formvalue(section)
  139. else
  140. value = self.map:get(section, option)
  141. end
  142. if not value then
  143. return nil
  144. elseif not self.cast or self.cast == type(value) then
  145. return value
  146. elseif self.cast == "string" then
  147. if type(value) == "table" then
  148. return value[1]
  149. end
  150. elseif self.cast == "table" then
  151. return { value }
  152. end
  153. end
  154. -- replacement of build-in Flag.parse of cbi.lua
  155. -- modified to mark section as changed if value changes
  156. -- current parse did not do this, but it is done AbstaractValue.parse()
  157. function flag_parse(self, section)
  158. local fexists = self.map:formvalue(
  159. luci.cbi.FEXIST_PREFIX .. self.config .. "." .. section .. "." .. self.option)
  160. if fexists then
  161. local fvalue = self:formvalue(section) and self.enabled or self.disabled
  162. local cvalue = self:cfgvalue(section)
  163. if fvalue ~= self.default or (not self.optional and not self.rmempty) then
  164. self:write(section, fvalue)
  165. else
  166. self:remove(section)
  167. end
  168. if (fvalue ~= cvalue) then self.section.changed = true end
  169. else
  170. self:remove(section)
  171. self.section.changed = true
  172. end
  173. end
  174. -----------------------------------------------------------------------------
  175. -- copied from https://svn.nmap.org/nmap/nselib/url.lua
  176. -- @author Diego Nehab
  177. -- @author Eddie Bell <ejlbell@gmail.com>
  178. --[[
  179. URI parsing, composition and relative URL resolution
  180. LuaSocket toolkit.
  181. Author: Diego Nehab
  182. RCS ID: $Id: url.lua,v 1.37 2005/11/22 08:33:29 diego Exp $
  183. parse_query and build_query added For nmap (Eddie Bell <ejlbell@gmail.com>)
  184. ]]--
  185. ---
  186. -- Parses a URL and returns a table with all its parts according to RFC 2396.
  187. --
  188. -- The following grammar describes the names given to the URL parts.
  189. -- <code>
  190. -- <url> ::= <scheme>://<authority>/<path>;<params>?<query>#<fragment>
  191. -- <authority> ::= <userinfo>@<host>:<port>
  192. -- <userinfo> ::= <user>[:<password>]
  193. -- <path> :: = {<segment>/}<segment>
  194. -- </code>
  195. --
  196. -- The leading <code>/</code> in <code>/<path></code> is considered part of
  197. -- <code><path></code>.
  198. -- @param url URL of request.
  199. -- @param default Table with default values for each field.
  200. -- @return A table with the following fields, where RFC naming conventions have
  201. -- been preserved:
  202. -- <code>scheme</code>, <code>authority</code>, <code>userinfo</code>,
  203. -- <code>user</code>, <code>password</code>, <code>host</code>,
  204. -- <code>port</code>, <code>path</code>, <code>params</code>,
  205. -- <code>query</code>, and <code>fragment</code>.
  206. -----------------------------------------------------------------------------
  207. function parse_url(url) --, default)
  208. -- initialize default parameters
  209. local parsed = {}
  210. -- for i,v in base.pairs(default or parsed) do
  211. -- parsed[i] = v
  212. -- end
  213. -- remove whitespace
  214. -- url = string.gsub(url, "%s", "")
  215. -- get fragment
  216. url = string.gsub(url, "#(.*)$",
  217. function(f)
  218. parsed.fragment = f
  219. return ""
  220. end)
  221. -- get scheme. Lower-case according to RFC 3986 section 3.1.
  222. url = string.gsub(url, "^([%w][%w%+%-%.]*)%:",
  223. function(s)
  224. parsed.scheme = string.lower(s);
  225. return ""
  226. end)
  227. -- get authority
  228. url = string.gsub(url, "^//([^/]*)",
  229. function(n)
  230. parsed.authority = n
  231. return ""
  232. end)
  233. -- get query stringing
  234. url = string.gsub(url, "%?(.*)",
  235. function(q)
  236. parsed.query = q
  237. return ""
  238. end)
  239. -- get params
  240. url = string.gsub(url, "%;(.*)",
  241. function(p)
  242. parsed.params = p
  243. return ""
  244. end)
  245. -- path is whatever was left
  246. parsed.path = url
  247. local authority = parsed.authority
  248. if not authority then
  249. return parsed
  250. end
  251. authority = string.gsub(authority,"^([^@]*)@",
  252. function(u)
  253. parsed.userinfo = u;
  254. return ""
  255. end)
  256. authority = string.gsub(authority, ":([0-9]*)$",
  257. function(p)
  258. if p ~= "" then
  259. parsed.port = p
  260. end;
  261. return ""
  262. end)
  263. if authority ~= "" then
  264. parsed.host = authority
  265. end
  266. local userinfo = parsed.userinfo
  267. if not userinfo then
  268. return parsed
  269. end
  270. userinfo = string.gsub(userinfo, ":([^:]*)$",
  271. function(p)
  272. parsed.password = p;
  273. return ""
  274. end)
  275. parsed.user = userinfo
  276. return parsed
  277. end