Fixes & improvements

This commit is contained in:
gSpot
2025-03-01 18:31:36 +03:00
parent d94260e7ec
commit 55802f750f
18 changed files with 222 additions and 161 deletions

View File

@@ -8,7 +8,7 @@
luaposix
libuci-lua
(с) 2024 gSpot (https://github.com/gSpotx2f/luci-app-internet-detector)
(с) 2025 gSpot (https://github.com/gSpotx2f/luci-app-internet-detector)
--]]
local getopt = require("posix.unistd").getopt

View File

@@ -268,7 +268,6 @@ function InternetDetector:TCPConnectionToHost(host, port)
io.stdout:write(string.format(
"SOCKET ERROR: %s, %s\n", errMsg, errNum))
end
unistd.close(sock)
return retCode
end
@@ -349,10 +348,12 @@ function InternetDetector:mainLoop()
signal.signal(signal.SIGUSR1, function(signo) self:resetUiCounter(signo) end)
local lastStatus, currentStatus, mTimeNow, mTimeDiff, mLastTime, uiTimeNow, uiLastTime
local interval = self.serviceConfig.interval_up
local counter = 0
local onStart = true
_RUNNING = true
local interval = self.serviceConfig.interval_up
local modulesStatus = {}
local counter = 0
local onStart = true
local inetChecked = false
_RUNNING = true
while _RUNNING do
if counter == 0 or counter >= interval then
currentStatus = self:checkHosts()
@@ -377,11 +378,11 @@ function InternetDetector:mainLoop()
self:writeLogMessage("notice", "Disconnected")
end
end
counter = 0
end
mTimeDiff = 0
mTimeDiff = 0
inetChecked = (counter == 0)
for _, e in ipairs(self.modules) do
mTimeNow = time.clock_gettime(time.CLOCK_MONOTONIC).tv_sec
if mLastTime then
@@ -390,16 +391,17 @@ function InternetDetector:mainLoop()
mTimeDiff = 1
end
mLastTime = mTimeNow
e:run(currentStatus, lastStatus, mTimeDiff, mTimeNow)
e:run(currentStatus, lastStatus, mTimeDiff, mTimeNow, inetChecked)
end
local modulesStatus = {}
local modStatusChanged = false
for k, v in ipairs(self.modules) do
if v.status ~= nil then
if modulesStatus[v.name] ~= v.status then
modulesStatus[v.name] = v.status
modStatusChanged = true
end
end
if next(modulesStatus) then
if modStatusChanged and next(modulesStatus) then
self:writeValueToFile(self.statusFile, self:statusJson(
currentStatus, self.serviceConfig.instance, modulesStatus))
end

View File

@@ -140,7 +140,7 @@ function Module:ledRunFunc(t, currentStatus)
end
end
function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
function Module:run(currentStatus, lastStatus, timeDiff, timeNow, inetChecked)
if not self._enabled then
return
end

View File

@@ -10,63 +10,68 @@ local Module = {
readValue = function(filePath) return nil end,
deadPeriod = 900,
attempts = 1,
iface = nil,
restartTimeout = 0,
status = nil,
_attemptsCounter = 0,
_deadCounter = 0,
_networkRestarted = false,
_ifaceRestarting = false,
_ifaceRestartCounter = 0,
_netIfaces = {},
_netDevices = {},
_netItemsNum = 0,
}
function Module:toggleFunc(flag)
return
end
function Module:toggleDevice(flag)
if not self.iface then
function Module:toggleDevices(flag)
if #self._netDevices == 0 then
return
end
local ip = "/sbin/ip"
if unistd.access(ip, "x") then
return os.execute(string.format(
"%s link set dev %s %s", ip, self.iface, (flag and "up" or "down"))
)
for _, v in ipairs(self._netDevices) do
os.execute(string.format("%s link set dev %s %s", ip, v, (flag and "up" or "down")))
end
end
end
function Module:toggleIface(flag)
if not self.iface then
function Module:toggleIfaces(flag)
if #self._netIfaces == 0 then
return
end
return os.execute(
string.format("%s %s", (flag and "/sbin/ifup" or "/sbin/ifdown"), self.iface)
)
for _, v in ipairs(self._netIfaces) do
os.execute(string.format("%s %s", (flag and "/sbin/ifup" or "/sbin/ifdown"), v))
end
end
function Module:ifaceUp()
self:toggleFunc(true)
function Module:netItemsUp()
self:toggleDevices(true)
self:toggleIfaces(true)
end
function Module:ifaceDown()
self:toggleFunc(false)
function Module:netItemsDown()
self:toggleIfaces(false)
self:toggleDevices(false)
end
function Module:networkRestart()
function Module:restartNetworkService()
return os.execute("/etc/init.d/network restart")
end
function Module:init(t)
local iface = t.iface
if iface then
self.iface = iface
if self.iface:match("^@") then
self.iface = self.iface:gsub("^@", "")
self.toggleFunc = self.toggleIface
else
self.toggleFunc = self.toggleDevice
if t.ifaces ~= nil and type(t.ifaces) == "table" then
self._netIfaces = {}
self._netDevices = {}
self._netItemsNum = 0
for k, v in ipairs(t.ifaces) do
if v:match("^@") then
self._netIfaces[#self._netIfaces + 1] = v:gsub("^@", "")
else
self._netDevices[#self._netDevices + 1] = v
end
self._netItemsNum = self._netItemsNum + 1
end
end
if t.attempts ~= nil then
self.attempts = tonumber(t.attempts)
end
@@ -79,10 +84,34 @@ function Module:init(t)
self._attemptsCounter = self.attempts
end
function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
function Module:networkRestartFunc()
if self._netItemsNum > 0 then
if #self._netIfaces > 0 then
self.syslog("info", string.format("%s: restarting interfaces: %s",
self.name, table.concat(self._netIfaces, ", ")))
end
if #self._netDevices > 0 then
self.syslog("info", string.format("%s: restarting devices: %s",
self.name, table.concat(self._netDevices, ", ")))
end
self:netItemsDown()
if self.restartTimeout < 1 then
self:netItemsUp()
else
self._ifaceRestarting = true
end
else
self.syslog("info", string.format(
"%s: restarting network", self.name))
self:restartNetworkService()
end
self._attemptsCounter = self._attemptsCounter + 1
end
function Module:run(currentStatus, lastStatus, timeDiff, timeNow, inetChecked)
if self._ifaceRestarting then
if self._ifaceRestartCounter >= self.restartTimeout then
self:ifaceUp()
self:netItemsUp()
self._ifaceRestarting = false
self._ifaceRestartCounter = 0
else
@@ -90,31 +119,23 @@ function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
end
else
if currentStatus == 1 then
if self._attemptsCounter < self.attempts then
if self._deadCounter >= self.deadPeriod then
if self.iface then
self.syslog("info", string.format(
"%s: restarting network interface '%s'", self.name, self.iface))
self:ifaceDown()
if self.restartTimeout < 1 then
self:ifaceUp()
else
self._ifaceRestarting = true
end
if not self._networkRestarted then
if self._attemptsCounter < self.attempts then
if self._deadCounter >= self.deadPeriod then
self:networkRestartFunc()
self._networkRestarted = true
self._deadCounter = 0
else
self.syslog("info", string.format(
"%s: restarting network", self.name))
self:networkRestart()
self._deadCounter = self._deadCounter + timeDiff
end
self._deadCounter = 0
self._attemptsCounter = self._attemptsCounter + 1
else
self._deadCounter = self._deadCounter + timeDiff
end
elseif inetChecked and self._attemptsCounter < self.attempts then
self:networkRestartFunc()
end
else
self._attemptsCounter = 0
self._deadCounter = 0
self._attemptsCounter = 0
self._deadCounter = 0
self._networkRestarted = false
end
self._ifaceRestartCounter = 0
end

View File

@@ -256,7 +256,6 @@ function Module:decodeMessage(message)
if numAnswers > 0 then
for answerCount = 1, numAnswers do
if answerSectionStarts < #message then
local ATYPE = tonumber(
message:sub(answerSectionStarts + 5, answerSectionStarts + 8), 16)
@@ -359,7 +358,7 @@ function Module:init(t)
self._enabled = true
end
function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
function Module:run(currentStatus, lastStatus, timeDiff, timeNow, inetChecked)
if not self._enabled then
return
end
@@ -384,22 +383,16 @@ function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
if self._counter > 0 then
self:runIpScript()
end
else
self.status = nil
end
self._currentIp = ip
self._counter = 0
else
self.status = nil
end
else
self.status = nil
self._currentIp = nil
self.status = self._currentIp
self._counter = 0
self._interval = self.runInterval
end
self._counter = self._counter + timeDiff
end

View File

@@ -36,7 +36,7 @@ function Module:init(t)
end
end
function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
function Module:run(currentStatus, lastStatus, timeDiff, timeNow, inetChecked)
if currentStatus == 1 then
if not self._rebooted then
if timeNow >= self.antiBootloopDelay and self._deadCounter >= self.deadPeriod then

View File

@@ -1,6 +1,7 @@
local stdlib = require("posix.stdlib")
local unistd = require("posix.unistd")
local time = require("posix.time")
local Module = {
name = "mod_regular_script",
@@ -14,6 +15,7 @@ local Module = {
script = "",
status = nil,
_nextTime = nil,
_firstRun = true,
}
function Module:runExternalScript(scriptPath, currentStatus)
@@ -36,7 +38,7 @@ function Module:init(t)
end
end
function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
function Module:run(currentStatus, lastStatus, timeDiff, timeNow, inetChecked)
if not self._nextTime then
if timeNow < self.runInterval then
self._nextTime = self.runInterval
@@ -44,11 +46,16 @@ function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
self._nextTime = timeNow - (timeNow % self.runInterval) + self.runInterval
end
end
if self._firstRun then
self.status = time.strftime ("%Y-%m-%d %H:%M:%S %z", time.localtime(time.time() + self._nextTime - timeNow))
self._firstRun = false
end
if timeNow >= self._nextTime then
self._nextTime = self._nextTime + self.runInterval
if self.inetState == 2 or (self.inetState == 0 and currentStatus == 0) or (self.inetState == 1 and currentStatus == 1) then
self.status = time.strftime ("%Y-%m-%d %H:%M:%S %z", time.localtime(time.time() + self._nextTime - timeNow))
self:runExternalScript(self.script, currentStatus)
end
self._nextTime = self._nextTime + self.runInterval
end
end

View File

@@ -40,7 +40,7 @@ function Module:init(t)
end
end
function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
function Module:run(currentStatus, lastStatus, timeDiff, timeNow, inetChecked)
if currentStatus == 1 then
self._aliveCounter = 0
self._upScriptExecuted = false