op-packages/luci-app-netspeedtest/root/usr/libexec/rpcd/luci.netspeedtest
github-actions[bot] 9f83df92e8 🚀 Sync 2026-03-08 00:54:34
2026-03-08 00:54:34 +08:00

88 lines
2.9 KiB
Bash
Executable File

#!/bin/sh
# Copyright (C) 2019-2026 sirpdboy
# RPC plugin for netspeedtest
. /usr/share/libubox/jshn.sh
get_ookla_servers() {
local servers="[]"
if [ -f '/usr/bin/ookla-speedtest' ] && [ -x '/usr/bin/ookla-speedtest' ]; then
local json_output=$(/usr/bin/ookla-speedtest --servers --format=json 2>/dev/null)
if [ -n "$json_output" ]; then
servers=$(echo "$json_output" | sed -n 's/.*"servers":\(\[.*\]\).*/\1/p')
[ -z "$servers" ] && servers="[]"
fi
fi
echo "$servers"
}
get_python_servers() {
local servers="[]"
local temp_file="/tmp/python_servers.tmp"
if [ -f '/usr/bin/speedtest' ] && [ -x '/usr/bin/speedtest' ]; then
/usr/bin/speedtest --list > "$temp_file" 2>/dev/null
if [ -s "$temp_file" ]; then
local json=""
local count=0
while IFS= read -r line; do
if echo "$line" | grep -qE '^[0-9]+\)'; then
local id=$(echo "$line" | sed -E 's/^([0-9]+)\).*/\1/')
local sponsor=$(echo "$line" | sed -E 's/^[0-9]+\)[[:space:]]+([^(]+).*/\1/' | sed -e 's/[[:space:]]*$//' -e 's/"/\\"/g')
local city_country=$(echo "$line" | grep -o '([^)]*)' | head -1 | sed 's/[()]//g')
local city=$(echo "$city_country" | cut -d',' -f1 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/"/\\"/g')
local country=$(echo "$city_country" | cut -d',' -f2 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/"/\\"/g')
local distance=$(echo "$line" | grep -o '\[[0-9.]* km\]' | sed 's/\[//g' | sed 's/ km\]//g')
[ -z "$distance" ] && distance="0"
if [ $count -gt 0 ]; then
json="$json,"
fi
json="$json{\"id\":$id,\"sponsor\":\"$sponsor\",\"city\":\"$city\",\"country\":\"$country\",\"distance\":$distance}"
count=$((count + 1))
[ $count -ge 10 ] && break
fi
done < "$temp_file"
[ -n "$json" ] && servers="[$json]"
fi
rm -f "$temp_file"
fi
echo "$servers"
}
case "$1" in
list)
echo '{
"get_servers": {
"signature": [],
"help": "Get speedtest servers"
}
}'
;;
call)
case "$2" in
get_servers)
json_init
json_add_string "ookla" "$(get_ookla_servers)"
json_add_string "python" "$(get_python_servers)"
json_dump
;;
*)
echo '{"error": "Method not found"}'
;;
esac
;;
*)
echo '{"error": "Invalid command"}'
;;
esac