/
/
/
1#!/bin/bash
2# ==============================================================================
3# WireGuard Client Manager Script
4# ==============================================================================
5#
6# Description: Manages WireGuard client configurations for the wg-easy service
7# Usage: ./wireguard-client-manager.sh [list|add|remove|backup|restore]
8#
9# This script is automatically generated by Ansible - DO NOT EDIT MANUALLY
10# Template: wireguard-client-manager.sh.j2
11#
12# ==============================================================================
13
14set -euo pipefail
15
16# Configuration
17WG_CONFIG_DIR="{{ docker_base_path }}/wireguard/config"
18WG_DB_FILE="${WG_CONFIG_DIR}/wg-easy.db"
19BACKUP_DIR="{{ docker_base_path }}/wireguard/backups"
20LOG_FILE="/var/log/wireguard-client-manager.log"
21
22# Ensure directories exist
23mkdir -p "${BACKUP_DIR}"
24
25# Logging function
26log() {
27 echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "${LOG_FILE}"
28}
29
30# Error handling function
31error_exit() {
32 log "ERROR: $1"
33 exit 1
34}
35
36# Check if WireGuard service is running
37check_service_running() {
38 if docker ps --format "table {{.Names}}" | grep -q "{{ wireguard_container_name }}"; then
39 return 0
40 else
41 error_exit "WireGuard service is not running"
42 fi
43}
44
45# List all clients
46list_clients() {
47 log "Listing all WireGuard clients"
48 if [[ -f "${WG_DB_FILE}" ]]; then
49 sqlite3 "${WG_DB_FILE}" "SELECT name, address, created_at FROM clients ORDER BY created_at;"
50 else
51 error_exit "WireGuard database file not found at ${WG_DB_FILE}"
52 fi
53}
54
55# Add a new client
56add_client() {
57 local client_name="${1:-}"
58
59 if [[ -z "${client_name}" ]]; then
60 read -p "Enter client name: " client_name
61 fi
62
63 # Validate client name (alphanumeric and hyphens only)
64 if ! [[ "${client_name}" =~ ^[a-zA-Z0-9-]+$ ]]; then
65 error_exit "Client name must contain only alphanumeric characters and hyphens"
66 fi
67
68 log "Adding new client: ${client_name}"
69
70 # Use wg-easy API to add client
71 curl -s -X POST "http://localhost:{{ wireguard_web_port }}/api/clients" \
72 -H "Content-Type: application/json" \
73 -d "{\"name\": \"${client_name}\"}" \
74 | jq -r '.config' > "${WG_CONFIG_DIR}/${client_name}.conf"
75
76 if [[ $? -eq 0 ]]; then
77 log "Client ${client_name} added successfully"
78 echo "Client configuration saved to: ${WG_CONFIG_DIR}/${client_name}.conf"
79 else
80 error_exit "Failed to add client ${client_name}"
81 fi
82}
83
84# Remove a client
85remove_client() {
86 local client_name="${1:-}"
87
88 if [[ -z "${client_name}" ]]; then
89 read -p "Enter client name to remove: " client_name
90 fi
91
92 log "Removing client: ${client_name}"
93
94 # Use wg-easy API to remove client
95 curl -s -X DELETE "http://localhost:{{ wireguard_web_port }}/api/clients/${client_name}"
96
97 if [[ $? -eq 0 ]]; then
98 # Remove configuration file
99 rm -f "${WG_CONFIG_DIR}/${client_name}.conf"
100 log "Client ${client_name} removed successfully"
101 else
102 error_exit "Failed to remove client ${client_name}"
103 fi
104}
105
106# Backup client configurations
107backup_clients() {
108 local backup_file="${BACKUP_DIR}/wireguard-clients-$(date +%Y%m%d-%H%M%S).tar.gz"
109
110 log "Creating backup of WireGuard clients"
111
112 # Backup database and config files
113 tar -czf "${backup_file}" -C "${WG_CONFIG_DIR}" . 2>/dev/null || true
114
115 if [[ -f "${backup_file}" ]]; then
116 log "Backup created: ${backup_file}"
117 echo "Backup size: $(du -h "${backup_file}" | cut -f1)"
118 else
119 error_exit "Failed to create backup"
120 fi
121}
122
123# Restore from backup
124restore_backup() {
125 local backup_file="${1:-}"
126
127 if [[ -z "${backup_file}" ]]; then
128 echo "Available backups:"
129 ls -la "${BACKUP_DIR}/"*.tar.gz 2>/dev/null || echo "No backups found"
130 read -p "Enter backup file to restore: " backup_file
131 fi
132
133 if [[ ! -f "${backup_file}" ]]; then
134 error_exit "Backup file not found: ${backup_file}"
135 fi
136
137 log "Restoring from backup: ${backup_file}"
138
139 # Stop WireGuard service temporarily
140 docker stop "{{ wireguard_container_name }}" 2>/dev/null || true
141
142 # Extract backup
143 tar -xzf "${backup_file}" -C "${WG_CONFIG_DIR}" --overwrite
144
145 # Restart service
146 docker start "{{ wireguard_container_name }}" 2>/dev/null || true
147
148 log "Restore completed successfully"
149}
150
151# Show usage
152usage() {
153 cat << EOF
154WireGuard Client Manager
155
156Usage: $0 [command]
157
158Commands:
159 list List all WireGuard clients
160 add [name] Add a new client (prompts for name if not provided)
161 remove [name] Remove a client (prompts for name if not provided)
162 backup Create backup of client configurations
163 restore [file] Restore from backup file
164 help Show this help message
165
166Examples:
167 $0 list
168 $0 add my-laptop
169 $0 remove old-device
170 $0 backup
171 $0 restore /docker/wireguard/backups/wireguard-clients-20231201-120000.tar.gz
172EOF
173}
174
175# Main execution
176main() {
177 local command="${1:-help}"
178
179 case "${command}" in
180 list)
181 check_service_running
182 list_clients
183 ;;
184 add)
185 check_service_running
186 add_client "${2:-}"
187 ;;
188 remove)
189 check_service_running
190 remove_client "${2:-}"
191 ;;
192 backup)
193 backup_clients
194 ;;
195 restore)
196 restore_backup "${2:-}"
197 ;;
198 help|*)
199 usage
200 ;;
201 esac
202}
203
204# Run main function with all arguments
205main "$@"