From a1598a7206f717c118eb7bd060b9f27ad01568d4 Mon Sep 17 00:00:00 2001 From: Russel Yasol Date: Fri, 31 May 2024 19:50:55 +0800 Subject: [PATCH] adding initial files for watchcat script --- simpleadmin/script/create_watchcat.sh | 60 ++++ simpleadmin/script/remove_watchcat.sh | 32 ++ simpleadmin/www/cgi-bin/get_watchcat_status | 13 +- simpleadmin/www/cgi-bin/set_ttl | 3 + simpleadmin/www/cgi-bin/watchcat_maker | 72 ++++ simpleadmin/www/scanner.html | 2 +- simpleadmin/www/watchcat.html | 255 +++++--------- simpleadmin/www/watchcat_backup.html | 351 ++++++++++++++++++++ 8 files changed, 607 insertions(+), 181 deletions(-) create mode 100644 simpleadmin/script/create_watchcat.sh create mode 100644 simpleadmin/script/remove_watchcat.sh create mode 100644 simpleadmin/www/cgi-bin/watchcat_maker create mode 100644 simpleadmin/www/watchcat_backup.html diff --git a/simpleadmin/script/create_watchcat.sh b/simpleadmin/script/create_watchcat.sh new file mode 100644 index 0000000..087f2c7 --- /dev/null +++ b/simpleadmin/script/create_watchcat.sh @@ -0,0 +1,60 @@ +#!/bin/sh + +# Function to create and run the Watchcat script +create_and_run_watchcat_script() { + local ip=$1 + local timeout=$2 + local failure_count=$3 + local script_path="/usrdata/simpleadmin/script/watchcat.sh" + + # Create the script with the watchcat logic + sudo cat << EOF > $script_path +#!/bin/sh + +failures=0 + +while :; do + if ping -c 1 $ip > /dev/null 2>&1; then + failures=0 + else + failures=\$((failures + 1)) + if [ "\$failures" -ge "$failure_count" ]; then + echo "Rebooting system due to \$failures consecutive ping failures." + /sbin/reboot + exit 0 + fi + fi + sleep $timeout +done +EOF + + # Make the watchcat script executable + chmod +x $script_path + + # Create a JSON to be fetched later + echo "{\"enabled\": true, \"track_ip\": \"$ip\", \"ping_timeout\": $timeout, \"ping_failure_count\": $failure_count}" > /usrdata/simpleadmin/script/watchcat.json + + # Check if the script was created successfully + if [ -f "$script_path" ]; then + # Make the script executable + chmod +x "$script_path" + + # Run the script in the background + # nohup /bin/sh "$script_path" & + /bin/sh "$script_path" & + + echo "Watchcat script created and running." + else + echo "Failed to create the Watchcat script." + echo "Please check the script path: $script_path" + fi +} + +# Check if the script is called with the required parameters +if [ "$#" -ne 3 ]; then + echo "Usage: $0 " + exit 1 +fi + +# Call the function with the provided arguments +create_and_run_watchcat_script "$1" "$2" "$3" \ No newline at end of file diff --git a/simpleadmin/script/remove_watchcat.sh b/simpleadmin/script/remove_watchcat.sh new file mode 100644 index 0000000..a217cb7 --- /dev/null +++ b/simpleadmin/script/remove_watchcat.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +# Function to remove the Watchcat script and JSON file +remove_watchcat_script() { + local script_path="/usrdata/simpleadmin/script/watchcat.sh" + local json_path="/usrdata/simpleadmin/script/watchcat.json" + + # Mount as read-write + mount -o remount,rw / + + # Remove the watchcat script if it exists + if [ -f "$script_path" ]; then + rm "$script_path" + echo "Removed $script_path" + else + echo "$script_path does not exist" + fi + + # Remove the JSON file if it exists + if [ -f "$json_path" ]; then + rm "$json_path" + echo "Removed $json_path" + else + echo "$json_path does not exist" + fi + + # Mount as read-only + mount -o remount,ro / +} + +# Call the function to remove the scripts +remove_watchcat_script \ No newline at end of file diff --git a/simpleadmin/www/cgi-bin/get_watchcat_status b/simpleadmin/www/cgi-bin/get_watchcat_status index 7180948..de30718 100644 --- a/simpleadmin/www/cgi-bin/get_watchcat_status +++ b/simpleadmin/www/cgi-bin/get_watchcat_status @@ -4,15 +4,14 @@ echo "Content-type: application/json" echo "" -# This script fetches the watchCat parameters from the /tmp/watchCatParams.json file and returns it as JSON +# This script fetches the watchCat parameters from the /tmp/watchcat.json and returns it as JSON +# Example content of /tmp/watchcat: +# {"watchcat": {"enabled": true, "track_ip": "1.1.1.1", "ping_timeout": 30, "ping_failure_count": 10}} # Check if the file exists -if [ -f /tmp/watchCatParams.json ]; then - # Read the file and return the content - cat /tmp/watchCatParams.json +if [ -f /tmp/watchcat.json ]; then + cat /tmp/watchcat.json else - # Return an empty JSON object + # return an empty JSON object echo "{}" fi - -exit 0 \ No newline at end of file diff --git a/simpleadmin/www/cgi-bin/set_ttl b/simpleadmin/www/cgi-bin/set_ttl index 0d6402a..ce4f910 100644 --- a/simpleadmin/www/cgi-bin/set_ttl +++ b/simpleadmin/www/cgi-bin/set_ttl @@ -48,6 +48,9 @@ if [ -n "${setTTL}" ]; then ttlenabled=false ttlvalue=0 fi + + log_debug "Starting service to apply rules" + /usrdata/simplefirewall/ttl-override start fi echo "Content-type: text/text" diff --git a/simpleadmin/www/cgi-bin/watchcat_maker b/simpleadmin/www/cgi-bin/watchcat_maker new file mode 100644 index 0000000..23ea3aa --- /dev/null +++ b/simpleadmin/www/cgi-bin/watchcat_maker @@ -0,0 +1,72 @@ +#!/bin/bash + +# Decode URL-encoded strings +function urldecode() { + local data=${1//+/ } + printf '%b' "${data//%/\\x}" +} + +# Parse QUERY_STRING +QUERY_STRING=$(echo "${QUERY_STRING}" | sed 's/;//g') +if [ "${QUERY_STRING}" ]; then + export IFS="&" + for cmd in ${QUERY_STRING}; do + if [[ "$cmd" == *"="* ]]; then + key=$(echo $cmd | awk -F '=' '{print $1}') + value=$(echo $cmd | awk -F '=' '{print $2}') + eval $key=$(urldecode $value) + fi + done +fi + +# Set default values +WATCHCAT_ENABLED=${WATCHCAT_ENABLED:-"disable"} +TRACK_IP=${TRACK_IP:-"1.1.1.1"} +PING_TIMEOUT=${PING_TIMEOUT:-30} +PING_FAILURE_COUNT=${PING_FAILURE_COUNT:-3} + +# Validate input +if ! [[ "$WATCHCAT_ENABLED" =~ ^(enable|disable)$ ]]; then + echo "Content-type: text/plain" + echo "" + echo "Invalid value for WATCHCAT_ENABLED. Use 'enable' or 'disable'." + exit 1 +fi + +if ! [[ "$TRACK_IP" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then + echo "Content-type: text/plain" + echo "" + echo "Invalid IP address format for TRACK_IP." + exit 1 +fi + +if ! [[ "$PING_TIMEOUT" =~ ^[0-9]+$ ]] || [ "$PING_TIMEOUT" -le 0 ]; then + echo "Content-type: text/plain" + echo "" + echo "PING_TIMEOUT must be a positive integer." + exit 1 +fi + +if ! [[ "$PING_FAILURE_COUNT" =~ ^[0-9]+$ ]] || [ "$PING_FAILURE_COUNT" -le 0 ]; then + echo "Content-type: text/plain" + echo "" + echo "PING_FAILURE_COUNT must be a positive integer." + exit 1 +fi + +# Implement the Watchcat logic +if [ "$WATCHCAT_ENABLED" == "enable" ]; then + echo "Content-type: text/plain" + echo "" + echo "Watchcat is enabled. Tracking IP: $TRACK_IP, Ping timeout: $PING_TIMEOUT seconds, Ping failure count: $PING_FAILURE_COUNT" + # Call the create script here and use the needed parameters + sudo /usrdata/simpleadmin/script/create_watchcat.sh "$TRACK_IP" "$PING_TIMEOUT" "$PING_FAILURE_COUNT" +else + echo "Content-type: text/plain" + echo "" + echo "Watchcat is disabled." + # Call the remove script here + sudo /usrdata/simpleadmin/script/remove_watchcat.sh +fi + +exit 0 \ No newline at end of file diff --git a/simpleadmin/www/scanner.html b/simpleadmin/www/scanner.html index f5022e8..c1f4ace 100644 --- a/simpleadmin/www/scanner.html +++ b/simpleadmin/www/scanner.html @@ -128,7 +128,7 @@ class="btn btn-primary me-md-2" type="button" x-on:click="startCellScan()" - :disabled='isLoading === true || cellScanMode === "Unspecified" || cellScanMode === "Select Scan Mode"' + :disabled="isLoading === true || cellScanMode === 'Unspecified' || cellScanMode === 'Select Scan Mode'" x-text="isCellScanning ? 'Scanning... Please wait.' : 'Start Cell Scan'" > + + + +
+
+
+
Simple Watchcat
+
+
+
+
+
+ +
+
+
+
+
+ +
+
+
+
+ +
+
+
+ +
+ +
+ +
+ +
+ +
+
+ +
+
+ +
+ +
+ +
+ +
+ +
+
+
+ + + + +
+
+ +
+
+
+
+
+
+
Simple Watchcat Logs
+
+
+
+ + +
+
+
+
+
+
+ + + + + +