New module: mod_public_ip

This commit is contained in:
gSpot
2022-08-13 17:17:45 +03:00
parent 78067be67f
commit f81cfc930f
18 changed files with 349 additions and 55 deletions

View File

@@ -5,7 +5,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=internet-detector
PKG_VERSION:=0.5
PKG_VERSION:=0.6
PKG_RELEASE:=0
PKG_MAINTAINER:=gSpot <https://github.com/gSpotx2f/luci-app-internet-detector>
@@ -48,6 +48,7 @@ define Package/$(PKG_NAME)/install
$(INSTALL_BIN) ./files/usr/bin/internet-detector $(1)/usr/bin/internet-detector
$(INSTALL_DIR) $(1)/usr/lib/internet-detector
$(INSTALL_DATA) ./files/usr/lib/internet-detector/mod_email.lua $(1)/usr/lib/internet-detector/mod_email.lua
$(INSTALL_DATA) ./files/usr/lib/internet-detector/mod_public_ip.lua $(1)/usr/lib/internet-detector/mod_public_ip.lua
$(INSTALL_DATA) ./files/usr/lib/internet-detector/mod_led_control.lua $(1)/usr/lib/internet-detector/mod_led_control.lua
$(INSTALL_DATA) ./files/usr/lib/internet-detector/mod_modem_restart.lua $(1)/usr/lib/internet-detector/mod_modem_restart.lua
$(INSTALL_DATA) ./files/usr/lib/internet-detector/mod_network_restart.lua $(1)/usr/lib/internet-detector/mod_network_restart.lua

View File

@@ -33,6 +33,12 @@ config module 'mod_modem_restart'
option dead_period '600'
option any_band '0'
config module 'mod_public_ip'
option enabled '0'
option provider 'opendns1'
option interval '600'
option timeout '3'
config module 'mod_email'
option enabled '0'
option alive_period '0'

View File

@@ -132,6 +132,16 @@ local function readValueFromFile(filePath)
return retValue
end
local function statusJson(inet, t)
local lines = { [1] = string.format('"inet":%d', inet) }
if t then
for k, v in pairs(t) do
lines[#lines + 1] = string.format('"%s":"%s"', k, v)
end
end
return "{" .. table.concat(lines, ",") .. "}"
end
local function writeLogMessage(level, msg)
if Config.enableLogger then
nixio.syslog(level, msg)
@@ -203,7 +213,7 @@ local function TCPConnectionToHost(host, port)
if addrInfo then
local family = addrInfo[1].family
if family then
local socket = nixio.socket(family, "stream")
local socket = nixio.socket(family, "stream")
socket:setopt("socket", "sndtimeo", Config.connectionTimeout)
socket:setopt("socket", "rcvtimeo", Config.connectionTimeout)
if Config.iface then
@@ -264,23 +274,22 @@ local function main()
if counter == 0 or counter >= interval then
currentStatus = checkHosts()
if not nixio.fs.access(Config.statusFile, "r") then
writeValueToFile(Config.statusFile, currentStatus)
writeValueToFile(Config.statusFile, statusJson(currentStatus))
end
if currentStatus == 0 then
interval = Config.intervalUp
if lastStatus ~= nil and currentStatus ~= lastStatus then
writeValueToFile(Config.statusFile, currentStatus)
writeValueToFile(Config.statusFile, statusJson(currentStatus))
writeLogMessage("notice", "Internet connected")
end
else
interval = Config.intervalDown
if lastStatus ~= nil and currentStatus ~= lastStatus then
writeValueToFile(Config.statusFile, currentStatus)
writeValueToFile(Config.statusFile, statusJson(currentStatus))
writeLogMessage("notice", "Internet disconnected")
end
end
counter = 0
end
@@ -295,6 +304,17 @@ local function main()
lastTime = timeNow
e:run(currentStatus, lastStatus, timeDiff)
end
local modulesStatus = {}
for k, v in ipairs(Config.modules) do
if v.status ~= nil then
modulesStatus[v.name] = v.status
end
end
if next(modulesStatus) then
writeValueToFile(Config.statusFile, statusJson(currentStatus, modulesStatus))
end
lastStatus = currentStatus
nixio.nanosleep(1)
counter = counter + 1
@@ -326,24 +346,29 @@ local function poll(attempts, timeout)
Config.connectionTimeout = timeout
end
if checkHosts() == 0 then
return "up"
return statusJson(0)
else
return "down"
return statusJson(1)
end
end
local function inetStatus()
local inetStat = "down"
local function inetStatus(json)
local inetStat = 1
if nixio.fs.access(Config.statusFile, "r") then
local inetStatVal = readValueFromFile(Config.statusFile)
if inetStatVal ~= nil and tonumber(inetStatVal) == 0 then
inetStat = "up"
end
inetStat = inetStatVal
elseif Config.mode == 1 then
inetStat = poll()
else
os.exit(126)
end
if not json then
local sVal = inetStat:match('"inet":[0-9]')
if sVal then
sVal = sVal:match("[0-9]")
inetStat = (tonumber(sVal) == 0) and "up" or "down"
end
end
return inetStat
end
@@ -489,7 +514,7 @@ parseHosts()
local function help()
return string.format(
"Usage: %s [start|stop|restart|no-daemon|debug|status|inet-status|poll [<attempts num>] [<timeout sec>]|--help]",
"Usage: %s [start|stop|restart|no-daemon|debug|status|inet-status|inet-status-json|poll [<attempts num>] [<timeout sec>]|--help]",
arg[0]
)
end
@@ -509,6 +534,8 @@ elseif arg[1] == "status" then
print(status())
elseif arg[1] == "inet-status" then
print(inetStatus())
elseif arg[1] == "inet-status-json" then
print(inetStatus(true))
elseif arg[1] == "poll" then
local attempts, timeout
if arg[2] and arg[2]:match("[0-9]+") then

View File

@@ -20,6 +20,7 @@ local Module = {
mailSmtp = "smtp.gmail.com",
mailSmtpPort = '587',
mailSecurity = "tls",
status = nil,
_enabled = false,
_aliveCounter = 0,
_msgSent = true,

View File

@@ -10,6 +10,7 @@ local Module = {
runInterval = 5,
sysLedsDir = "/sys/class/leds",
ledName = nil,
status = nil,
_enabled = false,
_ledDir = nil,
_ledMaxBrightnessFile = nil,

View File

@@ -15,6 +15,7 @@ local Module = {
deadPeriod = 0,
iface = nil,
anyBand = false,
status = nil,
_enabled = false,
_deadCounter = 0,
_restarted = false,

View File

@@ -11,6 +11,7 @@ local Module = {
attempts = 0,
deadPeriod = 0,
restartTimeout = 0,
status = nil,
_attemptsCounter = 0,
_deadCounter = 0,
}

View File

@@ -0,0 +1,139 @@
local nixio = require("nixio")
local Module = {
name = "mod_public_ip",
config = {},
syslog = function(level, msg) return true end,
writeValue = function(filePath, str) return false end,
readValue = function(filePath) return nil end,
runInterval = 600,
nslookup = "/usr/bin/nslookup",
timeout = 3,
providers = {
opendns1 = {
name = "opendns1", server = "208.67.222.222",
host = "myip.opendns.com", queryType = "a"
},
opendns2 = {
name = "opendns2", server = "208.67.220.220",
host = "myip.opendns.com", queryType = "a"
},
opendns3 = {
name = "opendns3", server = "208.67.222.220",
host = "myip.opendns.com", queryType = "a"
},
opendns4 = {
name = "opendns4", server = "208.67.220.222",
host = "myip.opendns.com", queryType = "a"
},
akamai = {
name = "akamai", server = "ns1-1.akamaitech.net",
host = "whoami.akamai.net", queryType = "a"
},
google = {
name = "google", server = "ns1.google.com",
host = "o-o.myaddr.l.google.com", queryType = "txt"
},
},
status = nil,
_provider = nil,
_nslookupCmd = nil,
_currentIp = nil,
_enabled = false,
_counter = 0,
}
function Module:parseA(str)
res = str:match("Name:%s+" .. self._provider.host .. "\nAddress:%s+[%w.:]+")
if res then
return res:match("[%w.:]+$")
end
end
function Module:parseGoogle(str)
res = str:match(self._provider.host .. '%s+text%s+=%s+"[%w.:]+"')
if res then
return res:gsub('"', ''):match("[%w.:]+$")
end
end
function Module:resolveIP()
local res
local fh = io.popen(self._nslookupCmd, "r")
if fh then
output = fh:read("*a")
fh:close()
if self._provider.name == "google" then
res = self:parseGoogle(output)
else
res = self:parseA(output)
end
else
self.syslog("err", string.format(
"%s: Nslookup call failed (%s)", self.name, self.nslookup))
end
return res or "Undefined"
end
function Module:init(t)
if t.interval then
self.runInterval = tonumber(t.interval)
end
if t.timeout then
self.timeout = tonumber(t.timeout)
end
if t.provider then
self._provider = self.providers[t.provider]
else
self._provider = self.providers.opendns1
end
if not nixio.fs.access(self.nslookup, "x") then
self._enabled = false
self.syslog(
"warning",
string.format("%s: '%s' does not exists", self.name, self.nslookup)
)
else
self._enabled = true
self._nslookupCmd = string.format(
"%s -type=%s -timeout=%d %s %s",
self.nslookup,
self._provider.queryType,
self.timeout,
self._provider.host,
self._provider.server
)
end
end
function Module:run(currentStatus, lastStatus, timeDiff)
if not self._enabled then
return
end
if currentStatus == 0 then
if self._counter == 0 or self._counter >= self.runInterval or currentStatus ~= lastStatus then
local ip = self:resolveIP()
if ip ~= self._currentIp then
self.status = ip
self.syslog(
"notice",
string.format("%s: public IP address %s", self.name, ip)
)
else
self.status = nil
end
self._currentIp = ip
self._counter = 0
else
self.status = nil
end
else
self.status = nil
self._currentIp = nil
self._counter = 0
end
self._counter = self._counter + timeDiff
end
return Module

View File

@@ -9,6 +9,7 @@ local Module = {
readValue = function(filePath) return nil end,
deadPeriod = 0,
forceRebootDelay = 0,
status = nil,
_deadCounter = 0,
}

View File

@@ -11,6 +11,7 @@ local Module = {
alivePeriod = 0,
upScript = "",
downScript = "",
status = nil,
_deadCounter = 0,
_aliveCounter = 0,
_upScriptExecuted = true,