#!/bin/bash
# Farben
GREEN="\e[32m"
RED="\e[31m"
YELLOW="\e[33m"
RESET="\e[0m"
# Verzeichnisse
OVPN_DIR="/etc/openvpn"
CONFIG_DIR="/home/ubuntu/udp/"
# Prüfen, ob laufende OpenVPN-Instanzen vorhanden sind und stoppen
function stop_running_instances {
running_instances=$(systemctl list-units --type=service --no-pager --no-legend | awk '{print $1}' | grep '^openvpn@.*\.service$')
if [ -z "$running_instances" ]; then
echo -e "${RED}Keine laufenden OpenVPN-Instanzen gefunden.${RESET}"
else
for instance in $running_instances; do
instance_name=$(echo $instance | cut -d'@' -f2 | cut -d'.' -f1)
conf_file="$OVPN_DIR/$instance_name.conf"
echo -e "${YELLOW}Stopping OpenVPN instance: $instance${RESET}"
sudo systemctl stop "$instance"
if [ -f "$conf_file" ]; then
echo -e "${YELLOW}Moving $conf_file back to $CONFIG_DIR${RESET}"
sudo mv -f "$conf_file" "$CONFIG_DIR"
fi
done
fi
# Sicherstellen, dass alle übrig gebliebenen .conf-Dateien verschoben werden
for conf in "$OVPN_DIR"/*.conf; do
if [ -f "$conf" ]; then
echo -e "${YELLOW}Forcing move of leftover $conf to $CONFIG_DIR${RESET}"
sudo mv -f "$conf" "$CONFIG_DIR"
fi
done
}
# Funktion zum Prüfen, ob eine IP-Adresse anpingbar ist
function is_ip_pingable {
ping -c 1 -W 1 "$1" &> /dev/null
return $?
}
# Hauptfunktion
function main {
# 1. Laufende OpenVPN-Instanzen beenden
stop_running_instances
# 2. Prüfen, welche .conf-Dateien anpingbare IPs enthalten
reachable_configs=()
conf_files=("$CONFIG_DIR"/*.conf)
for conf in "${conf_files[@]}"; do
declare -A checked_ips=()
ping_success=false
while IFS= read -r line; do
if [[ $line == remote* ]]; then
ip=$(echo "$line" | awk '{print $2}')
if [[ -n $ip && -z ${checked_ips[$ip]} ]]; then
if is_ip_pingable "$ip"; then
conf_name=$(basename "$conf")
echo -e "${GREEN}$conf_name has at least one reachable IP!${RESET}"
reachable_configs+=("$conf")
ping_success=true
break
fi
checked_ips[$ip]=1
fi
fi
done < "$conf"
if [ "$ping_success" = false ]; then
conf_name=$(basename "$conf")
echo -e "${RED}No reachable IP found in $conf_name.${RESET}"
fi
done
# 3. Prüfen, ob überhaupt anpingbare Konfigurationen gefunden wurden
if [ ${#reachable_configs[@]} -eq 0 ]; then
echo -e "${RED}Keine anpingbaren .conf-Dateien gefunden.${RESET}"
exit 1
fi
# 4. Hauptschleife zur Serverauswahl
echo "Verfügbare OpenVPN-Konfigurationsdateien (mit erreichbaren IPs):"
select conf in "${reachable_configs[@]##*/}" "Abbrechen"; do
if [ "$conf" == "Abbrechen" ]; then
echo -e "${YELLOW}Abbruch durch den Benutzer.${RESET}"
exit 0
fi
if [ -n "$conf" ]; then
instance=$(basename "$conf" .conf)
echo -e "${YELLOW}Copying $CONFIG_DIR/$instance.conf to $OVPN_DIR${RESET}"
sudo cp "$CONFIG_DIR/$instance.conf" "$OVPN_DIR/$instance.conf"
echo -e "${YELLOW}Starting OpenVPN instance: $instance${RESET}"
sudo systemctl start openvpn@$instance
if systemctl is-active --quiet openvpn@$instance; then
echo -e "${GREEN}OpenVPN instance $instance started successfully!${RESET}"
# Warten vor dem IP-Check
echo -ne "Waiting 10 seconds before checking public IP address: "
for i in {10..1}; do
echo -ne "$i " # Zeigt den Countdown an
sleep 1
done
# Öffentliche IP-Adresse überprüfen
echo "Checking current public IP address..."
ip_check=$(curl -s --max-time 10
https://checkip.perfect-privacy.com/csv)
if [[ -z "$ip_check" ]]; then
echo -e "${RED}Failed to retrieve public IP address. Please check your connection.${RESET}"
echo -e "${YELLOW}Stopping OpenVPN instance: $instance${RESET}"
sudo systemctl stop openvpn@$instance
# .conf-Datei zurück verschieben
echo -e "${YELLOW}Moving $OVPN_DIR/$instance.conf back to $CONFIG_DIR${RESET}"
sudo mv -f "$OVPN_DIR/$instance.conf" "$CONFIG_DIR"
exit 1
fi
# Überprüfen, ob der DNS-Eintrag zu *.perfect-privacy.com gehört
dns=$(echo "$ip_check" | awk -F',' '{print $2}')
if [[ $dns == *".perfect-privacy.com"* ]]; then
echo -e "${GREEN}Public IP check successful!${RESET}"
echo "Current public IP address: $ip_check"
exit 0
else
echo -e "${RED}Public IP check failed. The current server is not suitable.${RESET}"
echo -e "${YELLOW}Stopping OpenVPN instance: $instance${RESET}"
sudo systemctl stop openvpn@$instance
# .conf-Datei zurück verschieben
echo -e "${YELLOW}Moving $OVPN_DIR/$instance.conf back to $CONFIG_DIR${RESET}"
sudo mv -f "$OVPN_DIR/$instance.conf" "$CONFIG_DIR"
fi
else
echo -e "${RED}Failed to start OpenVPN instance $instance.${RESET}"
fi
else
echo -e "${RED}Ungültige Auswahl. Bitte erneut versuchen.${RESET}"
fi
done
}
# Aufruf der Hauptfunktion
main