2
0

overview.lua 8.4 KB

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