OpenVPNInstaller/install.sh
2025-04-14 15:55:32 -04:00

106 lines
3.0 KiB
Bash

#!/bin/bash
# Exit on error
set -e
# Function to print colored output
print_message() {
echo -e "\033[1;32m$1\033[0m"
}
print_error() {
echo -e "\033[1;31m$1\033[0m"
}
# Check if running on Linux
if [[ "$OSTYPE" != "linux-gnu"* ]]; then
print_error "This script is only compatible with Linux systems."
exit 1
fi
# Add a check to see if the user is root
if [ "$EUID" -ne 0 ]; then
print_error "Please run as root."
echo "sudo ./install.sh"
exit 1
fi
# Check if apt-get is available
if ! command -v apt-get &> /dev/null; then
print_error "This script requires apt-get package manager."
print_error "Please use a Debian-based distribution or modify the script for your package manager."
exit 1
fi
# Check if systemd is available
if ! command -v systemctl &> /dev/null; then
print_error "This script requires systemd."
exit 1
fi
print_message "Updating package lists..."
sudo apt-get update
# Check if OpenVPN is already installed
if ! command -v openvpn &> /dev/null; then
print_message "Installing OpenVPN..."
sudo apt-get install -y openvpn
else
print_message "OpenVPN is already installed."
fi
# Ask user for ovpn file location
read -p "Enter the path to your .ovpn file: " ovpn_path
# Check if the file exists
if [ ! -f "$ovpn_path" ]; then
print_error "File does not exist: $ovpn_path"
exit 1
fi
# Check if the file is a valid .ovpn file
if [[ ! "$ovpn_path" =~ \.ovpn$ ]]; then
print_error "The file must have a .ovpn extension."
exit 1
fi
# Create the client directory if it doesn't exist
if [ ! -d "/etc/openvpn/client" ]; then
print_message "Creating OpenVPN client directory..."
sudo mkdir -p /etc/openvpn/client
fi
# Get filename without path and change extension to .conf
filename=$(basename "$ovpn_path")
config_name="${filename%.ovpn}"
conf_filename="$config_name.conf"
# Check if a configuration with the same name already exists
if [ -f "/etc/openvpn/client/$conf_filename" ]; then
read -p "A configuration with this name already exists. Overwrite? (y/n): " overwrite
if [[ ! "$overwrite" =~ ^[Yy]$ ]]; then
print_error "Installation aborted."
exit 1
fi
fi
print_message "Copying and converting configuration file..."
sudo cp "$ovpn_path" "/etc/openvpn/client/$conf_filename"
# Set proper permissions
sudo chmod 600 "/etc/openvpn/client/$conf_filename"
sudo chown root:root "/etc/openvpn/client/$conf_filename"
print_message "Starting OpenVPN service..."
if ! sudo systemctl start "openvpn-client@$config_name"; then
print_error "Failed to start OpenVPN service."
print_error "Please check your configuration file and try again."
exit 1
fi
print_message "Enabling OpenVPN service to start on boot..."
sudo systemctl enable "openvpn-client@$config_name"
print_message "Installation completed successfully!"
print_message "Your OpenVPN connection should now be active."
print_message "You can check the status with: sudo systemctl status openvpn-client@$config_name"