nuno revisou este gist 1 month ago. Ir para a revisão
1 file changed, 163 insertions
gistfile1.txt(arquivo criado)
| @@ -0,0 +1,163 @@ | |||
| 1 | + | function cmd::logs::show_fw_events() { | |
| 2 | + | local filter_ip="${1:-}" filter_name="${2:-}" filter_type="${3:-}" \ | |
| 3 | + | limit="${4:-50}" net_file="${5:-}" collapse="${6:-1}" \ | |
| 4 | + | since="${7:-}" filter_dest_ip="${8:-}" filter_dest_port="${9:-}" \ | |
| 5 | + | sort_order="${10:-desc}" resolved_only="${11:-false}" | |
| 6 | + | ||
| 7 | + | [[ ! -f "$FW_EVENTS_LOG" ]] && return 0 | |
| 8 | + | ||
| 9 | + | local data | |
| 10 | + | data=$(json::fw_events \ | |
| 11 | + | "$FW_EVENTS_LOG" "$filter_ip" "$filter_type" \ | |
| 12 | + | "$(ctx::clients)" "${net_file:-}" \ | |
| 13 | + | "$limit" "$collapse" "$since" \ | |
| 14 | + | "$filter_dest_ip" "$filter_dest_port" \ | |
| 15 | + | "$sort_order" \ | |
| 16 | + | 2>/dev/null) | |
| 17 | + | ||
| 18 | + | [[ -z "$data" ]] && return 0 | |
| 19 | + | ||
| 20 | + | # ── Pass 1: resolve endpoints and measure widths ── | |
| 21 | + | local w_client=16 w_dest=20 w_endpoint=0 | |
| 22 | + | local resolved_data="" | |
| 23 | + | ||
| 24 | + | while IFS='|' read -r ts client dest_ip dest_port proto svc count src_endpoint; do | |
| 25 | + | [[ -z "$ts" ]] && continue | |
| 26 | + | ||
| 27 | + | # Measure client | |
| 28 | + | (( ${#client} > w_client )) && w_client=${#client} | |
| 29 | + | ||
| 30 | + | # Build svc_display (for w_dest measurement) | |
| 31 | + | local svc_display="" | |
| 32 | + | if [[ -n "$svc" ]]; then | |
| 33 | + | [[ -n "$dest_port" ]] && svc_display="${svc}/${proto}" \ | |
| 34 | + | || svc_display="${svc} (${proto})" | |
| 35 | + | else | |
| 36 | + | [[ -n "$dest_port" ]] && svc_display="${dest_ip}:${dest_port}/${proto}" \ | |
| 37 | + | || svc_display="${dest_ip} (${proto})" | |
| 38 | + | fi | |
| 39 | + | ||
| 40 | + | # Build raw_suffix plain (no ANSI) for w_dest measurement | |
| 41 | + | local raw_plain="" | |
| 42 | + | if [[ -n "$svc" ]]; then | |
| 43 | + | [[ -n "$dest_port" ]] && raw_plain=" (${dest_ip}:${dest_port})" \ | |
| 44 | + | || raw_plain=" (${dest_ip})" | |
| 45 | + | fi | |
| 46 | + | ||
| 47 | + | local measure_len | |
| 48 | + | if $resolved_only; then | |
| 49 | + | measure_len=${#svc_display} | |
| 50 | + | else | |
| 51 | + | local raw_plain="" | |
| 52 | + | [[ -n "$svc" && -n "$dest_port" ]] && raw_plain=" (${dest_ip}:${dest_port})" | |
| 53 | + | [[ -n "$svc" && -z "$dest_port" ]] && raw_plain=" (${dest_ip})" | |
| 54 | + | measure_len=$(( ${#svc_display} + ${#raw_plain} )) | |
| 55 | + | fi | |
| 56 | + | (( measure_len > w_dest )) && w_dest=$measure_len | |
| 57 | + | ||
| 58 | + | # Resolve endpoint once | |
| 59 | + | local src_resolved="" | |
| 60 | + | if [[ -n "$src_endpoint" ]]; then | |
| 61 | + | src_resolved=$(resolve::ip "$src_endpoint" 2>/dev/null || true) | |
| 62 | + | [[ "$src_resolved" == "$src_endpoint" ]] && src_resolved="" | |
| 63 | + | # Measure endpoint column: raw IP + " → resolved" | |
| 64 | + | local ep_display_len=${#src_endpoint} | |
| 65 | + | [[ -n "$src_resolved" ]] && ep_display_len=$(( ep_display_len + 4 + ${#src_resolved} )) | |
| 66 | + | (( ep_display_len > w_endpoint )) && w_endpoint=$ep_display_len | |
| 67 | + | fi | |
| 68 | + | ||
| 69 | + | if [[ -n "$src_endpoint" ]]; then | |
| 70 | + | local ep_measure_len | |
| 71 | + | if $resolved_only; then | |
| 72 | + | ep_measure_len=${#src_resolved} | |
| 73 | + | [[ -z "$src_resolved" ]] && ep_measure_len=${#src_endpoint} | |
| 74 | + | else | |
| 75 | + | ep_measure_len=${#src_endpoint} | |
| 76 | + | [[ -n "$src_resolved" ]] && \ | |
| 77 | + | ep_measure_len=$(( ${#src_endpoint} + 4 + ${#src_resolved} )) | |
| 78 | + | fi | |
| 79 | + | (( ep_measure_len > w_endpoint )) && w_endpoint=$ep_measure_len | |
| 80 | + | fi | |
| 81 | + | ||
| 82 | + | resolved_data+="${ts}|${client}|${dest_ip}|${dest_port}|${proto}|${svc}|${count}|${src_endpoint}|${src_resolved}"$'\n' | |
| 83 | + | done <<< "$data" | |
| 84 | + | ||
| 85 | + | (( w_client += 2 )) | |
| 86 | + | (( w_dest += 2 )) | |
| 87 | + | [[ "$w_endpoint" -gt 0 ]] && (( w_endpoint += 2 )) | |
| 88 | + | ||
| 89 | + | # ── Pass 2: render ── | |
| 90 | + | ui::logs::fw_section_header | |
| 91 | + | while IFS='|' read -r ts client dest_ip dest_port proto svc count src_endpoint src_resolved; do | |
| 92 | + | [[ -z "$ts" ]] && continue | |
| 93 | + | ui::logs::fw_row "$ts" "$client" "$dest_ip" "$dest_port" \ | |
| 94 | + | "$proto" "$svc" "$count" "$w_client" "$w_dest" \ | |
| 95 | + | "$src_endpoint" "$src_resolved" "$w_endpoint" "$resolved_only" | |
| 96 | + | done <<< "$resolved_data" | |
| 97 | + | printf "\n" | |
| 98 | + | } | |
| 99 | + | function cmd::logs::show_wg_events() { | |
| 100 | + | local filter_ip="${1:-}" filter_name="${2:-}" filter_type="${3:-}" \ | |
| 101 | + | limit="${4:-50}" collapse="${5:-1}" \ | |
| 102 | + | since="${6:-}" filter_event="${7:-}" sort_order="${8:-desc}" \ | |
| 103 | + | resolved_only="${9:-false}" | |
| 104 | + | ||
| 105 | + | [[ ! -f "$WG_EVENTS_LOG" ]] && return 0 | |
| 106 | + | ||
| 107 | + | local data | |
| 108 | + | data=$(json::wg_events \ | |
| 109 | + | "$WG_EVENTS_LOG" "$filter_name" "$filter_type" \ | |
| 110 | + | "$limit" "$collapse" "$since" "$filter_event" \ | |
| 111 | + | "$(ctx::endpoint_cache)" "$sort_order" \ | |
| 112 | + | 2>/dev/null) | |
| 113 | + | ||
| 114 | + | [[ -z "$data" ]] && return 0 | |
| 115 | + | ||
| 116 | + | local w_client=16 w_endpoint=16 | |
| 117 | + | local resolved_data="" | |
| 118 | + | ||
| 119 | + | while IFS='|' read -r ts client endpoint event count gap_seconds; do | |
| 120 | + | [[ -z "$ts" ]] && continue | |
| 121 | + | (( ${#client} > w_client )) && w_client=${#client} | |
| 122 | + | ||
| 123 | + | local resolved="" | |
| 124 | + | [[ -n "$endpoint" ]] && resolved=$(resolve::ip "$endpoint" 2>/dev/null || true) | |
| 125 | + | [[ "$resolved" == "$endpoint" ]] && resolved="" | |
| 126 | + | ||
| 127 | + | # In resolved_only mode, use resolved name as display (or raw if unresolved) | |
| 128 | + | local ep_display | |
| 129 | + | if $resolved_only; then | |
| 130 | + | ep_display="${resolved:-$endpoint}" | |
| 131 | + | [[ -z "$ep_display" ]] && ep_display="-" | |
| 132 | + | else | |
| 133 | + | ep_display="${endpoint:--}" | |
| 134 | + | fi | |
| 135 | + | ||
| 136 | + | (( ${#ep_display} > w_endpoint )) && w_endpoint=${#ep_display} | |
| 137 | + | # Extra width for annotation when not resolved_only | |
| 138 | + | $resolved_only || { | |
| 139 | + | [[ -n "$resolved" && -n "$endpoint" ]] && \ | |
| 140 | + | w_endpoint=$(( w_endpoint > ${#endpoint} + 4 + ${#resolved} \ | |
| 141 | + | ? w_endpoint : ${#endpoint} + 4 + ${#resolved} )) | |
| 142 | + | } | |
| 143 | + | ||
| 144 | + | resolved_data+="${ts}|${client}|${endpoint}|${event}|${count}|${gap_seconds}|${resolved}|${ep_display}"$'\n' | |
| 145 | + | done <<< "$data" | |
| 146 | + | ||
| 147 | + | (( w_client += 2 )) | |
| 148 | + | (( w_endpoint += 2 )) | |
| 149 | + | $resolved_only || (( w_endpoint += 4 )) # space for annotation | |
| 150 | + | ||
| 151 | + | ui::logs::wg_section_header | |
| 152 | + | while IFS='|' read -r ts client endpoint event count gap_seconds resolved ep_display; do | |
| 153 | + | [[ -z "$ts" ]] && continue | |
| 154 | + | if $resolved_only; then | |
| 155 | + | ui::logs::wg_row "$ts" "$client" "$ep_display" "$event" \ | |
| 156 | + | "$count" "$w_client" "$w_endpoint" "$gap_seconds" "" | |
| 157 | + | else | |
| 158 | + | ui::logs::wg_row "$ts" "$client" "$endpoint" "$event" \ | |
| 159 | + | "$count" "$w_client" "$w_endpoint" "$gap_seconds" "$resolved" | |
| 160 | + | fi | |
| 161 | + | done <<< "$resolved_data" | |
| 162 | + | printf "\n" | |
| 163 | + | } | |
Próximo
Anterior