2014-10-10 19:55:22 +00:00
|
|
|
--[[
|
|
|
|
LuCI - Lua Configuration Interface
|
|
|
|
|
|
|
|
Copyright 2014 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
]]--
|
|
|
|
|
2014-11-09 17:37:15 +00:00
|
|
|
local NXFS = require "nixio.fs"
|
|
|
|
local CTRL = require "luci.controller.ddns" -- this application's controller
|
|
|
|
local DISP = require "luci.dispatcher"
|
|
|
|
local HTTP = require "luci.http"
|
|
|
|
local SYS = require "luci.sys"
|
|
|
|
local DDNS = require "luci.tools.ddns" -- ddns multiused functions
|
2014-10-10 19:55:22 +00:00
|
|
|
|
|
|
|
-- show hints ?
|
2014-11-09 17:37:15 +00:00
|
|
|
show_hints = not (DDNS.check_ipv6() -- IPv6 support
|
|
|
|
and DDNS.check_ssl() -- HTTPS support
|
|
|
|
and DDNS.check_proxy() -- Proxy support
|
|
|
|
and DDNS.check_bind_host() -- DNS TCP support
|
2014-10-10 19:55:22 +00:00
|
|
|
)
|
2014-11-09 17:37:15 +00:00
|
|
|
need_update = CTRL.update_needed() -- correct ddns-scripts version
|
2014-10-10 19:55:22 +00:00
|
|
|
|
|
|
|
-- html constants
|
|
|
|
font_red = [[<font color="red">]]
|
|
|
|
font_off = [[</font>]]
|
|
|
|
bold_on = [[<strong>]]
|
|
|
|
bold_off = [[</strong>]]
|
|
|
|
|
2014-11-09 17:37:15 +00:00
|
|
|
-- cbi-map definition -- #######################################################
|
|
|
|
m = Map("ddns")
|
2014-10-10 19:55:22 +00:00
|
|
|
|
2014-11-09 17:37:15 +00:00
|
|
|
-- first need to close <a> from cbi map template our <a> closed by template
|
|
|
|
--m.title = [[</a><a href="javascript:alert(']] .. CTRL.show_versions() ..[[')">]] ..
|
|
|
|
-- translate("Dynamic DNS")
|
|
|
|
m.title = [[</a><a href="#" onclick="onclick_maptitle();">]] ..
|
|
|
|
translate("Dynamic DNS")
|
2014-10-10 19:55:22 +00:00
|
|
|
|
2014-11-09 17:37:15 +00:00
|
|
|
m.description = translate("Dynamic DNS allows that your router can be reached with " ..
|
|
|
|
"a fixed hostname while having a dynamically changing " ..
|
|
|
|
"IP address.")
|
|
|
|
|
|
|
|
m.on_after_commit = function(self)
|
|
|
|
if self.changed then -- changes ?
|
|
|
|
if SYS.init.enabled("ddns") then -- ddns service enabled, restart all
|
|
|
|
os.execute("/etc/init.d/ddns restart")
|
|
|
|
else -- ddns service disabled, send SIGHUP to running
|
|
|
|
os.execute("killall -1 dynamic_dns_updater.sh")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- SimpleSection definiton -- ##################################################
|
|
|
|
-- with all the JavaScripts we need for "a good Show"
|
|
|
|
a = m:section( SimpleSection )
|
|
|
|
a.template = "ddns/overview_status"
|
|
|
|
|
|
|
|
-- SimpleSection definition -- #################################################
|
2014-10-10 19:55:22 +00:00
|
|
|
-- show Hints to optimize installation and script usage
|
|
|
|
-- only show if service not enabled
|
|
|
|
-- or no IPv6 support
|
|
|
|
-- or not GNU Wget and not cURL (for https support)
|
|
|
|
-- or not GNU Wget but cURL without proxy support
|
|
|
|
-- or not BIND's host
|
2014-11-09 17:37:15 +00:00
|
|
|
-- or ddns-scripts package need update
|
|
|
|
if show_hints or need_update or not SYS.init.enabled("ddns") then
|
2014-10-10 19:55:22 +00:00
|
|
|
s = m:section( SimpleSection, translate("Hints") )
|
2014-11-09 17:37:15 +00:00
|
|
|
|
|
|
|
-- ddns_scripts needs to be updated for full functionality
|
|
|
|
if need_update then
|
|
|
|
local dv = s:option(DummyValue, "_update_needed")
|
|
|
|
dv.titleref = DISP.build_url("admin", "system", "packages")
|
|
|
|
dv.rawhtml = true
|
|
|
|
dv.title = font_red .. bold_on ..
|
|
|
|
translate("Software update required") .. bold_off .. font_off
|
|
|
|
dv.value = translate("The currently installed 'ddns-scripts' package did not support all available settings.") ..
|
|
|
|
"<br />" ..
|
|
|
|
translate("Please update to the current version!")
|
|
|
|
end
|
|
|
|
|
2014-10-10 19:55:22 +00:00
|
|
|
-- DDNS Service disabled
|
2014-11-09 17:37:15 +00:00
|
|
|
if not SYS.init.enabled("ddns") then
|
2014-10-10 19:55:22 +00:00
|
|
|
local dv = s:option(DummyValue, "_not_enabled")
|
2014-11-09 17:37:15 +00:00
|
|
|
dv.titleref = DISP.build_url("admin", "system", "startup")
|
2014-10-10 19:55:22 +00:00
|
|
|
dv.rawhtml = true
|
2014-11-09 17:37:15 +00:00
|
|
|
dv.title = bold_on ..
|
2014-10-10 19:55:22 +00:00
|
|
|
translate("DDNS Autostart disabled") .. bold_off
|
2014-11-09 17:37:15 +00:00
|
|
|
dv.value = translate("Currently DDNS updates are not started at boot or on interface events." .. "<br />" ..
|
2014-10-10 19:55:22 +00:00
|
|
|
"You can start/stop each configuration here. It will run until next reboot.")
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Show more hints on a separate page
|
|
|
|
if show_hints then
|
|
|
|
local dv = s:option(DummyValue, "_separate")
|
2014-11-09 17:37:15 +00:00
|
|
|
dv.titleref = DISP.build_url("admin", "services", "ddns", "hints")
|
2014-10-10 19:55:22 +00:00
|
|
|
dv.rawhtml = true
|
2014-11-09 17:37:15 +00:00
|
|
|
dv.title = bold_on ..
|
2014-10-10 19:55:22 +00:00
|
|
|
translate("Show more") .. bold_off
|
|
|
|
dv.value = translate("Follow this link" .. "<br />" ..
|
|
|
|
"You will find more hints to optimize your system to run DDNS scripts with all options")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-09 17:37:15 +00:00
|
|
|
-- TableSection definition -- ##################################################
|
|
|
|
ts = m:section( TypedSection, "service",
|
|
|
|
translate("Overview"),
|
2014-10-10 19:55:22 +00:00
|
|
|
translate("Below is a list of configured DDNS configurations and their current state." .. "<br />" ..
|
|
|
|
"If you want to send updates for IPv4 and IPv6 you need to define two separate Configurations " ..
|
|
|
|
"i.e. 'myddns_ipv4' and 'myddns_ipv6'") )
|
|
|
|
ts.sectionhead = translate("Configuration")
|
|
|
|
ts.template = "cbi/tblsection"
|
|
|
|
ts.addremove = true
|
2014-11-09 17:37:15 +00:00
|
|
|
ts.extedit = DISP.build_url("admin", "services", "ddns", "detail", "%s")
|
2014-10-10 19:55:22 +00:00
|
|
|
function ts.create(self, name)
|
|
|
|
AbstractSection.create(self, name)
|
2014-11-09 17:37:15 +00:00
|
|
|
HTTP.redirect( self.extedit:format(name) )
|
2014-10-10 19:55:22 +00:00
|
|
|
end
|
|
|
|
|
2014-11-09 17:37:15 +00:00
|
|
|
-- Domain and registered IP -- #################################################
|
2014-10-10 19:55:22 +00:00
|
|
|
dom = ts:option(DummyValue, "_domainIP",
|
|
|
|
translate("Hostname/Domain") .. "<br />" .. translate("Registered IP") )
|
|
|
|
dom.template = "ddns/overview_doubleline"
|
|
|
|
function dom.set_one(self, section)
|
|
|
|
local domain = self.map:get(section, "domain") or ""
|
|
|
|
if domain ~= "" then
|
|
|
|
return domain
|
|
|
|
else
|
|
|
|
return [[<em>]] .. translate("config error") .. [[</em>]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
function dom.set_two(self, section)
|
|
|
|
local domain = self.map:get(section, "domain") or ""
|
|
|
|
if domain == "" then return "" end
|
|
|
|
local dnsserver = self.map:get(section, "dnsserver") or ""
|
|
|
|
local use_ipv6 = tonumber(self.map:get(section, "use_ipv6") or 0)
|
|
|
|
local force_ipversion = tonumber(self.map:get(section, "force_ipversion") or 0)
|
|
|
|
local force_dnstcp = tonumber(self.map:get(section, "force_dnstcp") or 0)
|
|
|
|
local command = [[/usr/lib/ddns/dynamic_dns_lucihelper.sh]]
|
2014-11-09 17:37:15 +00:00
|
|
|
if not NXFS.access(command, "rwx", "rx", "rx") then
|
|
|
|
NXFS.chmod(command, 755)
|
2014-10-10 19:55:22 +00:00
|
|
|
end
|
2014-11-09 17:37:15 +00:00
|
|
|
command = command .. [[ get_registered_ip ]] .. domain .. [[ ]] .. use_ipv6 ..
|
2014-10-10 19:55:22 +00:00
|
|
|
[[ ]] .. force_ipversion .. [[ ]] .. force_dnstcp .. [[ ]] .. dnsserver
|
2014-11-09 17:37:15 +00:00
|
|
|
local ip = SYS.exec(command)
|
2014-10-10 19:55:22 +00:00
|
|
|
if ip == "" then ip = translate("no data") end
|
|
|
|
return ip
|
|
|
|
end
|
|
|
|
|
2014-11-09 17:37:15 +00:00
|
|
|
-- enabled
|
|
|
|
ena = ts:option( Flag, "enabled",
|
2014-10-10 19:55:22 +00:00
|
|
|
translate("Enabled"))
|
|
|
|
ena.template = "ddns/overview_enabled"
|
|
|
|
ena.rmempty = false
|
2014-11-09 17:37:15 +00:00
|
|
|
function ena.parse(self, section)
|
|
|
|
DDNS.flag_parse(self, section)
|
|
|
|
end
|
2014-10-10 19:55:22 +00:00
|
|
|
|
|
|
|
-- show PID and next update
|
2014-11-09 17:37:15 +00:00
|
|
|
upd = ts:option( DummyValue, "_update",
|
2014-10-10 19:55:22 +00:00
|
|
|
translate("Last Update") .. "<br />" .. translate("Next Update"))
|
|
|
|
upd.template = "ddns/overview_doubleline"
|
|
|
|
function upd.set_one(self, section) -- fill Last Update
|
|
|
|
-- get/validate last update
|
2014-11-09 17:37:15 +00:00
|
|
|
local uptime = SYS.uptime()
|
|
|
|
local lasttime = DDNS.get_lastupd(section)
|
2014-10-10 19:55:22 +00:00
|
|
|
if lasttime > uptime then -- /var might not be linked to /tmp and cleared on reboot
|
2014-11-09 17:37:15 +00:00
|
|
|
lasttime = 0
|
2014-10-10 19:55:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- no last update happen
|
|
|
|
if lasttime == 0 then
|
|
|
|
return translate("never")
|
|
|
|
|
|
|
|
-- we read last update
|
|
|
|
else
|
|
|
|
-- calc last update
|
|
|
|
-- os.epoch - sys.uptime + lastupdate(uptime)
|
|
|
|
local epoch = os.time() - uptime + lasttime
|
|
|
|
-- use linux date to convert epoch
|
2014-11-09 17:37:15 +00:00
|
|
|
return DDNS.epoch2date(epoch)
|
2014-10-10 19:55:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
function upd.set_two(self, section) -- fill Next Update
|
|
|
|
-- get enabled state
|
|
|
|
local enabled = tonumber(self.map:get(section, "enabled") or 0)
|
|
|
|
local datenext = translate("unknown error") -- formatted date of next update
|
|
|
|
|
|
|
|
-- get force seconds
|
|
|
|
local force_interval = tonumber(self.map:get(section, "force_interval") or 72)
|
|
|
|
local force_unit = self.map:get(section, "force_unit") or "hours"
|
2014-11-09 17:37:15 +00:00
|
|
|
local force_seconds = DDNS.calc_seconds(force_interval, force_unit)
|
2014-10-10 19:55:22 +00:00
|
|
|
|
|
|
|
-- get last update and get/validate PID
|
2014-11-09 17:37:15 +00:00
|
|
|
local uptime = SYS.uptime()
|
|
|
|
local lasttime = DDNS.get_lastupd(section)
|
2014-10-10 19:55:22 +00:00
|
|
|
if lasttime > uptime then -- /var might not be linked to /tmp and cleared on reboot
|
2014-11-09 17:37:15 +00:00
|
|
|
lasttime = 0
|
2014-10-10 19:55:22 +00:00
|
|
|
end
|
2014-11-09 17:37:15 +00:00
|
|
|
local pid = DDNS.get_pid(section)
|
2014-10-10 19:55:22 +00:00
|
|
|
|
|
|
|
-- calc next update
|
|
|
|
if lasttime > 0 then
|
|
|
|
local epoch = os.time() - uptime + lasttime + force_seconds
|
|
|
|
-- use linux date to convert epoch
|
2014-11-09 17:37:15 +00:00
|
|
|
datelast = DDNS.epoch2date(epoch)
|
|
|
|
end
|
2014-10-10 19:55:22 +00:00
|
|
|
|
2014-11-09 17:37:15 +00:00
|
|
|
-- process running but update needs to happen
|
2014-10-10 19:55:22 +00:00
|
|
|
if pid > 0 and ( lasttime + force_seconds - uptime ) < 0 then
|
|
|
|
datenext = translate("Verify")
|
|
|
|
|
2014-11-09 17:37:15 +00:00
|
|
|
-- run once
|
2014-10-10 19:55:22 +00:00
|
|
|
elseif force_seconds == 0 then
|
|
|
|
datenext = translate("Run once")
|
|
|
|
|
|
|
|
-- no process running and NOT enabled
|
|
|
|
elseif pid == 0 and enabled == 0 then
|
|
|
|
datenext = translate("Disabled")
|
|
|
|
|
2014-11-09 17:37:15 +00:00
|
|
|
-- no process running and NOT
|
2014-10-10 19:55:22 +00:00
|
|
|
elseif pid == 0 and enabled ~= 0 then
|
|
|
|
datenext = translate("Stopped")
|
|
|
|
end
|
|
|
|
|
|
|
|
return datenext
|
|
|
|
end
|
|
|
|
|
|
|
|
-- start/stop button
|
2014-11-09 17:37:15 +00:00
|
|
|
btn = ts:option( Button, "_startstop",
|
2014-10-10 19:55:22 +00:00
|
|
|
translate("Process ID") .. "<br />" .. translate("Start / Stop") )
|
|
|
|
btn.template = "ddns/overview_startstop"
|
|
|
|
function btn.cfgvalue(self, section)
|
2014-11-09 17:37:15 +00:00
|
|
|
local pid = DDNS.get_pid(section)
|
2014-10-10 19:55:22 +00:00
|
|
|
if pid > 0 then
|
|
|
|
btn.inputtitle = "PID: " .. pid
|
|
|
|
btn.inputstyle = "reset"
|
|
|
|
btn.disabled = false
|
|
|
|
elseif (self.map:get(section, "enabled") or "0") ~= "0" then
|
|
|
|
btn.inputtitle = translate("Start")
|
|
|
|
btn.inputstyle = "apply"
|
|
|
|
btn.disabled = false
|
|
|
|
else
|
|
|
|
btn.inputtitle = "----------"
|
|
|
|
btn.inputstyle = "button"
|
|
|
|
btn.disabled = true
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return m
|