small-package/luci-app-netspeedtest/luasrc/controller/netspeedtest.lua

86 lines
3.0 KiB
Lua
Raw Normal View History

2022-10-18 20:28:10 +08:00
-- Copyright (C) 2020-2022 sirpdboy <herboy2008@gmail.com> https://github.com/sirpdboy/netspeedtest
2021-10-01 02:17:38 +08:00
module("luci.controller.netspeedtest", package.seeall)
2022-10-18 20:28:10 +08:00
local fs=require"nixio.fs"
2023-01-14 20:16:49 +08:00
local sys = require "luci.sys"
2021-10-01 02:17:38 +08:00
function index()
2024-02-27 15:28:09 +08:00
if not nixio.fs.access("/etc/config/netspeedtest") then return end
local e = entry({"admin","network","netspeedtest"},alias("admin", "network", "netspeedtest", "homebox"),_("Net Speedtest"), 90)
e.dependent=false
e.acl_depends = { "luci-app-netspeedtest" }
2023-01-14 20:16:49 +08:00
entry({"admin","network","netspeedtest","homebox"},cbi("netspeedtest/homebox"),_("Lan homebox Web"),20).leaf = true
2022-10-18 20:28:10 +08:00
entry({"admin","network","netspeedtest","speedtestiperf3"},cbi("netspeedtest/speedtestiperf3", {hideapplybtn=true, hidesavebtn=true, hideresetbtn=true}),_("Lan Speedtest Iperf3"),30).leaf = true
entry({"admin","network","netspeedtest","speedtestwan"},cbi("netspeedtest/speedtestwan", {hideapplybtn=true, hidesavebtn=true, hideresetbtn=true}),_("Wan Speedtest"), 40).leaf = true
2021-10-01 02:17:38 +08:00
2022-10-18 20:28:10 +08:00
entry({"admin", "network", "netspeedtest", "checknet"}, call("check_net"))
entry({"admin", "network", "iperf3_status"}, call("iperf3_status"))
2021-10-01 02:17:38 +08:00
entry({"admin", "network","test_iperf0"}, post("test_iperf0"), nil).leaf = true
entry({"admin", "network","test_iperf1"}, post("test_iperf1"), nil).leaf = true
2023-01-14 20:16:49 +08:00
entry({"admin","network","netspeedtest", "speedtestrun"}, call("speedtestrun"))
entry({"admin","network","netspeedtest", "netperfrun"}, call("netperfrun"))
2021-10-01 02:17:38 +08:00
entry({"admin", "network", "netspeedtest", "realtime_log"}, call("get_log"))
2022-10-18 20:28:10 +08:00
entry({"admin", "network", "netspeedtest", "dellog"},call("dellog"))
2021-10-01 02:17:38 +08:00
end
2023-01-14 20:16:49 +08:00
2022-10-18 20:28:10 +08:00
function iperf3_status()
local e={}
2023-01-14 20:16:49 +08:00
e.run=sys.call("pgrep iperf3 >/dev/null")==0
2022-10-18 20:28:10 +08:00
luci.http.prepare_content("application/json")
luci.http.write_json(e)
end
2022-03-01 20:33:01 +08:00
2022-10-18 20:28:10 +08:00
function testout(cmd, addr)
2021-10-01 02:17:38 +08:00
luci.http.prepare_content("text/plain")
local util = io.popen(cmd)
if util then
while true do
local ln = util:read("*l")
if not ln then break end
luci.http.write(ln)
luci.http.write("\n")
end
util:close()
end
end
function test_iperf0(addr)
2023-01-14 20:16:49 +08:00
sys.call("pgrep -f unblockneteasemusic | xargs kill -9 >/dev/null 2>&1 ")
sys.call("/etc/init.d/unblockneteasemusic stop ")
sys.call("/etc/init.d/unblockmusic stop ")
2022-10-18 20:28:10 +08:00
testout("iperf3 -s ", addr)
2021-10-01 02:17:38 +08:00
end
function test_iperf1(addr)
2023-01-14 20:16:49 +08:00
sys.call("pgrep -f iperf3 | xargs kill -9 >/dev/null 2>&1 ")
sys.call("/etc/init.d/unblockneteasemusic restart")
sys.call("/etc/init.d/unblockmusic restart")
2021-10-01 02:17:38 +08:00
end
function get_log()
local e = {}
2023-01-14 20:16:49 +08:00
e.running = sys.call("busybox ps -w | grep netspeedtest | grep -v grep >/dev/null") == 0
2021-10-01 02:17:38 +08:00
e.log = fs.readfile("/var/log/netspeedtest.log") or ""
luci.http.prepare_content("application/json")
luci.http.write_json(e)
end
2022-10-18 20:28:10 +08:00
function dellog()
fs.writefile("/var/log/netspeedtest.log","")
http.prepare_content("application/json")
http.write('')
end
2023-01-14 20:16:49 +08:00
function speedtestrun()
2022-10-18 20:28:10 +08:00
testout("/etc/init.d/netspeedtest nstest ")
2021-10-01 02:17:38 +08:00
end
2022-10-18 20:28:10 +08:00
2023-01-14 20:16:49 +08:00
function netperfrun()
testout("sh /usr/bin/netperftest")
end