main.InternetDetector.debugOutput().

This commit is contained in:
gSpot
2025-06-24 18:24:15 +03:00
parent ef40cb3051
commit f9aa55ca4d
15 changed files with 77 additions and 86 deletions

View File

@@ -5,7 +5,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=internet-detector
PKG_VERSION:=1.5.1
PKG_VERSION:=1.5.2
PKG_RELEASE:=1
PKG_MAINTAINER:=gSpot <https://github.com/gSpotx2f/luci-app-internet-detector>

View File

@@ -157,6 +157,13 @@ function InternetDetector:writeLogMessage(level, msg)
end
end
function InternetDetector:debugOutput(msg)
if self.debug then
io.stdout:write(string.format("[%s] %s\n", os.date("%Y.%m.%d-%H:%M:%S"), msg))
io.stdout:flush()
end
end
function InternetDetector:loadModules()
self.modules = {}
local ok, modulesDir = pcall(dirent.files, self.modulesDir)
@@ -180,10 +187,11 @@ function InternetDetector:loadModules()
m = self:prequire(string.format("%s.modules.%s", self.appName, modName))
end
if m then
m.config = self
m.syslog = function(level, msg) self:writeLogMessage(level, msg) end
m.writeValue = function(filePath, str) return self:writeValueToFile(filePath, str) end
m.readValue = function(filePath) return self:readValueFromFile(filePath) end
m.config = self
m.syslog = function(level, msg) self:writeLogMessage(level, msg) end
m.debugOutput = function(msg) self:debugOutput(msg) end
m.writeValue = function(filePath, str) return self:writeValueToFile(filePath, str) end
m.readValue = function(filePath) return self:readValueFromFile(filePath) end
m:init(modConfig)
self.modules[#self.modules + 1] = m
end
@@ -223,12 +231,8 @@ function InternetDetector:pingHost(host)
)
local retCode = os.execute(ping)
if self.debug then
io.stdout:write(string.format(
"--- Ping ---\ntime = %s\n%s\nretCode = %s\n", os.time(), ping, retCode)
)
io.stdout:flush()
end
self:debugOutput(string.format(
"--- Ping ---\ntime = %s\n%s\nretCode = %s", os.time(), ping, retCode))
return retCode
end
@@ -238,21 +242,16 @@ function InternetDetector:TCPConnectionToHost(host, port)
local saTable, errMsg, errNum = socket.getaddrinfo(host, port or self.serviceConfig.tcp_port)
if not saTable then
if self.debug then
io.stdout:write(string.format(
"GETADDRINFO ERROR: %s, %s\n", errMsg, errNum))
end
self:debugOutput(string.format(
"GETADDRINFO ERROR: %s, %s", errMsg, errNum))
else
local family = saTable[1].family
if family then
local sock, errMsg, errNum = socket.socket(family, socket.SOCK_STREAM, 0)
if not sock then
if self.debug then
io.stdout:write(string.format(
"SOCKET ERROR: %s, %s\n", errMsg, errNum))
end
self:debugOutput(string.format(
"SOCKET ERROR: %s, %s", errMsg, errNum))
return retCode
end
@@ -265,10 +264,8 @@ function InternetDetector:TCPConnectionToHost(host, port)
local ok, errMsg, errNum = socket.setsockopt(sock, socket.SOL_SOCKET,
socket.SO_BINDTODEVICE, self.serviceConfig.iface)
if not ok then
if self.debug then
io.stdout:write(string.format(
"SOCKET ERROR: %s, %s\n", errMsg, errNum))
end
self:debugOutput(string.format(
"SOCKET ERROR: %s, %s", errMsg, errNum))
unistd.close(sock)
return retCode
end
@@ -278,23 +275,23 @@ function InternetDetector:TCPConnectionToHost(host, port)
if self.debug then
if not success then
io.stdout:write(string.format(
"SOCKET CONNECT ERROR: %s\n", tostring(success)))
self:debugOutput(string.format(
"SOCKET CONNECT ERROR: %s", tostring(success)))
end
local sockTable, err_s, e_s = socket.getsockname(sock)
local peerTable, err_p, e_p = socket.getpeername(sock)
if not sockTable then
sockTable = {}
io.stdout:write(
string.format("SOCKET ERROR: %s, %s\n", err_s, e_s))
self:debugOutput(
string.format("SOCKET ERROR: %s, %s", err_s, e_s))
end
if not peerTable then
peerTable = {}
io.stdout:write(
string.format("SOCKET ERROR: %s, %s\n", err_p, e_p))
self:debugOutput(
string.format("SOCKET ERROR: %s, %s", err_p, e_p))
end
io.stdout:write(string.format(
"--- TCP ---\ntime = %s\nconnection_timeout = %s\niface = %s\nhost:port = [%s]:%s\nsockname = [%s]:%s\npeername = [%s]:%s\nsuccess = %s\n",
self:debugOutput(string.format(
"--- TCP ---\ntime = %s\nconnection_timeout = %s\niface = %s\nhost:port = [%s]:%s\nsockname = [%s]:%s\npeername = [%s]:%s\nsuccess = %s",
os.time(),
self.serviceConfig.connection_timeout,
tostring(self.serviceConfig.iface),
@@ -306,7 +303,6 @@ function InternetDetector:TCPConnectionToHost(host, port)
tostring(peerTable.port),
tostring(success))
)
io.stdout:flush()
end
socket.shutdown(sock, socket.SHUT_RDWR)
@@ -400,6 +396,7 @@ function InternetDetector:mainLoop()
mTimeDiff = 1
end
mLastTime = mTimeNow
if self.debug then
e:run(currentStatus, lastStatus, mTimeDiff, mTimeNow, inetChecked)
else
@@ -423,7 +420,6 @@ function InternetDetector:mainLoop()
end
lastStatus = currentStatus
unistd.sleep(1)
counter = counter + 1
@@ -589,8 +585,8 @@ function InternetDetector:run()
f = function(t, prefix)
tables[t] = true
for k, v in pairs(t) do
io.stdout:write(string.format(
"%s%s = %s\n", prefix, k, tostring(v))
self:debugOutput(string.format(
"%s%s = %s", prefix, k, tostring(v))
)
if type(v) == "table" and not tables[v] then
f(v, string.format("%s%s.", prefix, k))
@@ -600,9 +596,8 @@ function InternetDetector:run()
return f
end
io.stdout:write("--- Config ---\n")
self:debugOutput("--- Config ---")
inspectTable()(self, "self.")
io.stdout:flush()
end
self:writeValueToFile(

View File

@@ -7,6 +7,7 @@ local Module = {
runPrio = 10,
config = {},
syslog = function(level, msg) return true end,
debugOutput = function(msg) return true end,
writeValue = function(filePath, str) return false end,
readValue = function(filePath) return nil end,
runInterval = 5,

View File

@@ -6,6 +6,7 @@ local Module = {
runPrio = 30,
config = {},
syslog = function(level, msg) return true end,
debugOutput = function(msg) return true end,
writeValue = function(filePath, str) return false end,
readValue = function(filePath) return nil end,
deadPeriod = 900,

View File

@@ -14,6 +14,7 @@ local Module = {
},
},
syslog = function(level, msg) return true end,
debugOutput = function(msg) return true end,
writeValue = function(filePath, str) return false end,
readValue = function(filePath) return nil end,
port = 53,
@@ -136,29 +137,21 @@ function Module:sendUDPMessage(message, server, port)
local retCode = 1
local data
if self.config.debug then
io.stdout:write(string.format("--- %s ---\n", self.name))
io.stdout:flush()
end
self.debugOutput(string.format("--- %s ---", self.name))
local saTable, errMsg, errNum = socket.getaddrinfo(server, port)
if not saTable then
if self.config.debug then
io.stdout:write(string.format(
"GETADDRINFO ERROR: %s, %s\n", errMsg, errNum))
end
self.debugOutput(string.format(
"GETADDRINFO ERROR: %s, %s", errMsg, errNum))
else
local family = saTable[1].family
if family then
local sock, errMsg, errNum = socket.socket(family, socket.SOCK_DGRAM, 0)
if not sock then
if self.config.debug then
io.stdout:write(string.format(
"SOCKET ERROR: %s, %s\n", errMsg, errNum))
end
self.debugOutput(string.format(
"SOCKET ERROR: %s, %s", errMsg, errNum))
return retCode
end
@@ -171,10 +164,8 @@ function Module:sendUDPMessage(message, server, port)
local ok, errMsg, errNum = socket.setsockopt(sock, socket.SOL_SOCKET,
socket.SO_BINDTODEVICE, self.config.serviceConfig.iface)
if not ok then
if self.config.debug then
io.stdout:write(string.format(
"SOCKET ERROR: %s, %s\n", errMsg, errNum))
end
self.debugOutput(string.format(
"SOCKET ERROR: %s, %s", errMsg, errNum))
unistd.close(sock)
return retCode
end
@@ -189,17 +180,17 @@ function Module:sendUDPMessage(message, server, port)
success = true
response = resp
elseif self.config.debug then
io.stdout:write(string.format(
"SOCKET RECV ERROR: %s, %s\n", tostring(resp), tostring(errNum)))
self.debugOutput(string.format(
"SOCKET RECV ERROR: %s, %s", tostring(resp), tostring(errNum)))
end
elseif self.config.debug then
io.stdout:write(string.format(
"SOCKET SEND ERROR: %s, %s\n", tostring(errMsg), tostring(errNum)))
self.debugOutput(string.format(
"SOCKET SEND ERROR: %s, %s", tostring(errMsg), tostring(errNum)))
end
if self.config.debug then
io.stdout:write(string.format(
"--- UDP ---\ntime = %s\nconnection_timeout = %s\niface = %s\nserver = %s:%s\nsockname = %s:%s\nsuccess = %s\n",
self.debugOutput(string.format(
"--- UDP ---\ntime = %s\nconnection_timeout = %s\niface = %s\nserver = %s:%s\nsockname = %s:%s\nsuccess = %s",
os.time(),
self.timeout,
tostring(self.config.serviceConfig.iface),
@@ -209,7 +200,6 @@ function Module:sendUDPMessage(message, server, port)
tostring(response.port),
tostring(success))
)
io.stdout:flush()
end
unistd.close(sock)
@@ -239,7 +229,7 @@ end
function Module:decodeMessage(message)
local retTable = {}
local t = {}
local t = {}
for i = 1, #message do
t[#t + 1] = string.format("%.2x", string.byte(message, i))
end

View File

@@ -6,6 +6,7 @@ local Module = {
runPrio = 20,
config = {},
syslog = function(level, msg) return true end,
debugOutput = function(msg) return true end,
writeValue = function(filePath, str) return false end,
readValue = function(filePath) return nil end,
deadPeriod = 3600,

View File

@@ -8,6 +8,7 @@ local Module = {
runPrio = 90,
config = {},
syslog = function(level, msg) return true end,
debugOutput = function(msg) return true end,
writeValue = function(filePath, str) return false end,
readValue = function(filePath) return nil end,
inetState = 2, -- 0: connected, 1: disconnected, 2: both

View File

@@ -6,6 +6,7 @@ local Module = {
runPrio = 80,
config = {},
syslog = function(level, msg) return true end,
debugOutput = function(msg) return true end,
writeValue = function(filePath, str) return false end,
readValue = function(filePath) return nil end,
deadPeriod = 0,