#!/bin/bash # Ensure the script is run as root if [ "$EUID" -ne 0 ]; then echo "āŒ This script must be run as root. Exiting..." exit 1 fi # Function to check and install missing dependencies check_dependencies() { local dependencies=(curl node git) local missing=() for dependency in "${dependencies[@]}"; do if ! command -v "$dependency" &> /dev/null; then missing+=("$dependency") fi done if [[ ${#missing[@]} -gt 0 ]]; then echo "🚨 Missing dependencies: ${missing[*]}" for dep in "${missing[@]}"; do read -r -p "šŸ“¦ Do you want to install $dep? [Yes/No]: " answer case $answer in [Yy]*|[Yy]es ) case $dep in curl ) apt-get install -y curl ;; node ) curl -fsSL https://deb.nodesource.com/setup_22.x -o nodesource_setup.sh && bash nodesource_setup.sh && apt-get install -y nodejs && rm -f nodesource_setup.sh ;; git ) add-apt-repository ppa:git-core/ppa && apt-get update && apt-get -y install git ;; esac ;; [Nn]*|[Nn]o ) echo "āŒ Cannot proceed without $dep. Exiting." exit 1 ;; * ) echo "āŒ Invalid input. Exiting." exit 1 ;; esac done else echo "āœ… All dependencies are installed." fi } # Check dependencies before proceeding check_dependencies # Function to validate AbuseIPDB API key validate_token() { local api_key=$1 local response response=$(curl -s -o /dev/null -w "%{http_code}" -H "Key: $api_key" https://api.abuseipdb.com/api/v2/check?ipAddress=8.8.8.8) if [[ $response -eq 200 ]]; then return 0 else echo "🚨 Invalid token. Please try again." return 1 fi } # Check for UFW log file if [[ ! -f /var/log/ufw.log ]]; then read -r -p "šŸ” /var/log/ufw.log not found. Please enter the path to your log file: " ufw_log_path if [[ -f $ufw_log_path ]]; then echo "āœ… Log file found at $ufw_log_path." else echo "āŒ Provided log file path does not exist. Exiting." exit 1 fi else ufw_log_path="/var/log/ufw.log" echo "āœ… /var/log/ufw.log exists." fi # Prompt for AbuseIPDB API token while true; do read -r -p "šŸ”‘ Please enter your AbuseIPDB API token: " api_token if validate_token "$api_token"; then break fi continue done # Prompt for server ID read -r -p "šŸ–„ļø Enter your Server ID (leave blank for null): " server_id if [[ -z $server_id ]]; then server_id=null fi # Prompt for system update and upgrade read -r -p "šŸ› ļø Do you want to update and upgrade the system (apt upgrade)? [Yes/No]: " answer case $answer in [Yy]*|[Yy]es ) echo "šŸ”§ Updating and upgrading the system..." apt-get update && apt-get upgrade -y ;; [Nn]*|[Nn]o ) echo "ā© Skipping system update and upgrade..." ;; * ) echo "āŒ Invalid input. Exiting..." exit 1 ;; esac # Clone repository cd /home || exit if [ ! -d "UFW-AbuseIPDB-Reporter" ]; then echo "šŸ“‚ Cloning the UFW-AbuseIPDB-Reporter repository..." git clone https://github.com/sefinek/UFW-AbuseIPDB-Reporter.git --branch node.js else echo "šŸ“‚ The UFW-AbuseIPDB-Reporter repository already exists. Pulling latest changes..." cd UFW-AbuseIPDB-Reporter || exit git pull fi # Install npm dependencies echo "šŸ“¦ Installing npm dependencies..." npm install --silent # Copy configuration file echo "šŸ“‘ Copying default.config.js to config.js..." cp default.config.js config.js # Update config.js with API token, Server ID, and UFW log path config_file="config.js" if [[ -f $config_file ]]; then echo "šŸ”§ Updating $PWD/$config_file..." sed -i "s|UFW_FILE: .*|UFW_FILE: '$ufw_log_path',|" $config_file sed -i "s|SERVER_ID: .*|SERVER_ID: '$server_id',|" $config_file sed -i "s|ABUSEIPDB_API_KEY: .*|ABUSEIPDB_API_KEY: '$api_token',|" $config_file else echo "āŒ $config_file not found. Make sure the repository was cloned and initialized correctly." exit 1 fi # Change permissions for UFW log file echo "šŸ”’ Changing permissions for $ufw_log_path..." chmod 644 "$ufw_log_path" # Uninstall corepack echo "šŸ—‘ļø Uninstalling corepack..." npm uninstall corepack -g --silent # Install pm2 echo "šŸ“¦ Installing pm2..." npm install pm2 -g --silent # Create logs directory echo "šŸ“‚ Creating /var/log/ufw-abuseipdb directory..." mkdir -p /var/log/ufw-abuseipdb chown "$USER":"$USER" /var/log/ufw-abuseipdb -R # Configure pm2 echo "āš™ļø Configuring pm2..." pm2 start pm2 startup # Execute the command generated by pm2 startup echo "šŸ”§ Complete pm2 startup configuration:" sudo env PATH="$PATH":/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u "$USER" --hp /home/"$USER" pm2 save # Final echo -e "\n\nšŸŽ‰ Installation and configuration completed!" echo -e "\n====================================== Summary ======================================" echo "šŸ”‘ API Token : $api_token" echo "šŸ–„ļø Server ID : ${server_id:-null}" echo "šŸ“‚ Script : $PWD" echo "āš™ļø Config File : $PWD/config.js" echo -e "\n====================================== Support ======================================" echo "šŸ“© Email : contact@sefinek.net" echo "šŸ”µ Discord : https://discord.gg/RVH8UXgmzs" echo "😺 GitHub Issues : https://github.com/sefinek/UFW-AbuseIPDB-Reporter/issues"