203 lines
No EOL
5.5 KiB
Bash
203 lines
No EOL
5.5 KiB
Bash
#!/bin/bash
|
|
|
|
#-----------------------------------#
|
|
# VARIABLES #
|
|
#-----------------------------------#
|
|
|
|
this_script_url="https://git.hhf.technology/hhf/script-management-cloudpanel/raw/branch/main/maintenance/crowdsec-removal.sh"
|
|
this_script_name="CrowdSec Removal Script"
|
|
formatter_url="https://git.hhf.technology/hhf/TaskFormatter/raw/branch/main/bash_task_formatter/task_formatter.sh"
|
|
scriptname=$0
|
|
|
|
# Initialize success flag
|
|
success=0
|
|
|
|
# Determine the user (use the first argument if provided, otherwise fallback)
|
|
USER_TO_RUN_AS="${1:-$SUDO_USER}"
|
|
USER_HOME=$(eval echo ~$USER_TO_RUN_AS)
|
|
|
|
#-----------------------------------#
|
|
# FORMATTER #
|
|
#-----------------------------------#
|
|
|
|
# Download and source the formatter with error handling
|
|
download_formatter() {
|
|
if [ ! -f "task_formatter.sh" ]; then
|
|
if ! wget "$formatter_url" --no-check-certificate -O task_formatter.sh > /dev/null 2>&1; then
|
|
echo "Error: Failed to download task_formatter.sh"
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
if [ ! -f "task_formatter.sh" ]; then
|
|
echo "Error: task_formatter.sh not found after download attempt"
|
|
exit 1
|
|
fi
|
|
|
|
# Make the formatter executable
|
|
chmod +x task_formatter.sh
|
|
|
|
# Source the formatter with error checking
|
|
source ./task_formatter.sh || { echo "Error: Failed to source task_formatter.sh"; exit 1; }
|
|
|
|
# Check if print_header is available after sourcing
|
|
if ! declare -f print_header > /dev/null; then
|
|
echo "Error: print_header function not found after sourcing."
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Call the download_formatter function
|
|
download_formatter
|
|
|
|
#-----------------------------------#
|
|
# FUNCTIONS #
|
|
#-----------------------------------#
|
|
|
|
# Function to check if CrowdSec is installed
|
|
check_crowdsec_installed() {
|
|
if command -v cscli >/dev/null 2>&1 || [ -d "/etc/crowdsec" ]; then
|
|
echo "CrowdSec installation detected"
|
|
return 0
|
|
else
|
|
echo "CrowdSec is not installed"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Function to stop CrowdSec services
|
|
stop_services() {
|
|
echo "Stopping CrowdSec services..."
|
|
systemctl stop crowdsec 2>/dev/null
|
|
systemctl stop crowdsec-firewall-bouncer 2>/dev/null
|
|
systemctl stop crowdsec-cloudflare-bouncer 2>/dev/null
|
|
echo -e "Services stopped $CHECK_MARK"
|
|
return 0
|
|
}
|
|
|
|
# Function to remove CrowdSec bouncers
|
|
remove_bouncers() {
|
|
echo "Removing CrowdSec bouncers..."
|
|
|
|
# Remove firewall bouncer
|
|
if dpkg -l | grep -q crowdsec-firewall-bouncer; then
|
|
apt-get remove --purge -y crowdsec-firewall-bouncer >/dev/null 2>&1
|
|
fi
|
|
|
|
# Remove cloudflare bouncer
|
|
if dpkg -l | grep -q crowdsec-cloudflare-bouncer; then
|
|
apt-get remove --purge -y crowdsec-cloudflare-bouncer >/dev/null 2>&1
|
|
fi
|
|
|
|
# Remove bouncer configurations
|
|
rm -rf /etc/crowdsec/bouncers/ 2>/dev/null
|
|
|
|
echo -e "Bouncers removed $CHECK_MARK"
|
|
return 0
|
|
}
|
|
|
|
# Function to remove CrowdSec main package
|
|
remove_crowdsec() {
|
|
echo "Removing CrowdSec main package..."
|
|
|
|
# Remove the main package
|
|
apt-get remove --purge -y crowdsec >/dev/null 2>&1
|
|
|
|
# Remove repository configuration
|
|
rm -f /etc/apt/sources.list.d/crowdsec.list 2>/dev/null
|
|
|
|
echo -e "CrowdSec removed $CHECK_MARK"
|
|
return 0
|
|
}
|
|
|
|
# Function to clean up remaining files
|
|
cleanup_crowdsec_files() {
|
|
echo "Cleaning up remaining files..."
|
|
|
|
# Remove configuration directory
|
|
rm -rf /etc/crowdsec 2>/dev/null
|
|
|
|
# Remove data directory
|
|
rm -rf /var/lib/crowdsec 2>/dev/null
|
|
|
|
# Remove log directory
|
|
rm -rf /var/log/crowdsec 2>/dev/null
|
|
|
|
# Remove temporary files
|
|
rm -rf /tmp/crowdsec* 2>/dev/null
|
|
|
|
echo -e "Cleanup completed $CHECK_MARK"
|
|
return 0
|
|
}
|
|
|
|
# Remove the script itself
|
|
remove_script() {
|
|
if [ -f "$0" ]; then
|
|
echo "Deleted removal script..."
|
|
rm -- "$0"
|
|
fi
|
|
if [ -f "task_formatter.sh" ]; then
|
|
rm task_formatter.sh
|
|
fi
|
|
echo -e "Cleaned up $CHECK_MARK"
|
|
return 0
|
|
}
|
|
|
|
#-----------------------------------#
|
|
# MAIN LOGIC #
|
|
#-----------------------------------#
|
|
|
|
# Check if script is run as root
|
|
if [ "$EUID" -ne 0 ]; then
|
|
echo "Please run as root"
|
|
exit 1
|
|
fi
|
|
|
|
# Check if print_header function exists
|
|
if ! command -v print_header >/dev/null 2>&1; then
|
|
echo "Error: print_header function not found. Formatter may not be properly sourced."
|
|
exit 1
|
|
fi
|
|
|
|
# Print header
|
|
print_header "$this_script_name" "$this_script_url"
|
|
|
|
echo -e "Running as User: $USER_TO_RUN_AS\nUser Home: $USER_HOME\n"
|
|
|
|
# Check if CrowdSec is installed
|
|
if ! format_output check_crowdsec_installed "Checking CrowdSec Installation"; then
|
|
echo "CrowdSec is not installed. Nothing to remove."
|
|
format_output remove_script "Removing script"
|
|
exit 0
|
|
fi
|
|
|
|
# Run the removal functions with formatted output
|
|
if ! format_output stop_services "Stopping CrowdSec Services"; then
|
|
success=1
|
|
fi
|
|
|
|
if ! format_output remove_bouncers "Removing CrowdSec Bouncers"; then
|
|
success=1
|
|
fi
|
|
|
|
if ! format_output remove_crowdsec "Removing CrowdSec Main Package"; then
|
|
success=1
|
|
fi
|
|
|
|
if ! format_output cleanup_crowdsec_files "Cleaning Up Remaining Files"; then
|
|
success=1
|
|
fi
|
|
|
|
format_output remove_script "Removing script"
|
|
|
|
# Check if final_message function exists
|
|
if ! command -v final_message >/dev/null 2>&1; then
|
|
echo "Error: final_message function not found. Formatter may not be properly sourced."
|
|
exit 1
|
|
fi
|
|
|
|
# Print final message
|
|
final_message "$this_script_name" "$success"
|
|
|
|
# Exit with appropriate status
|
|
exit $success |