[CORRECTION] Merging changes for 2.2.4 release

This commit is contained in:
Russel Yasol
2025-07-27 08:03:27 +08:00
parent 73bb838b38
commit 5db402136c
101 changed files with 460 additions and 238 deletions

View File

@@ -2,6 +2,7 @@
# AT Queue Client for OpenWRT
# Located in /www/cgi-bin/services/at_queue_client
AUTH_FILE="/tmp/auth_success"
QUEUE_DIR="/tmp/at_queue"
RESULTS_DIR="$QUEUE_DIR/results"
QUEUE_MANAGER="/www/cgi-bin/services/at_queue_manager.sh"
@@ -184,10 +185,35 @@ if [ "${SCRIPT_NAME}" != "" ]; then
# Output headers only once at the beginning
echo "Content-Type: application/json"
echo ""
# Get Token from Authorization Header
TOKEN="${HTTP_AUTHORIZATION}"
if [ ! -f $AUTH_FILE ]; then
output_json "{\"error\":\"Unauthenticated Request\"}" "0"
exit 1
fi
if [ -z "$TOKEN" ] || "${TOKEN}" = "" || [ $(grep "${TOKEN}" "${AUTH_FILE}" | wc -l) -eq 0 ]; then
output_json "{\"error\":\"Not Authorized\"}" "0"
exit 1
fi
# Check if token is within 2 hours
TOKEN_LINE=$(grep "${TOKEN}" "${AUTH_FILE}")
TOKEN_DATE=$(echo "$TOKEN_LINE" | awk '{print $1}' | sed 's/T/ /')
TOKEN_TIME=$(date -d "$TOKEN_DATE" +%s 2>/dev/null)
NOW_TIME=$(date +%s)
MAX_AGE=$((2 * 3600)) # 2 hours in seconds
if [ -z "$TOKEN_TIME" ] || [ $((NOW_TIME - TOKEN_TIME)) -gt $MAX_AGE ]; then
output_json "{\"error\":\"Token expired\"}" "0"
# Cleanup/Remove token from file
sed -i -e "s/.*${TOKEN}.*//g" /tmp/auth_success 2>/dev/null
exit 1
fi
# Parse query string
eval $(echo "$QUERY_STRING" | sed 's/&/;/g')
# Handle different actions
if [ -n "$command_id" ]; then
# Get result for specific command ID
@@ -196,13 +222,13 @@ if [ "${SCRIPT_NAME}" != "" ]; then
# URL decode and normalize the command
command=$(urldecode "$command")
command=$(normalize_at_command "$command")
# Check if it's a valid AT command
if echo "$command" | grep -qi "^AT"; then
# Submit command and get response
response=$(submit_command "$command")
cmd_id=$(get_command_id "$response")
if [ "$wait" = "1" ]; then
if [ -n "$cmd_id" ]; then
wait_for_completion "$cmd_id" "${timeout:-180}" "0" # Don't show headers

View File

@@ -1,8 +1,8 @@
#!/bin/sh
#!/bin/bash
# Set content-type for JSON response
echo "Content-type: application/json"
echo ""
printf "Content-type: application/json\r\n"
printf "\r\n"
# Define paths and constants to match queue system
QUEUE_DIR="/tmp/at_queue"
@@ -13,11 +13,11 @@ TOKEN_FILE="$QUEUE_DIR/token"
# Logging function (minimized)
log_message() {
# Only log errors and critical info
if [ "$1" = "error" ] || [ "$1" = "crit" ]; then
if [ "$1" = "error" ] || [ "$1" = "crit" ]; then
logger -t at_queue -p "daemon.$1" "$2"
fi
fi
}
mkdir -m755 -p ${QUEUE_DIR}
# Enhanced JSON string escaping function
escape_json() {
printf '%s' "$1" | awk '
@@ -36,39 +36,46 @@ escape_json() {
# Acquire token directly (avoid CGI overhead)
acquire_token() {
local priority="${1:-10}"
local max_attempts=10
local attempt=0
priority="${1:-10}"
max_attempts=10
attempt=0
log_message "debug" "Acquiring token"
while [ $attempt -lt $max_attempts ]; do
# Check if token file exists
if [ -f "$TOKEN_FILE" ]; then
local current_holder=$(cat "$TOKEN_FILE" | jsonfilter -e '@.id' 2>/dev/null)
local current_priority=$(cat "$TOKEN_FILE" | jsonfilter -e '@.priority' 2>/dev/null)
local timestamp=$(cat "$TOKEN_FILE" | jsonfilter -e '@.timestamp' 2>/dev/null)
local current_time=$(date +%s)
current_holder=$(cat "$TOKEN_FILE" | jsonfilter -e '@.id' 2>/dev/null)
current_priority=$(cat "$TOKEN_FILE" | jsonfilter -e '@.priority' 2>/dev/null)
timestamp=$(cat "$TOKEN_FILE" | jsonfilter -e '@.timestamp' 2>/dev/null)
current_time=$(date +%s)
log_message "info" "current_holder: ${current_holder}"
log_message "info" "current_priority: ${current_priority}"
log_message "info" "timestamp: ${timestamp}"
log_message "info" "current_time: ${current_time}"
# Check for expired token (> 30 seconds old)
if [ $((current_time - timestamp)) -gt 30 ] || [ -z "$current_holder" ]; then
# Remove expired token
log_message "debug" "Removing token, cur time minus timestamp gt 30 or current-holder not set"
rm -f "$TOKEN_FILE" 2>/dev/null
elif [ $priority -lt $current_priority ]; then
# Preempt lower priority token
log_message "debug" "Current priority lower priority than other task"
rm -f "$TOKEN_FILE" 2>/dev/null
else
# Try again
sleep 0.1
attempt=$((attempt + 1))
log_message "debug" "Trying again $attempt"
continue
fi
else
log_message "debug" "No token file"
fi
# Try to create token file
echo "{\"id\":\"$LOCK_ID\",\"priority\":$priority,\"timestamp\":$(date +%s)}" >"$TOKEN_FILE" 2>/dev/null
printf "{\"id\":\"$LOCK_ID\",\"priority\":$priority,\"timestamp\":$(date +%s)}" >"$TOKEN_FILE" 2>/dev/null
chmod 644 "$TOKEN_FILE" 2>/dev/null
# Verify we got the token
local holder=$(cat "$TOKEN_FILE" 2>/dev/null | jsonfilter -e '@.id' 2>/dev/null)
holder=$(cat "$TOKEN_FILE" 2>/dev/null | jsonfilter -e '@.id' 2>/dev/null)
if [ "$holder" = "$LOCK_ID" ]; then
return 0
fi
@@ -79,13 +86,16 @@ acquire_token() {
return 1
}
# Release token directly
release_token() {
log_message "debug" "Release Token"
# Only remove if it's our token
if [ -f "$TOKEN_FILE" ]; then
local current_holder=$(cat "$TOKEN_FILE" | jsonfilter -e '@.id' 2>/dev/null)
log_message "debug" "Has Token file"
current_holder=$(cat "$TOKEN_FILE" | jsonfilter -e '@.id' 2>/dev/null)
log_message "debug" "Release Token, Current Holder: ${current_holder}"
if [ "$current_holder" = "$LOCK_ID" ]; then
log_message "debug" "Release Token, Current Holder: ${current_holder}, removing token"
rm -f "$TOKEN_FILE" 2>/dev/null
fi
fi
@@ -93,18 +103,21 @@ release_token() {
# Direct AT command execution with minimal overhead
execute_at_command() {
local CMD="$1"
CMD="$1"
sms_tool at "$CMD" -t 3 2>/dev/null
}
# Batch process all commands with a single token
process_all_commands() {
local commands="$1"
local priority="${2:-10}"
local first=1
commands="$1"
priority="${2:-10}"
first=1
log_message "info" "Before acquire_token check"
acquire_token "$priority"
trying=$?
log_message "debug" "trying: ${trying}"
# Acquire a single token for all commands
if ! acquire_token "$priority"; then
if [ $trying -ne 0 ]; then
log_message "error" "Failed to acquire token for batch processing"
# Return all failed responses
printf '['
@@ -115,7 +128,7 @@ process_all_commands() {
ESCAPED_CMD=$(escape_json "$cmd")
printf '{"command":"%s","response":"Failed to acquire token","status":"error"}' "${ESCAPED_CMD}"
done
printf ']\n'
printf ']\r\n'
return 1
fi
@@ -124,10 +137,9 @@ process_all_commands() {
for cmd in $commands; do
[ $first -eq 0 ] && printf ','
first=0
OUTPUT=$(execute_at_command "$cmd")
local CMD_STATUS=$?
CMD_STATUS=$?
log_message "debug" "CMD: ${cmd}, OUTPUT: ${OUTPUT}, CMD_STAT: ${CMD_STATUS}"
ESCAPED_CMD=$(escape_json "$cmd")
ESCAPED_OUTPUT=$(escape_json "$OUTPUT")
@@ -140,8 +152,7 @@ process_all_commands() {
"${ESCAPED_CMD}"
fi
done
printf ']\n'
printf ']\r\n'
# Release token after all commands are done
release_token
return 0
@@ -184,15 +195,14 @@ if echo "$COMMANDS" | grep -qi "AT+QSCAN"; then
PRIORITY=1
fi
# Process commands with timeout protection
(
sleep 60
kill -TERM $$ 2>/dev/null
) &
TIMEOUT_PID=$!
# (
# sleep 60
# kill -TERM $$
# ) &
# TIMEOUT_PID=$!
process_all_commands "$COMMANDS" "$PRIORITY"
process_all_commands "$COMMANDS" "$PRIORITY"
# kill $TIMEOUT_PID 2>/dev/null
release_token
# Clean up
kill $TIMEOUT_PID 2>/dev/null
release_token

View File

@@ -9,7 +9,7 @@ read -r POST_DATA
# Debug log for generated hash
DEBUG_LOG="/tmp/auth.log"
AUTH_FILE="/tmp/auth_success"
# Extract the password from POST data (URL encoded)
USER="root"
INPUT_PASSWORD=$(echo "$POST_DATA" | grep -o 'password=[^&]*' | cut -d= -f2-)
@@ -56,7 +56,34 @@ printf "Generated hash: %s\n" "$GENERATED_HASH" >> "$DEBUG_LOG"
# Compare the generated hash with the one in the shadow file
if [ "$GENERATED_HASH" = "$USER_HASH" ]; then
echo '{"state":"success"}'
TOKEN=$(head -c 16 /dev/urandom | hexdump -v -e '/1 "%02x"')
CREATED_DATE=$(date +"%Y-%m-%dT%H:%M:%S")
touch ${AUTH_FILE}
echo "${CREATED_DATE} ${TOKEN}" >> ${AUTH_FILE}
echo "" >> ${AUTH_FILE}
echo "{\"state\":\"success\",\"token\":\"${TOKEN}\"}"
else
# Remove token from file
if [ -n ${TOKEN} ]; then
sed -i -e "s/.*${TOKEN}.*//g" ${AUTH_FILE} 2>/dev/null
fi
echo '{"state":"failed", "message":"Authentication failed"}'
fi
fi
# AUTH_FILE cleanup process, Remove any token lines older than 2 hours from AUTH_FILE
MAX_AGE=$((2 * 3600)) # 2 hours in seconds
NOW_TIME=$(date +%s)
TMP_FILE=$(mktemp)
while read -r line; do
if [ -n "$(echo "$line" | tr -d '[:space:]')" ]; then
# Extract the date from the line and convert it to a timestamp
TOKEN_DATE=$(echo "$line" | awk '{print $1}' | sed 's/T/ /')
TOKEN_TIME=$(date -d "$TOKEN_DATE" +%s 2>/dev/null)
# If date is valid and not older than MAX_AGE, keep the line
if [ -n "$TOKEN_TIME" ] && [ $((NOW_TIME - TOKEN_TIME)) -le $MAX_AGE ]; then
echo "$line" >> "$TMP_FILE"
fi
fi
done < "$AUTH_FILE"
mv "$TMP_FILE" "$AUTH_FILE"

View File

@@ -0,0 +1,99 @@
#!/bin/sh
# Set content type to JSON
echo "Content-type: application/json"
echo ""
# Configuration
QUEUE_DIR="/tmp/at_queue"
RESULTS_DIR="$QUEUE_DIR/results"
RESULT_FILE="/tmp/qscan_result.json"
PID_FILE="/tmp/cell_scan.pid"
TOKEN_FILE="$QUEUE_DIR/token"
# Function to log messages
log_message() {
local level="${2:-info}"
logger -t at_queue -p "daemon.$level" "check_scan: $1"
}
# Function to output JSON response
output_json() {
local status="$1"
local message="$2"
if [ "$status" = "success" ] && [ -f "$RESULT_FILE" ]; then
# Return the contents of the result file
cat "$RESULT_FILE"
else
printf '{"status":"%s","message":"%s","timestamp":"","output":""}\n' "$status" "$message"
fi
}
# Check for scan token holder
check_token_holder() {
if [ -f "$TOKEN_FILE" ]; then
local current_holder=$(cat "$TOKEN_FILE" | jsonfilter -e '@.id' 2>/dev/null)
if [ -n "$current_holder" ] && echo "$current_holder" | grep -q "CELL_SCAN"; then
log_message "Cell scan token is active: $current_holder" "debug"
return 0
fi
fi
return 1
}
# Check if a scan is already in progress
check_scan_progress() {
# First check PID file
if [ -f "$PID_FILE" ]; then
pid=$(cat "$PID_FILE")
if kill -0 "$pid" 2>/dev/null; then
log_message "Scan in progress (PID: $pid)" "info"
output_json "running" "Scan in progress"
exit 0
else
log_message "Removing stale PID file" "warn"
rm -f "$PID_FILE"
fi
fi
# Also check token holder
if check_token_holder; then
log_message "Scan in progress (Token active)" "info"
output_json "running" "Scan in progress (Token active)"
exit 0
fi
}
# Check for existing results
check_results() {
if [ -f "$RESULT_FILE" ]; then
rm -f "$RESULT_FILE" # Remove the result file if it exists
log_message "Result file removed" "info"
output_json "success" "Scan results removed"
exit 0
else
log_message "No result file found to clear" "info"
output_json "success" "No result file to clear"
exit 0
fi
}
# Main execution
{
# First check if a scan is in progress
check_scan_progress
# Then check for existing results
check_results
# If no results and no running scan, indicate idle state
log_message "No active scan or recent results" "info"
output_json "success" "No active scan"
exit 0
} || {
# Error handler
log_message "Failed to remove scan results" "error"
output_json "error" "Failed to remove scan results"
exit 1
}

View File

@@ -0,0 +1,15 @@
#!/bin/sh
# Get token from Request Header Authorization
USER_TOKEN="${HTTP_AUTHORIZATION}"
# Remove token from file
sed -i -e "s/.*${USER_TOKEN}.*//g" /tmp/auth_success 2>/dev/null
echo "Content-Type: application/json"
echo "Cache-Control: no-cache, no-store, must-revalidate"
echo "Pragma: no-cache"
echo "Expires: 0"
echo ""
echo '{"state":"success", "message":"Logged out successfully"}'