gistfile1.txt
· 2.0 KiB · Text
Bruto
function cmd::unblock::run() {
local name=""
local type=""
local ips=()
local subnets=()
local ports=()
local all=false
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 ;;
--all) all=true; shift ;;
--help) cmd::unblock::help; return ;;
*)
log::error "Unknown flag: $1"
cmd::unblock::help
return 1
;;
esac
done
if [[ -z "$name" ]]; then
log::error "Missing required flag: --name"
cmd::unblock::help
return 1
fi
name=$(peers::resolve_and_require "$name" "$type") || return 1
# Check if actually blocked
if ! peers::is_blocked "$name" && ! block::has_file "$name"; then
log::wg_warning "Client is not blocked: ${name}"
return 0
fi
# Default to full unblock if no specific flags given
if [[ ${#ips[@]} -eq 0 && ${#subnets[@]} -eq 0 && ${#ports[@]} -eq 0 ]]; then
all=true
fi
local client_ip
client_ip=$(peers::get_ip "$name") || return 1
# $quiet || log::section "Unblocking client: ${name} (${client_ip})"
if $all; then
cmd::unblock::_unblock_all "$name" "$client_ip" "$quiet"
return 0
fi
# Unblock specific IPs
for ip in "${ips[@]}"; do
fw::unblock_ip "$client_ip" "$ip"
block::remove_rule "$name" "ip" "$ip"
done
# Unblock specific subnets
for subnet in "${subnets[@]}"; do
fw::unblock_subnet "$client_ip" "$subnet"
done
# Unblock specific ports
for entry in "${ports[@]}"; do
local target port proto
IFS=":" read -r target port proto <<< "$entry"
proto="${proto:-tcp}"
fw::unblock_port "$client_ip" "$target" "$port" "$proto"
done
$quiet || log::wg_success "Unblock rules applied for: ${name}"
return 0
}
| 1 | function cmd::unblock::run() { |
| 2 | local name="" |
| 3 | local type="" |
| 4 | local ips=() |
| 5 | local subnets=() |
| 6 | local ports=() |
| 7 | local all=false |
| 8 | local quiet=false |
| 9 | |
| 10 | while [[ $# -gt 0 ]]; do |
| 11 | case "$1" in |
| 12 | --name) name="$2"; shift 2 ;; |
| 13 | --type) type="$2"; shift 2 ;; |
| 14 | --ip) ips+=("$2"); shift 2 ;; |
| 15 | --force) force=true; shift ;; |
| 16 | --quiet) quiet=true; shift ;; |
| 17 | --subnet) subnets+=("$2"); shift 2 ;; |
| 18 | --port) ports+=("$2"); shift 2 ;; |
| 19 | --all) all=true; shift ;; |
| 20 | --help) cmd::unblock::help; return ;; |
| 21 | *) |
| 22 | log::error "Unknown flag: $1" |
| 23 | cmd::unblock::help |
| 24 | return 1 |
| 25 | ;; |
| 26 | esac |
| 27 | done |
| 28 | |
| 29 | if [[ -z "$name" ]]; then |
| 30 | log::error "Missing required flag: --name" |
| 31 | cmd::unblock::help |
| 32 | return 1 |
| 33 | fi |
| 34 | |
| 35 | name=$(peers::resolve_and_require "$name" "$type") || return 1 |
| 36 | |
| 37 | # Check if actually blocked |
| 38 | if ! peers::is_blocked "$name" && ! block::has_file "$name"; then |
| 39 | log::wg_warning "Client is not blocked: ${name}" |
| 40 | return 0 |
| 41 | fi |
| 42 | |
| 43 | # Default to full unblock if no specific flags given |
| 44 | if [[ ${#ips[@]} -eq 0 && ${#subnets[@]} -eq 0 && ${#ports[@]} -eq 0 ]]; then |
| 45 | all=true |
| 46 | fi |
| 47 | |
| 48 | local client_ip |
| 49 | client_ip=$(peers::get_ip "$name") || return 1 |
| 50 | |
| 51 | # $quiet || log::section "Unblocking client: ${name} (${client_ip})" |
| 52 | |
| 53 | if $all; then |
| 54 | cmd::unblock::_unblock_all "$name" "$client_ip" "$quiet" |
| 55 | return 0 |
| 56 | fi |
| 57 | |
| 58 | # Unblock specific IPs |
| 59 | for ip in "${ips[@]}"; do |
| 60 | fw::unblock_ip "$client_ip" "$ip" |
| 61 | block::remove_rule "$name" "ip" "$ip" |
| 62 | done |
| 63 | |
| 64 | # Unblock specific subnets |
| 65 | for subnet in "${subnets[@]}"; do |
| 66 | fw::unblock_subnet "$client_ip" "$subnet" |
| 67 | done |
| 68 | |
| 69 | # Unblock specific ports |
| 70 | for entry in "${ports[@]}"; do |
| 71 | local target port proto |
| 72 | IFS=":" read -r target port proto <<< "$entry" |
| 73 | proto="${proto:-tcp}" |
| 74 | fw::unblock_port "$client_ip" "$target" "$port" "$proto" |
| 75 | done |
| 76 | |
| 77 | $quiet || log::wg_success "Unblock rules applied for: ${name}" |
| 78 | |
| 79 | return 0 |
| 80 | } |