nuno revisou este gist 1 month ago. Ir para a revisão
1 file changed, 108 insertions
gistfile1.txt(arquivo criado)
| @@ -0,0 +1,108 @@ | |||
| 1 | + | # ui::_render_endpoint_col | |
| 2 | + | # Builds padded endpoint string: "raw_ip → resolved" or "raw_ip" or "-" | |
| 3 | + | # Args: endpoint resolved w_endpoint | |
| 4 | + | # Returns: padded colored string via stdout | |
| 5 | + | function ui::_render_endpoint_col() { | |
| 6 | + | local endpoint="${1:-}" resolved="${2:-}" w_endpoint="${3:-20}" | |
| 7 | + | local colored visible_len pad_n | |
| 8 | + | ||
| 9 | + | if [[ -n "$endpoint" ]]; then | |
| 10 | + | if [[ -n "$resolved" ]]; then | |
| 11 | + | colored="${endpoint} \033[2m→ ${resolved}\033[0m" | |
| 12 | + | visible_len=$(( ${#endpoint} + 3 + ${#resolved} )) | |
| 13 | + | else | |
| 14 | + | colored="$endpoint" | |
| 15 | + | visible_len=${#endpoint} | |
| 16 | + | fi | |
| 17 | + | else | |
| 18 | + | colored="\033[2m—\033[0m" | |
| 19 | + | visible_len=1 | |
| 20 | + | fi | |
| 21 | + | ||
| 22 | + | pad_n=$(( w_endpoint - visible_len )) | |
| 23 | + | [[ $pad_n -lt 0 ]] && pad_n=0 | |
| 24 | + | printf "%b%*s" "$colored" "$pad_n" "" | |
| 25 | + | } | |
| 26 | + | # ui::_render_dest_col | |
| 27 | + | # Builds padded destination string: "svc/proto (ip:port)" or raw | |
| 28 | + | # Args: dest_ip dest_port proto svc_name w_dest resolved_only | |
| 29 | + | # Returns: two vars via nameref — dest_colored dest_pad_n | |
| 30 | + | function ui::_build_dest() { | |
| 31 | + | local dest_ip="${1:-}" dest_port="${2:-}" proto="${3:-}" \ | |
| 32 | + | svc_name="${4:-}" w_dest="${5:-20}" resolved_only="${6:-false}" | |
| 33 | + | local svc_display raw_suffix="" raw_plain="" | |
| 34 | + | ||
| 35 | + | if [[ -n "$svc_name" ]]; then | |
| 36 | + | [[ -n "$dest_port" ]] && svc_display="${svc_name}/${proto}" \ | |
| 37 | + | || svc_display="${svc_name} (${proto})" | |
| 38 | + | if ! $resolved_only; then | |
| 39 | + | [[ -n "$dest_port" ]] && raw_suffix=" \033[2m(${dest_ip}:${dest_port})\033[0m" \ | |
| 40 | + | || raw_suffix=" \033[2m(${dest_ip})\033[0m" | |
| 41 | + | [[ -n "$dest_port" ]] && raw_plain=" (${dest_ip}:${dest_port})" \ | |
| 42 | + | || raw_plain=" (${dest_ip})" | |
| 43 | + | fi | |
| 44 | + | else | |
| 45 | + | [[ -n "$dest_port" ]] && svc_display="${dest_ip}:${dest_port}/${proto}" \ | |
| 46 | + | || svc_display="${dest_ip} (${proto})" | |
| 47 | + | fi | |
| 48 | + | ||
| 49 | + | local full_len=$(( ${#svc_display} + ${#raw_plain} )) | |
| 50 | + | local dest_pad_n=$(( w_dest - full_len )) | |
| 51 | + | [[ $dest_pad_n -lt 0 ]] && dest_pad_n=0 | |
| 52 | + | ||
| 53 | + | # Output: svc_display|raw_suffix|dest_pad_n | |
| 54 | + | printf "%s\t%s\t%s" "$svc_display" "$raw_suffix" "$dest_pad_n" | |
| 55 | + | } | |
| 56 | + | function ui::watch::fw_row() { | |
| 57 | + | local ts="${1:-}" client="${2:-}" dest_ip="${3:-}" dest_port="${4:-}" \ | |
| 58 | + | proto="${5:-}" svc_name="${6:-}" \ | |
| 59 | + | src_endpoint="${7:-}" src_resolved="${8:-}" \ | |
| 60 | + | w_client="${9:-20}" w_dest="${10:-18}" w_endpoint="${11:-0}" | |
| 61 | + | ||
| 62 | + | local ts_pad client_pad | |
| 63 | + | ts_pad=$(printf "%-11s" "$ts") | |
| 64 | + | client_pad=$(printf "%-${w_client}s" "$client") | |
| 65 | + | ||
| 66 | + | local dest_parts | |
| 67 | + | dest_parts=$(ui::_build_dest "$dest_ip" "$dest_port" "$proto" "$svc_name" "$w_dest" "false") | |
| 68 | + | local svc_display raw_suffix dest_pad_n | |
| 69 | + | IFS=$'\t' read -r svc_display raw_suffix dest_pad_n <<< "$dest_parts" | |
| 70 | + | ||
| 71 | + | if [[ "$w_endpoint" -gt 0 ]]; then | |
| 72 | + | local src_padded | |
| 73 | + | src_padded=$(ui::_render_endpoint_col "$src_endpoint" "$src_resolved" "$w_endpoint") | |
| 74 | + | printf " %s %s %s \033[1;31m→\033[0m %s%b%*s \033[1;31mdrop\033[0m\n" \ | |
| 75 | + | "$ts_pad" "$client_pad" "$src_padded" \ | |
| 76 | + | "$svc_display" "$raw_suffix" "$dest_pad_n" "" | |
| 77 | + | else | |
| 78 | + | printf " %s %s \033[1;31m→\033[0m %s%b%*s \033[1;31mdrop\033[0m\n" \ | |
| 79 | + | "$ts_pad" "$client_pad" \ | |
| 80 | + | "$svc_display" "$raw_suffix" "$dest_pad_n" "" | |
| 81 | + | fi | |
| 82 | + | } | |
| 83 | + | ||
| 84 | + | ||
| 85 | + | function ui::watch::wg_row() { | |
| 86 | + | local ts="${1:-}" client="${2:-}" endpoint="${3:-}" event="${4:-}" \ | |
| 87 | + | src_resolved="${5:-}" \ | |
| 88 | + | w_client="${6:-20}" w_endpoint="${7:-18}" | |
| 89 | + | ||
| 90 | + | local event_color | |
| 91 | + | case "$event" in | |
| 92 | + | handshake) event_color="\033[1;32m" ;; | |
| 93 | + | attempt) event_color="\033[1;31m" ;; | |
| 94 | + | *) event_color="\033[0;37m" ;; | |
| 95 | + | esac | |
| 96 | + | ||
| 97 | + | local ep_padded | |
| 98 | + | ep_padded=$(ui::_render_endpoint_col "$endpoint" "$src_resolved" "$w_endpoint") | |
| 99 | + | ||
| 100 | + | local ts_pad client_pad | |
| 101 | + | ts_pad=$(printf "%-11s" "$ts") | |
| 102 | + | client_pad=$(printf "%-${w_client}s" "$client") | |
| 103 | + | ||
| 104 | + | printf " %s %s %s %b%s\033[0m\n" \ | |
| 105 | + | "$ts_pad" "$client_pad" "$ep_padded" \ | |
| 106 | + | "$event_color" "$event" | |
| 107 | + | } | |
| 108 | + | ||
Próximo
Anterior