nuno ревизій цього gist 1 month ago. До ревизії
1 file changed, 109 insertions
gistfile1.txt(файл створено)
| @@ -0,0 +1,109 @@ | |||
| 1 | + | function cmd::block::run() { | |
| 2 | + | local name="" | |
| 3 | + | local type="" | |
| 4 | + | local ips=() | |
| 5 | + | local subnets=() | |
| 6 | + | local ports=() | |
| 7 | + | local quiet=false | |
| 8 | + | ||
| 9 | + | while [[ $# -gt 0 ]]; do | |
| 10 | + | case "$1" in | |
| 11 | + | --name) name="$2"; shift 2 ;; | |
| 12 | + | --type) type="$2"; shift 2 ;; | |
| 13 | + | --ip) ips+=("$2"); shift 2 ;; | |
| 14 | + | --force) force=true; shift ;; | |
| 15 | + | --quiet) quiet=true; shift ;; | |
| 16 | + | --subnet) subnets+=("$2"); shift 2 ;; | |
| 17 | + | --port) ports+=("$2"); shift 2 ;; | |
| 18 | + | --help) cmd::block::help; return ;; | |
| 19 | + | *) | |
| 20 | + | log::error "Unknown flag: $1" | |
| 21 | + | cmd::block::help | |
| 22 | + | return 1 | |
| 23 | + | ;; | |
| 24 | + | esac | |
| 25 | + | done | |
| 26 | + | ||
| 27 | + | if [[ -z "$name" ]]; then | |
| 28 | + | log::error "Missing required flag: --name" | |
| 29 | + | cmd::block::help | |
| 30 | + | return 1 | |
| 31 | + | fi | |
| 32 | + | ||
| 33 | + | name=$(peers::resolve_and_require "$name" "$type") || return 1 | |
| 34 | + | ||
| 35 | + | # Check if actually blocked | |
| 36 | + | if peers::is_blocked "$name" || [[ -f "$(ctx::block::path "${name}.block")" ]]; then | |
| 37 | + | log::wg_warning "Client is already blocked: ${name}" | |
| 38 | + | return 0 | |
| 39 | + | fi | |
| 40 | + | ||
| 41 | + | # Update cache first | |
| 42 | + | monitor::update_endpoint_cache | |
| 43 | + | ||
| 44 | + | local public_key | |
| 45 | + | public_key=$(keys::public "$name") || return 1 | |
| 46 | + | ||
| 47 | + | local endpoint | |
| 48 | + | endpoint=$(monitor::endpoint_for_key "$public_key") | |
| 49 | + | ||
| 50 | + | # Fall back to cache if live endpoint not available | |
| 51 | + | if [[ -z "$endpoint" || "$endpoint" == "(none)" ]]; then | |
| 52 | + | endpoint=$(monitor::get_cached_endpoint "$name") | |
| 53 | + | fi | |
| 54 | + | ||
| 55 | + | local client_ip | |
| 56 | + | client_ip=$(cmd::block::get_client_ip "$name") || return 1 | |
| 57 | + | ||
| 58 | + | # $quiet || log::section "Blocking client: ${name} (${client_ip})" | |
| 59 | + | ||
| 60 | + | # No specific target — block everything | |
| 61 | + | if [[ ${#ips[@]} -eq 0 && ${#subnets[@]} -eq 0 && ${#ports[@]} -eq 0 ]]; then | |
| 62 | + | # Get real endpoint IP before removing peer from server | |
| 63 | + | local public_key endpoint | |
| 64 | + | public_key=$(keys::public "$name") || return 1 | |
| 65 | + | endpoint=$(monitor::endpoint_for_key "$public_key") | |
| 66 | + | ||
| 67 | + | fw::block_all "$client_ip" "$name" | |
| 68 | + | fw::save_block "$name" "$client_ip" | |
| 69 | + | ||
| 70 | + | # Watch real endpoint IP if available | |
| 71 | + | if [[ -n "$endpoint" ]]; then | |
| 72 | + | monitor::unwatch "$client_ip" # remove tunnel IP if added by block_all | |
| 73 | + | monitor::watch "$endpoint" "$name" | |
| 74 | + | fi | |
| 75 | + | ||
| 76 | + | # Remove peer from server to kill active connection | |
| 77 | + | peers::remove_from_server "$name" | |
| 78 | + | peers::reload | |
| 79 | + | ||
| 80 | + | $quiet || log::wg_success "${name} has been blocked." | |
| 81 | + | return 0 | |
| 82 | + | fi | |
| 83 | + | ||
| 84 | + | # Block specific IPs | |
| 85 | + | for ip in "${ips[@]}"; do | |
| 86 | + | ip::require_valid "$ip" | |
| 87 | + | fw::block_ip "$client_ip" "$ip" | |
| 88 | + | fw::save_block "$name" "$client_ip" "$ip" | |
| 89 | + | done | |
| 90 | + | ||
| 91 | + | # Block specific subnets | |
| 92 | + | for subnet in "${subnets[@]}"; do | |
| 93 | + | ip::require_valid "$subnet" | |
| 94 | + | fw::block_subnet "$client_ip" "$subnet" | |
| 95 | + | fw::save_block "$name" "$client_ip" "$subnet" | |
| 96 | + | done | |
| 97 | + | ||
| 98 | + | # Block specific ports | |
| 99 | + | for entry in "${ports[@]}"; do | |
| 100 | + | local target port proto | |
| 101 | + | IFS=":" read -r target port proto <<< "$entry" | |
| 102 | + | proto="${proto:-tcp}" | |
| 103 | + | ip::require_valid "$target" | |
| 104 | + | fw::block_port "$client_ip" "$target" "$port" "$proto" | |
| 105 | + | fw::save_block "$name" "$client_ip" "$target" "$port" "$proto" | |
| 106 | + | done | |
| 107 | + | ||
| 108 | + | log::debug "Block rules applied for: ${name}" | |
| 109 | + | } | |
Новіше
Пізніше