overview.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. -- Copyright 2014 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
  2. -- Licensed to the public under the Apache License 2.0.
  3. local NXFS = require "nixio.fs"
  4. local CTRL = require "luci.controller.ddns" -- this application's controller
  5. local DISP = require "luci.dispatcher"
  6. local HTTP = require "luci.http"
  7. local SYS = require "luci.sys"
  8. local DDNS = require "luci.tools.ddns" -- ddns multiused functions
  9. -- show hints ?
  10. show_hints = not (DDNS.check_ipv6() -- IPv6 support
  11. and DDNS.check_ssl() -- HTTPS support
  12. and DDNS.check_proxy() -- Proxy support
  13. and DDNS.check_bind_host() -- DNS TCP support
  14. )
  15. -- correct ddns-scripts version
  16. need_update = DDNS.ipkg_ver_compare(DDNS.ipkg_ver_installed("ddns-scripts"), "<<", CTRL.DDNS_MIN)
  17. -- html constants
  18. font_red = [[<font color="red">]]
  19. font_off = [[</font>]]
  20. bold_on = [[<strong>]]
  21. bold_off = [[</strong>]]
  22. -- cbi-map definition -- #######################################################
  23. m = Map("ddns")
  24. -- first need to close <a> from cbi map template our <a> closed by template
  25. m.title = [[</a><a href="javascript:alert(']]
  26. .. translate("Version Information")
  27. .. [[\n\nluci-app-ddns]]
  28. .. [[\n\t]] .. translate("Version") .. [[:\t]] .. DDNS.ipkg_ver_installed("luci-app-ddns")
  29. .. [[\n\nddns-scripts ]] .. translate("required") .. [[:]]
  30. .. [[\n\t]] .. translate("Version") .. [[:\t]] .. CTRL.DDNS_MIN .. [[ ]] .. translate("or higher")
  31. .. [[\n\nddns-scripts ]] .. translate("installed") .. [[:]]
  32. .. [[\n\t]] .. translate("Version") .. [[:\t]] .. DDNS.ipkg_ver_installed("ddns-scripts")
  33. .. [[\n\n]]
  34. .. [[')">]]
  35. .. translate("Dynamic DNS")
  36. m.description = translate("Dynamic DNS allows that your router can be reached with " ..
  37. "a fixed hostname while having a dynamically changing " ..
  38. "IP address.")
  39. m.on_after_commit = function(self)
  40. if self.changed then -- changes ?
  41. if SYS.init.enabled("ddns") then -- ddns service enabled, restart all
  42. os.execute("/etc/init.d/ddns restart")
  43. else -- ddns service disabled, send SIGHUP to running
  44. os.execute("killall -1 dynamic_dns_updater.sh")
  45. end
  46. end
  47. end
  48. -- SimpleSection definiton -- ##################################################
  49. -- with all the JavaScripts we need for "a good Show"
  50. a = m:section( SimpleSection )
  51. a.template = "ddns/overview_status"
  52. -- SimpleSection definition -- #################################################
  53. -- show Hints to optimize installation and script usage
  54. -- only show if service not enabled
  55. -- or no IPv6 support
  56. -- or not GNU Wget and not cURL (for https support)
  57. -- or not GNU Wget but cURL without proxy support
  58. -- or not BIND's host
  59. -- or ddns-scripts package need update
  60. if show_hints or need_update or not SYS.init.enabled("ddns") then
  61. s = m:section( SimpleSection, translate("Hints") )
  62. -- ddns_scripts needs to be updated for full functionality
  63. if need_update then
  64. local dv = s:option(DummyValue, "_update_needed")
  65. dv.titleref = DISP.build_url("admin", "system", "packages")
  66. dv.rawhtml = true
  67. dv.title = font_red .. bold_on ..
  68. translate("Software update required") .. bold_off .. font_off
  69. dv.value = translate("The currently installed 'ddns-scripts' package did not support all available settings.") ..
  70. "<br />" ..
  71. translate("Please update to the current version!")
  72. end
  73. -- DDNS Service disabled
  74. if not SYS.init.enabled("ddns") then
  75. local dv = s:option(DummyValue, "_not_enabled")
  76. dv.titleref = DISP.build_url("admin", "system", "startup")
  77. dv.rawhtml = true
  78. dv.title = bold_on ..
  79. translate("DDNS Autostart disabled") .. bold_off
  80. dv.value = translate("Currently DDNS updates are not started at boot or on interface events." .. "<br />" ..
  81. "You can start/stop each configuration here. It will run until next reboot.")
  82. end
  83. -- Show more hints on a separate page
  84. if show_hints then
  85. local dv = s:option(DummyValue, "_separate")
  86. dv.titleref = DISP.build_url("admin", "services", "ddns", "hints")
  87. dv.rawhtml = true
  88. dv.title = bold_on ..
  89. translate("Show more") .. bold_off
  90. dv.value = translate("Follow this link" .. "<br />" ..
  91. "You will find more hints to optimize your system to run DDNS scripts with all options")
  92. end
  93. end
  94. -- TableSection definition -- ##################################################
  95. ts = m:section( TypedSection, "service",
  96. translate("Overview"),
  97. translate("Below is a list of configured DDNS configurations and their current state.")
  98. .. "<br />"
  99. .. translate("If you want to send updates for IPv4 and IPv6 you need to define two separate Configurations "
  100. .. "i.e. 'myddns_ipv4' and 'myddns_ipv6'")
  101. .. "<br />"
  102. .. [[<a href="]] .. DISP.build_url("admin", "services", "ddns", "global") .. [[">]]
  103. .. translate("To change global settings click here") .. [[</a>]] )
  104. ts.sectionhead = translate("Configuration")
  105. ts.template = "cbi/tblsection"
  106. ts.addremove = true
  107. ts.extedit = DISP.build_url("admin", "services", "ddns", "detail", "%s")
  108. function ts.create(self, name)
  109. AbstractSection.create(self, name)
  110. HTTP.redirect( self.extedit:format(name) )
  111. end
  112. -- Domain and registered IP -- #################################################
  113. dom = ts:option(DummyValue, "_domainIP",
  114. translate("Hostname/Domain") .. "<br />" .. translate("Registered IP") )
  115. dom.template = "ddns/overview_doubleline"
  116. function dom.set_one(self, section)
  117. local domain = self.map:get(section, "domain") or ""
  118. if domain ~= "" then
  119. return domain
  120. else
  121. return [[<em>]] .. translate("config error") .. [[</em>]]
  122. end
  123. end
  124. function dom.set_two(self, section)
  125. local domain = self.map:get(section, "domain") or ""
  126. if domain == "" then return "" end
  127. local dnsserver = self.map:get(section, "dnsserver") or ""
  128. local use_ipv6 = tonumber(self.map:get(section, "use_ipv6") or 0)
  129. local force_ipversion = tonumber(self.map:get(section, "force_ipversion") or 0)
  130. local force_dnstcp = tonumber(self.map:get(section, "force_dnstcp") or 0)
  131. local command = [[/usr/lib/ddns/dynamic_dns_lucihelper.sh]]
  132. if not NXFS.access(command, "rwx", "rx", "rx") then
  133. NXFS.chmod(command, 755)
  134. end
  135. command = command .. [[ get_registered_ip ]] .. domain .. [[ ]] .. use_ipv6 ..
  136. [[ ]] .. force_ipversion .. [[ ]] .. force_dnstcp .. [[ ]] .. dnsserver
  137. local ip = SYS.exec(command)
  138. if ip == "" then ip = translate("no data") end
  139. return ip
  140. end
  141. -- enabled
  142. ena = ts:option( Flag, "enabled",
  143. translate("Enabled"))
  144. ena.template = "ddns/overview_enabled"
  145. ena.rmempty = false
  146. function ena.parse(self, section)
  147. DDNS.flag_parse(self, section)
  148. end
  149. -- show PID and next update
  150. upd = ts:option( DummyValue, "_update",
  151. translate("Last Update") .. "<br />" .. translate("Next Update"))
  152. upd.template = "ddns/overview_doubleline"
  153. function upd.set_one(self, section) -- fill Last Update
  154. -- get/validate last update
  155. local uptime = SYS.uptime()
  156. local lasttime = DDNS.get_lastupd(section)
  157. if lasttime > uptime then -- /var might not be linked to /tmp and cleared on reboot
  158. lasttime = 0
  159. end
  160. -- no last update happen
  161. if lasttime == 0 then
  162. return translate("never")
  163. -- we read last update
  164. else
  165. -- calc last update
  166. -- os.epoch - sys.uptime + lastupdate(uptime)
  167. local epoch = os.time() - uptime + lasttime
  168. -- use linux date to convert epoch
  169. return DDNS.epoch2date(epoch)
  170. end
  171. end
  172. function upd.set_two(self, section) -- fill Next Update
  173. -- get enabled state
  174. local enabled = tonumber(self.map:get(section, "enabled") or 0)
  175. local datenext = translate("unknown error") -- formatted date of next update
  176. -- get force seconds
  177. local force_interval = tonumber(self.map:get(section, "force_interval") or 72)
  178. local force_unit = self.map:get(section, "force_unit") or "hours"
  179. local force_seconds = DDNS.calc_seconds(force_interval, force_unit)
  180. -- get last update and get/validate PID
  181. local uptime = SYS.uptime()
  182. local lasttime = DDNS.get_lastupd(section)
  183. if lasttime > uptime then -- /var might not be linked to /tmp and cleared on reboot
  184. lasttime = 0
  185. end
  186. local pid = DDNS.get_pid(section)
  187. -- calc next update
  188. if lasttime > 0 then
  189. local epoch = os.time() - uptime + lasttime + force_seconds
  190. -- use linux date to convert epoch
  191. datelast = DDNS.epoch2date(epoch)
  192. end
  193. -- process running but update needs to happen
  194. if pid > 0 and ( lasttime + force_seconds - uptime ) < 0 then
  195. datenext = translate("Verify")
  196. -- run once
  197. elseif force_seconds == 0 then
  198. datenext = translate("Run once")
  199. -- no process running and NOT enabled
  200. elseif pid == 0 and enabled == 0 then
  201. datenext = translate("Disabled")
  202. -- no process running and NOT
  203. elseif pid == 0 and enabled ~= 0 then
  204. datenext = translate("Stopped")
  205. end
  206. return datenext
  207. end
  208. -- start/stop button
  209. btn = ts:option( Button, "_startstop",
  210. translate("Process ID") .. "<br />" .. translate("Start / Stop") )
  211. btn.template = "ddns/overview_startstop"
  212. function btn.cfgvalue(self, section)
  213. local pid = DDNS.get_pid(section)
  214. if pid > 0 then
  215. btn.inputtitle = "PID: " .. pid
  216. btn.inputstyle = "reset"
  217. btn.disabled = false
  218. elseif (self.map:get(section, "enabled") or "0") ~= "0" then
  219. btn.inputtitle = translate("Start")
  220. btn.inputstyle = "apply"
  221. btn.disabled = false
  222. else
  223. btn.inputtitle = "----------"
  224. btn.inputstyle = "button"
  225. btn.disabled = true
  226. end
  227. return true
  228. end
  229. return m