#!/usr/bin/lua --[[ Part of AREDN® -- Used for creating Amateur Radio Emergency Data Networks Copyright (C) 2022 Tim Wilkinson 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 . Additional Terms: Additional use restrictions exist on the AREDN® 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("uci") require("nixio") require("aredn.utils") require("aredn.info") local node = aredn.info.get_nvram("node") local q = os.getenv("QUERY_STRING") or "" local server = q:match("server=([^&]*)") local protocol = q:match("protocol=([^&]*)") or "tcp" local kill = q:match("kill=1") and true or false print("Content-type: text/html\r") print("Cache-Control: no-store\r") print("Access-Control-Allow-Origin: *\r") print("\r") if uci.cursor():get("aredn", "@iperf[0]", "enable") == "0" then print("CLIENT DISABLED
iperf is disabled
") elseif not server then print("ERROR
Provide a server name to run a test between this client and a server [/cgi-bin/iperf?server=<ServerName>&protocol=<udp|tcp>]
") elseif server:match("[^%w%-%.]") then print("ERROR
Illegal server name
") else if server == "" then -- iperf server if kill then os.execute("/usr/bin/killall -9 iperf3 > /dev/null 2>&1") else if io.popen("/bin/pidof iperf3"):read("*a") ~= "" then print("BUSY
iperf server busy
") return end end local first = true local running = io.popen("/usr/bin/iperf3 -s -1 --idle-timeout 20 --forceflush -B 0.0.0.0") if not running then print("SERVER ERROR
iperf server failed to start
") return end running:read("*l") print("RUNNING
iperf server running
") io.flush() return else -- iperf client if not server:match("%.") then server = server .. ".local.mesh" end local ip = iplookup(server) if not ip then print("SERVER ERROR
iperf no such server
") else local remote = io.popen("/usr/bin/wget -O - 'http://" .. ip .. ":8080/cgi-bin/iperf?" .. (kill and "kill=1&" or "") .. "server=' 2>&1") if not remote then print("CLIENT ERROR
iperf failed to call remote server
") return end while true do local line = remote:read("*l") if not line then print("ERROR
iperf unknown error
") break elseif line:match("CLIENT DISABLED") then print("SERVER DISABLED
iperf server is disabled
") break elseif line:match("BUSY") then print("SERVER BUSY
iperf server is busy
") break elseif line:match("ERROR") then print("SERVER ERROR
iperf server error
") break elseif line:match("RUNNING") then local running = io.popen("/usr/bin/iperf3 --forceflush --rcv-timeout 20000 -b 0 -Z -c " .. ip .. (protocol == "udp" and " -u" or "") .. " -l 16K 2>&1") if not running then print("CLIENT ERROR
iperf client failed
") break end print("SUCCESS") print("
Client: " .. node .. "\nServer: " .. server)
                    io.flush()
                    for line in running:lines()
                    do
                        print(line)
                        io.flush()
                    end
                    running:close()
                    print("
") break end end remote:close() end end end