gistfile1.txt
· 3.3 KiB · Text
Исходник
function cmd::logs::show() {
local name="" type="" limit=50 since=""
local fw_only=false wg_only=false follow=false merged=false
local raw=false detailed=false
local filter_service="" filter_event=""
while [[ $# -gt 0 ]]; do
case "$1" in
--name) name="$2"; shift 2 ;;
--type) type="$2"; shift 2 ;;
--limit) limit="$2"; shift 2 ;;
--since) since="$2"; shift 2 ;;
--service) filter_service="$2"; shift 2 ;;
--event) filter_event="$2"; shift 2 ;;
--fw) fw_only=true; shift ;;
--wg) wg_only=true; shift ;;
--merged) merged=true; shift ;;
--follow|-f) follow=true; shift ;;
--raw) raw=true; shift ;;
--detailed) detailed=true; shift ;;
--help) cmd::logs::help; return ;;
*)
log::error "Unknown flag: $1"
return 1
;;
esac
done
local collapse=1
$detailed && collapse=0
if [[ -n "$name" && -n "$type" ]]; then
name=$(peers::resolve_and_require "$name" "$type") || return 1
fi
local filter_ip=""
if [[ -n "$name" ]]; then
filter_ip=$(peers::get_ip "$name")
[[ -z "$filter_ip" ]] && log::error "Could not find IP for: $name" && return 1
fi
if $follow; then
cmd::logs::follow "$filter_ip" "$name" "$type" "$fw_only" "$wg_only"
return
fi
local net_file=""
$raw || net_file="$(ctx::net)"
# Parse --service into dest_ip and dest_port
local filter_dest_ip="" filter_dest_port=""
if [[ -n "$filter_service" ]]; then
if [[ "$filter_service" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(:[0-9]+)?$ ]]; then
filter_dest_ip="${filter_service%%:*}"
local maybe_port="${filter_service##*:}"
[[ "$maybe_port" != "$filter_dest_ip" ]] && filter_dest_port="$maybe_port"
else
local svc_resolved
svc_resolved=$(net::resolve "$filter_service" 2>/dev/null | head -1)
if [[ -n "$svc_resolved" ]]; then
filter_dest_ip="${svc_resolved%%:*}"
local rest="${svc_resolved#*:}"
[[ "$rest" != "$filter_dest_ip" ]] && filter_dest_port="${rest%%:*}"
else
log::error "Service not found: ${filter_service}"
return 1
fi
fi
fi
if $merged; then
log::section "WireGuard Activity Log"
printf "\n"
cmd::logs::show_merged "$filter_ip" "$name" "$type" "$limit" "$net_file" "$since"
return
fi
# Collect output — only show header if there's data
local fw_output="" wg_output=""
$wg_only || fw_output=$(cmd::logs::show_fw_events \
"$filter_ip" "$name" "$type" "$limit" "$net_file" \
"$collapse" "$since" "$filter_dest_ip" "$filter_dest_port")
$fw_only || wg_output=$(cmd::logs::show_wg_events \
"$filter_ip" "$name" "$type" "$limit" \
"$collapse" "$since" "$filter_event")
if [[ -z "$(echo "$fw_output" | tr -d '[:space:]')" && \
-z "$(echo "$wg_output" | tr -d '[:space:]')" ]]; then
log::wg_warning "No logs found"
return 0
fi
log::section "WireGuard Activity Log"
printf "\n"
if [[ -n "$fw_output" && -n "$wg_output" ]]; then
printf "%s\n\n" "$fw_output"
printf "%s\n" "$wg_output"
elif [[ -n "$fw_output" ]]; then
printf "%s\n" "$fw_output"
else
printf "%s\n" "$wg_output"
fi
}
| 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 | } |