gistfile1.txt
· 3.0 KiB · Text
Исходник
function cmd::block::run() {
local name=""
local type=""
local ips=()
local subnets=()
local ports=()
local quiet=false
while [[ $# -gt 0 ]]; do
case "$1" in
--name) name="$2"; shift 2 ;;
--type) type="$2"; shift 2 ;;
--ip) ips+=("$2"); shift 2 ;;
--force) force=true; shift ;;
--quiet) quiet=true; shift ;;
--subnet) subnets+=("$2"); shift 2 ;;
--port) ports+=("$2"); shift 2 ;;
--help) cmd::block::help; return ;;
*)
log::error "Unknown flag: $1"
cmd::block::help
return 1
;;
esac
done
if [[ -z "$name" ]]; then
log::error "Missing required flag: --name"
cmd::block::help
return 1
fi
name=$(peers::resolve_and_require "$name" "$type") || return 1
# Check if actually blocked
if peers::is_blocked "$name" || [[ -f "$(ctx::block::path "${name}.block")" ]]; then
log::wg_warning "Client is already blocked: ${name}"
return 0
fi
# Update cache first
monitor::update_endpoint_cache
local public_key
public_key=$(keys::public "$name") || return 1
local endpoint
endpoint=$(monitor::endpoint_for_key "$public_key")
# Fall back to cache if live endpoint not available
if [[ -z "$endpoint" || "$endpoint" == "(none)" ]]; then
endpoint=$(monitor::get_cached_endpoint "$name")
fi
local client_ip
client_ip=$(cmd::block::get_client_ip "$name") || return 1
# $quiet || log::section "Blocking client: ${name} (${client_ip})"
# No specific target — block everything
if [[ ${#ips[@]} -eq 0 && ${#subnets[@]} -eq 0 && ${#ports[@]} -eq 0 ]]; then
# Get real endpoint IP before removing peer from server
local public_key endpoint
public_key=$(keys::public "$name") || return 1
endpoint=$(monitor::endpoint_for_key "$public_key")
fw::block_all "$client_ip" "$name"
fw::save_block "$name" "$client_ip"
# Watch real endpoint IP if available
if [[ -n "$endpoint" ]]; then
monitor::unwatch "$client_ip" # remove tunnel IP if added by block_all
monitor::watch "$endpoint" "$name"
fi
# Remove peer from server to kill active connection
peers::remove_from_server "$name"
peers::reload
$quiet || log::wg_success "${name} has been blocked."
return 0
fi
# Block specific IPs
for ip in "${ips[@]}"; do
ip::require_valid "$ip"
fw::block_ip "$client_ip" "$ip"
fw::save_block "$name" "$client_ip" "$ip"
done
# Block specific subnets
for subnet in "${subnets[@]}"; do
ip::require_valid "$subnet"
fw::block_subnet "$client_ip" "$subnet"
fw::save_block "$name" "$client_ip" "$subnet"
done
# Block specific ports
for entry in "${ports[@]}"; do
local target port proto
IFS=":" read -r target port proto <<< "$entry"
proto="${proto:-tcp}"
ip::require_valid "$target"
fw::block_port "$client_ip" "$target" "$port" "$proto"
fw::save_block "$name" "$client_ip" "$target" "$port" "$proto"
done
log::debug "Block rules applied for: ${name}"
}
| 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 | } |