From ecbe7bd861d3ad312189379340134e65868a320c Mon Sep 17 00:00:00 2001 From: Sefinek Date: Wed, 25 Dec 2024 18:07:20 +0100 Subject: [PATCH] Create install.sh (beta) --- install.sh | 176 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 install.sh diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..f11af69 --- /dev/null +++ b/install.sh @@ -0,0 +1,176 @@ +#!/bin/bash + +# 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 -p "📦 Do you want to install $dep? [Yes/No]: " answer + case $answer in + [Yy]*|[Yy]es ) + case $dep in + curl ) sudo apt-get install -y curl ;; + node ) curl -fsSL https://deb.nodesource.com/setup_22.x -o nodesource_setup.sh && sudo -E bash nodesource_setup.sh && sudo apt-get install -y nodejs ;; + git ) sudo add-apt-repository ppa:git-core/ppa && sudo apt-get update && sudo 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 -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 -p "🔑 Please enter your AbuseIPDB API token: " api_token + if validate_token "$api_token"; then + break + fi + continue +done + +# Prompt for server ID +read -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 -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..." + sudo apt-get update && sudo apt-get upgrade -y + ;; + [Nn]*|[Nn]o ) + echo "⏩ Skipping system update and upgrade..." + ;; + * ) + echo "❌ Invalid input. Exiting..." + exit 1 + ;; +esac + +# Clone repository +cd /home +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 + 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..." +sudo 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..." +sudo mkdir -p /var/log/ufw-abuseipdb +sudo 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 + +echo "🎉 Installation and configuration completed!" + + +# Final +echo -e "\n=================================== Installation 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"