diff --git a/files/www/cgi-bin/iperf b/files/www/cgi-bin/iperf index ea5bd3a9..37bac4dd 100755 --- a/files/www/cgi-bin/iperf +++ b/files/www/cgi-bin/iperf @@ -36,6 +36,7 @@ require("uci") require("nixio") +require("aredn.utils") local info = require("aredn.info") local node = info.get_nvram("node") @@ -43,6 +44,7 @@ local node = 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" @@ -51,35 +53,73 @@ print("\r") if uci.cursor():get("aredn", "@iperf[0]", "enable") == "0" then print("
iperf is disabled") elseif not server then - print("
Provide a server name to run a test between this client and a server [/cgi-bin/iperf?server=<ServerName>&protocol=<udp|tcp>]") + print("
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("
Illegal server name") else - -- Using io.popen here causes the script to terminate (a bug?), so we have this workaround - if os.execute('if [ "$(pidof iperf3)" = "" ]; then exit 0; else exit 1; fi') ~= 0 then - print("
iperf server busy") - return - end + if server == "" then - os.execute("iperf3 -s -D -1 --idle-timeout 20 -B " .. node) - nixio.nanosleep(1, 0) + -- 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("
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("
iperf server failed to start") + return + end + running:read("*l") print("
iperf server running") + io.flush() + return else + -- iperf client if not server:match("%.") then server = server .. ".local.mesh" end - local status = "" - local f = io.popen("wget -q -O - 'http://" .. server .. ":8080/cgi-bin/iperf?server=' 2>&1") - if f then - status = f:read("*a") - f:close() - end - if status:match("running") then - print("
Client: " .. node .. "\nServer: " .. server .. "\n" .. io.popen("p=$$;setsid sh -c \"(sleep 20; kill $p)\" > /dev/null 2>&1 & /usr/bin/iperf3 --forceflush -b 0 -Z -c " .. server .. (protocol == "udp" and " -u" or "") .. " -l 1K 2>&1"):read("*a") .. "") - elseif status:match("iperf is disabled") then - print("
iperf server is disabled") + local ip = iplookup(server) + if not ip then + print("
iperf no such server") else - print("
iperf server failed to start server") + 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("
iperf failed to call remote server") + return + end + while true + do + local line = remote:read("*l") + if not line then + print("
iperf unknown error") + break + elseif line:match("CLIENT DISABLED") then + print("
iperf server is disabled") + break + elseif line:match("BUSY") then + print("
iperf server is busy") + break + elseif line:match("ERROR") then + print("
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("
iperf client failed") + break + end + print("
Client: " .. node .. "\nServer: " .. server .. "\n" .. running:read("*a") .. "") + break + end + end + remote:close() end end end