Improve the quality of the scan output (#609)

This commit is contained in:
Tim Wilkinson 2022-12-31 19:20:12 -08:00 committed by GitHub
parent 599819c73c
commit 8a3bc2d09c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 82 additions and 57 deletions

View File

@ -37,6 +37,7 @@
require("aredn.http") require("aredn.http")
require("aredn.hardware") require("aredn.hardware")
require("aredn.utils")
local html = require("aredn.html") local html = require("aredn.html")
local aredn_info = require("aredn.info") local aredn_info = require("aredn.info")
@ -56,33 +57,42 @@ end
local scanned = {} local scanned = {}
local f = io.popen("iw dev " .. wifiiface .. " scan passive") local f = io.popen("iw dev " .. wifiiface .. " scan passive")
if f then if f then
local scan local scan = {}
for line in f:lines() for line in f:lines()
do do
local m = line:match("^BSS ([%da-fA-F:]+)") local m = line:match("^BSS ([%da-fA-F:]+)")
if m then if m then
scan = { scan = scanned[m]
mac = m, if not scan then
mode = "AP", scan = {
ssid = "", mac = m,
signal = 0, mode = "AP",
freq = 0, ssid = "",
key = "" signal = 0,
} freq = {},
scanned[#scanned + 1] = scan key = "",
joined = false
}
scanned[m] = scan
elseif scan.joined then
scan = {
freq = {}
}
end
if line:match("joined") then if line:match("joined") then
scan.mode = "My Ad-Hoc Network" scan.mode = "My Ad-Hoc Network"
scan.joined = true
end end
end end
m = line:match("freq: (%d+)") m = line:match("freq: (%d+)")
if m then if m then
scan.freq = tonumber(m) scan.freq[m] = true
end end
m = line:match("SSID: (.+)") m = line:match("SSID: (.+)")
if m then if m then
scan.ssid = m scan.ssid = m
end end
m = line:match("signal: ([%d-]+)") m = line:match("signal: ([%d%-]+)")
if m then if m then
scan.signal = tonumber(m) scan.signal = tonumber(m)
end end
@ -99,24 +109,29 @@ end
local f = io.popen("iw dev " .. wifiiface .. " station dump") local f = io.popen("iw dev " .. wifiiface .. " station dump")
if f then if f then
local scan local scan = {}
local myssid = aredn_info.getSSID() local myssid = aredn_info.getSSID()
local myfreq = tonumber(aredn_info.getFreq()) local myfreq = tonumber(aredn_info.getFreq())
for line in f:lines() for line in f:lines()
do do
local m = line:match("^Station ([%da-fA-F:]+) %(on " .. wifiiface .. "%)") local m = line:match("^Station ([%da-fA-F:]+) %(on " .. wifiiface .. "%)")
if m then if m then
scan = { scan = scanned[m]
mac = m, if not scan then
mode = "Connected Ad-Hoc Station", scan = {
ssid = myssid, mac = m,
signal = 0, signal = 0,
freq = myfreq, freq = {},
key = "" key = "",
} joined = false
scanned[#scanned + 1] = scan }
scanned[m] = scan
end
scan.mode = "Connected Ad-Hoc Station"
scan.ssid = myssid
scan.freq[myfreq] = true
end end
m = line:match("signal avg:%s+([%d-]+)") m = line:match("signal avg:%s+([%d%-]+)")
if m then if m then
scan.signal = tonumber(m) scan.signal = tonumber(m)
end end
@ -195,9 +210,9 @@ arptable(function(a)
end) end)
local scanlist = {} local scanlist = {}
for _, v in ipairs(scanned) for _, v in pairs(scanned)
do do
if v.signal ~= 0 then if v.signal ~= 0 or v.joined then
scanlist[#scanlist + 1] = v scanlist[#scanlist + 1] = v
end end
end end
@ -205,40 +220,50 @@ table.sort(scanlist, function(a, b) return a.signal > b.signal end)
for _, scan in ipairs(scanlist) for _, scan in ipairs(scanlist)
do do
-- freq to chan -- freq to chan
local chan = scan.freq local chan = {}
if chan < 256 then for f, _ in pairs(scan.freq)
elseif chan == 2484 then do
chan = 14 f = tonumber(f)
elseif chan == 2407 then if f < 256 then
chan = 0 elseif f == 2484 then
elseif chan < 2484 then chan[#chan + 1] = 14
chan = (chan - 2407) / 5 elseif f == 2407 then
elseif chan < 5000 then chan[#chan + 1] = 0
elseif chan < 5380 then elseif f < 2484 then
chan = (chan - 5000) / 5 chan[#chan + 1] = (f - 2407) / 5
elseif chan < 5500 then elseif f < 5000 then
chan = chan - 2000 elseif f < 5380 then
elseif chan < 6000 then chan[#chan + 1] = (f - 5000) / 5
chan = (chan - 5000) / 5 elseif f < 5500 then
end chan[#chan + 1] = f - 2000
-- ip lookup then host lookup elseif f < 6000 then
local ip = arpcache[scan.mac] chan[#chan + 1] = (f - 5000) / 5
if ip then
hostname = ip
local f = io.popen("nslookup " .. ip)
if f then
for line in f:lines()
do
local m = line:match("name = (.*)%.local%.mesh")
if m then
hostname = m
break
end
end
f:close()
end end
end
table.sort(chan)
chan = table.concat(chan, " ")
if scan.joined then
hostname = node
else else
hostname = "N/A" -- ip lookup then host lookup
local ip = arpcache[scan.mac]
if ip then
hostname = ip
local f = io.popen("nslookup " .. ip)
if f then
for line in f:lines()
do
local m = line:match("name = (.*)%.local%.mesh")
if m then
hostname = m:gsub("^mid[0-9]*%.",""):gsub("^dtdlink%.",""):gsub("%.local%.mesh$","")
break
end
end
f:close()
end
else
hostname = "-"
end
end end
if scan.ssid:match("^AREDN-") then if scan.ssid:match("^AREDN-") then
html.print("<tr class=\"wscan-row-node\">") html.print("<tr class=\"wscan-row-node\">")