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 )
2024-03-23 04:15:49 +08:00
local http = require " luci.http "
2022-10-18 20:28:10 +08:00
local fs = require " nixio.fs "
2024-03-04 21:26:03 +08:00
local sys = require " luci.sys "
2024-03-07 20:27:54 +08:00
local uci = luci.model . uci.cursor ( )
name = ' netspeedtest '
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
2024-03-23 04:15:49 +08:00
local e = entry ( { " admin " , " network " , " netspeedtest " } , alias ( " admin " , " network " , " netspeedtest " , " speedtestlan " ) , _ ( " Net Speedtest " ) , 90 )
e.dependent = false
e.acl_depends = { " luci-app-netspeedtest " }
2024-03-07 20:27:54 +08:00
entry ( { " admin " , " network " , " netspeedtest " , " speedtestlan " } , cbi ( " netspeedtest/speedtestlan " ) , _ ( " Lan Speedtest Web " ) , 20 ) . leaf = true
entry ( { " admin " , " network " , " netspeedtest " , " speedtestiperf3 " } , cbi ( " netspeedtest/speedtestiperf3 " , { hideapplybtn = true , hidesavebtn = true , hideresetbtn = true } ) , _ ( " Lan Speedtest Iperf3 " ) , 30 ) . leaf = true
2024-03-09 12:17:55 +08:00
entry ( { " admin " , " network " , " netspeedtest " , " speedtestwan " } , cbi ( " netspeedtest/speedtestwan " , { hideapplybtn = true , hidesavebtn = true , hideresetbtn = true } ) , _ ( " Broadband speed test " ) , 40 ) . leaf = true
2024-03-07 20:27:54 +08:00
entry ( { " admin " , " network " , " netspeedtest " , " speedtestport " } , cbi ( " netspeedtest/speedtestport " , { hideapplybtn = true , hidesavebtn = true , hideresetbtn = true } ) , _ ( " Server Port Latency Test " ) , 50 ) . leaf = true
2024-03-23 04:15:49 +08:00
entry ( { " admin " , " network " , " netspeedtest " , " log " } , form ( " netspeedtest/log " ) , _ ( " Log " ) , 60 ) . leaf = true
2024-03-04 21:26:03 +08:00
entry ( { " admin " , " network " , " netspeedtest " , " test_port " } , call ( " test_port " ) )
2022-10-18 20:28:10 +08:00
entry ( { " admin " , " network " , " iperf3_status " } , call ( " iperf3_status " ) )
2024-03-07 20:27:54 +08:00
entry ( { " admin " , " network " , " test_iperf0 " } , post ( " test_iperf0 " ) , nil ) . leaf = true
entry ( { " admin " , " network " , " test_iperf1 " } , post ( " test_iperf1 " ) , nil ) . leaf = true
2024-03-09 12:17:55 +08:00
entry ( { " admin " , " network " , " netspeedtest " , " speedtestwanrun " } , call ( " speedtestwanrun " ) )
2024-03-23 04:15:49 +08:00
entry ( { " admin " , " network " , " netspeedtest " , " netcheck " } , call ( " netcheck " ) )
2022-10-18 20:28:10 +08:00
entry ( { " admin " , " network " , " netspeedtest " , " dellog " } , call ( " dellog " ) )
2024-03-23 04:15:49 +08:00
entry ( { " admin " , " network " , " netspeedtest " , " getlog " } , call ( " getlog " ) )
2021-10-01 02:17:38 +08:00
end
2024-03-23 04:15:49 +08:00
function netcheck ( )
http.prepare_content ( " text/plain; charset=utf-8 " )
local f = io.open ( " /etc/netspeedtest/netspeedtest.log " , " r+ " )
local fdp = fs.readfile ( " /etc/netspeedtest/netspeedtestpos " ) or 0
f : seek ( " set " , fdp )
local a = f : read ( 2048000 ) or " "
fdp = f : seek ( )
fs.writefile ( " /etc/netspeedtest/netspeedtestpos " , tostring ( fdp ) )
f : close ( )
http.write ( a )
end
function speedtestwanrun ( )
local cli = luci.http . formvalue ( ' cli ' )
uci : set ( name , ' speedtestwan ' , ' speedtest_cli ' , cli )
uci : commit ( name )
fs.writefile ( " /etc/netspeedtest/netspeedtestpos " , " 0 " )
http.prepare_content ( " application/json " )
http.write ( ' ' )
sys.exec ( string.format ( " /etc/init.d/netspeedtest wantest " .. cli .. " > /etc/netspeedtest/netspeedtest.log 2>&1 & " ) )
end
2024-03-07 20:27:54 +08:00
2024-03-04 21:26:03 +08:00
function test_port ( )
local e = { }
2024-03-07 20:27:54 +08:00
local domain = luci.http . formvalue ( ' sdomain ' )
local port = luci.http . formvalue ( ' sport ' )
2024-03-04 21:26:03 +08:00
local ip = sys.exec ( " echo " .. domain .. " | grep -E ^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$ || nslookup " .. domain .. " 2>/dev/null | grep Address | awk -F' ' '{print$NF}' | grep -E ^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$ | sed -n 1p " )
ip = sys.exec ( " echo -n " .. ip )
e.ping = luci.sys . exec ( string.format ( " echo -n $(tcping -q -c 1 -i 1 -t 2 -p %s %s 2>&1 | grep -o 'time=[0-9]*.[0-9]*' | awk -F '=' '{print $2}') 2>/dev/null " , port , ip ) )
e.type = " tcping "
2024-03-07 20:27:54 +08:00
if e.ping == " " then
2024-03-04 21:26:03 +08:00
e.ping = sys.call ( " echo -n $(ping -c 1 -W 1 %q 2>&1 | grep -o 'time=[0-9]*.[0-9]*' | awk -F '=' '{print $2}') 2>/dev/null " % ip )
e.type = " ping "
end
if e.ping == " " then e.ping = " 0 " end
2024-03-07 20:27:54 +08:00
sys.exec ( string.format ( ' echo -ne " \n 【$(date)】 服务器:%s -- 端口:%s -- TCP延时: %s ms \n ">> /var/log/netspeedtest.log ' , domain , port , e.ping ) )
uci : set ( name , ' speedtestport ' , ' sdomain ' , domain )
uci : set ( name , ' speedtestport ' , ' sport ' , port )
2024-03-09 12:17:55 +08:00
uci : set ( name , ' speedtestport ' , ' tcpspeed ' , e.ping .. " ms " )
2024-03-07 20:27:54 +08:00
uci : commit ( name )
2024-03-04 21:26:03 +08:00
luci.http . prepare_content ( " application/json " )
luci.http . write_json ( e )
end
2023-01-14 20:16:49 +08:00
2022-10-18 20:28:10 +08:00
function iperf3_status ( )
local e = { }
2024-03-07 20:27:54 +08:00
e.run = sys.call ( " busybox ps -w | grep iperf3 | grep -v grep >/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
2022-10-18 20:28:10 +08:00
function dellog ( )
fs.writefile ( " /var/log/netspeedtest.log " , " " )
http.prepare_content ( " application/json " )
http.write ( ' ' )
end
2024-03-23 04:15:49 +08:00
function getlog ( )
logfile = " /var/log/netspeedtest.log "
if not fs.access ( logfile ) then
http.write ( " " )
return
end
local f = io.open ( logfile , " r " )
local a = f : read ( " *a " ) or " "
f : close ( )
a = string.gsub ( a , " \n $ " , " " )
http.prepare_content ( " text/plain; charset=utf-8 " )
http.write ( a )
2021-10-01 02:17:38 +08:00
end
2022-10-18 20:28:10 +08:00