aredn/files/www/cgi-bin/scan

287 lines
8.4 KiB
Lua
Executable File

#!/usr/bin/lua
--[[
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
Copyright (C) 2021 Tim Wilkinson
Original Perl Copyright (C) 2015 Conrad Lara
See Contributors file for additional contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional Terms:
Additional use restrictions exist on the AREDN(TM) trademark and logo.
See AREDNLicense.txt for more info.
Attributions to the AREDN Project must be retained in the source code.
If importing this code into a new or existing project attribution
to the AREDN project must be added to the source code.
You must not misrepresent the origin of the material contained within.
Modified versions must be modified to attribute to the original source
and be marked in reasonable ways as differentiate it from the original
version
--]]
require("aredn.http")
require("aredn.hardware")
require("aredn.utils")
local html = require("aredn.html")
local aredn_info = require("aredn.info")
local node = aredn_info.get_nvram("node")
if not node then
node = "NOCALL"
end
local wifiiface = aredn.hardware.get_iface_name("wifi")
local nf = iwinfo.nl80211.noise(wifiiface) or -95
if not nixio.fs.stat("/tmp/web") then
nixio.fs.mkdir("/tmp/web")
end
local channels = aredn.hardware.get_rfchannels(wifiiface)
local scan_list = ""
for _, channel in ipairs(channels)
do
scan_list = scan_list .. " " .. channel.frequency
end
-- scan start
local scanned = {}
local f = io.popen("iw dev " .. wifiiface .. " scan freq" .. scan_list .. " passive")
if f then
local scan = {}
for line in f:lines()
do
local m = line:match("^BSS ([%da-fA-F:]+)")
if m then
scan = scanned[m]
if not scan then
scan = {
mac = m,
mode = "AP",
ssid = "",
signal = 0,
freq = {},
key = "",
joined = false
}
scanned[m] = scan
elseif scan.joined then
scan = {
freq = {}
}
end
if line:match("joined") then
scan.mode = "My Ad-Hoc Network"
scan.joined = true
end
end
m = line:match("freq: (%d+)")
if m then
scan.freq[m] = true
end
m = line:match("SSID: (.+)")
if m then
scan.ssid = m
end
m = line:match("signal: ([%d%-]+)")
if m then
scan.signal = tonumber(m)
end
m = line:match("Group cipher: (.+)")
if m then
scan.key = m
end
if line:match("capability: IBSS") and scan.mode == "AP" then
scan.mode = "Foreign Ad-Hoc Network"
end
end
f:close()
end
local f = io.popen("iw dev " .. wifiiface .. " station dump")
if f then
local scan = {}
local myssid = aredn_info.getSSID()
local myfreq = tonumber(aredn_info.getFreq(aredn_info.getMeshRadioDevice()))
for line in f:lines()
do
local m = line:match("^Station ([%da-fA-F:]+) %(on " .. wifiiface .. "%)")
if m then
scan = scanned[m]
if not scan then
scan = {
mac = m,
signal = 0,
freq = {},
key = "",
joined = false
}
scanned[m] = scan
end
scan.mode = "Connected Ad-Hoc Station"
scan.ssid = myssid
scan.freq[myfreq] = true
end
m = line:match("signal avg:%s+([%d%-]+)")
if m then
scan.signal = tonumber(m)
end
end
f:close()
end
-- scan end
if os.getenv("REQUEST_METHOD") == "POST" then
require('luci.ohttp')
local request = luci.http.Request(nixio.getenv(),
function()
local v = io.read(1024)
if not v then
io.close()
end
return v
end
)
if request:formvalue("auto") then
io.open("/tmp/web/autoscan", "w"):close()
end
if request:formvalue("stop") then
os.remove("/tmp/web/autoscan")
end
end
-- generate page
http_header()
html.header(node .. " WiFi scan", false)
local autoscan = nixio.fs.stat("/tmp/web/autoscan");
if autoscan then
html.print("<meta http-equiv='refresh' content='5;url=/cgi-bin/scan'>")
end
html.print([[
<script src="/js/sorttable-min.js"></script>
<style>
table {
border-collapse:collapse;
}
table.sortable thead {
background-color:#eee;
color:#666666;
font-weight: bold;
cursor: default;
}
</style>
</head>
<body><form method=post action=/cgi-bin/scan enctype='multipart/form-data'>
<center>
]])
html.alert_banner()
html.print("<h1>" .. node .. " WiFi scan</h1><hr>")
if autoscan then
html.print("<input type=submit name=stop value=Stop title='Abort continuous scan'>")
else
html.print("<input type=button name=refresh value=Refresh title='Refresh this page' onclick='window.location.reload();'>")
html.print("&nbsp;&nbsp;&nbsp;")
html.print("<input type=submit name=auto value=Auto title='Begin continuous scan'>")
end
html.print("&nbsp;&nbsp;&nbsp;")
html.print("<button type=button onClick='window.location=\"status\"' title='Return to status page'>Quit</button><br><br>")
-- display scan
html.print("<table class=sortable border=1 cellpadding=5>")
html.print("<tr><th>SNR</th><th>Signal</th><th>Chan</th><th>Enc</th><th>SSID</th><th>Hostname</th><th>MAC/BSSID</th><th>802.11 Mode</th></tr>")
-- load arp cache
local arpcache = {}
arptable(function(a)
arpcache[a["HW address"]] = a["IP address"]
end)
local scanlist = {}
for _, v in pairs(scanned)
do
if v.signal ~= 0 or v.joined then
scanlist[#scanlist + 1] = v
end
end
table.sort(scanlist, function(a, b) return a.signal > b.signal end)
for _, scan in ipairs(scanlist)
do
-- freq to chan
local chan = {}
for f, _ in pairs(scan.freq)
do
f = tonumber(f)
if f < 256 then
elseif f == 2484 then
chan[#chan + 1] = 14
elseif f == 2407 then
chan[#chan + 1] = 0
elseif f < 2484 then
chan[#chan + 1] = (f - 2407) / 5
elseif f < 5000 then
elseif f < 5380 then
chan[#chan + 1] = (f - 5000) / 5
elseif f < 5500 then
chan[#chan + 1] = f - 2000
elseif f < 6000 then
chan[#chan + 1] = (f - 5000) / 5
end
end
table.sort(chan)
chan = table.concat(chan, " ")
if scan.joined then
hostname = node
else
-- 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
if scan.ssid:match("^AREDN-") then
html.print("<tr class=\"wscan-row-node\">")
else
html.print("<tr>")
end
html.print("<td>" .. (scan.signal - nf) .. "</td><td>" .. scan.signal .. "</td><td>" .. chan .. "</td><td>" .. scan.key .. "</td><td>" .. scan.ssid .. "</td><td align=center>" .. hostname .. "</td><td>" .. scan.mac:upper() .. "</td><td>" .. scan.mode .. "</td>")
html.print("</tr>")
end
html.print("</table><br></center>")
html.footer()
html.print("</body></html>")