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

View File

@@ -35,15 +35,15 @@ if [ -f "$STATUS_FILE" ]; then
if [ -s "$STATUS_FILE" ]; then
# Cat the entire file content (more reliable than grep)
status_content=$(cat "$STATUS_FILE")
# Log content for debugging
log_message "Status file content: $status_content" "debug"
# Check if it looks like valid JSON
if echo "$status_content" | grep -q "status"; then
# Output the status file content
cat "$STATUS_FILE"
# Extract status for logging only
status=$(echo "$status_content" | sed -n 's/.*"status":"\([^"]*\)".*/\1/p')
log_message "Status from file: $status" "info"
@@ -63,7 +63,7 @@ if [ -f "$TRACK_FILE" ]; then
status=$(echo "$status_info" | cut -d':' -f1)
profile=$(echo "$status_info" | cut -d':' -f2)
progress=$(echo "$status_info" | cut -d':' -f3)
# Make sure the message reflects the actual status
if [ "$status" = "success" ]; then
message="Profile successfully applied"
@@ -76,7 +76,7 @@ if [ -f "$TRACK_FILE" ]; then
else
message="Profile operation status: $status"
fi
# Output JSON based on track file
cat <<EOF
{

View File

@@ -34,35 +34,35 @@ fi
# Function to extract profiles from UCI config
get_profiles() {
log_message "Fetching profiles from UCI config"
# Check if UCI config exists
if [ ! -f /etc/config/quecprofiles ]; then
log_message "No profiles config found" "warn"
echo "{\"status\":\"success\",\"profiles\":[]}"
return 0
fi
# Start JSON output
local json_output=""
local first=1
local count=0
# Get all profile indices - make sure this succeeds
local indices=$(uci -q show quecprofiles | grep -o '@profile\[[0-9]*\]' | sort -u)
# Debug output
echo "Found indices: $indices" >>/tmp/list_profiles_error.log
if [ -z "$indices" ]; then
log_message "No profile indices found" "warn"
echo "{\"status\":\"success\",\"profiles\":[]}"
return 0
fi
# Process each profile
for idx in $indices; do
log_message "Processing profile index: $idx"
# Try different UCI get approaches
local name
name=$(uci -q get "quecprofiles.$idx.name" 2>/dev/null)
@@ -72,7 +72,7 @@ get_profiles() {
section=${section%]}
name=$(uci -q get "quecprofiles.@profile[$section].name" 2>/dev/null)
fi
# Get profile details
local iccid=$(uci -q get "quecprofiles.$idx.iccid" 2>/dev/null)
local imei=$(uci -q get "quecprofiles.$idx.imei" 2>/dev/null)
@@ -83,8 +83,9 @@ 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 mobile_provider=$(uci -q get "quecprofiles.$idx.mobile_provider" 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, paused=$paused"
@@ -93,7 +94,7 @@ get_profiles() {
log_message "Skipping invalid profile: $idx (missing required fields)" "warn"
continue
fi
# Sanitize all values to ensure valid JSON
name=$(sanitize_for_json "$name")
iccid=$(sanitize_for_json "$iccid")
@@ -105,8 +106,9 @@ 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}")
mobile_provider=$(sanitize_for_json "${mobile_provider:-""}")
paused=$(sanitize_for_json "${paused:-0}")
# Create profile JSON
local profile_json="{"
profile_json="${profile_json}\"name\":\"${name}\","
@@ -119,27 +121,28 @@ get_profiles() {
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}\"mobile_provider\":\"${mobile_provider}\","
profile_json="${profile_json}\"paused\":\"${paused}\""
profile_json="${profile_json}}"
# Add comma if not first
if [ $first -eq 0 ]; then
json_output="${json_output},"
else
first=0
fi
# Add profile to output
json_output="${json_output}${profile_json}"
count=$((count+1))
done
# Complete the JSON response
local response="{\"status\":\"success\",\"profiles\":[${json_output}]}"
# Save the response for debugging
echo "$response" > /tmp/list_profiles_response.json
echo "$response"
log_message "Found and returned $count profiles"
return 0

View File

@@ -136,6 +136,7 @@ create_profile() {
local nsa_nr5g_bands="$8"
local network_type="$9"
local ttl="${10}"
local mobile_provider="${11}"
# Generate a unique ID for the profile
local profile_id="profile_$(date +%s)_$(head -c 4 /dev/urandom | hexdump -e '"%x"')"
@@ -154,6 +155,7 @@ 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'
set quecprofiles.@profile[-1].mobile_provider='$mobile_provider'
commit quecprofiles
EOF
@@ -206,6 +208,7 @@ if [ "$REQUEST_METHOD" = "POST" ]; then
nsa_nr5g_bands=$(echo "$POST_DATA" | jsonfilter -e '@.nsa_nr5g_bands' 2>/dev/null)
network_type=$(echo "$POST_DATA" | jsonfilter -e '@.network_type' 2>/dev/null)
ttl=$(echo "$POST_DATA" | jsonfilter -e '@.ttl' 2>/dev/null)
mobile_provider=$(echo "$POST_DATA" | jsonfilter -e '@.mobile_provider' 2>/dev/null)
log_message "Parsed JSON data for profile: $name" "debug"
else
@@ -221,6 +224,7 @@ if [ "$REQUEST_METHOD" = "POST" ]; then
nsa_nr5g_bands=$(echo "$POST_DATA" | grep -o '"nsa_nr5g_bands":"[^"]*"' | head -1 | cut -d':' -f2 | tr -d '"')
network_type=$(echo "$POST_DATA" | grep -o '"network_type":"[^"]*"' | head -1 | cut -d':' -f2 | tr -d '"')
ttl=$(echo "$POST_DATA" | grep -o '"ttl":"[^"]*"' | head -1 | cut -d':' -f2 | tr -d '"')
mobile_provider=$(echo "$POST_DATA" | grep -o '"mobile_provider":"[^"]*"' | head -1 | cut -d':' -f2 | tr -d '"')
log_message "Basic parsing for profile: $name" "warn"
fi
@@ -240,6 +244,7 @@ else
nsa_nr5g_bands=$(echo "$QUERY_STRING" | grep -o 'nsa_nr5g_bands=[^&]*' | cut -d'=' -f2)
network_type=$(echo "$QUERY_STRING" | grep -o 'network_type=[^&]*' | cut -d'=' -f2)
ttl=$(echo "$QUERY_STRING" | grep -o 'ttl=[^&]*' | cut -d'=' -f2)
mobile_provider=$(echo "$QUERY_STRING" | grep -o 'mobile_provider=[^&]*' | cut -d'=' -f2)
# URL decode values
name=$(echo "$name" | sed 's/+/ /g;s/%\(..\)/\\x\1/g;' | xargs -0 printf "%b")
@@ -252,6 +257,7 @@ else
nsa_nr5g_bands=$(echo "$nsa_nr5g_bands" | sed 's/+/ /g;s/%\(..\)/\\x\1/g;' | xargs -0 printf "%b")
network_type=$(echo "$network_type" | sed 's/+/ /g;s/%\(..\)/\\x\1/g;' | xargs -0 printf "%b")
ttl=$(echo "$ttl" | sed 's/+/ /g;s/%\(..\)/\\x\1/g;' | xargs -0 printf "%b")
mobile_provider=$(echo "$mobile_provider" | sed 's/+/ /g;s/%\(..\)/\\x\1/g;' | xargs -0 printf "%b")
log_message "Using URL parameters" "warn"
fi
@@ -267,6 +273,7 @@ sa_nr5g_bands=$(sanitize "${sa_nr5g_bands:-}")
nsa_nr5g_bands=$(sanitize "${nsa_nr5g_bands:-}")
network_type=$(sanitize "${network_type:-LTE}")
ttl=$(sanitize "${ttl:-0}") # Default to 0 (disabled)
mobile_provider=$(sanitize "${mobile_provider:-Other}")
# Output debug info
log_message "Creating profile: $name, ICCID: $iccid, IMEI: $imei, APN: $apn" "debug"
@@ -340,14 +347,14 @@ elif [ $dup_status -eq 2 ]; then
fi
# Create the profile
if create_profile "$name" "$iccid" "$imei" "$apn" "$pdp_type" "$lte_bands" "$sa_nr5g_bands" "$nsa_nr5g_bands" "$network_type" "$ttl"; then
if create_profile "$name" "$iccid" "$imei" "$apn" "$pdp_type" "$lte_bands" "$sa_nr5g_bands" "$nsa_nr5g_bands" "$network_type" "$ttl" "$mobile_provider"; then
# Trigger immediate profile application
touch "/tmp/quecprofiles_check"
chmod 644 "/tmp/quecprofiles_check"
log_message "Triggered immediate profile check after creation" "info"
# Create profile data JSON for return - WITHOUT outer curly braces
profile_data="\"name\":\"$name\",\"iccid\":\"$iccid\",\"imei\":\"$imei\",\"apn\":\"$apn\",\"pdp_type\":\"$pdp_type\",\"lte_bands\":\"$lte_bands\",\"sa_nr5g_bands\":\"$sa_nr5g_bands\",\"nsa_nr5g_bands\":\"$nsa_nr5g_bands\",\"network_type\":\"$network_type\",\"ttl\":\"$ttl\""
profile_data="\"name\":\"$name\",\"iccid\":\"$iccid\",\"imei\":\"$imei\",\"apn\":\"$apn\",\"pdp_type\":\"$pdp_type\",\"lte_bands\":\"$lte_bands\",\"sa_nr5g_bands\":\"$sa_nr5g_bands\",\"nsa_nr5g_bands\":\"$nsa_nr5g_bands\",\"network_type\":\"$network_type\",\"ttl\":\"$ttl\",\"mobile_provider\":\"$mobile_provider\""
# Wrap the data field in curly braces inside output_json
output_json "success" "Profile created successfully" "{$profile_data}"

View File

@@ -17,7 +17,7 @@ output_json() {
local status="$1"
local message="$2"
local data="${3:-{}}"
printf '{"status":"%s","message":"%s","data":%s}\n' "$status" "$message" "$data"
exit 0
}
@@ -32,7 +32,7 @@ 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
@@ -40,7 +40,7 @@ find_profile_by_iccid() {
return 0
fi
done
return 1
}
@@ -48,13 +48,13 @@ find_profile_by_iccid() {
delete_profile() {
local profile_index="$1"
local profile_name=$(uci -q get quecprofiles.$profile_index.name)
# Delete the profile from UCI config
uci -q batch <<EOF
delete quecprofiles.$profile_index
commit quecprofiles
EOF
# Check if the operation was successful
if [ $? -eq 0 ]; then
log_message "Successfully deleted profile '$profile_name'" "info"
@@ -80,14 +80,14 @@ iccid=""
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)
@@ -102,10 +102,10 @@ if [ "$REQUEST_METHOD" = "POST" ]; then
elif [ -n "$QUERY_STRING" ]; then
# URL parameters for GET or DELETE requests
iccid=$(echo "$QUERY_STRING" | grep -o 'iccid=[^&]*' | cut -d'=' -f2)
# URL decode value
iccid=$(echo "$iccid" | sed 's/+/ /g;s/%\(..\)/\\x\1/g;' | xargs -0 printf "%b")
log_message "Using URL parameter: iccid=$iccid" "debug"
fi

View File

@@ -171,6 +171,7 @@ update_profile() {
local nsa_nr5g_bands="$8"
local network_type="$9"
local ttl="${10}"
local mobile_provider="${11}"
# Update the profile in UCI config
uci -q batch <<EOF
@@ -183,6 +184,7 @@ set quecprofiles.$profile_index.sa_nr5g_bands='$sa_nr5g_bands'
set quecprofiles.$profile_index.nsa_nr5g_bands='$nsa_nr5g_bands'
set quecprofiles.$profile_index.network_type='$network_type'
set quecprofiles.$profile_index.ttl='$ttl'
set quecprofiles.$profile_index.mobile_provider='$mobile_provider'
commit quecprofiles
EOF
@@ -237,6 +239,7 @@ if [ "$REQUEST_METHOD" = "POST" ]; then
nsa_nr5g_bands=$(echo "$POST_DATA" | jsonfilter -e '@.nsa_nr5g_bands' 2>/dev/null)
network_type=$(echo "$POST_DATA" | jsonfilter -e '@.network_type' 2>/dev/null)
ttl=$(echo "$POST_DATA" | jsonfilter -e '@.ttl' 2>/dev/null)
mobile_provider=$(echo "$POST_DATA" | jsonfilter -e '@.mobile_provider' 2>/dev/null)
log_message "Parsed JSON data for profile: $name" "debug"
else
@@ -252,6 +255,7 @@ if [ "$REQUEST_METHOD" = "POST" ]; then
nsa_nr5g_bands=$(echo "$POST_DATA" | grep -o '"nsa_nr5g_bands":"[^"]*"' | head -1 | cut -d':' -f2 | tr -d '"')
network_type=$(echo "$POST_DATA" | grep -o '"network_type":"[^"]*"' | head -1 | cut -d':' -f2 | tr -d '"')
ttl=$(echo "$POST_DATA" | grep -o '"ttl":"[^"]*"' | head -1 | cut -d':' -f2 | tr -d '"')
mobile_provider=$(echo "$POST_DATA" | grep -o '"mobile_provider":"[^"]*"' | head -1 | cut -d':' -f2 | tr -d '"')
log_message "Basic parsing for profile: $name" "warn"
fi
@@ -271,6 +275,7 @@ else
nsa_nr5g_bands=$(echo "$QUERY_STRING" | grep -o 'nsa_nr5g_bands=[^&]*' | cut -d'=' -f2)
network_type=$(echo "$QUERY_STRING" | grep -o 'network_type=[^&]*' | cut -d'=' -f2)
ttl=$(echo "$QUERY_STRING" | grep -o 'ttl=[^&]*' | cut -d'=' -f2)
mobile_provider=$(echo "$QUERY_STRING" | grep -o 'mobile_provider=[^&]*' | cut -d'=' -f2)
# URL decode values
iccid=$(echo "$iccid" | sed 's/+/ /g;s/%\(..\)/\\x\1/g;' | xargs -0 printf "%b")
@@ -283,6 +288,7 @@ else
nsa_nr5g_bands=$(echo "$nsa_nr5g_bands" | sed 's/+/ /g;s/%\(..\)/\\x\1/g;' | xargs -0 printf "%b")
network_type=$(echo "$network_type" | sed 's/+/ /g;s/%\(..\)/\\x\1/g;' | xargs -0 printf "%b")
ttl=$(echo "$ttl" | sed 's/+/ /g;s/%\(..\)/\\x\1/g;' | xargs -0 printf "%b")
mobile_provider=$(echo "$mobile_provider" | sed 's/+/ /g;s/%\(..\)/\\x\1/g;' | xargs -0 printf "%b")
log_message "Using URL parameters" "warn"
fi
@@ -298,6 +304,7 @@ sa_nr5g_bands=$(sanitize "${sa_nr5g_bands:-}")
nsa_nr5g_bands=$(sanitize "${nsa_nr5g_bands:-}")
network_type=$(sanitize "${network_type:-LTE}")
ttl=$(sanitize "${ttl:-0}") # Default to 0 (disabled)
mobile_provider=$(sanitize "${mobile_provider:-Other}")
# Output debug info
log_message "Editing profile: $name, ICCID: $iccid, IMEI: $imei, APN: $apn" "debug"
@@ -373,18 +380,18 @@ if check_duplicate_name "$name" "$iccid"; then
fi
# Update profile
if update_profile "$profile_index" "$name" "$imei" "$apn" "$pdp_type" "$lte_bands" "$nr5g_bands" "$network_type"; then
if update_profile "$profile_index" "$name" "$imei" "$apn" "$pdp_type" "$lte_bands" "$sa_nr5g_bands" "$nsa_nr5g_bands" "$network_type" "$ttl" "$mobile_provider"; then
# Trigger immediate profile application
touch "/tmp/quecprofiles_check"
chmod 644 "/tmp/quecprofiles_check"
log_message "Triggered immediate profile check after update" "info"
# Create a clean JSON response with properly escaped quotes
printf '{"status":"success","message":"Profile updated successfully","data":{"name":"%s","iccid":"%s","imei":"%s","apn":"%s","pdp_type":"%s","lte_bands":"%s","nr5g_bands":"%s","network_type":"%s"}}' \
"$name" "$iccid" "$imei" "$apn" "$pdp_type" "$lte_bands" "$nr5g_bands" "$network_type"
log_message "Profile updated successfully: $name" "info"
# Note: The conditional trigger is replaced with the direct trigger above
else
printf '{"status":"error","message":"Failed to update profile. Please check system logs."}'

View File

@@ -145,10 +145,10 @@ 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

View File

@@ -0,0 +1,301 @@
#!/bin/sh
# Memory Settings Configuration Script
# Manages memory service (enable/disable) and daemon settings with dynamic service management
# 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, POST, DELETE, 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, POST, DELETE, OPTIONS"
echo "Access-Control-Allow-Headers: Content-Type"
echo ""
# Configuration paths
CONFIG_DIR="/etc/quecmanager/settings"
CONFIG_FILE="$CONFIG_DIR/memory_settings.conf"
FALLBACK_CONFIG_DIR="/tmp/quecmanager/settings"
FALLBACK_CONFIG_FILE="$FALLBACK_CONFIG_DIR/memory_settings.conf"
LOG_FILE="/tmp/memory_settings.log"
SERVICES_INIT="/etc/init.d/quecmanager_services"
# Logging function
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
}
# Error response function
send_error() {
local error_code="$1"
local error_message="$2"
log_message "ERROR: $error_message"
echo "{\"status\":\"error\",\"code\":\"$error_code\",\"message\":\"$error_message\"}"
exit 1
}
# Success response function
send_success() {
local message="$1"
local data="$2"
log_message "SUCCESS: $message"
if [ -n "$data" ]; then
echo "{\"status\":\"success\",\"message\":\"$message\",\"data\":$data}"
else
echo "{\"status\":\"success\",\"message\":\"$message\"}"
fi
}
# 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)
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
}
# Save configuration
save_config() {
local enabled="$1"
local interval="$2"
# Try primary location first
if mkdir -p "$CONFIG_DIR" 2>/dev/null && [ -w "$CONFIG_DIR" ]; then
{
echo "MEMORY_ENABLED=$enabled"
echo "MEMORY_INTERVAL=$interval"
} > "$CONFIG_FILE" && chmod 644 "$CONFIG_FILE" 2>/dev/null
log_message "Saved config to primary location: enabled=$enabled, interval=$interval"
return 0
fi
# Fallback to tmp
mkdir -p "$FALLBACK_CONFIG_DIR" 2>/dev/null
{
echo "MEMORY_ENABLED=$enabled"
echo "MEMORY_INTERVAL=$interval"
} > "$FALLBACK_CONFIG_FILE" && chmod 644 "$FALLBACK_CONFIG_FILE" 2>/dev/null
log_message "Saved config to fallback location: enabled=$enabled, interval=$interval"
}
# Add memory daemon to services init script
add_memory_daemon_to_services() {
if [ ! -f "$SERVICES_INIT" ]; then
log_message "Services init file not found: $SERVICES_INIT"
return 1
fi
# Check if memory daemon is already present
if grep -q "memory_daemon.sh" "$SERVICES_INIT" 2>/dev/null; then
log_message "Memory daemon already present in services"
return 0
fi
# Create a temporary file with the memory daemon block
local temp_file="/tmp/services_temp_$$"
# Find the line before "echo \"All QuecManager services Started\"" and insert memory daemon
awk '
/echo "All QuecManager services Started"/ {
print " # Start memory daemon"
print " echo \"Starting Memory Daemon...\""
print " procd_open_instance"
print " procd_set_param command /www/cgi-bin/services/memory_daemon.sh"
print " procd_set_param respawn"
print " procd_set_param stdout 1"
print " procd_set_param stderr 1"
print " procd_close_instance"
print " echo \"Memory Daemon started\""
print ""
}
{ print }
' "$SERVICES_INIT" > "$temp_file"
if [ -s "$temp_file" ]; then
mv "$temp_file" "$SERVICES_INIT"
chmod +x "$SERVICES_INIT"
log_message "Added memory daemon to services init script"
return 0
else
rm -f "$temp_file"
log_message "Failed to add memory daemon to services"
return 1
fi
}
# Remove memory daemon from services init script
remove_memory_daemon_from_services() {
if [ ! -f "$SERVICES_INIT" ]; then
log_message "Services init file not found: $SERVICES_INIT"
return 1
fi
# Check if memory daemon is present
if ! grep -q "memory_daemon.sh" "$SERVICES_INIT" 2>/dev/null; then
log_message "Memory daemon not present in services"
return 0
fi
# Remove the memory daemon block (from "# Start memory daemon" to the empty line after)
local temp_file="/tmp/services_temp_$$"
awk '
/# Start memory daemon/ { skip=1; next }
skip && /^$/ { skip=0; next }
!skip { print }
' "$SERVICES_INIT" > "$temp_file"
if [ -s "$temp_file" ]; then
mv "$temp_file" "$SERVICES_INIT"
chmod +x "$SERVICES_INIT"
log_message "Removed memory daemon from services init script"
return 0
else
rm -f "$temp_file"
log_message "Failed to remove memory daemon from services"
return 1
fi
}
# Restart QuecManager services
restart_services() {
log_message "Restarting QuecManager services..."
# Stop services
if [ -x "$SERVICES_INIT" ]; then
"$SERVICES_INIT" stop >/dev/null 2>&1
sleep 2
"$SERVICES_INIT" start >/dev/null 2>&1
log_message "Services restarted successfully"
return 0
else
log_message "Cannot restart services - init script not found or not executable"
return 1
fi
}
# Check if memory daemon is running
is_memory_daemon_running() {
pgrep -f "memory_daemon.sh" >/dev/null 2>&1
}
# Handle POST request - Update memory setting
handle_post() {
log_message "POST request received"
local content_length=${CONTENT_LENGTH:-0}
if [ "$content_length" -eq 0 ]; then
send_error "NO_DATA" "No data provided"
fi
# Read POST data
local post_data=$(dd bs=$content_length count=1 2>/dev/null)
log_message "Received POST data: $post_data"
# Parse enabled and interval from JSON
local enabled=$(echo "$post_data" | sed -n 's/.*"enabled"[[:space:]]*:[[:space:]]*\([^,}]*\).*/\1/p' | tr -d ' "')
local interval=$(echo "$post_data" | sed -n 's/.*"interval"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p')
# Set defaults if not provided
[ -z "$enabled" ] && enabled="false"
[ -z "$interval" ] && interval="1"
# Validate input
case "$enabled" in
true|false) ;;
*) send_error "INVALID_SETTING" "Invalid enabled value. Must be true or false." ;;
esac
if ! echo "$interval" | grep -qE '^[0-9]+$' || [ "$interval" -lt 1 ] || [ "$interval" -gt 10 ]; then
send_error "INVALID_INTERVAL" "Interval must be a number between 1 and 10 seconds."
fi
# Get current config to compare
get_config
local prev_enabled="$ENABLED"
local prev_interval="$INTERVAL"
# Save new configuration
save_config "$enabled" "$interval"
# Handle service changes
if [ "$enabled" = "true" ]; then
# Enable memory daemon
add_memory_daemon_to_services
if [ "$prev_enabled" != "true" ] || [ "$prev_interval" != "$interval" ]; then
restart_services
fi
else
# Disable memory daemon
remove_memory_daemon_from_services
restart_services
fi
# Return current status
sleep 1 # Give services time to start/stop
local running="false"
if is_memory_daemon_running; then
running="true"
fi
send_success "Memory setting updated successfully" "{\"enabled\":$enabled,\"interval\":$interval,\"running\":$running}"
}
# Handle DELETE request - Reset to default
handle_delete() {
log_message "DELETE request received"
# Remove memory daemon from services and restart
remove_memory_daemon_from_services
restart_services
# Remove config files
rm -f "$CONFIG_FILE" "$FALLBACK_CONFIG_FILE" 2>/dev/null
send_success "Memory setting reset to default (disabled)" "{\"enabled\":false,\"interval\":1,\"running\":false,\"isDefault\":true}"
}
# Main execution
log_message "Memory settings script called with method: ${REQUEST_METHOD:-GET}"
case "${REQUEST_METHOD:-GET}" in
POST)
handle_post
;;
DELETE)
handle_delete
;;
*)
send_error "METHOD_NOT_ALLOWED" "HTTP method ${REQUEST_METHOD} not supported."
;;
esac

View File

@@ -1,11 +1,22 @@
#!/bin/sh
# Ping Settings Configuration Script
# Manages ping enable/disable preferences
# Manages ping service (enable/disable) and daemon settings
# Author: dr-dolomite
# Date: 2025-08-04
# Set content type and CORS headers
# Handle OPTIONS request first (before any headers)
if [ "${REQUEST_METHOD:-GET}" = "OPTIONS" ]; then
echo "Content-Type: text/plain"
echo "Access-Control-Allow-Origin: *"
echo "Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS"
echo "Access-Control-Allow-Headers: Content-Type"
echo "Access-Control-Max-Age: 86400"
echo ""
exit 0
fi
# Set content type and CORS headers for other requests
echo "Content-Type: application/json"
echo "Access-Control-Allow-Origin: *"
echo "Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS"
@@ -15,7 +26,12 @@ echo ""
# Configuration
CONFIG_DIR="/etc/quecmanager/settings"
CONFIG_FILE="$CONFIG_DIR/ping_settings.conf"
FALLBACK_CONFIG_DIR="/tmp/quecmanager/settings"
FALLBACK_CONFIG_FILE="$FALLBACK_CONFIG_DIR/ping_settings.conf"
LOG_FILE="/tmp/ping_settings.log"
PID_FILE="/tmp/quecmanager/ping_daemon.pid"
# Prefer the new services location, fall back to the legacy path for compatibility
DAEMON_RELATIVE_PATHS="/cgi-bin/services/ping_daemon.sh"
# Logging function
log_message() {
@@ -43,99 +59,176 @@ send_success() {
fi
}
# Ensure configuration directory exists
ensure_config_directory() {
if [ ! -d "$CONFIG_DIR" ]; then
log_message "Creating directory: $CONFIG_DIR"
mkdir -p "$CONFIG_DIR"
if [ $? -ne 0 ]; then
# Try to use a fallback location in /tmp
CONFIG_DIR="/tmp/quecmanager/settings"
CONFIG_FILE="$CONFIG_DIR/ping_settings.conf"
log_message "Fallback to alternative location: $CONFIG_DIR"
mkdir -p "$CONFIG_DIR"
if [ $? -ne 0 ]; then
send_error "DIRECTORY_ERROR" "Failed to create configuration directory"
fi
fi
chmod 755 "$CONFIG_DIR"
log_message "Created configuration directory: $CONFIG_DIR"
# Resolve config file for reading: prefer primary, then fallback
resolve_config_for_read() {
if [ -f "$CONFIG_FILE" ]; then
return 0
elif [ -f "$FALLBACK_CONFIG_FILE" ]; then
CONFIG_FILE="$FALLBACK_CONFIG_FILE"
CONFIG_DIR="$FALLBACK_CONFIG_DIR"
return 0
fi
# Default to primary path if none exist
return 0
}
# Determine daemon path (absolute) based on typical web root layouts
resolve_daemon_path() {
# Common locations where CGI/WWW is mounted
for rel in $DAEMON_RELATIVE_PATHS; do
for base in \
/www \
/; do
if [ -x "$base$rel" ]; then
echo "$base$rel"
return 0
fi
done
# Also try as-is if busybox httpd cwd matches web root
if [ -x "$rel" ]; then
echo "$rel"
return 0
fi
done
# Nothing found; return first candidate as a best-effort path
set -- $DAEMON_RELATIVE_PATHS
echo "$1"
}
daemon_running() {
if [ -f "$PID_FILE" ]; then
pid="$(cat "$PID_FILE" 2>/dev/null || true)"
if [ -n "${pid:-}" ] && kill -0 "$pid" 2>/dev/null; then
return 0
fi
fi
return 1
}
start_daemon() {
# Ensure /tmp/quecmanager exists for PID
[ -d "/tmp/quecmanager" ] || mkdir -p "/tmp/quecmanager"
if daemon_running; then
log_message "Daemon already running"
return 0
fi
local daemon_path
daemon_path="$(resolve_daemon_path)"
if [ ! -x "$daemon_path" ]; then
# Try to make it executable if present
if [ -f "$daemon_path" ]; then
chmod +x "$daemon_path" 2>/dev/null || true
fi
fi
if [ -x "$daemon_path" ]; then
nohup "$daemon_path" >/dev/null 2>&1 &
log_message "Started ping daemon: $daemon_path (pid $!)"
return 0
else
log_message "Daemon script not found or not executable: $daemon_path"
return 1
fi
}
stop_daemon() {
if daemon_running; then
pid="$(cat "$PID_FILE" 2>/dev/null || true)"
if [ -n "${pid:-}" ]; then
kill "$pid" 2>/dev/null || true
sleep 0.2
kill -9 "$pid" 2>/dev/null || true
fi
fi
rm -f "$PID_FILE" 2>/dev/null || true
}
# Get current ping setting
get_ping_setting() {
# If config file exists, read from it
get_config_values() {
# defaults
ENABLED="true"
HOST="8.8.8.8"
INTERVAL="5"
resolve_config_for_read
if [ -f "$CONFIG_FILE" ]; then
ping_enabled=$(grep "^PING_ENABLED=" "$CONFIG_FILE" | cut -d'=' -f2)
if [ -n "$ping_enabled" ]; then
if [ "$ping_enabled" = "true" ] || [ "$ping_enabled" = "1" ] || [ "$ping_enabled" = "on" ]; then
echo "true"
else
echo "false"
fi
return
val=$(grep -E "^PING_ENABLED=" "$CONFIG_FILE" | tail -n1 | cut -d'=' -f2)
if [ -n "${val:-}" ]; then
case "$val" in
true|1|on|yes|enabled) ENABLED="true" ;;
*) ENABLED="false" ;;
esac
fi
val=$(grep -E "^PING_HOST=" "$CONFIG_FILE" | tail -n1 | cut -d'=' -f2)
[ -n "${val:-}" ] && HOST="$val"
val=$(grep -E "^PING_INTERVAL=" "$CONFIG_FILE" | tail -n1 | cut -d'=' -f2)
if echo "${val:-}" | grep -qE '^[0-9]+$'; then
INTERVAL="$val"
fi
fi
# Default to enabled if no config exists
echo "true"
}
# Save ping setting to config file
save_ping_setting() {
save_config() {
local enabled="$1"
ensure_config_directory
# Create or update config file
if [ -f "$CONFIG_FILE" ]; then
# Update existing file
sed -i "s/^PING_ENABLED=.*$/PING_ENABLED=$enabled/" "$CONFIG_FILE"
if [ $? -ne 0 ]; then
# If sed fails (e.g., no match), append the setting
echo "PING_ENABLED=$enabled" >> "$CONFIG_FILE"
local host="$2"
local interval="$3"
# Try primary directory first
if mkdir -p "$CONFIG_DIR" 2>/dev/null; then
local tmp="$CONFIG_FILE.tmp.$$"
echo "PING_ENABLED=$enabled" > "$tmp" || rm -f "$tmp" || return 1
echo "PING_HOST=$host" >> "$tmp" || rm -f "$tmp" || return 1
echo "PING_INTERVAL=$interval" >> "$tmp" || rm -f "$tmp" || return 1
if mv -f "$tmp" "$CONFIG_FILE" 2>/dev/null; then
chmod 644 "$CONFIG_FILE" 2>/dev/null || true
log_message "Saved ping config (primary): enabled=$enabled host=$host interval=$interval"
return 0
fi
else
# Create new file
echo "PING_ENABLED=$enabled" > "$CONFIG_FILE"
fi
chmod 644 "$CONFIG_FILE"
log_message "Saved ping setting: $enabled"
# Fallback to /tmp
mkdir -p "$FALLBACK_CONFIG_DIR" 2>/dev/null || true
local tmp2="$FALLBACK_CONFIG_FILE.tmp.$$"
echo "PING_ENABLED=$enabled" > "$tmp2" || rm -f "$tmp2" || return 1
echo "PING_HOST=$host" >> "$tmp2" || rm -f "$tmp2" || return 1
echo "PING_INTERVAL=$interval" >> "$tmp2" || rm -f "$tmp2" || return 1
mv -f "$tmp2" "$FALLBACK_CONFIG_FILE" 2>/dev/null || return 1
chmod 644 "$FALLBACK_CONFIG_FILE" 2>/dev/null || true
# Point CONFIG_FILE to fallback for subsequent reads in this request
CONFIG_FILE="$FALLBACK_CONFIG_FILE"; CONFIG_DIR="$FALLBACK_CONFIG_DIR"
log_message "Saved ping config (fallback): enabled=$enabled host=$host interval=$interval"
}
# Delete ping configuration (reset to default)
delete_ping_setting() {
if [ -f "$CONFIG_FILE" ]; then
# Remove the PING_ENABLED line
sed -i '/^PING_ENABLED=/d' "$CONFIG_FILE"
log_message "Deleted ping configuration"
# If file is empty after deletion, remove it
if [ ! -s "$CONFIG_FILE" ]; then
rm -f "$CONFIG_FILE"
log_message "Removed empty config file"
local removed=1
for f in "$CONFIG_FILE" "$FALLBACK_CONFIG_FILE"; do
if [ -f "$f" ]; then
sed -i '/^PING_ENABLED=/d' "$f" 2>/dev/null || true
sed -i '/^PING_HOST=/d' "$f" 2>/dev/null || true
sed -i '/^PING_INTERVAL=/d' "$f" 2>/dev/null || true
log_message "Deleted ping configuration entries in $f"
[ -s "$f" ] || { rm -f "$f" 2>/dev/null || true; log_message "Removed empty config file $f"; }
removed=0
fi
return 0
else
return 1
fi
done
return $removed
}
# Handle GET request - Retrieve ping setting
handle_get() {
log_message "GET request received"
# Get current setting (from config or default)
local enabled=$(get_ping_setting)
# Check if it's from config or default
get_config_values
local running=false
if daemon_running; then running=true; fi
local is_default=true
if [ -f "$CONFIG_FILE" ] && grep -q "^PING_ENABLED=" "$CONFIG_FILE"; then
is_default=false
fi
send_success "Ping setting retrieved" "{\"enabled\":$enabled,\"isDefault\":$is_default}"
send_success "Ping configuration retrieved" "{\"enabled\":$ENABLED,\"host\":\"$HOST\",\"interval\":$INTERVAL,\"running\":$running,\"isDefault\":$is_default}"
}
# Handle POST request - Update ping setting
@@ -148,38 +241,58 @@ handle_post() {
local post_data=$(dd bs=$content_length count=1 2>/dev/null)
log_message "Received POST data: $post_data"
# Parse JSON to extract enabled value
local enabled=""
# Approach 1: Simple regex extraction for boolean
enabled=$(echo "$post_data" | sed -n 's/.*"enabled"[[:space:]]*:[[:space:]]*\([^,}]*\).*/\1/p' | tr -d ' ')
# Approach 2: grep + cut extraction
if [ -z "$enabled" ]; then
enabled=$(echo "$post_data" | grep -o '"enabled":[^,}]*' | cut -d':' -f2 | tr -d ' ')
# Parse fields
local enabled host interval
enabled=$(echo "$post_data" | sed -n 's/.*"enabled"[[:space:]]*:[[:space:]]*\([^,}]*\).*/\1/p' | tr -d ' ' | sed 's/"//g')
host=$(echo "$post_data" | sed -n 's/.*"host"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p')
interval=$(echo "$post_data" | sed -n 's/.*"interval"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p')
# Defaults when missing
[ -z "$enabled" ] && enabled="true"
[ -z "$host" ] && host="8.8.8.8"
[ -z "$interval" ] && interval="5"
# Validate
case "$enabled" in
true|false) : ;;
*) send_error "INVALID_SETTING" "Invalid enabled value. Must be true or false." ;;
esac
if ! echo "$interval" | grep -qE '^[0-9]+$'; then
send_error "INVALID_INTERVAL" "Interval must be a number (seconds)."
fi
# Approach 3: Look for true/false in the payload
if [ -z "$enabled" ]; then
if echo "$post_data" | grep -q '"enabled"[[:space:]]*:[[:space:]]*true'; then
enabled="true"
elif echo "$post_data" | grep -q '"enabled"[[:space:]]*:[[:space:]]*false'; then
enabled="false"
if [ "$interval" -lt 1 ] || [ "$interval" -gt 3600 ]; then
send_error "INVALID_INTERVAL" "Interval must be between 1 and 3600 seconds."
fi
# Capture previous values to decide on restart
get_config_values
local prev_enabled="$ENABLED"
local prev_host="$HOST"
local prev_interval="$INTERVAL"
save_config "$enabled" "$host" "$interval" || send_error "WRITE_FAILED" "Failed to save configuration"
if [ "$enabled" = "true" ]; then
if daemon_running; then
# Restart only if effective parameters changed
if [ "$prev_host" != "$host" ] || [ "$prev_interval" != "$interval" ] || [ "$prev_enabled" != "$enabled" ]; then
log_message "Config change detected (host/interval/enabled). Restarting daemon."
stop_daemon
start_daemon || log_message "Failed to restart daemon"
else
log_message "No change requiring restart; daemon remains running"
fi
else
start_daemon || log_message "Failed to start daemon"
fi
fi
# Clean up the value (remove quotes if present)
enabled=$(echo "$enabled" | sed 's/"//g')
log_message "Received enabled: $enabled"
# Validate setting
if [ "$enabled" = "true" ] || [ "$enabled" = "false" ]; then
save_ping_setting "$enabled"
send_success "Ping setting updated successfully" "{\"enabled\":$enabled}"
else
send_error "INVALID_SETTING" "Invalid setting provided. Must be 'true' or 'false'."
stop_daemon
fi
get_config_values
local running=false
if daemon_running; then running=true; fi
send_success "Ping setting updated successfully" "{\"enabled\":$ENABLED,\"host\":\"$HOST\",\"interval\":$INTERVAL,\"running\":$running}"
else
send_error "NO_DATA" "No data provided"
fi
@@ -188,24 +301,15 @@ handle_post() {
# Handle DELETE request - Reset to default (delete configuration)
handle_delete() {
log_message "DELETE request received"
stop_daemon
if delete_ping_setting; then
# Default is enabled
send_success "Ping setting reset to default" "{\"enabled\":true,\"isDefault\":true}"
send_success "Ping setting reset to default" "{\"enabled\":true,\"isDefault\":true,\"running\":false}"
else
send_error "NOT_FOUND" "Ping setting configuration not found"
fi
}
# Handle OPTIONS request for CORS preflight
handle_options() {
log_message "OPTIONS request received"
echo "Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS"
echo "Access-Control-Allow-Headers: Content-Type"
echo "Access-Control-Max-Age: 86400"
exit 0
}
# Main execution
log_message "Ping settings script called with method: ${REQUEST_METHOD:-GET}"
@@ -220,10 +324,7 @@ case "${REQUEST_METHOD:-GET}" in
DELETE)
handle_delete
;;
OPTIONS)
handle_options
;;
*)
send_error "METHOD_NOT_ALLOWED" "HTTP method ${REQUEST_METHOD} not supported"
;;
esac
esac

View File

@@ -0,0 +1,198 @@
#!/bin/sh
# Memory Daemon - Monitors system memory usage and writes to JSON file
# This daemon only runs when memory monitoring is enabled via settings
set -eu
# Ensure PATH for OpenWrt/BusyBox
export PATH="/usr/sbin:/usr/bin:/sbin:/bin:$PATH"
# Configuration
TMP_DIR="/tmp/quecmanager"
OUT_JSON="$TMP_DIR/memory.json"
PID_FILE="$TMP_DIR/memory_daemon.pid"
LOG_FILE="$TMP_DIR/memory_daemon.log"
CONFIG_FILE="/etc/quecmanager/settings/memory_settings.conf"
[ -f "$CONFIG_FILE" ] || CONFIG_FILE="/tmp/quecmanager/settings/memory_settings.conf"
DEFAULT_INTERVAL=1
# Ensure temp directory exists
ensure_tmp_dir() {
[ -d "$TMP_DIR" ] || mkdir -p "$TMP_DIR" || exit 1
}
# Logging function
log() {
printf '%s - %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$1" >> "$LOG_FILE" 2>/dev/null || true
}
# Check if this daemon instance is already running
daemon_is_running() {
if [ -f "$PID_FILE" ]; then
pid="$(cat "$PID_FILE" 2>/dev/null || true)"
if [ -n "${pid:-}" ] && kill -0 "$pid" 2>/dev/null; then
# Verify it's actually our daemon by checking process cmdline
if [ -r "/proc/$pid/cmdline" ] && grep -q "memory_daemon.sh" "/proc/$pid/cmdline" 2>/dev/null; then
return 0
else
# PID file is stale, remove it
rm -f "$PID_FILE" 2>/dev/null || true
fi
fi
fi
return 1
}
# Write our PID to file
write_pid() {
echo "$$" > "$PID_FILE"
}
# Cleanup function
cleanup() {
rm -f "$PID_FILE" 2>/dev/null || true
log "Memory daemon stopped"
}
# Create default config if none exists
create_default_config() {
local primary_config="/etc/quecmanager/settings/memory_settings.conf"
local fallback_config="/tmp/quecmanager/settings/memory_settings.conf"
if [ ! -f "$primary_config" ] && [ ! -f "$fallback_config" ]; then
log "No config file found, creating default configuration"
# Try primary location first
if mkdir -p "/etc/quecmanager/settings" 2>/dev/null; then
{
echo "MEMORY_ENABLED=false"
echo "MEMORY_INTERVAL=1"
} > "$primary_config" 2>/dev/null && {
chmod 644 "$primary_config" 2>/dev/null || true
CONFIG_FILE="$primary_config"
log "Created default config at $primary_config"
return 0
}
fi
# Fallback to tmp location
mkdir -p "/tmp/quecmanager/settings" 2>/dev/null || true
{
echo "MEMORY_ENABLED=false"
echo "MEMORY_INTERVAL=1"
} > "$fallback_config" && {
chmod 644 "$fallback_config" 2>/dev/null || true
CONFIG_FILE="$fallback_config"
log "Created default config at $fallback_config"
return 0
}
log "Failed to create default config file"
return 1
fi
}
# Read configuration from file
read_config() {
ENABLED="false"
INTERVAL="$DEFAULT_INTERVAL"
if [ -f "$CONFIG_FILE" ]; then
MEMORY_ENABLED=$(grep -E "^MEMORY_ENABLED=" "$CONFIG_FILE" 2>/dev/null | tail -n1 | cut -d'=' -f2 | tr -d '\r' | tr -d '"')
MEMORY_INTERVAL=$(grep -E "^MEMORY_INTERVAL=" "$CONFIG_FILE" 2>/dev/null | tail -n1 | cut -d'=' -f2 | tr -d '\r')
case "${MEMORY_ENABLED:-}" in
true|1|on|yes|enabled) ENABLED="true" ;;
*) ENABLED="false" ;;
esac
if echo "${MEMORY_INTERVAL:-}" | grep -qE '^[0-9]+$'; then
if [ "$MEMORY_INTERVAL" -ge 1 ] && [ "$MEMORY_INTERVAL" -le 10 ]; then
INTERVAL="$MEMORY_INTERVAL"
fi
fi
fi
}
# Write JSON data atomically
write_json_atomic() {
local json_data="$1"
local tmpfile="$(mktemp "$TMP_DIR/memory.XXXXXX" 2>/dev/null || echo "$TMP_DIR/memory.tmp.$$")"
if [ -n "$tmpfile" ] && printf '%s' "$json_data" > "$tmpfile" 2>/dev/null; then
mv "$tmpfile" "$OUT_JSON" 2>/dev/null || {
# Fallback if move fails
printf '%s' "$json_data" > "$OUT_JSON" 2>/dev/null || true
rm -f "$tmpfile" 2>/dev/null || true
}
else
# Direct write fallback
printf '%s' "$json_data" > "$OUT_JSON" 2>/dev/null || true
rm -f "$tmpfile" 2>/dev/null || true
fi
}
# Main execution starts here
ensure_tmp_dir
log "Starting memory daemon (PID: $$)"
# Check if already running
if daemon_is_running; then
log "Memory daemon already running, exiting"
exit 0
fi
# Create default config if needed
create_default_config
# Set up signal handlers
trap cleanup EXIT INT TERM
write_pid
# Main monitoring loop
while true; do
read_config
# Exit if disabled
if [ "$ENABLED" != "true" ]; then
log "Memory monitoring disabled in config, exiting"
exit 0
fi
# Get current timestamp
ts="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
# Get memory information using /proc/meminfo (most reliable method)
if [ -r "/proc/meminfo" ]; then
# Extract values from /proc/meminfo (values are in kB)
TOTAL_KB=$(grep "^MemTotal:" /proc/meminfo 2>/dev/null | awk '{print $2}' || echo "0")
AVAIL_KB=$(grep "^MemAvailable:" /proc/meminfo 2>/dev/null | awk '{print $2}' || echo "0")
FREE_KB=$(grep "^MemFree:" /proc/meminfo 2>/dev/null | awk '{print $2}' || echo "0")
# If MemAvailable is not available (older kernels), estimate it
if [ "$AVAIL_KB" = "0" ]; then
CACHED_KB=$(grep "^Cached:" /proc/meminfo 2>/dev/null | awk '{print $2}' || echo "0")
BUFFERS_KB=$(grep "^Buffers:" /proc/meminfo 2>/dev/null | awk '{print $2}' || echo "0")
AVAIL_KB=$((FREE_KB + CACHED_KB + BUFFERS_KB))
fi
# Convert to bytes (multiply by 1024)
TOTAL_BYTES=$((TOTAL_KB * 1024))
AVAIL_BYTES=$((AVAIL_KB * 1024))
USED_BYTES=$((TOTAL_BYTES - AVAIL_BYTES))
json="{\"total\": $TOTAL_BYTES, \"used\": $USED_BYTES, \"available\": $AVAIL_BYTES, \"timestamp\": \"$ts\"}"
else
# Fallback if /proc/meminfo is not available
log "Warning: /proc/meminfo not readable, using error response"
json="{\"total\": 0, \"used\": 0, \"available\": 0, \"timestamp\": \"$ts\", \"error\": \"meminfo_unavailable\"}"
fi
# Write the JSON data
write_json_atomic "$json"
log "Updated memory data: total=${TOTAL_KB:-0}KB, used=${USED_BYTES:-0}B, available=${AVAIL_KB:-0}KB"
# Sleep for the configured interval
sleep "$INTERVAL"
done

View File

@@ -0,0 +1,134 @@
#!/bin/sh
set -eu
# Ensure PATH for OpenWrt/BusyBox
export PATH="/usr/sbin:/usr/bin:/sbin:/bin:$PATH"
TMP_DIR="/tmp/quecmanager"
OUT_JSON="$TMP_DIR/ping_latency.json"
PID_FILE="$TMP_DIR/ping_daemon.pid"
LOG_FILE="$TMP_DIR/ping_daemon.log"
CONFIG_FILE="/etc/quecmanager/settings/ping_settings.conf"
[ -f "$CONFIG_FILE" ] || CONFIG_FILE="/tmp/quecmanager/settings/ping_settings.conf"
DEFAULT_HOST="8.8.8.8"
DEFAULT_INTERVAL=5
ensure_tmp_dir() { [ -d "$TMP_DIR" ] || mkdir -p "$TMP_DIR" || exit 1; }
log() {
printf '%s - %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$1" >> "$LOG_FILE" 2>/dev/null || true
}
daemon_is_running() {
if [ -f "$PID_FILE" ]; then
pid="$(cat "$PID_FILE" 2>/dev/null || true)"
if [ -n "${pid:-}" ] && kill -0 "$pid" 2>/dev/null; then
# Avoid false positive if PID reused
if [ -r "/proc/$pid/cmdline" ] && grep -q "ping_daemon.sh" "/proc/$pid/cmdline" 2>/dev/null; then
return 0
else
rm -f "$PID_FILE" 2>/dev/null || true
fi
fi
fi
return 1
}
write_pid() { echo "$$" > "$PID_FILE"; }
cleanup() { rm -f "$PID_FILE" 2>/dev/null || true; }
read_config() {
ENABLED="true"; HOST="$DEFAULT_HOST"; INTERVAL="$DEFAULT_INTERVAL"
if [ -f "$CONFIG_FILE" ]; then
PING_ENABLED=$(grep -E "^PING_ENABLED=" "$CONFIG_FILE" | tail -n1 | cut -d'=' -f2 | tr -d '\r') || true
PING_HOST=$(grep -E "^PING_HOST=" "$CONFIG_FILE" | tail -n1 | cut -d'=' -f2 | tr -d '\r') || true
PING_INTERVAL=$(grep -E "^PING_INTERVAL=" "$CONFIG_FILE" | tail -n1 | cut -d'=' -f2 | tr -d '\r') || true
case "${PING_ENABLED:-}" in true|1|on|yes|enabled) ENABLED=true ;; *) ENABLED=false ;; esac
[ -n "${PING_HOST:-}" ] && HOST="$PING_HOST"
if echo "${PING_INTERVAL:-}" | grep -qE '^[0-9]+$'; then
if [ "$PING_INTERVAL" -ge 1 ] && [ "$PING_INTERVAL" -le 3600 ]; then
INTERVAL="$PING_INTERVAL"
fi
fi
fi
}
# Create default config if none exists
create_default_config() {
local primary_config="/etc/quecmanager/settings/ping_settings.conf"
local fallback_config="/tmp/quecmanager/settings/ping_settings.conf"
# Check if either config exists
if [ -f "$primary_config" ] || [ -f "$fallback_config" ]; then
return 0
fi
# Try to create in primary location first
if mkdir -p "/etc/quecmanager/settings" 2>/dev/null; then
{
echo "PING_ENABLED=true"
echo "PING_HOST=$DEFAULT_HOST"
echo "PING_INTERVAL=$DEFAULT_INTERVAL"
} > "$primary_config" 2>/dev/null && {
chmod 644 "$primary_config" 2>/dev/null || true
CONFIG_FILE="$primary_config"
log "Created default config at $primary_config"
return 0
}
fi
# Fallback to tmp location
mkdir -p "/tmp/quecmanager/settings" 2>/dev/null || true
{
echo "PING_ENABLED=true"
echo "PING_HOST=$DEFAULT_HOST"
echo "PING_INTERVAL=$DEFAULT_INTERVAL"
} > "$fallback_config" && {
chmod 644 "$fallback_config" 2>/dev/null || true
CONFIG_FILE="$fallback_config"
log "Created default config at $fallback_config"
return 0
}
log "Failed to create default config file"
return 1
}
write_json_atomic() {
tmpfile="$(mktemp "$TMP_DIR/ping_latency.XXXXXX" 2>/dev/null || true)"
if [ -n "${tmpfile:-}" ] && [ -w "$TMP_DIR" ]; then
printf '%s' "$1" > "$tmpfile" 2>/dev/null || true
mv -f "$tmpfile" "$OUT_JSON" 2>/dev/null || printf '%s' "$1" > "$OUT_JSON"
else
printf '%s' "$1" > "$OUT_JSON"
fi
}
ensure_tmp_dir
log "Starting ping daemon"
if daemon_is_running; then log "Already running"; exit 0; fi
# Create default config if none exists
create_default_config
trap cleanup EXIT INT TERM
write_pid
while true; do
read_config
if [ "$ENABLED" != "true" ]; then log "Disabled in config"; exit 0; fi
ts="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
PING_BIN="$(command -v ping || echo /bin/ping)"
output="$("$PING_BIN" -c 1 -w 2 "$HOST" 2>/dev/null || true)"
if echo "$output" | grep -q "time="; then
latency_ms="$(echo "$output" | grep -o 'time=[0-9.]*' | head -n1 | cut -d'=' -f2 | cut -d'.' -f1)"; [ -z "$latency_ms" ] && latency_ms=0
json="{\"timestamp\":\"$ts\",\"host\":\"$HOST\",\"latency\":$latency_ms,\"ok\":true}"
else
json="{\"timestamp\":\"$ts\",\"host\":\"$HOST\",\"latency\":null,\"ok\":false}"
fi
write_json_atomic "$json"
log "Wrote: $json"
sleep "$INTERVAL"
done

View File

@@ -607,6 +607,7 @@ apply_profile_settings() {
local current_nsa_nr5g_bands="${14}"
local current_imei="${15}"
local iccid="${16}"
local mobile_provider="${17}"
# Set TTL to 0 (disabled) if not specified
ttl="${ttl:-0}"
@@ -619,6 +620,7 @@ apply_profile_settings() {
log_message "- APN: $apn ($pdp_type)" "info"
log_message "- IMEI: $imei" "info"
log_message "- TTL: $ttl" "info"
log_message "- Mobile Provider: $mobile_provider" "info"
# Check if any changes are needed using improved comparison
local needs_apn_change=0
@@ -630,6 +632,7 @@ apply_profile_settings() {
local needs_ttl_change=0
local changes_needed=0
local requires_reboot=0
local change_for_reboot=""
# Use normalized comparison
compare_values "$current_apn" "$apn" "apn" && needs_apn_change=1 && changes_needed=1
@@ -804,6 +807,7 @@ apply_profile_settings() {
if [ $? -eq 0 ]; then
changes_made=1
requires_reboot=1
change_for_reboot="IMEI"
log_message "IMEI changed successfully to $imei (device will reboot)" "info"
update_track "rebooting" "IMEI changed, device will reboot" "$profile_name" "95"
else
@@ -813,9 +817,56 @@ apply_profile_settings() {
fi
fi
# Apply unique rule setup for Verizon, but also handle "Other" Mobile Providers because of MPDN_rule shenanigans
# Probably requires reboot
output_check=$(execute_at_command "AT+QMAP=\"mpdn_rule\"")
sleep 1 # Short delay to ensure command is processed
qmap_rule0=$(echo "$output_check" | grep '+QMAP: "MPDN_rule",0,')
qmap_ippt_rule0=$(echo "$qmap_rule0" | cut -d',' -f5)
if [ $apply_success -eq 1 ] && [ -n "$mobile_provider" ]; then
if [ "$mobile_provider" = "Verizon" ]; then
# If Verizon, data call should be set to rule 3, AT+QMAP="mpdn_rule",0,3,0,0,1
if echo "$qmap_rule0" | awk -F',' '{exit !($2==0 && $3==3 && $6==1)}'; then
log_message "Verizon rule already set correctly, no changes needed" "info"
else
log_message "Setting Verizon data call mpdn_rule to 3" "info"
update_track "applying" "Setting Verizon data call rule to 3" "$profile_name" "100"
verizon_cmd="AT+QMAP=\"mpdn_rule\",0,3,0,$qmap_ippt_rule0,1"
execute_at_command "$verizon_cmd" 10 "$token_id" >/dev/null
sleep 1 # Short delay to ensure command is processed
fi
elif [ "$mobile_provider" = "Other" ]; then
# Check if MPDN_rule 0 is already set to all zeros
if echo "$qmap_rule0" | awk -F',' '{exit !($2==0 && $3==0 && $6==0)}'; then
log_message "Default rule already set correctly, no changes needed" "info"
else
log_message "Setting to default mpdn_rule and releasing" "info"
update_track "applying" "Setting Default data call mpdn_rule to 0" "$profile_name" "100"
def_cmd1="AT+QMAP=\"mpdn_rule\",0"
execute_at_command "$def_cmd1" 10 "$token_id"
sleep 1 # Short delay to ensure command is processed
def_cmd2="AT+QMAP=\"mpdn_rule\",0,1,0,$qmap_ippt_rule0,1"
execute_at_command "$def_cmd2" 10 "$token_id"
sleep 1 # Short delay to ensure command is processed
if [ "$qmap_ippt_rule0" = "0" ]; then
log_message "IPPT is disabled for rule, release the MPDN_rule" "info"
def_cmd3="AT+QMAP=\"mpdn_rule\",0"
execute_at_command "$def_cmd3" 10 "$token_id"
sleep 1 # Short delay to ensure command is processed
if [ "$(cat /sys/devices/soc0/machine)" = "SDXPINN" ]; then
requires_reboot=1
change_for_reboot="MPDN_rule"
update_track "rebooting" "MPDN_rule released, device will reboot" "$profile_name" "105"
fi
else
log_message "IPPT is enabled for rule0 not releasing MPDN_rule, no reboot needed: IPPT Value $qmap_ippt_rule0" "info"
fi
fi
fi
fi
# Release token
release_token "$token_id"
# Mark profile as applied if changes were made
if [ $changes_made -eq 1 ]; then
mark_profile_applied "$iccid" "$profile_name"
@@ -824,7 +875,7 @@ apply_profile_settings() {
# If IMEI was changed, need to reboot
if [ $requires_reboot -eq 1 ]; then
log_message "IMEI change requires reboot, scheduling reboot..." "info"
update_track "rebooting" "Device is rebooting to apply IMEI change" "$profile_name" "100"
update_track "rebooting" "Device is rebooting to apply $change_for_reboot change" "$profile_name" "100"
sleep 2
reboot &
return 0
@@ -913,11 +964,12 @@ check_profile() {
local pdp_type=$(uci -q get quecprofiles.$profile_index.pdp_type)
local imei=$(uci -q get quecprofiles.$profile_index.imei)
local ttl=$(uci -q get quecprofiles.$profile_index.ttl)
local mobile_provider=$(uci -q get quecprofiles.$profile_index.mobile_provider)
# Check if profile is paused
local paused=$(uci -q get quecprofiles.$profile_index.paused)
paused="${paused:-0}" # Default to not paused if not set
# Skip applying paused profiles
if [ "$paused" = "1" ]; then
log_message "Profile '$profile_name' is paused, skipping application" "info"
@@ -982,7 +1034,7 @@ check_profile() {
# Apply profile settings with the new parameters
apply_profile_settings "$profile_name" "$network_type" "$lte_bands" "$sa_nr5g_bands" "$nsa_nr5g_bands" \
"$apn" "$pdp_type" "$imei" "$ttl" "$current_apn" "$current_mode" "$current_lte_bands" \
"$current_sa_nr5g_bands" "$current_nsa_nr5g_bands" "$current_imei" "$current_iccid"
"$current_sa_nr5g_bands" "$current_nsa_nr5g_bands" "$current_imei" "$current_iccid" "$mobile_provider"
return $?
else
log_message "Automatic profile switching is disabled, not applying profile" "info"
@@ -1038,7 +1090,7 @@ main() {
while [ $sleep_counter -lt $check_interval ]; do
sleep 5
sleep_counter=$((sleep_counter + 5))
# Check for manual trigger during sleep
if [ -f "$CHECK_TRIGGER" ]; then
log_message "Manual check triggered during sleep" "info"