Hot patch for speedtest
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
#!/bin/sh
|
||||
export HOME=/tmp/home
|
||||
|
||||
# Create named pipe for speedtest output if it doesn't exist
|
||||
[ ! -p /tmp/realtime_spd.json ] && mkfifo /tmp/realtime_spd.json
|
||||
|
||||
# Run speedtest in background
|
||||
/usr/bin/speedtest --accept-license -f json -p yes --progress-update-interval=100 > /tmp/realtime_spd.json
|
||||
|
||||
# Remove named pipe
|
||||
rm /tmp/realtime_spd.json
|
||||
@@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
|
||||
echo "Content-Type: text/event-stream"
|
||||
echo "Cache-Control: no-cache"
|
||||
echo "Connection: keep-alive"
|
||||
echo ""
|
||||
|
||||
# Use cat to read from the FIFO
|
||||
cat /tmp/realtime_spd.json | while read line; do
|
||||
echo "data: $line"
|
||||
echo
|
||||
sleep 0.1
|
||||
done
|
||||
@@ -0,0 +1,10 @@
|
||||
#!/bin/sh
|
||||
# /www/cgi-bin/start_speedtest.sh
|
||||
echo "Content-Type: application/json"
|
||||
echo ""
|
||||
|
||||
# Run speedtest in background
|
||||
/www/cgi-bin/quecmanager/home/speedtest/speedtest.sh
|
||||
|
||||
# Immediately return a success response
|
||||
echo '{"status":"started"}'
|
||||
@@ -83,9 +83,10 @@ get_profiles() {
|
||||
local nsa_nr5g_bands=$(uci -q get "quecprofiles.$idx.nsa_nr5g_bands" 2>/dev/null)
|
||||
local network_type=$(uci -q get "quecprofiles.$idx.network_type" 2>/dev/null)
|
||||
local ttl=$(uci -q get "quecprofiles.$idx.ttl" 2>/dev/null)
|
||||
local paused=$(uci -q get "quecprofiles.$idx.paused" 2>/dev/null)
|
||||
|
||||
# Debug output
|
||||
log_message "Retrieved for $idx: name=$name, iccid=$iccid, apn=$apn"
|
||||
log_message "Retrieved for $idx: name=$name, iccid=$iccid, apn=$apn, paused=$paused"
|
||||
|
||||
# Skip if missing required fields
|
||||
if [ -z "$name" ] || [ -z "$iccid" ] || [ -z "$apn" ]; then
|
||||
@@ -104,6 +105,7 @@ get_profiles() {
|
||||
nsa_nr5g_bands=$(sanitize_for_json "${nsa_nr5g_bands:-""}")
|
||||
network_type=$(sanitize_for_json "${network_type:-"LTE"}")
|
||||
ttl=$(sanitize_for_json "${ttl:-0}")
|
||||
paused=$(sanitize_for_json "${paused:-0}")
|
||||
|
||||
# Create profile JSON
|
||||
local profile_json="{"
|
||||
@@ -116,7 +118,8 @@ get_profiles() {
|
||||
profile_json="${profile_json}\"sa_nr5g_bands\":\"${sa_nr5g_bands}\","
|
||||
profile_json="${profile_json}\"nsa_nr5g_bands\":\"${nsa_nr5g_bands}\","
|
||||
profile_json="${profile_json}\"network_type\":\"${network_type}\","
|
||||
profile_json="${profile_json}\"ttl\":\"${ttl}\""
|
||||
profile_json="${profile_json}\"ttl\":\"${ttl}\","
|
||||
profile_json="${profile_json}\"paused\":\"${paused}\""
|
||||
profile_json="${profile_json}}"
|
||||
|
||||
# Add comma if not first
|
||||
|
||||
@@ -153,6 +153,7 @@ set quecprofiles.@profile[-1].sa_nr5g_bands='$sa_nr5g_bands'
|
||||
set quecprofiles.@profile[-1].nsa_nr5g_bands='$nsa_nr5g_bands'
|
||||
set quecprofiles.@profile[-1].network_type='$network_type'
|
||||
set quecprofiles.@profile[-1].ttl='$ttl'
|
||||
set quecprofiles.@profile[-1].paused='0'
|
||||
commit quecprofiles
|
||||
EOF
|
||||
|
||||
|
||||
@@ -0,0 +1,193 @@
|
||||
#!/bin/sh
|
||||
# Location: /www/cgi-bin/quecmanager/profiles/toggle_pause.sh
|
||||
|
||||
# Set content type to JSON
|
||||
echo -n ""
|
||||
echo "Content-type: application/json"
|
||||
echo ""
|
||||
|
||||
# Configuration
|
||||
CHECK_TRIGGER="/tmp/quecprofiles_check"
|
||||
STATUS_FILE="/tmp/quecprofiles_status.json"
|
||||
TRACK_FILE="/tmp/quecprofiles_active"
|
||||
|
||||
# Function to log messages
|
||||
log_message() {
|
||||
local level="${2:-info}"
|
||||
logger -t quecprofiles -p "daemon.$level" "toggle_pause: $1"
|
||||
}
|
||||
|
||||
# Function to update status file directly - used when pausing a profile
|
||||
update_status_to_paused() {
|
||||
local profile_name="$1"
|
||||
|
||||
# Create JSON status for paused profile
|
||||
cat > "$STATUS_FILE" <<EOF
|
||||
{
|
||||
"status": "paused",
|
||||
"message": "Profile is paused. Resume the profile to apply settings.",
|
||||
"profile": "$profile_name",
|
||||
"progress": 0,
|
||||
"timestamp": $(date +%s)
|
||||
}
|
||||
EOF
|
||||
|
||||
# Create simple track file for easy checking
|
||||
echo "paused:$profile_name:0" > "$TRACK_FILE"
|
||||
chmod 644 "$TRACK_FILE" "$STATUS_FILE"
|
||||
|
||||
log_message "Status updated: paused - Profile is paused ($profile_name)" "info"
|
||||
}
|
||||
|
||||
# Function to output JSON response
|
||||
output_json() {
|
||||
local status="$1"
|
||||
local message="$2"
|
||||
local data="${3:-{}}"
|
||||
|
||||
printf '{"status":"%s","message":"%s","data":%s}\n' "$status" "$message" "$data"
|
||||
exit 0
|
||||
}
|
||||
|
||||
# Function to sanitize input
|
||||
sanitize() {
|
||||
echo "$1" | tr -d '\r\n' | sed 's/[^a-zA-Z0-9,.:_-]//g'
|
||||
}
|
||||
|
||||
# Function to find profile by ICCID
|
||||
find_profile_by_iccid() {
|
||||
local iccid="$1"
|
||||
# Get all profile indices
|
||||
local profile_indices=$(uci show quecprofiles | grep -o '@profile\[[0-9]\+\]' | sort -u)
|
||||
|
||||
for profile_index in $profile_indices; do
|
||||
local current_iccid=$(uci -q get quecprofiles.$profile_index.iccid)
|
||||
if [ "$current_iccid" = "$iccid" ]; then
|
||||
echo "$profile_index"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
# Function to toggle pause state
|
||||
toggle_pause_state() {
|
||||
local profile_index="$1"
|
||||
local paused="$2" # 0 or 1
|
||||
local profile_name=$(uci -q get quecprofiles.$profile_index.name)
|
||||
|
||||
# Update the profile in UCI config
|
||||
uci -q batch <<EOF
|
||||
set quecprofiles.$profile_index.paused='$paused'
|
||||
commit quecprofiles
|
||||
EOF
|
||||
|
||||
# Check if the operation was successful
|
||||
if [ $? -eq 0 ]; then
|
||||
if [ "$paused" = "1" ]; then
|
||||
log_message "Successfully paused profile '$profile_name'" "info"
|
||||
# Immediately update status to paused without waiting for daemon
|
||||
update_status_to_paused "$profile_name"
|
||||
return 0
|
||||
else
|
||||
log_message "Successfully resumed profile '$profile_name'" "info"
|
||||
# Touch the check trigger file to force daemon to check ASAP
|
||||
touch "$CHECK_TRIGGER"
|
||||
chmod 644 "$CHECK_TRIGGER"
|
||||
log_message "Triggered profile check for resumed profile '$profile_name'" "info"
|
||||
return 0
|
||||
fi
|
||||
else
|
||||
log_message "Failed to update pause state for profile '$profile_name'" "error"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Output debug info
|
||||
log_message "Received toggle pause request" "debug"
|
||||
|
||||
# Ensure UCI config exists
|
||||
if [ ! -f /etc/config/quecprofiles ]; then
|
||||
log_message "quecprofiles config does not exist" "error"
|
||||
output_json "error" "Configuration file not found"
|
||||
fi
|
||||
|
||||
# Get POST data
|
||||
iccid=""
|
||||
paused=""
|
||||
|
||||
if [ "$REQUEST_METHOD" = "POST" ]; then
|
||||
# Get content length
|
||||
CONTENT_LENGTH=$(echo "$CONTENT_LENGTH" | tr -cd '0-9')
|
||||
|
||||
if [ -n "$CONTENT_LENGTH" ]; then
|
||||
# Read POST data
|
||||
POST_DATA=$(dd bs=1 count=$CONTENT_LENGTH 2>/dev/null)
|
||||
|
||||
# Debug log
|
||||
log_message "Received POST data: $POST_DATA" "debug"
|
||||
|
||||
# Parse JSON with jsonfilter if available
|
||||
if command -v jsonfilter >/dev/null 2>&1; then
|
||||
iccid=$(echo "$POST_DATA" | jsonfilter -e '@.iccid' 2>/dev/null)
|
||||
paused=$(echo "$POST_DATA" | jsonfilter -e '@.paused' 2>/dev/null)
|
||||
else
|
||||
# If jsonfilter is not available, try basic parsing
|
||||
iccid=$(echo "$POST_DATA" | grep -o '"iccid":"[^"]*"' | head -1 | cut -d':' -f2 | tr -d '"')
|
||||
paused=$(echo "$POST_DATA" | grep -o '"paused":[0-1]' | head -1 | cut -d':' -f2)
|
||||
fi
|
||||
else
|
||||
log_message "No content length specified" "error"
|
||||
output_json "error" "No data received"
|
||||
fi
|
||||
elif [ -n "$QUERY_STRING" ]; then
|
||||
# URL parameters for GET requests (for testing)
|
||||
iccid=$(echo "$QUERY_STRING" | grep -o 'iccid=[^&]*' | cut -d'=' -f2)
|
||||
paused=$(echo "$QUERY_STRING" | grep -o 'paused=[^&]*' | cut -d'=' -f2)
|
||||
|
||||
# URL decode values
|
||||
iccid=$(echo "$iccid" | sed 's/+/ /g;s/%\(..\)/\\x\1/g;' | xargs -0 printf "%b")
|
||||
|
||||
log_message "Using URL parameters: iccid=$iccid, paused=$paused" "debug"
|
||||
fi
|
||||
|
||||
# Sanitize inputs
|
||||
iccid=$(sanitize "${iccid:-}")
|
||||
paused=$(sanitize "${paused:-}")
|
||||
|
||||
# Validate required inputs
|
||||
if [ -z "$iccid" ]; then
|
||||
log_message "ICCID is missing" "error"
|
||||
output_json "error" "ICCID is required to identify the profile"
|
||||
fi
|
||||
|
||||
# Validate pause state (must be 0 or 1)
|
||||
if [ "$paused" != "0" ] && [ "$paused" != "1" ]; then
|
||||
log_message "Invalid paused state: $paused" "error"
|
||||
output_json "error" "Paused state must be 0 (resumed) or 1 (paused)"
|
||||
fi
|
||||
|
||||
# Find profile to toggle
|
||||
profile_index=$(find_profile_by_iccid "$iccid")
|
||||
if [ $? -ne 0 ]; then
|
||||
log_message "Profile with ICCID $iccid not found" "error"
|
||||
output_json "error" "Profile not found"
|
||||
fi
|
||||
|
||||
# Get profile info for response
|
||||
profile_name=$(uci -q get quecprofiles.$profile_index.name)
|
||||
|
||||
# Toggle pause state
|
||||
if toggle_pause_state "$profile_index" "$paused"; then
|
||||
if [ "$paused" = "1" ]; then
|
||||
log_message "Profile paused successfully: $profile_name" "info"
|
||||
output_json "success" "Profile paused successfully" "{\"iccid\":\"$iccid\",\"name\":\"$profile_name\",\"paused\":true}"
|
||||
else
|
||||
log_message "Profile resumed successfully: $profile_name" "info"
|
||||
output_json "success" "Profile resumed successfully" "{\"iccid\":\"$iccid\",\"name\":\"$profile_name\",\"paused\":false}"
|
||||
fi
|
||||
else
|
||||
log_message "Failed to update pause state for profile: $profile_name" "error"
|
||||
output_json "error" "Failed to update profile status. Please check system logs."
|
||||
fi
|
||||
Reference in New Issue
Block a user