unblock.command.sh
· 3.9 KiB · Bash
Исходник
#!/usr/bin/env bash
# ============================================
# Lifecycle
# ============================================
function cmd::unblock::on_load() {
flag::register --name
flag::register --type
flag::register --force
flag::register --quiet
flag::register --ip
flag::register --port
flag::register --proto
flag::register --subnet
flag::register --all
}
# ============================================
# Help
# ============================================
function cmd::unblock::help() {
cat <<EOF
Usage: wgctl unblock --name <name> [options]
Remove block rules for a client.
Options:
--name <name> Client name (e.g. phone-nuno)
--ip <ip> Unblock specific IP (repeatable)
--subnet <cidr> Unblock specific subnet (repeatable)
--port <ip:port:proto> Unblock specific port (repeatable)
--all Remove all block rules for this client
Examples:
wgctl unblock --name phone-nuno --all
wgctl unblock --name phone-nuno --ip 10.0.0.210
wgctl unban --name phone-nuno --all
EOF
}
# ============================================
# Helpers
# ============================================
function cmd::unblock::get_client_ip() {
local name="$1"
local conf
conf="$(ctx::clients)/${name}.conf"
if [[ ! -f "$conf" ]]; then
log::error "Client not found: ${name}"
return 1
fi
grep "^Address" "$conf" | awk '{print $3}' | cut -d'/' -f1
}
# ============================================
# Unblock Run
# ============================================
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" && [[ ! -f "$(ctx::block::path "${name}.block")" ]]; 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=$(cmd::unblock::get_client_ip "$name") || return 1
# $quiet || log::section "Unblocking client: ${name} (${client_ip})"
if $all; then
fw::unblock_all "$client_ip"
fw::remove_block_file "$name"
monitor::unwatch_client "$name"
# Re-add peer to server if missing
if ! peers::exists_in_server "$name"; then
local public_key
public_key=$(keys::public "$name") || return 1
peers::add_to_server "$name" "$public_key" "$client_ip"
peers::reload
fi
$quiet || log::wg_success "${name} has been unblocked."
return 0
fi
# Unblock specific IPs
for ip in "${ips[@]}"; do
fw::unblock_ip "$client_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}"
}
| 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 | |
| 19 | # ============================================ |
| 20 | # Help |
| 21 | # ============================================ |
| 22 | |
| 23 | function cmd::unblock::help() { |
| 24 | cat <<EOF |
| 25 | Usage: wgctl unblock --name <name> [options] |
| 26 | |
| 27 | Remove block rules for a client. |
| 28 | |
| 29 | Options: |
| 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 | |
| 36 | Examples: |
| 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 |
| 40 | EOF |
| 41 | } |
| 42 | |
| 43 | # ============================================ |
| 44 | # Helpers |
| 45 | # ============================================ |
| 46 | |
| 47 | function 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 | |
| 64 | function 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 |