nuno revised this gist 1 month ago. Go to revision
1 file changed, 92 insertions
remove.command.sh(file created)
| @@ -0,0 +1,92 @@ | |||
| 1 | + | #!/usr/bin/env bash | |
| 2 | + | ||
| 3 | + | # ============================================ | |
| 4 | + | # Lifecycle | |
| 5 | + | # ============================================ | |
| 6 | + | ||
| 7 | + | function cmd::remove::on_load() { | |
| 8 | + | flag::register --name | |
| 9 | + | flag::register --type | |
| 10 | + | flag::register --force | |
| 11 | + | } | |
| 12 | + | ||
| 13 | + | # ============================================ | |
| 14 | + | # Help | |
| 15 | + | # ============================================ | |
| 16 | + | ||
| 17 | + | function cmd::remove::help() { | |
| 18 | + | cat <<EOF | |
| 19 | + | Usage: wgctl remove --name <name> [options] | |
| 20 | + | ||
| 21 | + | Permanently remove a WireGuard client. | |
| 22 | + | This will delete the client config, keys, and remove it from the server. | |
| 23 | + | ||
| 24 | + | Options: | |
| 25 | + | --name <name> Full client name (e.g. phone-nuno) | |
| 26 | + | --force Skip confirmation prompt | |
| 27 | + | ||
| 28 | + | Examples: | |
| 29 | + | wgctl remove --name phone-nuno | |
| 30 | + | wgctl rm --name phone-nuno --force | |
| 31 | + | EOF | |
| 32 | + | } | |
| 33 | + | ||
| 34 | + | # ============================================ | |
| 35 | + | # Run | |
| 36 | + | # ============================================ | |
| 37 | + | ||
| 38 | + | function cmd::remove::run() { | |
| 39 | + | local name="" type="" force=false | |
| 40 | + | ||
| 41 | + | while [[ $# -gt 0 ]]; do | |
| 42 | + | case "$1" in | |
| 43 | + | --name) name="$2"; shift 2 ;; | |
| 44 | + | --type) type="$2"; shift 2 ;; | |
| 45 | + | --force) force=true; shift ;; | |
| 46 | + | --help) cmd::remove::help; return ;; | |
| 47 | + | *) | |
| 48 | + | log::error "Unknown flag: $1" | |
| 49 | + | cmd::remove::help | |
| 50 | + | return 1 | |
| 51 | + | ;; | |
| 52 | + | esac | |
| 53 | + | done | |
| 54 | + | ||
| 55 | + | if [[ -z "$name" ]]; then | |
| 56 | + | log::error "Missing required flag: --name" | |
| 57 | + | cmd::remove::help | |
| 58 | + | return 1 | |
| 59 | + | fi | |
| 60 | + | ||
| 61 | + | name=$(peers::resolve_and_require "$name" "$type") || return 1 | |
| 62 | + | ||
| 63 | + | if ! $force; then | |
| 64 | + | read -r -p "Are you sure you want to permanently remove '${name}'? [y/N] " confirm | |
| 65 | + | case "$confirm" in | |
| 66 | + | [yY][eE][sS]|[yY]) ;; | |
| 67 | + | *) | |
| 68 | + | log::info "Aborted" | |
| 69 | + | return 0 | |
| 70 | + | ;; | |
| 71 | + | esac | |
| 72 | + | fi | |
| 73 | + | ||
| 74 | + | log::section "Removing client: ${name}" | |
| 75 | + | ||
| 76 | + | local client_ip was_blocked=false | |
| 77 | + | client_ip=$(peers::get_ip "$name") | |
| 78 | + | peers::is_blocked "$name" && was_blocked=true | |
| 79 | + | ||
| 80 | + | peers::purge "$name" "$client_ip" "$was_blocked" || return 1 | |
| 81 | + | ||
| 82 | + | # Detach from identity after successful removal | |
| 83 | + | identity::auto_detach "$name" | |
| 84 | + | ||
| 85 | + | log::wg_success "Client removed: ${name}" | |
| 86 | + | } | |
| 87 | + | ||
| 88 | + | # _cleanup kept as a shim — callers should prefer peers::purge directly | |
| 89 | + | function cmd::remove::_cleanup() { | |
| 90 | + | local name="${1:-}" client_ip="${2:-}" was_blocked="${3:-false}" | |
| 91 | + | peers::purge "$name" "$client_ip" "$was_blocked" | |
| 92 | + | } | |
Newer
Older