nuno hat die Gist bearbeitet 1 month ago. Zu Änderung gehen
1 file changed, 115 insertions
gistfile1.txt(Datei erstellt)
| @@ -0,0 +1,115 @@ | |||
| 1 | + | function cmd::block::run() { | |
| 2 | + | local name="" | |
| 3 | + | local type="" | |
| 4 | + | local block_name="" | |
| 5 | + | local ips=() | |
| 6 | + | local subnets=() | |
| 7 | + | local ports=() | |
| 8 | + | local quiet=false | |
| 9 | + | local services=() | |
| 10 | + | ||
| 11 | + | while [[ $# -gt 0 ]]; do | |
| 12 | + | case "$1" in | |
| 13 | + | --name) name="$2"; shift 2 ;; | |
| 14 | + | --type) type="$2"; shift 2 ;; | |
| 15 | + | --ip) ips+=("$2"); shift 2 ;; | |
| 16 | + | --block-name) block_name="$2"; shift 2 ;; | |
| 17 | + | --service) services+=("$2"); shift 2 ;; | |
| 18 | + | --force) force=true; shift ;; | |
| 19 | + | --quiet) quiet=true; shift ;; | |
| 20 | + | --subnet) subnets+=("$2"); shift 2 ;; | |
| 21 | + | --port) ports+=("$2"); shift 2 ;; | |
| 22 | + | --help) cmd::block::help; return ;; | |
| 23 | + | *) | |
| 24 | + | log::error "Unknown flag: $1" | |
| 25 | + | cmd::block::help | |
| 26 | + | return 1 | |
| 27 | + | ;; | |
| 28 | + | esac | |
| 29 | + | done | |
| 30 | + | ||
| 31 | + | if [[ -z "$name" ]]; then | |
| 32 | + | log::error "Missing required flag: --name" | |
| 33 | + | cmd::block::help | |
| 34 | + | return 1 | |
| 35 | + | fi | |
| 36 | + | ||
| 37 | + | name=$(peers::resolve_and_require "$name" "$type") || return 1 | |
| 38 | + | ||
| 39 | + | # Check if actually blocked | |
| 40 | + | if peers::is_blocked "$name" || block::has_file "$name"; then | |
| 41 | + | log::wg_warning "Client is already blocked: ${name}" | |
| 42 | + | return 0 | |
| 43 | + | fi | |
| 44 | + | ||
| 45 | + | # Update cache first | |
| 46 | + | monitor::update_endpoint_cache | |
| 47 | + | ||
| 48 | + | local public_key | |
| 49 | + | public_key=$(keys::public "$name") || return 1 | |
| 50 | + | ||
| 51 | + | local endpoint | |
| 52 | + | endpoint=$(cmd::block::_get_endpoint "$name" "$public_key") | |
| 53 | + | ||
| 54 | + | local client_ip | |
| 55 | + | client_ip=$(peers::get_ip "$name") || return 1 | |
| 56 | + | ||
| 57 | + | # No specific target — block everything | |
| 58 | + | # Only full block if no specific targets provided | |
| 59 | + | if [[ ${#ips[@]} -eq 0 && ${#ports[@]} -eq 0 && ${#subnets[@]} -eq 0 ]]; then | |
| 60 | + | cmd::block::_block_all "$name" "$client_ip" "$quiet" | |
| 61 | + | return 0 | |
| 62 | + | fi | |
| 63 | + | ||
| 64 | + | # Specific rules — don't full block | |
| 65 | + | block::set_direct "$name" "$client_ip" "false" # ensure not marked as full block | |
| 66 | + | ||
| 67 | + | # Block specific IPs | |
| 68 | + | for ip in "${ips[@]}"; do | |
| 69 | + | ip::require_valid "$ip" | |
| 70 | + | fw::block_ip "$client_ip" "$ip" | |
| 71 | + | block::add_rule "$name" "$client_ip" "ip" "" "$ip" | |
| 72 | + | done | |
| 73 | + | ||
| 74 | + | # Block specific subnets | |
| 75 | + | for subnet in "${subnets[@]}"; do | |
| 76 | + | ip::require_valid "$subnet" | |
| 77 | + | fw::block_subnet "$client_ip" "$subnet" | |
| 78 | + | block::add_rule "$name" "$client_ip" "subnet" "" "$target_ip" | |
| 79 | + | done | |
| 80 | + | ||
| 81 | + | # Block specific ports | |
| 82 | + | for entry in "${ports[@]}"; do | |
| 83 | + | local target port proto | |
| 84 | + | IFS=":" read -r target port proto <<< "$entry" | |
| 85 | + | ip::require_valid "$target" | |
| 86 | + | ||
| 87 | + | fw::block_port "$client_ip" "$target" "$port" "${proto:-tcp}" | |
| 88 | + | block::add_rule "$name" "$client_ip" "port" "" "$target" "$port" "${proto:-tcp}" | |
| 89 | + | done | |
| 90 | + | ||
| 91 | + | for svc in "${services[@]}"; do | |
| 92 | + | local resolved_lines=() | |
| 93 | + | mapfile -t resolved_lines < <(net::resolve "$svc" 2>/dev/null) | |
| 94 | + | if [[ ${#resolved_lines[@]} -eq 0 ]]; then | |
| 95 | + | log::error "Service not found or has no ports: ${svc}" | |
| 96 | + | return 1 | |
| 97 | + | fi | |
| 98 | + | for resolved in "${resolved_lines[@]}"; do | |
| 99 | + | if [[ "$resolved" == *:*:* ]]; then | |
| 100 | + | # ip:port:proto | |
| 101 | + | local b_ip b_port b_proto | |
| 102 | + | IFS=":" read -r b_ip b_port b_proto <<< "$resolved" | |
| 103 | + | fw::block_port "$client_ip" "$b_ip" "$b_port" "$b_proto" | |
| 104 | + | block::add_rule "$name" "$client_ip" "port" "$svc" \ | |
| 105 | + | "$b_ip" "$b_port" "$b_proto" | |
| 106 | + | else | |
| 107 | + | # just ip | |
| 108 | + | fw::block_ip "$client_ip" "$resolved" | |
| 109 | + | block::add_rule "$name" "$client_ip" "ip" "$svc" "$resolved" | |
| 110 | + | fi | |
| 111 | + | done | |
| 112 | + | done | |
| 113 | + | ||
| 114 | + | log::debug "Block rules applied for: ${name}" | |
| 115 | + | } | |
Neuer
Älter