nuno 已修改 1 month ago. 還原成這個修訂版本
1 file changed, 214 insertions
gistfile1.txt(檔案已創建)
| @@ -0,0 +1,214 @@ | |||
| 1 | + | #!/usr/bin/env bash | |
| 2 | + | ||
| 3 | + | # ============================================ | |
| 4 | + | # Lifecycle | |
| 5 | + | # ============================================ | |
| 6 | + | ||
| 7 | + | function 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 | + | # System - NET Services | |
| 19 | + | flag::register --service | |
| 20 | + | } | |
| 21 | + | ||
| 22 | + | # ============================================ | |
| 23 | + | # Help | |
| 24 | + | # ============================================ | |
| 25 | + | ||
| 26 | + | function cmd::unblock::help() { | |
| 27 | + | cat <<EOF | |
| 28 | + | Usage: wgctl unblock --name <name> [options] | |
| 29 | + | ||
| 30 | + | Remove block rules for a client. Without specific flags, performs a full unblock. | |
| 31 | + | Direct unblock overrides any group blocks. | |
| 32 | + | ||
| 33 | + | Options: | |
| 34 | + | --name <name> Client name (e.g. phone-nuno) | |
| 35 | + | --type <type> Device type (optional, combines with --name) | |
| 36 | + | --ip <ip> Unblock specific IP (repeatable) | |
| 37 | + | --subnet <cidr> Unblock specific subnet (repeatable) | |
| 38 | + | --port <ip:port:proto> Unblock specific port (repeatable) | |
| 39 | + | --service <name> Unblock a named service (repeatable) | |
| 40 | + | --all Remove all block rules (same as no flags) | |
| 41 | + | --quiet Suppress output (used by group unblock) | |
| 42 | + | ||
| 43 | + | Examples: | |
| 44 | + | wgctl unblock --name phone-nuno | |
| 45 | + | wgctl unblock --name nuno --type phone | |
| 46 | + | wgctl unblock --name phone-nuno --ip 10.0.0.210 | |
| 47 | + | wgctl unblock --name phone-nuno --service proxmox | |
| 48 | + | wgctl unblock --name phone-nuno --service truenas:web-ui | |
| 49 | + | wgctl unban --name phone-nuno | |
| 50 | + | EOF | |
| 51 | + | } | |
| 52 | + | ||
| 53 | + | # ============================================ | |
| 54 | + | # Unblock Run | |
| 55 | + | # ============================================ | |
| 56 | + | ||
| 57 | + | function cmd::unblock::run() { | |
| 58 | + | local name="" | |
| 59 | + | local type="" | |
| 60 | + | local ips=() | |
| 61 | + | local subnets=() | |
| 62 | + | local ports=() | |
| 63 | + | local services=() | |
| 64 | + | local all=false | |
| 65 | + | local quiet=false | |
| 66 | + | ||
| 67 | + | while [[ $# -gt 0 ]]; do | |
| 68 | + | case "$1" in | |
| 69 | + | --name) name="$2"; shift 2 ;; | |
| 70 | + | --type) type="$2"; shift 2 ;; | |
| 71 | + | --ip) ips+=("$2"); shift 2 ;; | |
| 72 | + | --force) force=true; shift ;; | |
| 73 | + | --quiet) quiet=true; shift ;; | |
| 74 | + | --subnet) subnets+=("$2"); shift 2 ;; | |
| 75 | + | --port) ports+=("$2"); shift 2 ;; | |
| 76 | + | --service) services+=("$2"); shift 2 ;; | |
| 77 | + | --all) all=true; shift ;; | |
| 78 | + | --help) cmd::unblock::help; return ;; | |
| 79 | + | *) | |
| 80 | + | log::error "Unknown flag: $1" | |
| 81 | + | cmd::unblock::help | |
| 82 | + | return 1 | |
| 83 | + | ;; | |
| 84 | + | esac | |
| 85 | + | done | |
| 86 | + | ||
| 87 | + | if [[ -z "$name" ]]; then | |
| 88 | + | log::error "Missing required flag: --name" | |
| 89 | + | cmd::unblock::help | |
| 90 | + | return 1 | |
| 91 | + | fi | |
| 92 | + | ||
| 93 | + | name=$(peers::resolve_and_require "$name" "$type") || return 1 | |
| 94 | + | ||
| 95 | + | # Check if actually blocked | |
| 96 | + | if ! peers::is_blocked "$name" && ! block::has_file "$name"; then | |
| 97 | + | log::wg_warning "Client is not blocked: ${name}" | |
| 98 | + | return 0 | |
| 99 | + | fi | |
| 100 | + | ||
| 101 | + | # Default to full unblock if no specific flags given | |
| 102 | + | if [[ ${#ips[@]} -eq 0 && ${#subnets[@]} -eq 0 && ${#ports[@]} -eq 0 && ${#services[@]} -eq 0 ]]; then | |
| 103 | + | all=true | |
| 104 | + | fi | |
| 105 | + | ||
| 106 | + | local client_ip | |
| 107 | + | client_ip=$(peers::get_ip "$name") || return 1 | |
| 108 | + | ||
| 109 | + | # $quiet || log::section "Unblocking client: ${name} (${client_ip})" | |
| 110 | + | ||
| 111 | + | if $all; then | |
| 112 | + | cmd::unblock::_unblock_all "$name" "$client_ip" "$quiet" | |
| 113 | + | return 0 | |
| 114 | + | fi | |
| 115 | + | ||
| 116 | + | # Unblock specific IPs | |
| 117 | + | for ip in "${ips[@]}"; do | |
| 118 | + | fw::unblock_ip "$client_ip" "$ip" | |
| 119 | + | block::remove_rule "$name" "ip" "$ip" | |
| 120 | + | $quiet || log::wg_success "${ip} has been unblocked for ${name}" | |
| 121 | + | done | |
| 122 | + | ||
| 123 | + | # Unblock specific subnets | |
| 124 | + | for subnet in "${subnets[@]}"; do | |
| 125 | + | fw::unblock_subnet "$client_ip" "$subnet" | |
| 126 | + | block::remove_rule "$name" "subnet" "$subnet" | |
| 127 | + | $quiet || log::wg_success "${subnet} has been unblocked for ${name}" | |
| 128 | + | done | |
| 129 | + | ||
| 130 | + | # Unblock specific ports | |
| 131 | + | for entry in "${ports[@]}"; do | |
| 132 | + | local target port proto | |
| 133 | + | IFS=":" read -r target port proto <<< "$entry" | |
| 134 | + | proto="${proto:-tcp}" | |
| 135 | + | fw::unblock_port "$client_ip" "$target" "$port" "$proto" | |
| 136 | + | block::remove_rule "$name" "port" "$b_target" "$b_port" "$b_proto" | |
| 137 | + | $quiet || log::wg_success "${client_ip}:${b_port}:${b_proto:-tcp} has been unblocked for ${name}" | |
| 138 | + | done | |
| 139 | + | ||
| 140 | + | # Unblock services | |
| 141 | + | for svc in "${services[@]}"; do | |
| 142 | + | local resolved_lines=() | |
| 143 | + | mapfile -t resolved_lines < <(net::resolve "$svc" 2>/dev/null) | |
| 144 | + | if [[ ${#resolved_lines[@]} -eq 0 ]]; then | |
| 145 | + | log::error "Service not found: ${svc}" | |
| 146 | + | return 1 | |
| 147 | + | fi | |
| 148 | + | ||
| 149 | + | # Check if actually blocked | |
| 150 | + | local is_blocked=false | |
| 151 | + | for resolved in "${resolved_lines[@]}"; do | |
| 152 | + | if [[ "$resolved" == *:*:* ]]; then | |
| 153 | + | local b_ip b_port b_proto | |
| 154 | + | IFS=":" read -r b_ip b_port b_proto <<< "$resolved" | |
| 155 | + | fw::has_block_rule "$client_ip" "$b_ip" "$b_port" "$b_proto" 2>/dev/null && \ | |
| 156 | + | { is_blocked=true; break; } | |
| 157 | + | else | |
| 158 | + | fw::has_block_rule "$client_ip" "$resolved" 2>/dev/null && \ | |
| 159 | + | { is_blocked=true; break; } | |
| 160 | + | fi | |
| 161 | + | done | |
| 162 | + | ||
| 163 | + | if ! $is_blocked; then | |
| 164 | + | $quiet || log::wg_warning "${svc} is not blocked for ${name}" | |
| 165 | + | continue | |
| 166 | + | fi | |
| 167 | + | ||
| 168 | + | for resolved in "${resolved_lines[@]}"; do | |
| 169 | + | if [[ "$resolved" == *:*:* ]]; then | |
| 170 | + | local b_ip b_port b_proto | |
| 171 | + | IFS=":" read -r b_ip b_port b_proto <<< "$resolved" | |
| 172 | + | fw::unblock_port "$client_ip" "$b_ip" "$b_port" "$b_proto" | |
| 173 | + | block::remove_rule "$name" "port" "$b_ip" "$b_port" "$b_proto" | |
| 174 | + | else | |
| 175 | + | fw::unblock_ip "$client_ip" "$resolved" | |
| 176 | + | block::remove_rule "$name" "ip" "$resolved" | |
| 177 | + | fi | |
| 178 | + | done | |
| 179 | + | ||
| 180 | + | $quiet || log::wg_success "${svc} has been unblocked for ${name}" | |
| 181 | + | done | |
| 182 | + | ||
| 183 | + | # Clean up block file if now empty | |
| 184 | + | block::cleanup "$name" | |
| 185 | + | ||
| 186 | + | return 0 | |
| 187 | + | } | |
| 188 | + | ||
| 189 | + | function cmd::unblock::_unblock_all() { | |
| 190 | + | local name="${1:?}" client_ip="${2:?}" quiet="${3:-false}" | |
| 191 | + | ||
| 192 | + | # Direct unblock overrides everything — clear all block state | |
| 193 | + | block::set_direct "$name" "$client_ip" "false" | |
| 194 | + | block::clear_full_block "$name" | |
| 195 | + | ||
| 196 | + | block::restore_peer "$name" "$client_ip" | |
| 197 | + | block::cleanup "$name" | |
| 198 | + | ||
| 199 | + | local rule | |
| 200 | + | rule=$(peers::get_meta "$name" "rule") | |
| 201 | + | ||
| 202 | + | [[ -n "$rule" ]] && rule::exists "$rule" && \ | |
| 203 | + | rule::apply "$rule" "$client_ip" "$name" | |
| 204 | + | ||
| 205 | + | local groups | |
| 206 | + | groups=$(block::get_groups "$name") | |
| 207 | + | if [[ -n "$groups" ]]; then | |
| 208 | + | log::wg_warning "${name} was blocked by group(s): ${groups} — unblocking anyway" | |
| 209 | + | fi | |
| 210 | + | ||
| 211 | + | $quiet || log::wg_success "${name} has been unblocked." | |
| 212 | + | ||
| 213 | + | return 0 | |
| 214 | + | } | |
上一頁
下一頁