nuno a révisé ce gist 1 month ago. Aller à la révision
1 file changed, 106 insertions
gistfile1.txt(fichier créé)
| @@ -0,0 +1,106 @@ | |||
| 1 | + | function cmd::logs::show() { | |
| 2 | + | local name="" type="" limit=50 since="" | |
| 3 | + | local fw_only=false wg_only=false follow=false merged=false | |
| 4 | + | local raw=false detailed=false | |
| 5 | + | local filter_service="" filter_event="" | |
| 6 | + | ||
| 7 | + | while [[ $# -gt 0 ]]; do | |
| 8 | + | case "$1" in | |
| 9 | + | --name) name="$2"; shift 2 ;; | |
| 10 | + | --type) type="$2"; shift 2 ;; | |
| 11 | + | --limit) limit="$2"; shift 2 ;; | |
| 12 | + | --since) since="$2"; shift 2 ;; | |
| 13 | + | --service) filter_service="$2"; shift 2 ;; | |
| 14 | + | --event) filter_event="$2"; shift 2 ;; | |
| 15 | + | --fw) fw_only=true; shift ;; | |
| 16 | + | --wg) wg_only=true; shift ;; | |
| 17 | + | --merged) merged=true; shift ;; | |
| 18 | + | --follow|-f) follow=true; shift ;; | |
| 19 | + | --raw) raw=true; shift ;; | |
| 20 | + | --detailed) detailed=true; shift ;; | |
| 21 | + | --help) cmd::logs::help; return ;; | |
| 22 | + | *) | |
| 23 | + | log::error "Unknown flag: $1" | |
| 24 | + | return 1 | |
| 25 | + | ;; | |
| 26 | + | esac | |
| 27 | + | done | |
| 28 | + | ||
| 29 | + | local collapse=1 | |
| 30 | + | $detailed && collapse=0 | |
| 31 | + | ||
| 32 | + | if [[ -n "$name" && -n "$type" ]]; then | |
| 33 | + | name=$(peers::resolve_and_require "$name" "$type") || return 1 | |
| 34 | + | fi | |
| 35 | + | ||
| 36 | + | local filter_ip="" | |
| 37 | + | if [[ -n "$name" ]]; then | |
| 38 | + | filter_ip=$(peers::get_ip "$name") | |
| 39 | + | [[ -z "$filter_ip" ]] && log::error "Could not find IP for: $name" && return 1 | |
| 40 | + | fi | |
| 41 | + | ||
| 42 | + | if $follow; then | |
| 43 | + | cmd::logs::follow "$filter_ip" "$name" "$type" "$fw_only" "$wg_only" | |
| 44 | + | return | |
| 45 | + | fi | |
| 46 | + | ||
| 47 | + | local net_file="" | |
| 48 | + | $raw || net_file="$(ctx::net)" | |
| 49 | + | ||
| 50 | + | # Parse --service into dest_ip and dest_port | |
| 51 | + | local filter_dest_ip="" filter_dest_port="" | |
| 52 | + | if [[ -n "$filter_service" ]]; then | |
| 53 | + | if [[ "$filter_service" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(:[0-9]+)?$ ]]; then | |
| 54 | + | filter_dest_ip="${filter_service%%:*}" | |
| 55 | + | local maybe_port="${filter_service##*:}" | |
| 56 | + | [[ "$maybe_port" != "$filter_dest_ip" ]] && filter_dest_port="$maybe_port" | |
| 57 | + | else | |
| 58 | + | local svc_resolved | |
| 59 | + | svc_resolved=$(net::resolve "$filter_service" 2>/dev/null | head -1) | |
| 60 | + | if [[ -n "$svc_resolved" ]]; then | |
| 61 | + | filter_dest_ip="${svc_resolved%%:*}" | |
| 62 | + | local rest="${svc_resolved#*:}" | |
| 63 | + | [[ "$rest" != "$filter_dest_ip" ]] && filter_dest_port="${rest%%:*}" | |
| 64 | + | else | |
| 65 | + | log::error "Service not found: ${filter_service}" | |
| 66 | + | return 1 | |
| 67 | + | fi | |
| 68 | + | fi | |
| 69 | + | fi | |
| 70 | + | ||
| 71 | + | if $merged; then | |
| 72 | + | log::section "WireGuard Activity Log" | |
| 73 | + | printf "\n" | |
| 74 | + | cmd::logs::show_merged "$filter_ip" "$name" "$type" "$limit" "$net_file" "$since" | |
| 75 | + | return | |
| 76 | + | fi | |
| 77 | + | ||
| 78 | + | # Collect output — only show header if there's data | |
| 79 | + | local fw_output="" wg_output="" | |
| 80 | + | ||
| 81 | + | $wg_only || fw_output=$(cmd::logs::show_fw_events \ | |
| 82 | + | "$filter_ip" "$name" "$type" "$limit" "$net_file" \ | |
| 83 | + | "$collapse" "$since" "$filter_dest_ip" "$filter_dest_port") | |
| 84 | + | ||
| 85 | + | $fw_only || wg_output=$(cmd::logs::show_wg_events \ | |
| 86 | + | "$filter_ip" "$name" "$type" "$limit" \ | |
| 87 | + | "$collapse" "$since" "$filter_event") | |
| 88 | + | ||
| 89 | + | if [[ -z "$(echo "$fw_output" | tr -d '[:space:]')" && \ | |
| 90 | + | -z "$(echo "$wg_output" | tr -d '[:space:]')" ]]; then | |
| 91 | + | log::wg_warning "No logs found" | |
| 92 | + | return 0 | |
| 93 | + | fi | |
| 94 | + | ||
| 95 | + | log::section "WireGuard Activity Log" | |
| 96 | + | printf "\n" | |
| 97 | + | ||
| 98 | + | if [[ -n "$fw_output" && -n "$wg_output" ]]; then | |
| 99 | + | printf "%s\n\n" "$fw_output" | |
| 100 | + | printf "%s\n" "$wg_output" | |
| 101 | + | elif [[ -n "$fw_output" ]]; then | |
| 102 | + | printf "%s\n" "$fw_output" | |
| 103 | + | else | |
| 104 | + | printf "%s\n" "$wg_output" | |
| 105 | + | fi | |
| 106 | + | } | |
Plus récent
Plus ancien