gistfile1.txt
· 2.6 KiB · Text
原始檔案
#!/usr/bin/env bash
# ============================================
# Lifecycle
# ============================================
function cmd::remove::on_load() {
flag::register --name
flag::register --type
flag::register --force
}
# ============================================
# Help
# ============================================
function cmd::remove::help() {
cat <<EOF
Usage: wgctl remove --name <name> [options]
Permanently remove a WireGuard client.
This will delete the client config, keys, and remove it from the server.
Options:
--name <name> Full client name (e.g. phone-nuno)
--force Skip confirmation prompt
Examples:
wgctl remove --name phone-nuno
wgctl rm --name phone-nuno --force
EOF
}
# ============================================
# Run
# ============================================
function cmd::remove::run() {
local name=""
local type=""
local force=false
while [[ $# -gt 0 ]]; do
case "$1" in
--name) name="$2"; shift 2 ;;
--type) type="$2"; shift 2 ;;
--force) force=true; shift ;;
--help) cmd::remove::help; return ;;
*)
log::error "Unknown flag: $1"
cmd::remove::help
return 1
;;
esac
done
if [[ -z "$name" ]]; then
log::error "Missing required flag: --name"
cmd::remove::help
return 1
fi
name=$(peers::resolve_and_require "$name" "$type") || return 1
# Confirmation prompt unless --force
if ! $force; then
read -r -p "Are you sure you want to permanently remove '${name}'? [y/N] " confirm
case "$confirm" in
[yY][eE][sS]|[yY]) ;;
*)
log::info "Aborted"
return 0
;;
esac
fi
log::section "Removing client: ${name}"
local client_ip
client_ip=$(peers::get_ip "$name")
local was_blocked=false
peers::is_blocked "$name" && was_blocked=true
cmd::remove::_cleanup "$name" "$client_ip" "$was_blocked" || return 1
log::wg_success "Client removed: ${name}"
}
function cmd::remove::_cleanup() {
local name="${1:-}" client_ip="${2:-}" was_blocked="${3:-false}"
[[ -n "$client_ip" ]] && fw::flush_peer "$client_ip"
peers::remove_from_server "$name" || return 1
peers::remove_client_config "$name" || return 1
keys::remove "$name" || return 1
group::remove_peer_from_all "$name" || return 1
[[ -n "$client_ip" ]] && $was_blocked && fw::unblock_all "$client_ip"
block::remove_file "$name" 2>/dev/null || true
peers::remove_meta "$name" 2>/dev/null || true
peers::reload || return 1
}
| 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="" |
| 40 | local type="" |
| 41 | local force=false |
| 42 | |
| 43 | while [[ $# -gt 0 ]]; do |
| 44 | case "$1" in |
| 45 | --name) name="$2"; shift 2 ;; |
| 46 | --type) type="$2"; shift 2 ;; |
| 47 | --force) force=true; shift ;; |
| 48 | --help) cmd::remove::help; return ;; |
| 49 | *) |
| 50 | log::error "Unknown flag: $1" |
| 51 | cmd::remove::help |
| 52 | return 1 |
| 53 | ;; |
| 54 | esac |
| 55 | done |
| 56 | |
| 57 | if [[ -z "$name" ]]; then |
| 58 | log::error "Missing required flag: --name" |
| 59 | cmd::remove::help |
| 60 | return 1 |
| 61 | fi |
| 62 | |
| 63 | name=$(peers::resolve_and_require "$name" "$type") || return 1 |
| 64 | |
| 65 | # Confirmation prompt unless --force |
| 66 | if ! $force; then |
| 67 | read -r -p "Are you sure you want to permanently remove '${name}'? [y/N] " confirm |
| 68 | case "$confirm" in |
| 69 | [yY][eE][sS]|[yY]) ;; |
| 70 | *) |
| 71 | log::info "Aborted" |
| 72 | return 0 |
| 73 | ;; |
| 74 | esac |
| 75 | fi |
| 76 | |
| 77 | log::section "Removing client: ${name}" |
| 78 | |
| 79 | local client_ip |
| 80 | client_ip=$(peers::get_ip "$name") |
| 81 | |
| 82 | local was_blocked=false |
| 83 | peers::is_blocked "$name" && was_blocked=true |
| 84 | |
| 85 | cmd::remove::_cleanup "$name" "$client_ip" "$was_blocked" || return 1 |
| 86 | log::wg_success "Client removed: ${name}" |
| 87 | } |
| 88 | |
| 89 | function cmd::remove::_cleanup() { |
| 90 | local name="${1:-}" client_ip="${2:-}" was_blocked="${3:-false}" |
| 91 | |
| 92 | [[ -n "$client_ip" ]] && fw::flush_peer "$client_ip" |
| 93 | peers::remove_from_server "$name" || return 1 |
| 94 | peers::remove_client_config "$name" || return 1 |
| 95 | keys::remove "$name" || return 1 |
| 96 | group::remove_peer_from_all "$name" || return 1 |
| 97 | |
| 98 | [[ -n "$client_ip" ]] && $was_blocked && fw::unblock_all "$client_ip" |
| 99 | block::remove_file "$name" 2>/dev/null || true |
| 100 | peers::remove_meta "$name" 2>/dev/null || true |
| 101 | peers::reload || return 1 |
| 102 | } |