#!/bin/bash # WireGuard Secure Installer # Copyright (c) 2025 Muhammad Fadhila Abiyyu Faris # GitHub: [github.com/fadhila36/wireguard-secure-installer](https://github.com/fadhila36/wireguard-secure-installer) generate_keys() { if [ -f "$WG_CONFIG" ]; then log_warn "WireGuard config already exists. Skipping key generation to prevent overwrite." # Extract existing private key for context if needed, or just return SERVER_PRIV_KEY=$(grep "PrivateKey" "$WG_CONFIG" | cut -d ' ' -f 3) SERVER_PUB_KEY=$(echo "$SERVER_PRIV_KEY" | wg pubkey) return fi log_info "Generating Server Keys..." umask 077 SERVER_PRIV_KEY=$(wg genkey) SERVER_PUB_KEY=$(echo "$SERVER_PRIV_KEY" | wg pubkey) } generate_server_config() { if [ -f "$WG_CONFIG" ]; then log_warn "WireGuard config already exists. Skipping config generation." return fi log_info "Generating Server Config..." cat > "$WG_CONFIG" <> "$LOG_FILE" 2>&1 systemctl start "wg-quick@$SERVER_WG_NIC" >> "$LOG_FILE" 2>&1 # Verify status if systemctl is-active --quiet "wg-quick@$SERVER_WG_NIC"; then log_info "WireGuard Service is RUNNING." else fatal_error "Failed to start WireGuard service." fi } create_client_config() { local CLIENT_NAME=$1 local CLIENT_IP_SUFFIX=$2 # e.g., 2 for 10.66.66.2 log_info "Creating Client: $CLIENT_NAME" CLIENT_PRIV_KEY=$(wg genkey) CLIENT_PUB_KEY=$(echo "$CLIENT_PRIV_KEY" | wg pubkey) CLIENT_PRESHARED_KEY=$(wg genpsk) # Add peer to server config cat >> "$WG_CONFIG" < "$INSTALL_DIR/clients/$CLIENT_NAME.conf" <