Template
91 lines
2.8 KiB
Bash
91 lines
2.8 KiB
Bash
#!/bin/bash
|
|
|
|
# ==============================================================================
|
|
# 1. SETTINGS & GLOBALS
|
|
# ==============================================================================
|
|
# Exit immediately if a command exits with a non-zero status
|
|
set -e
|
|
|
|
# Determine the directory where this script resides
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
cd "$SCRIPT_DIR"
|
|
|
|
# Global variables
|
|
server_pubkey=""
|
|
ips=""
|
|
|
|
# ==============================================================================
|
|
# 2. HELPER FUNCTIONS
|
|
# ==============================================================================
|
|
# Load environment variable of the script
|
|
environment_var() {
|
|
echo "--- Starting Environment & Pre-flight Checks ---"
|
|
# --- .env LOADING ---
|
|
# This safely loads the variables from .env without touching other files
|
|
if [ -f "$SCRIPT_DIR/.env" ]; then
|
|
set -a
|
|
source "$SCRIPT_DIR/.env"
|
|
set +a
|
|
echo "[✔] Environment variables loaded."
|
|
else
|
|
echo "[!] No .env file found. Using defaults."
|
|
fi
|
|
|
|
# --- Variable Checks ---
|
|
server_pubkey=$(cat ${WG0_DIR}/publickey)
|
|
ips=$(grep -i '^\s*AllowedIPs\s*=\s*' "${WG0_DIR}/wg0.conf" | sed 's/^\s*AllowedIPs\s*=\s*//I; s/\s*$//')
|
|
}
|
|
|
|
# Find the last registered client IP
|
|
find_last_ip() {
|
|
# Find the last ip an create the next ip
|
|
if [ -n "$ips" ]; then
|
|
last=$(printf '%s\n' "$ips" | tail -1)
|
|
addr=${last%/*} # strip /32 if present
|
|
IFS=. read -r a b c d <<< "$addr"
|
|
next_ip="10.0.0.$((d + 1))"
|
|
printf 'Last CIDR: %s\nNext IP: %s/32\n' "$last" "$next_ip"
|
|
else
|
|
next_ip="10.0.0.2"
|
|
printf 'No peers found, starting at %s/32\n' "$next_ip"
|
|
fi
|
|
}
|
|
|
|
# Show the template of the client's configuration file
|
|
client_show_template() {
|
|
# Show template from .client_template file
|
|
template_file="${TOOLS_DIR}/client_template"
|
|
|
|
if [ -f "$template_file" ]; then
|
|
printf "\n--- Wireguard client template (generated from %s) ---\n" "$template_file"
|
|
|
|
# Process template: replace placeholders
|
|
sed -e "s|<%CLIENT_IP%>|${next_ip}|g" \
|
|
-e "s|<%SERVER_PUBLIC_KEY%>|${server_pubkey}|g" \
|
|
-e "s|<%DOMAIN_NAME%>|${DOMAIN_NAME}|g" \
|
|
"$template_file"
|
|
else
|
|
printf "\n--- Error: Template file %s not found ---\n" "$template_file"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# ==============================================================================
|
|
# 3. MAIN LOGIC
|
|
# ==============================================================================
|
|
main() {
|
|
# Load environment variables first
|
|
environment_var
|
|
|
|
# Print current values
|
|
printf 'Public Key of the server: %s\n' "$server_pubkey"
|
|
printf 'List of current clients: %s\n' "$ips"
|
|
|
|
# Find the last IP
|
|
find_last_ip
|
|
# Show client template
|
|
client_show_template
|
|
}
|
|
|
|
# Invoke main
|
|
main "$@" |