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("CLIENT DISABLED
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("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 - -- 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("BUSY
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("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 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("SUCCESS
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("SERVER DISABLED
iperf server is disabled
") + local ip = iplookup(server) + if not ip then + print("SERVER ERROR
iperf no such server
") else - print("SERVER ERROR
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("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") + io.flush() + print("
Client: " .. node .. "\nServer: " .. server .. "\n" .. running:read("*a") .. "
") + break + end + end + remote:close() end end end