最終更新 1 month ago

nuno revised this gist 1 month ago. Go to revision

1 file changed, 120 insertions

rename.command.sh(file created)

@@ -0,0 +1,120 @@
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 + # Unapply rule if assigned
86 + local assigned_rule
87 + assigned_rule=$(peers::get_meta "$name" "rule")
88 +
89 + if [[ -z "$assigned_rule" ]]; then
90 + assigned_rule=$(peers::default_rule "$name")
91 + fi
92 +
93 + # Flush all iptables rules for this peer IP
94 + if [[ -n "$client_ip" ]]; then
95 + fw::flush_peer "$client_ip"
96 + fi
97 +
98 + # Remove peer from server config
99 + peers::remove_from_server "$name" || return 1
100 +
101 + # Remove client config
102 + peers::remove_client_config "$name" || return 1
103 +
104 + # Remove keys
105 + keys::remove "$name" || return 1
106 +
107 + # Remove block rules only if client was fully blocked
108 + if [[ -n "$client_ip" ]] && $was_blocked; then
109 + fw::unblock_all "$client_ip"
110 + fi
111 +
112 + fw::remove_block_file "$name" 2>/dev/null || true
113 +
114 + peers::remove_meta "$name" 2>/dev/null || true
115 +
116 + # Reload WireGuard
117 + peers::reload || return 1
118 +
119 + log::wg_success "Client removed: ${name}"
120 + }
Newer Older