#!/usr/bin/env bash # Zion Peer Configuration Generator # This script generates the exact peer configuration needed for Zion set -euo pipefail # Exit on error, undefined vars, pipe failures # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # Configuration file for peer information CONFIG_FILE="$(dirname "$0")/CURRENT_WORKING/zion.conf" print_status() { echo -e "${GREEN}[INFO]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } print_header() { echo -e "${BLUE}================================${NC}" echo -e "${BLUE}Zion Peer Configuration Generator${NC}" echo -e "${BLUE}================================${NC}" } # Validate IP address format and range validate_ip() { local ip="$1" # Check basic format if [[ ! $ip =~ ^10\.8\.0\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]]; then return 1 fi # Extract last octet local last_octet="${ip##*.}" # Check if IP is in reserved ranges if [[ $last_octet -eq 0 ]] || [[ $last_octet -eq 1 ]] || [[ $last_octet -eq 255 ]]; then return 1 fi return 0 } # Validate WireGuard public key format validate_public_key() { local key="$1" # WireGuard keys are base64 encoded and exactly 44 characters long if [[ ! $key =~ ^[A-Za-z0-9+/]{43}=$ ]]; then return 1 fi return 0 } # Load Zion configuration if available load_zion_config() { if [[ -f "$CONFIG_FILE" ]]; then print_status "Found Zion configuration file: $CONFIG_FILE" return 0 else print_warning "Zion configuration file not found: $CONFIG_FILE" print_warning "Using hardcoded peer information" return 1 fi } show_usage() { echo "Usage: $0 " echo "" echo "Arguments:" echo " node_name - Name of the node (e.g., mynode)" echo " public_key - WireGuard public key (base64 encoded)" echo " ip_address - IP address in 10.8.0.x format" echo "" echo "Example:" echo " $0 mynode ABC123... 10.8.0.30" echo "" echo "This will generate the peer configuration to add to Zion's /etc/wireguard/wg0.conf" echo "" echo "Options:" echo " -c, --config FILE Use custom Zion config file" echo " -h, --help Show this help message" } main() { local config_file="$CONFIG_FILE" # Parse command line options while [[ $# -gt 0 ]]; do case $1 in -c|--config) config_file="$2" shift 2 ;; -h|--help) show_usage exit 0 ;; -*) print_error "Unknown option: $1" show_usage exit 1 ;; *) break ;; esac done print_header echo "" if [[ $# -ne 3 ]]; then print_error "Incorrect number of arguments" show_usage exit 1 fi local NODE_NAME="$1" local PUBLIC_KEY="$2" local IP_ADDRESS="$3" # Validate node name if [[ ! $NODE_NAME =~ ^[a-zA-Z0-9][a-zA-Z0-9_-]*[a-zA-Z0-9]$ ]] && [[ $NODE_NAME != [a-zA-Z0-9] ]]; then print_error "Invalid node name format. Use alphanumeric characters, hyphens, and underscores only" exit 1 fi # Validate IP address if ! validate_ip "$IP_ADDRESS"; then print_error "IP address must be in 10.8.0.x format (x cannot be 0, 1, or 255)" exit 1 fi # Validate public key if ! validate_public_key "$PUBLIC_KEY"; then print_error "Invalid WireGuard public key format" print_error "Expected: 44 character base64 string ending with =" exit 1 fi print_status "Generating Zion peer configuration for: $NODE_NAME" echo "" echo "Add the following to Zion's /etc/wireguard/wg0.conf:" echo "----------------------------------------" echo "# $NODE_NAME" echo "[Peer]" echo "PublicKey = $PUBLIC_KEY" echo "AllowedIPs = $IP_ADDRESS/32" echo "----------------------------------------" echo "" print_warning "After adding this to Zion's config:" echo "1. Save the file" echo "2. Restart Zion's WireGuard: sudo systemctl restart wg-quick@wg0" echo "3. Start the new node's WireGuard: sudo wg-quick up $NODE_NAME" echo "" # Try to load Zion config, fall back to hardcoded if not available if ! load_zion_config; then # Show hardcoded peer structure echo "Zion's current peer structure (add your peer at the end):" echo "----------------------------------------" echo "#Cth" echo "[Peer]" echo "PublicKey = NBktXKy1s0n2lIlIMODvOqKNwAtYdoZH5feKt5P43i0=" echo "AllowedIPs = 10.8.0.10/32" echo "" echo "#Aza" echo "[Peer]" echo "PublicKey = qmTKA257DLOrfhk5Zw8RyRmBSonmm6epbloT0P0ZWDc=" echo "AllowedIPs = 10.8.0.2/32" echo "" echo "#Nyar" echo "[Peer]" echo "PublicKey = 2BA7L1oJP1tK6dIUNHMgcZmOmYmlyPRe2RaBqfUsEWo=" echo "AllowedIPs = 10.8.0.20/32" echo "" echo "#Galaxy" echo "[Peer]" echo "PublicKey = QBNt00VSedxPlq3ZvsdYaqIcbudCAyxv9TG65aPVZzM=" echo "AllowedIPs = 10.8.0.99/32" echo "" echo "# Add your peer here:" echo "# $NODE_NAME" echo "# [Peer]" echo "# PublicKey = $PUBLIC_KEY" echo "# AllowedIPs = $IP_ADDRESS/32" echo "----------------------------------------" else # Parse and display current peers from config file print_status "Current peers in Zion configuration:" echo "----------------------------------------" if grep -E "^#.*" "$config_file" | grep -E "^#[A-Za-z]" | head -10; then echo "" echo "# Add your peer here:" echo "# $NODE_NAME" echo "# [Peer]" echo "# PublicKey = $PUBLIC_KEY" echo "# AllowedIPs = $IP_ADDRESS/32" else print_warning "No peer sections found in Zion config" fi echo "----------------------------------------" fi } # Run main function main "$@"