chore(shell): clean up shell script formatting and structure
This commit is contained in:
@ -2,12 +2,10 @@
|
|||||||
|
|
||||||
source /etc/hysteria/core/scripts/path.sh
|
source /etc/hysteria/core/scripts/path.sh
|
||||||
|
|
||||||
# --- Configuration ---
|
|
||||||
SERVICE_NAME="hysteria-ip-limit.service"
|
SERVICE_NAME="hysteria-ip-limit.service"
|
||||||
DB_NAME="blitz_panel"
|
DB_NAME="blitz_panel"
|
||||||
CONNECTIONS_COLLECTION="active_connections"
|
CONNECTIONS_COLLECTION="active_connections"
|
||||||
|
|
||||||
# Load configurations from .configs.env
|
|
||||||
if [ -f "$CONFIG_ENV" ]; then
|
if [ -f "$CONFIG_ENV" ]; then
|
||||||
source "$CONFIG_ENV"
|
source "$CONFIG_ENV"
|
||||||
BLOCK_DURATION="${BLOCK_DURATION:-60}" # Default to 60 seconds
|
BLOCK_DURATION="${BLOCK_DURATION:-60}" # Default to 60 seconds
|
||||||
@ -19,17 +17,14 @@ else
|
|||||||
echo -e "BLOCK_DURATION=240\nMAX_IPS=5" > "$CONFIG_ENV"
|
echo -e "BLOCK_DURATION=240\nMAX_IPS=5" > "$CONFIG_ENV"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# --- Ensure files exist ---
|
|
||||||
[ ! -f "$BLOCK_LIST" ] && touch "$BLOCK_LIST"
|
[ ! -f "$BLOCK_LIST" ] && touch "$BLOCK_LIST"
|
||||||
|
|
||||||
# --- Logging function ---
|
|
||||||
log_message() {
|
log_message() {
|
||||||
local level="$1"
|
local level="$1"
|
||||||
local message="$2"
|
local message="$2"
|
||||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [$level] $message"
|
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [$level] $message"
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Add an IP to the database for a user ---
|
|
||||||
add_ip_to_db() {
|
add_ip_to_db() {
|
||||||
local username="$1"
|
local username="$1"
|
||||||
local ip_address="$2"
|
local ip_address="$2"
|
||||||
@ -44,7 +39,6 @@ add_ip_to_db() {
|
|||||||
log_message "INFO" "DB Update: Added $ip_address for user $username"
|
log_message "INFO" "DB Update: Added $ip_address for user $username"
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Remove an IP from the database for a user ---
|
|
||||||
remove_ip_from_db() {
|
remove_ip_from_db() {
|
||||||
local username="$1"
|
local username="$1"
|
||||||
local ip_address="$2"
|
local ip_address="$2"
|
||||||
@ -61,7 +55,6 @@ remove_ip_from_db() {
|
|||||||
log_message "INFO" "DB Update: Removed $ip_address for user $username"
|
log_message "INFO" "DB Update: Removed $ip_address for user $username"
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Block an IP using iptables and track it ---
|
|
||||||
block_ip() {
|
block_ip() {
|
||||||
local ip_address="$1"
|
local ip_address="$1"
|
||||||
local username="$2"
|
local username="$2"
|
||||||
@ -77,7 +70,6 @@ block_ip() {
|
|||||||
log_message "WARN" "Blocked IP $ip_address for user $username for $BLOCK_DURATION seconds"
|
log_message "WARN" "Blocked IP $ip_address for user $username for $BLOCK_DURATION seconds"
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Explicitly unblock an IP using iptables ---
|
|
||||||
unblock_ip() {
|
unblock_ip() {
|
||||||
local ip_address="$1"
|
local ip_address="$1"
|
||||||
|
|
||||||
@ -88,7 +80,6 @@ unblock_ip() {
|
|||||||
sed -i "/$ip_address,/d" "$BLOCK_LIST"
|
sed -i "/$ip_address,/d" "$BLOCK_LIST"
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Block all IPs for a user ---
|
|
||||||
block_all_user_ips() {
|
block_all_user_ips() {
|
||||||
local username="$1"
|
local username="$1"
|
||||||
|
|
||||||
@ -114,7 +105,6 @@ block_all_user_ips() {
|
|||||||
log_message "WARN" "User $username has been completely blocked for $BLOCK_DURATION seconds"
|
log_message "WARN" "User $username has been completely blocked for $BLOCK_DURATION seconds"
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Check for and unblock expired IPs ---
|
|
||||||
check_expired_blocks() {
|
check_expired_blocks() {
|
||||||
local current_time=$(date +%s)
|
local current_time=$(date +%s)
|
||||||
local ip username expiry
|
local ip username expiry
|
||||||
@ -129,7 +119,6 @@ check_expired_blocks() {
|
|||||||
done < "$BLOCK_LIST"
|
done < "$BLOCK_LIST"
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Check if a user has exceeded the IP limit ---
|
|
||||||
check_ip_limit() {
|
check_ip_limit() {
|
||||||
local username="$1"
|
local username="$1"
|
||||||
|
|
||||||
@ -154,7 +143,6 @@ check_ip_limit() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Parse log lines for connections and disconnections ---
|
|
||||||
parse_log_line() {
|
parse_log_line() {
|
||||||
local log_line="$1"
|
local log_line="$1"
|
||||||
local ip_address
|
local ip_address
|
||||||
@ -180,7 +168,6 @@ parse_log_line() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Install Systemd Service ---
|
|
||||||
install_service() {
|
install_service() {
|
||||||
cat <<EOF > /etc/systemd/system/${SERVICE_NAME}
|
cat <<EOF > /etc/systemd/system/${SERVICE_NAME}
|
||||||
[Unit]
|
[Unit]
|
||||||
@ -204,7 +191,6 @@ EOF
|
|||||||
log_message "INFO" "IP Limiter service started"
|
log_message "INFO" "IP Limiter service started"
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Uninstall Systemd Service ---
|
|
||||||
uninstall_service() {
|
uninstall_service() {
|
||||||
systemctl stop ${SERVICE_NAME} 2>/dev/null
|
systemctl stop ${SERVICE_NAME} 2>/dev/null
|
||||||
systemctl disable ${SERVICE_NAME} 2>/dev/null
|
systemctl disable ${SERVICE_NAME} 2>/dev/null
|
||||||
@ -213,7 +199,6 @@ uninstall_service() {
|
|||||||
log_message "INFO" "IP Limiter service stopped and removed"
|
log_message "INFO" "IP Limiter service stopped and removed"
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Change Configuration ---
|
|
||||||
change_config() {
|
change_config() {
|
||||||
local new_block_duration="$1"
|
local new_block_duration="$1"
|
||||||
local new_max_ips="$2"
|
local new_max_ips="$2"
|
||||||
@ -244,7 +229,6 @@ change_config() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Startup Checks ---
|
|
||||||
if [[ $EUID -ne 0 ]]; then
|
if [[ $EUID -ne 0 ]]; then
|
||||||
echo "Error: This script must be run as root."
|
echo "Error: This script must be run as root."
|
||||||
exit 1
|
exit 1
|
||||||
@ -257,7 +241,6 @@ if ! command -v jq &>/dev/null; then
|
|||||||
log_message "WARN" "'jq' is not installed. JSON parsing for blocking might fail."
|
log_message "WARN" "'jq' is not installed. JSON parsing for blocking might fail."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# --- Command execution ---
|
|
||||||
case "$1" in
|
case "$1" in
|
||||||
start)
|
start)
|
||||||
install_service
|
install_service
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
CLI_PATH="/etc/hysteria/core/cli.py"
|
CLI_PATH="/etc/hysteria/core/cli.py"
|
||||||
USERS_FILE="/etc/hysteria/users.json"
|
USERS_FILE="/etc/hysteria/users.json"
|
||||||
TRAFFIC_FILE="/etc/hysteria/traffic_data.json"
|
|
||||||
CONFIG_FILE="/etc/hysteria/config.json"
|
CONFIG_FILE="/etc/hysteria/config.json"
|
||||||
CONFIG_ENV="/etc/hysteria/.configs.env"
|
CONFIG_ENV="/etc/hysteria/.configs.env"
|
||||||
TELEGRAM_ENV="/etc/hysteria/core/scripts/telegrambot/.env"
|
TELEGRAM_ENV="/etc/hysteria/core/scripts/telegrambot/.env"
|
||||||
|
|||||||
@ -9,7 +9,6 @@ declare -a services=(
|
|||||||
"hysteria-telegram-bot.service"
|
"hysteria-telegram-bot.service"
|
||||||
"hysteria-normal-sub.service"
|
"hysteria-normal-sub.service"
|
||||||
"hysteria-caddy-normalsub.service"
|
"hysteria-caddy-normalsub.service"
|
||||||
# "hysteria-singbox.service"
|
|
||||||
"hysteria-ip-limit.service"
|
"hysteria-ip-limit.service"
|
||||||
"wg-quick@wgcf.service"
|
"wg-quick@wgcf.service"
|
||||||
)
|
)
|
||||||
|
|||||||
@ -1,7 +1,5 @@
|
|||||||
source /etc/hysteria/core/scripts/path.sh
|
source /etc/hysteria/core/scripts/path.sh
|
||||||
# source /etc/hysteria/core/scripts/services_status.sh
|
|
||||||
|
|
||||||
# Function to define colors
|
|
||||||
define_colors() {
|
define_colors() {
|
||||||
green='\033[0;32m'
|
green='\033[0;32m'
|
||||||
cyan='\033[0;36m'
|
cyan='\033[0;36m'
|
||||||
@ -81,7 +79,6 @@ load_hysteria2_ips() {
|
|||||||
IP6=$(grep -E "^IP6=" "$CONFIG_ENV" | cut -d '=' -f 2)
|
IP6=$(grep -E "^IP6=" "$CONFIG_ENV" | cut -d '=' -f 2)
|
||||||
|
|
||||||
if [[ -z "$IP4" || -z "$IP6" ]]; then
|
if [[ -z "$IP4" || -z "$IP6" ]]; then
|
||||||
# echo "Warning: IP4 or IP6 is not set in configs.env. Fetching from system..."
|
|
||||||
default_interface=$(ip route | grep default | awk '{print $5}')
|
default_interface=$(ip route | grep default | awk '{print $5}')
|
||||||
|
|
||||||
if [ -n "$default_interface" ]; then
|
if [ -n "$default_interface" ]; then
|
||||||
@ -90,7 +87,6 @@ load_hysteria2_ips() {
|
|||||||
if [ -n "$system_IP4" ]; then
|
if [ -n "$system_IP4" ]; then
|
||||||
IP4="$system_IP4"
|
IP4="$system_IP4"
|
||||||
else
|
else
|
||||||
# echo "Attempting to fetch IPv4 from external service..."
|
|
||||||
system_IP4=$(curl -s -4 ip.sb)
|
system_IP4=$(curl -s -4 ip.sb)
|
||||||
[ -n "$system_IP4" ] && IP4="$system_IP4" || IP4="None"
|
[ -n "$system_IP4" ] && IP4="$system_IP4" || IP4="None"
|
||||||
fi
|
fi
|
||||||
@ -101,13 +97,11 @@ load_hysteria2_ips() {
|
|||||||
if [ -n "$system_IP6" ]; then
|
if [ -n "$system_IP6" ]; then
|
||||||
IP6="$system_IP6"
|
IP6="$system_IP6"
|
||||||
else
|
else
|
||||||
# echo "Attempting to fetch IPv6 from external service..."
|
|
||||||
system_IP6=$(curl -s -6 ip.sb)
|
system_IP6=$(curl -s -6 ip.sb)
|
||||||
[ -n "$system_IP6" ] && IP6="$system_IP6" || IP6="None"
|
[ -n "$system_IP6" ] && IP6="$system_IP6" || IP6="None"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
# echo "Warning: Could not determine default interface, trying external services..."
|
|
||||||
if [ -z "$IP4" ]; then
|
if [ -z "$IP4" ]; then
|
||||||
system_IP4=$(curl -s -4 ip.sb)
|
system_IP4=$(curl -s -4 ip.sb)
|
||||||
[ -n "$system_IP4" ] && IP4="$system_IP4" || IP4="None"
|
[ -n "$system_IP4" ] && IP4="$system_IP4" || IP4="None"
|
||||||
@ -164,19 +158,3 @@ load_hysteria2_ips() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# check_services() {
|
|
||||||
# # source /etc/hysteria/core/scripts/services_status.sh
|
|
||||||
# for service in "${services[@]}"; do
|
|
||||||
# service_base_name=$(basename "$service" .service)
|
|
||||||
|
|
||||||
# display_name=$(echo "$service_base_name" | sed -E 's/([^-]+)-?/\u\1/g')
|
|
||||||
|
|
||||||
# if systemctl is-active --quiet "$service"; then
|
|
||||||
# echo -e "${NC}${display_name}:${green} Active${NC}"
|
|
||||||
# else
|
|
||||||
# echo -e "${NC}${display_name}:${red} Inactive${NC}"
|
|
||||||
# fi
|
|
||||||
# done
|
|
||||||
# }
|
|
||||||
|
|||||||
Reference in New Issue
Block a user