Hot fix for version 2.28

This commit is contained in:
Russel Yasol
2025-08-24 18:13:37 +08:00
parent 752fce41b4
commit 8e99618f2a
157 changed files with 1609 additions and 478 deletions

View File

@@ -1,5 +1,8 @@
#!/bin/sh
# Ethernet Hardware Details Fetch Script
# Provides ethernet interface information using ethtool
# Set common headers
echo "Content-Type: application/json"
echo "Access-Control-Allow-Origin: *"
@@ -57,13 +60,6 @@ cleanup() {
# Set trap for cleanup
trap cleanup EXIT INT TERM
# Function to get memory information
get_memory_info() {
free_output=$(free -b)
memory_info=$(echo "$free_output" | awk '/Mem:/ {print "{\"total\": " $2 ", \"used\": " $3 ", \"available\": " $7 "}"}')
echo "$memory_info"
}
# Function to get ethernet information
get_ethernet_info() {
interface=${1:-eth0}
@@ -93,27 +89,13 @@ get_ethernet_info() {
# Acquire lock before proceeding
acquire_lock
# Parse query string for type and interface
type=$(echo "$QUERY_STRING" | sed -n 's/.*type=\([^&]*\).*/\1/p')
# Parse query string for interface parameter
interface=$(echo "$QUERY_STRING" | sed -n 's/.*interface=\([^&]*\).*/\1/p')
# Default interface if not specified
[ -z "$interface" ] && interface="eth0"
# Convert type to lowercase using tr
type=$(echo "$type" | tr '[:upper:]' '[:lower:]')
# Check type parameter and call appropriate function
case "$type" in
"memory")
get_memory_info
;;
"eth")
get_ethernet_info "$interface"
;;
*)
error_response "Invalid type. Use 'memory' or 'eth'"
;;
esac
# Get ethernet information for the specified interface
get_ethernet_info "$interface"
# Lock will be automatically released by the cleanup trap

View File

@@ -6,6 +6,15 @@
echo "Content-Type: application/json"
echo ""
# Check for internet connectivity by pinging 8.8.8.8 twice
ping -c 2 8.8.8.8 >/dev/null 2>&1
# If ping fails, return error immediately
if [ $? -ne 0 ]; then
echo '{"error": "Failed to fetch public IP"}'
exit 1
fi
# Fetch public IP using multiple fallback methods
PUBLIC_IP=$(
curl -s https://api.ipify.org 2>/dev/null || \

View File

@@ -0,0 +1,121 @@
#!/bin/sh
# Memory Data Fetch Script
# Returns current memory usage data from the memory daemon
# Handle OPTIONS request first
if [ "${REQUEST_METHOD:-GET}" = "OPTIONS" ]; then
echo "Content-Type: text/plain"
echo "Access-Control-Allow-Origin: *"
echo "Access-Control-Allow-Methods: GET, OPTIONS"
echo "Access-Control-Allow-Headers: Content-Type"
echo "Access-Control-Max-Age: 86400"
echo ""
exit 0
fi
# Set content type and CORS headers
echo "Content-Type: application/json"
echo "Access-Control-Allow-Origin: *"
echo "Access-Control-Allow-Methods: GET, OPTIONS"
echo "Access-Control-Allow-Headers: Content-Type"
echo ""
# Configuration paths
MEMORY_JSON="/tmp/quecmanager/memory.json"
CONFIG_FILE="/etc/quecmanager/settings/memory_settings.conf"
FALLBACK_CONFIG_FILE="/tmp/quecmanager/settings/memory_settings.conf"
# Check if memory monitoring is enabled
is_memory_enabled() {
local config_to_read=""
if [ -f "$CONFIG_FILE" ]; then
config_to_read="$CONFIG_FILE"
elif [ -f "$FALLBACK_CONFIG_FILE" ]; then
config_to_read="$FALLBACK_CONFIG_FILE"
fi
if [ -n "$config_to_read" ]; then
local enabled_val=$(grep "^MEMORY_ENABLED=" "$config_to_read" 2>/dev/null | tail -n1 | cut -d'=' -f2 | tr -d '"')
case "$enabled_val" in
true|1|on|yes|enabled) return 0 ;;
*) return 1 ;;
esac
fi
return 1 # Default to disabled
}
# Check if memory daemon is running
is_memory_daemon_running() {
pgrep -f "memory_daemon.sh" >/dev/null 2>&1
}
# Handle GET request only
if [ "${REQUEST_METHOD:-GET}" != "GET" ]; then
echo "{\"status\":\"error\",\"code\":\"METHOD_NOT_ALLOWED\",\"message\":\"Only GET method is supported\"}"
exit 1
fi
# Check if memory monitoring is enabled
if ! is_memory_enabled; then
echo "{\"status\":\"error\",\"code\":\"MEMORY_DISABLED\",\"message\":\"Memory monitoring is disabled. Enable it in settings to view memory data.\"}"
exit 1
fi
# Check if daemon is running
if ! is_memory_daemon_running; then
echo "{\"status\":\"error\",\"code\":\"DAEMON_NOT_RUNNING\",\"message\":\"Memory daemon is not running. Check memory settings.\"}"
exit 1
fi
# Check if memory data file exists and is recent (within last 30 seconds)
if [ ! -f "$MEMORY_JSON" ]; then
echo "{\"status\":\"error\",\"code\":\"NO_DATA\",\"message\":\"Memory data file not found. Memory daemon may be starting up.\"}"
exit 1
fi
# Check if file is recent (modified within last 30 seconds)
# Get current time and file modification time
current_time=$(date +%s)
file_time=$(stat -c %Y "$MEMORY_JSON" 2>/dev/null)
if [ -z "$file_time" ]; then
echo "{\"status\":\"error\",\"code\":\"STAT_ERROR\",\"message\":\"Cannot determine file modification time.\"}"
exit 1
fi
# Check if file is older than 30 seconds
time_diff=$((current_time - file_time))
if [ "$time_diff" -gt 30 ]; then
echo "{\"status\":\"error\",\"code\":\"STALE_DATA\",\"message\":\"Memory data is stale (${time_diff}s old). Memory daemon may have stopped.\"}"
exit 1
fi
# Read and validate the memory data
if [ -r "$MEMORY_JSON" ]; then
memory_content=$(cat "$MEMORY_JSON" 2>/dev/null)
# Basic validation - check if it looks like valid JSON with required fields
if echo "$memory_content" | grep -q '"total"' && echo "$memory_content" | grep -q '"used"' && echo "$memory_content" | grep -q '"available"'; then
# Extract the data part and ensure it's properly formatted
total=$(echo "$memory_content" | sed -n 's/.*"total"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p')
used=$(echo "$memory_content" | sed -n 's/.*"used"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p')
available=$(echo "$memory_content" | sed -n 's/.*"available"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p')
# Validate that we got valid numbers
if [ -n "$total" ] && [ -n "$used" ] && [ -n "$available" ] && \
[ "$total" -gt 0 ] && [ "$used" -ge 0 ] && [ "$available" -ge 0 ]; then
# Return properly formatted response
echo "{\"status\":\"success\",\"data\":{\"total\":$total,\"used\":$used,\"available\":$available}}"
else
echo "{\"status\":\"error\",\"code\":\"INVALID_DATA\",\"message\":\"Memory data contains invalid values.\"}"
exit 1
fi
else
echo "{\"status\":\"error\",\"code\":\"INVALID_FORMAT\",\"message\":\"Memory data file has invalid format.\"}"
exit 1
fi
else
echo "{\"status\":\"error\",\"code\":\"READ_ERROR\",\"message\":\"Cannot read memory data file.\"}"
exit 1
fi

View File

@@ -0,0 +1,78 @@
#!/bin/sh
# Memory Service Fetch Script
# Returns current memory configuration and status
# Handle OPTIONS request first
if [ "${REQUEST_METHOD:-GET}" = "OPTIONS" ]; then
echo "Content-Type: text/plain"
echo "Access-Control-Allow-Origin: *"
echo "Access-Control-Allow-Methods: GET, OPTIONS"
echo "Access-Control-Allow-Headers: Content-Type"
echo "Access-Control-Max-Age: 86400"
echo ""
exit 0
fi
# Set content type and CORS headers
echo "Content-Type: application/json"
echo "Access-Control-Allow-Origin: *"
echo "Access-Control-Allow-Methods: GET, OPTIONS"
echo "Access-Control-Allow-Headers: Content-Type"
echo ""
# Configuration paths
CONFIG_FILE="/etc/quecmanager/settings/memory_settings.conf"
FALLBACK_CONFIG_FILE="/tmp/quecmanager/settings/memory_settings.conf"
# Get current configuration
get_config() {
# Defaults
ENABLED="false"
INTERVAL="1"
# Try primary config first, then fallback
local config_to_read=""
if [ -f "$CONFIG_FILE" ]; then
config_to_read="$CONFIG_FILE"
elif [ -f "$FALLBACK_CONFIG_FILE" ]; then
config_to_read="$FALLBACK_CONFIG_FILE"
fi
if [ -n "$config_to_read" ]; then
local enabled_val=$(grep "^MEMORY_ENABLED=" "$config_to_read" 2>/dev/null | tail -n1 | cut -d'=' -f2 | tr -d '"')
local interval_val=$(grep "^MEMORY_INTERVAL=" "$config_to_read" 2>/dev/null | tail -n1 | cut -d'=' -f2)
case "$enabled_val" in
true|1|on|yes|enabled) ENABLED="true" ;;
*) ENABLED="false" ;;
esac
if echo "$interval_val" | grep -qE '^[0-9]+$' && [ "$interval_val" -ge 1 ] && [ "$interval_val" -le 10 ]; then
INTERVAL="$interval_val"
fi
fi
}
# Check if memory daemon is running
is_memory_daemon_running() {
pgrep -f "memory_daemon.sh" >/dev/null 2>&1
}
# Handle GET request only
if [ "${REQUEST_METHOD:-GET}" != "GET" ]; then
echo "{\"status\":\"error\",\"code\":\"METHOD_NOT_ALLOWED\",\"message\":\"Only GET method is supported\"}"
exit 1
fi
# Get current configuration
get_config
# Check daemon status
running="false"
if is_memory_daemon_running; then
running="true"
fi
# Return configuration and status
echo "{\"status\":\"success\",\"data\":{\"enabled\":$ENABLED,\"interval\":$INTERVAL,\"running\":$running}}"

View File

@@ -0,0 +1,86 @@
#!/bin/sh
# Fetch Ping Result (relocated under /home/ping)
# OpenWrt/BusyBox compatible version
# Handle OPTIONS first
if [ "${REQUEST_METHOD:-GET}" = "OPTIONS" ]; then
echo "Content-Type: application/json"
echo "Access-Control-Allow-Origin: *"
echo "Access-Control-Allow-Methods: GET, OPTIONS"
echo "Access-Control-Allow-Headers: Content-Type"
echo ""
exit 0
fi
# Set headers for other requests
echo "Content-Type: application/json"
echo "Access-Control-Allow-Origin: *"
echo "Access-Control-Allow-Methods: GET, OPTIONS"
echo "Access-Control-Allow-Headers: Content-Type"
echo ""
# Configuration
OUT_JSON="/tmp/quecmanager/ping_latency.json"
CONFIG_FILE="/etc/quecmanager/settings/ping_settings.conf"
[ -f "$CONFIG_FILE" ] || CONFIG_FILE="/tmp/quecmanager/settings/ping_settings.conf"
# Get enabled setting
get_enabled() {
local enabled="true"
if [ -f "$CONFIG_FILE" ]; then
val=$(grep -E "^PING_ENABLED=" "$CONFIG_FILE" 2>/dev/null | tail -n1 | cut -d'=' -f2 | tr -d '\r' || echo "")
case "${val:-}" in
true|1|on|yes|enabled) enabled="true" ;;
false|0|off|no|disabled) enabled="false" ;;
esac
fi
echo "$enabled"
}
# Get interval setting
get_interval() {
local interval="5"
if [ -f "$CONFIG_FILE" ]; then
val=$(grep -E "^PING_INTERVAL=" "$CONFIG_FILE" 2>/dev/null | tail -n1 | cut -d'=' -f2 | tr -d '\r' || echo "")
if [ -n "$val" ] && echo "$val" | grep -qE '^[0-9]+$'; then
interval="$val"
fi
fi
echo "$interval"
}
# Get host setting
get_host() {
local host="8.8.8.8"
if [ -f "$CONFIG_FILE" ]; then
val=$(grep -E "^PING_HOST=" "$CONFIG_FILE" 2>/dev/null | tail -n1 | cut -d'=' -f2 | tr -d '\r' || echo "")
if [ -n "$val" ]; then
host="$val"
fi
fi
echo "$host"
}
# Get config values
ENABLED=$(get_enabled)
INTERVAL=$(get_interval)
HOST=$(get_host)
# Check if daemon JSON exists and is readable
if [ -f "$OUT_JSON" ] && [ -r "$OUT_JSON" ]; then
# Read the daemon output
PING_DATA=$(cat "$OUT_JSON" 2>/dev/null || echo "")
if [ -n "$PING_DATA" ]; then
# Simple approach: just wrap the daemon data with our response format
echo "{\"status\":\"success\",\"data\":$PING_DATA,\"config\":{\"enabled\":$ENABLED,\"interval\":$INTERVAL,\"host\":\"$HOST\"}}"
else
# JSON file exists but is empty/unreadable
echo "{\"status\":\"error\",\"message\":\"Ping data file exists but is empty or unreadable\"}"
fi
else
# Fallback: return default structure when daemon file doesn't exist
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || date +"%Y-%m-%dT%H:%M:%SZ")
echo "{\"status\":\"success\",\"data\":{\"timestamp\":\"$TIMESTAMP\",\"host\":\"$HOST\",\"latency\":null,\"ok\":false},\"config\":{\"enabled\":$ENABLED,\"interval\":$INTERVAL,\"host\":\"$HOST\"}}"
fi