Updated scirpts for Hot Fix

This commit is contained in:
Russel Yasol
2025-08-27 11:39:02 +08:00
parent 6bd2c7ea52
commit 96dd20a758
5 changed files with 125 additions and 67 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
# Windows Zone.Identifier files (created when downloading files from internet)
*:Zone.Identifier
*Zone.Identifier

View File

@@ -2,11 +2,40 @@
# AT Queue Client for OpenWRT # AT Queue Client for OpenWRT
# Located in /www/cgi-bin/services/at_queue_client # Located in /www/cgi-bin/services/at_queue_client
# Load centralized logging
. /www/cgi-bin/services/quecmanager_logger.sh
AUTH_FILE="/tmp/auth_success" AUTH_FILE="/tmp/auth_success"
QUEUE_DIR="/tmp/at_queue" QUEUE_DIR="/tmp/at_queue"
RESULTS_DIR="$QUEUE_DIR/results" RESULTS_DIR="$QUEUE_DIR/results"
QUEUE_MANAGER="/www/cgi-bin/services/at_queue_manager.sh" QUEUE_MANAGER="/www/cgi-bin/services/at_queue_manager.sh"
POLL_INTERVAL=0.01 POLL_INTERVAL=0.01
SCRIPT_NAME_LOG="at_queue_client"
# Logging function - uses both centralized and system logging
log_at_queue_client() {
local level="$1"
local message="$2"
# Use centralized logging
case "$level" in
"error")
qm_log_error "service" "$SCRIPT_NAME_LOG" "$message"
;;
"warn")
qm_log_warn "service" "$SCRIPT_NAME_LOG" "$message"
;;
"debug")
qm_log_debug "service" "$SCRIPT_NAME_LOG" "$message"
;;
*)
qm_log_info "service" "$SCRIPT_NAME_LOG" "$message"
;;
esac
# Also maintain system logging for compatibility
logger -t at_queue -p "daemon.$level" "$message"
}
usage() { usage() {
echo "Usage: $0 [options] <AT command>" echo "Usage: $0 [options] <AT command>"
@@ -20,14 +49,14 @@ usage() {
# Output JSON response # Output JSON response
output_json() { output_json() {
local content="$1" local content="$1"
local headers="${2:-1}" # Default to showing headers local headers="${2:-1}" # Default to showing headers
echo "$content" echo "$content"
} }
# URL decode function # URL decode function
urldecode() { urldecode() {
local encoded="$1" local encoded="$1"
logger -t at_queue -p daemon.debug "urldecode: input='$encoded'" log_at_queue_client "debug" "urldecode: input='$encoded'"
# Handle %2B -> + and %22 -> " conversions # Handle %2B -> + and %22 -> " conversions
local decoded="${encoded//%2B/+}" local decoded="${encoded//%2B/+}"
@@ -35,10 +64,23 @@ urldecode() {
# Then handle other encoded characters # Then handle other encoded characters
decoded=$(printf '%b' "${decoded//%/\\x}") decoded=$(printf '%b' "${decoded//%/\\x}")
logger -t at_queue -p daemon.debug "urldecode: output='$decoded'" log_at_queue_client "debug" "urldecode: output='$decoded'"
echo "$decoded" echo "$decoded"
} }
# URL encode function (simplified for AT commands)
urlencode() {
local string="$1"
# Simple encoding for common AT command characters
string="${string// /%20}"
string="${string//+/%2B}"
string="${string//\"/%22}"
string="${string//=/%3D}"
string="${string//&/%26}"
string="${string//?/%3F}"
echo "$string"
}
# Extract command ID from response with improved error handling # Extract command ID from response with improved error handling
get_command_id() { get_command_id() {
local response="$1" local response="$1"
@@ -72,19 +114,19 @@ get_command_id() {
# Normalize AT command # Normalize AT command
normalize_at_command() { normalize_at_command() {
local cmd="$1" local cmd="$1"
logger -t at_queue -p daemon.debug "normalize: input='$cmd'" log_at_queue_client "debug" "normalize: input='$cmd'"
# URL decode the command # URL decode the command
cmd=$(urldecode "$cmd") cmd=$(urldecode "$cmd")
logger -t at_queue -p daemon.debug "normalize: after urldecode='$cmd'" log_at_queue_client "debug" "normalize: after urldecode='$cmd'"
# Remove any carriage returns or newlines # Remove any carriage returns or newlines
cmd=$(echo "$cmd" | tr -d '\r\n') cmd=$(echo "$cmd" | tr -d '\r\n')
logger -t at_queue -p daemon.debug "normalize: after cleanup='$cmd'" log_at_queue_client "debug" "normalize: after cleanup='$cmd'"
# Trim leading/trailing whitespace while preserving quotes # Trim leading/trailing whitespace while preserving quotes
cmd=$(echo "$cmd" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') cmd=$(echo "$cmd" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
logger -t at_queue -p daemon.debug "normalize: final output='$cmd'" log_at_queue_client "debug" "normalize: final output='$cmd'"
echo "$cmd" echo "$cmd"
} }
@@ -101,7 +143,7 @@ submit_command() {
# Submit using appropriate method # Submit using appropriate method
if [ "${SCRIPT_NAME}" != "" ]; then if [ "${SCRIPT_NAME}" != "" ]; then
# CGI mode - direct execution # CGI mode - direct execution like the original working version
local escaped_cmd=$(echo "$cmd" | sed 's/"/\\"/g') local escaped_cmd=$(echo "$cmd" | sed 's/"/\\"/g')
QUERY_STRING="action=enqueue&command=${escaped_cmd}&priority=$priority" "$QUEUE_MANAGER" QUERY_STRING="action=enqueue&command=${escaped_cmd}&priority=$priority" "$QUEUE_MANAGER"
else else
@@ -118,7 +160,7 @@ check_result() {
if [ -f "$RESULTS_DIR/$cmd_id.json" ]; then if [ -f "$RESULTS_DIR/$cmd_id.json" ]; then
local result_content=$(cat "$RESULTS_DIR/$cmd_id.json") local result_content=$(cat "$RESULTS_DIR/$cmd_id.json")
if [ -z "$result_content" ]; then if [ -z "$result_content" ]; then
logger -t at_queue -p daemon.error "Empty result file for command ID: $cmd_id" log_at_queue_client "error" "Empty result file for command ID: $cmd_id"
local error_json="{\"error\":\"Empty result file\",\"command_id\":\"$cmd_id\"}" local error_json="{\"error\":\"Empty result file\",\"command_id\":\"$cmd_id\"}"
output_json "$error_json" "$show_headers" output_json "$error_json" "$show_headers"
return 1 return 1

View File

@@ -18,7 +18,7 @@ RESULTS_MAX_AGE=3600 # 1 hour in seconds
POLL_INTERVAL=0.01 POLL_INTERVAL=0.01
PREEMPTION_THRESHOLD=2 # 3 seconds threshold for preemption PREEMPTION_THRESHOLD=2 # 3 seconds threshold for preemption
TOKEN_TIMEOUT=30 # seconds before token expires TOKEN_TIMEOUT=30 # seconds before token expires
SCRIPT_NAME="at_queue_manager" SCRIPT_NAME_LOG="at_queue_manager"
# Logging function - uses both centralized and system logging # Logging function - uses both centralized and system logging
log_at_queue() { log_at_queue() {
@@ -28,16 +28,16 @@ log_at_queue() {
# Use centralized logging # Use centralized logging
case "$level" in case "$level" in
"error") "error")
qm_log_error "service" "$SCRIPT_NAME" "$message" qm_log_error "service" "$SCRIPT_NAME_LOG" "$message"
;; ;;
"warn") "warn")
qm_log_warn "service" "$SCRIPT_NAME" "$message" qm_log_warn "service" "$SCRIPT_NAME_LOG" "$message"
;; ;;
"debug") "debug")
qm_log_debug "service" "$SCRIPT_NAME" "$message" qm_log_debug "service" "$SCRIPT_NAME_LOG" "$message"
;; ;;
*) *)
qm_log_info "service" "$SCRIPT_NAME" "$message" qm_log_info "service" "$SCRIPT_NAME_LOG" "$message"
;; ;;
esac esac
@@ -81,20 +81,23 @@ acquire_lock() {
} }
release_lock() { release_lock() {
if rmdir "$LOCK_DIR" 2>/dev/null; then if [ -d "$LOCK_DIR" ]; then
rmdir "$LOCK_DIR" 2>/dev/null
log_at_queue "debug" "Lock released" log_at_queue "debug" "Lock released"
return 0 return 0
else
log_at_queue "error" "Lock directory doesn't exist"
return 1
fi fi
log_at_queue "error" "Lock directory doesn't exist"
return 1
} }
# Ensure required directories exist # Ensure required directories exist
initialize_queue() { init_queue_system() {
mkdir -p "$QUEUE_DIR" "$RESULTS_DIR" mkdir -p "$QUEUE_DIR" "$RESULTS_DIR"
touch "$QUEUE_FILE" "$ACTIVE_FILE" touch "$QUEUE_FILE"
chmod 666 "$QUEUE_FILE" "$ACTIVE_FILE" chmod 755 "$QUEUE_DIR"
chmod 644 "$QUEUE_FILE"
chmod 755 "$RESULTS_DIR"
log_at_queue "info" "Queue system initialized" log_at_queue "info" "Queue system initialized"
} }
@@ -102,10 +105,13 @@ initialize_queue() {
cleanup_old_results() { cleanup_old_results() {
local current_time=$(date +%s) local current_time=$(date +%s)
# Remove old tracking files # Clean up old execution tracking files
find "$QUEUE_DIR" -name "start_time.*" -o -name "pid.*" -type f -mmin +60 -delete 2>/dev/null find "$QUEUE_DIR" -name "pid.*" -type f -mmin +60 -delete 2>/dev/null
find "$QUEUE_DIR" -name "*.exit" -type f -mmin +60 -delete 2>/dev/null
find "$QUEUE_DIR" -name "start_time.*" -type f -mmin +60 -delete 2>/dev/null
log_at_queue "debug" "Cleaned up old tracking files"
log_at_queue "debug" "Cleaned up old tracking files" # Use find with -delete and basic timestamp check for OpenWRT # Use find with -delete and basic timestamp check for OpenWRT
find "$RESULTS_DIR" -name "*.json" -type f -mmin +60 -delete 2>/dev/null || { find "$RESULTS_DIR" -name "*.json" -type f -mmin +60 -delete 2>/dev/null || {
# Fallback method if find fails # Fallback method if find fails
for file in "$RESULTS_DIR"/*.json; do for file in "$RESULTS_DIR"/*.json; do
@@ -635,24 +641,9 @@ if [ "${SCRIPT_NAME}" != "" ]; then
echo "" echo ""
fi fi
# Log the incoming request for debugging
log_at_queue "debug" "CGI: Incoming request - QUERY_STRING='$QUERY_STRING', REQUEST_METHOD='$REQUEST_METHOD', HTTP_USER_AGENT='$HTTP_USER_AGENT'"
# Parse query string for CGI mode # Parse query string for CGI mode
eval $(echo "$QUERY_STRING" | sed 's/&/;/g') eval $(echo "$QUERY_STRING" | sed 's/&/;/g')
# Handle empty action parameter specifically
if [ -z "$action" ]; then
if [ -z "$QUERY_STRING" ]; then
log_at_queue "warn" "CGI: No query string provided - possible health check or browser prefetch"
echo "{\"error\":\"No action specified\",\"help\":\"Valid actions: enqueue, status, request_token, release_token\"}"
else
log_at_queue "warn" "CGI: Query string present but no action parameter: '$QUERY_STRING'"
echo "{\"error\":\"Missing action parameter\",\"query_string\":\"$QUERY_STRING\"}"
fi
exit 0
fi
case "$action" in case "$action" in
"enqueue") "enqueue")
if [ -n "$command" ]; then if [ -n "$command" ]; then
@@ -691,8 +682,8 @@ if [ "${SCRIPT_NAME}" != "" ]; then
fi fi
;; ;;
*) *)
log_at_queue "error" "CGI: Invalid action received: '$action' (QUERY_STRING: '$QUERY_STRING')" log_at_queue "error" "CGI: Invalid action received: $action"
echo "{\"error\":\"Invalid action: $action\",\"valid_actions\":[\"enqueue\",\"status\",\"request_token\",\"release_token\"]}" echo "{\"error\":\"Invalid action\"}"
;; ;;
esac esac
exit 0 exit 0
@@ -707,4 +698,4 @@ fi
# If not run as CGI, start queue processing # If not run as CGI, start queue processing
if [ "${SCRIPT_NAME}" = "" ] && [ -z "$1" ]; then if [ "${SCRIPT_NAME}" = "" ] && [ -z "$1" ]; then
process_queue process_queue
fi fi

View File

@@ -12,39 +12,57 @@ TRACK_FILE="/tmp/quecprofiles_active"
CHECK_TRIGGER="/tmp/quecprofiles_check" CHECK_TRIGGER="/tmp/quecprofiles_check"
STATUS_FILE="/tmp/quecprofiles_status.json" STATUS_FILE="/tmp/quecprofiles_status.json"
APPLIED_FLAG="/tmp/quecprofiles_applied" APPLIED_FLAG="/tmp/quecprofiles_applied"
DEBUG_LOG="/tmp/quecprofiles_debug.log"
DETAILED_LOG="/tmp/quecprofiles_detailed.log"
DEFAULT_CHECK_INTERVAL=60 # Default check interval in seconds DEFAULT_CHECK_INTERVAL=60 # Default check interval in seconds
COMMAND_TIMEOUT=10 # Default timeout for AT commands in seconds COMMAND_TIMEOUT=10 # Default timeout for AT commands in seconds
QUEUE_PRIORITY=3 # Medium-high priority (1 is highest for cell scan) QUEUE_PRIORITY=3 # Medium-high priority (1 is highest for cell scan)
MAX_TOKEN_WAIT=15 # Maximum seconds to wait for token acquisition MAX_TOKEN_WAIT=15 # Maximum seconds to wait for token acquisition
SCRIPT_NAME="quecprofile" SCRIPT_NAME_LOG="quecprofiles_daemon"
# Initialize logging # Initialize log files and use centralized logging
qm_log_info "service" "$SCRIPT_NAME" "Starting QuecProfiles daemon with SA/NSA NR5G and TTL support (PID: $$)" mkdir -p "$(dirname "$DEBUG_LOG")" "$(dirname "$DETAILED_LOG")"
touch "$DEBUG_LOG" "$DETAILED_LOG"
chmod 644 "$DEBUG_LOG" "$DETAILED_LOG"
# Function to log messages # Log startup message using centralized logging
qm_log_info "service" "$SCRIPT_NAME_LOG" "Starting QuecProfiles daemon with SA/NSA NR5G and TTL support (PID: $$)"
# Also maintain file logging for compatibility
echo "$(date) - Starting QuecProfiles daemon with SA/NSA NR5G and TTL support (PID: $$)" >"$DEBUG_LOG"
echo "$(date) - Starting QuecProfiles daemon with SA/NSA NR5G and TTL support (PID: $$)" >"$DETAILED_LOG"
# Function to log messages - now uses centralized logging
log_message() { log_message() {
local message="$1" local message="$1"
local level="${2:-info}" local level="${2:-info}"
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
# Use centralized logging # Use centralized logging
case "$level" in case "$level" in
"error") "error")
qm_log_error "service" "$SCRIPT_NAME" "$message" qm_log_error "service" "$SCRIPT_NAME_LOG" "$message"
;; ;;
"warn") "warn")
qm_log_warn "service" "$SCRIPT_NAME" "$message" qm_log_warn "service" "$SCRIPT_NAME_LOG" "$message"
;; ;;
"debug") "debug")
qm_log_debug "service" "$SCRIPT_NAME" "$message" qm_log_debug "service" "$SCRIPT_NAME_LOG" "$message"
;; ;;
*) *)
qm_log_info "service" "$SCRIPT_NAME" "$message" qm_log_info "service" "$SCRIPT_NAME_LOG" "$message"
;; ;;
esac esac
# Also log to system log for important messages # Also maintain system logging for compatibility
if [ "$level" = "error" ] || [ "$level" = "warn" ] || [ "$level" = "info" ]; then logger -t quecprofiles_daemon -p "daemon.$level" "$message"
logger -t quecprofiles_daemon -p "daemon.$level" "$message"
# Log to debug file (maintain existing behavior)
echo "[$timestamp] [$level] $message" >>"$DEBUG_LOG"
# For detailed logs or errors (maintain existing behavior)
if [ "$level" = "error" ] || [ "$level" = "debug" ]; then
echo "[$timestamp] [$level] $message" >>"$DETAILED_LOG"
fi fi
} }

View File

@@ -3,55 +3,59 @@
# QuecWatch Daemon # QuecWatch Daemon
# Monitors cellular connectivity and performs recovery actions # Monitors cellular connectivity and performs recovery actions
# Load UCI configuration functions
. /lib/functions.sh
# Load centralized logging # Load centralized logging
. /www/cgi-bin/services/quecmanager_logger.sh . /www/cgi-bin/services/quecmanager_logger.sh
# Load UCI configuration functions
. /lib/functions.sh
# Configuration # Configuration
QUEUE_DIR="/tmp/at_queue" QUEUE_DIR="/tmp/at_queue"
TOKEN_FILE="$QUEUE_DIR/token" TOKEN_FILE="$QUEUE_DIR/token"
LOG_DIR="/tmp/log/quecwatch"
LOG_FILE="$LOG_DIR/quecwatch.log"
PID_FILE="/var/run/quecwatch.pid" PID_FILE="/var/run/quecwatch.pid"
STATUS_FILE="/tmp/quecwatch_status.json" STATUS_FILE="/tmp/quecwatch_status.json"
RETRY_COUNT_FILE="/tmp/quecwatch_retry_count" RETRY_COUNT_FILE="/tmp/quecwatch_retry_count"
UCI_CONFIG="quecmanager" UCI_CONFIG="quecmanager"
MAX_TOKEN_WAIT=10 # Maximum seconds to wait for token acquisition MAX_TOKEN_WAIT=10 # Maximum seconds to wait for token acquisition
TOKEN_PRIORITY=15 # Medium priority (between profiles and metrics) TOKEN_PRIORITY=15 # Medium priority (between profiles and metrics)
SCRIPT_NAME="quecwatch" SCRIPT_NAME_LOG="quecwatch"
# Ensure directories exist # Ensure directories exist
mkdir -p "$QUEUE_DIR" mkdir -p "$LOG_DIR" "$QUEUE_DIR"
# Store PID # Store PID
echo "$$" > "$PID_FILE" echo "$$" > "$PID_FILE"
chmod 644 "$PID_FILE" chmod 644 "$PID_FILE"
# Function to log messages # Function to log messages - now uses centralized logging
log_message() { log_message() {
local level="${2:-info}" local level="${2:-info}"
local message="$1" local message="$1"
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
# Use centralized logging # Use centralized logging
case "$level" in case "$level" in
"error") "error")
qm_log_error "service" "$SCRIPT_NAME" "$message" qm_log_error "service" "$SCRIPT_NAME_LOG" "$message"
;; ;;
"warn") "warn")
qm_log_warn "service" "$SCRIPT_NAME" "$message" qm_log_warn "service" "$SCRIPT_NAME_LOG" "$message"
;; ;;
"debug") "debug")
qm_log_debug "service" "$SCRIPT_NAME" "$message" qm_log_debug "service" "$SCRIPT_NAME_LOG" "$message"
;; ;;
*) *)
qm_log_info "service" "$SCRIPT_NAME" "$message" qm_log_info "service" "$SCRIPT_NAME_LOG" "$message"
;; ;;
esac esac
# Also log to system log for important messages # Also maintain system logging for compatibility
if [ "$level" = "error" ] || [ "$level" = "warn" ] || [ "$level" = "info" ]; then logger -t quecwatch -p "daemon.$level" "$message"
logger -t quecwatch -p "daemon.$level" "$message"
fi # Log to file (maintain existing behavior)
echo "[$timestamp] [$level] $message" >> "$LOG_FILE"
} }
# Function to update status # Function to update status