Последняя активность 1 month ago

unblock.command.sh Исходник
1#!/usr/bin/env bash
2
3# ============================================
4# Lifecycle
5# ============================================
6
7function cmd::unblock::on_load() {
8 flag::register --name
9 flag::register --type
10 flag::register --force
11 flag::register --quiet
12 flag::register --ip
13 flag::register --port
14 flag::register --proto
15 flag::register --subnet
16 flag::register --all
17}
18
19# ============================================
20# Help
21# ============================================
22
23function cmd::unblock::help() {
24 cat <<EOF
25Usage: wgctl unblock --name <name> [options]
26
27Remove block rules for a client.
28
29Options:
30 --name <name> Client name (e.g. phone-nuno)
31 --ip <ip> Unblock specific IP (repeatable)
32 --subnet <cidr> Unblock specific subnet (repeatable)
33 --port <ip:port:proto> Unblock specific port (repeatable)
34 --all Remove all block rules for this client
35
36Examples:
37 wgctl unblock --name phone-nuno --all
38 wgctl unblock --name phone-nuno --ip 10.0.0.210
39 wgctl unban --name phone-nuno --all
40EOF
41}
42
43# ============================================
44# Helpers
45# ============================================
46
47function cmd::unblock::get_client_ip() {
48 local name="$1"
49 local conf
50 conf="$(ctx::clients)/${name}.conf"
51
52 if [[ ! -f "$conf" ]]; then
53 log::error "Client not found: ${name}"
54 return 1
55 fi
56
57 grep "^Address" "$conf" | awk '{print $3}' | cut -d'/' -f1
58}
59
60# ============================================
61# Unblock Run
62# ============================================
63
64function cmd::unblock::run() {
65 local name=""
66 local type=""
67 local ips=()
68 local subnets=()
69 local ports=()
70 local all=false
71 local quiet=false
72
73 while [[ $# -gt 0 ]]; do
74 case "$1" in
75 --name) name="$2"; shift 2 ;;
76 --type) type="$2"; shift 2 ;;
77 --ip) ips+=("$2"); shift 2 ;;
78 --force) force=true; shift ;;
79 --quiet) quiet=true; shift ;;
80 --subnet) subnets+=("$2"); shift 2 ;;
81 --port) ports+=("$2"); shift 2 ;;
82 --all) all=true; shift ;;
83 --help) cmd::unblock::help; return ;;
84 *)
85 log::error "Unknown flag: $1"
86 cmd::unblock::help
87 return 1
88 ;;
89 esac
90 done
91
92 if [[ -z "$name" ]]; then
93 log::error "Missing required flag: --name"
94 cmd::unblock::help
95 return 1
96 fi
97
98 name=$(peers::resolve_and_require "$name" "$type") || return 1
99
100 # Check if actually blocked
101 if ! peers::is_blocked "$name" && [[ ! -f "$(ctx::block::path "${name}.block")" ]]; then
102 log::wg_warning "Client is not blocked: ${name}"
103 return 0
104 fi
105
106 # Default to full unblock if no specific flags given
107 if [[ ${#ips[@]} -eq 0 && ${#subnets[@]} -eq 0 && ${#ports[@]} -eq 0 ]]; then
108 all=true
109 fi
110
111 local client_ip
112 client_ip=$(cmd::unblock::get_client_ip "$name") || return 1
113
114 # $quiet || log::section "Unblocking client: ${name} (${client_ip})"
115
116 if $all; then
117 fw::unblock_all "$client_ip"
118 fw::remove_block_file "$name"
119 monitor::unwatch_client "$name"
120
121 # Re-add peer to server if missing
122 if ! peers::exists_in_server "$name"; then
123 local public_key
124 public_key=$(keys::public "$name") || return 1
125 peers::add_to_server "$name" "$public_key" "$client_ip"
126 peers::reload
127 fi
128
129 $quiet || log::wg_success "${name} has been unblocked."
130 return 0
131 fi
132
133 # Unblock specific IPs
134 for ip in "${ips[@]}"; do
135 fw::unblock_ip "$client_ip" "$ip"
136 done
137
138 # Unblock specific subnets
139 for subnet in "${subnets[@]}"; do
140 fw::unblock_subnet "$client_ip" "$subnet"
141 done
142
143 # Unblock specific ports
144 for entry in "${ports[@]}"; do
145 local target port proto
146 IFS=":" read -r target port proto <<< "$entry"
147 proto="${proto:-tcp}"
148 fw::unblock_port "$client_ip" "$target" "$port" "$proto"
149 done
150
151 $quiet || log::wg_success "Unblock rules applied for: ${name}"
152}
153