gistfile1.txt
· 7.2 KiB · Text
Eredeti
#!/usr/bin/env bash
FW_EVENTS_LOG="$(ctx::fw_events_log)"
WG_EVENTS_LOG="$(ctx::events_log)"
function cmd::logs::on_load() {
flag::register --name
flag::register --type
flag::register --since
flag::register --limit
flag::register --fw
flag::register --wg
flag::register --follow
}
function cmd::logs::help() {
cat <<EOF
Usage: wgctl logs [options]
Show WireGuard and firewall activity logs.
Options:
--name <name> Filter by client name
--type <type> Filter by device type
--since <time> Time filter (e.g. 1h, 24h, 7d)
--limit <n> Max results per source (default 50)
--fw Show only firewall drops
--wg Show only WireGuard events
Examples:
wgctl logs
wgctl logs --name guest-test
wgctl logs --type guest
wgctl logs --since 1h
wgctl logs --fw --limit 100
EOF
}
function cmd::logs::run() {
local subcmd="${1:-show}"
# Check if first arg is a flag
if [[ "$subcmd" == --* ]]; then
subcmd="show"
else
shift || true
fi
case "$subcmd" in
show) cmd::logs::show "$@" ;;
remove|rm|del) cmd::logs::remove "$@" ;;
help) cmd::logs::help ;;
*)
log::error "Unknown subcommand: '${subcmd}'"
cmd::logs::help
return 1
;;
esac
}
function cmd::logs::show() {
local name="" type="" since="" limit=50
local fw_only=false wg_only=false follow=false
while [[ $# -gt 0 ]]; do
case "$1" in
--name) name="$2"; shift 2 ;;
--type) type="$2"; shift 2 ;;
--since) since="$2"; shift 2 ;;
--limit) limit="$2"; shift 2 ;;
--fw) fw_only=true; shift ;;
--wg) wg_only=true; shift ;;
--follow|-f) follow=true; shift ;;
--help) cmd::logs::help; return ;;
*)
log::error "Unknown flag: $1"
return 1
;;
esac
done
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
log::section "WireGuard Activity Log"
printf "\n"
$wg_only || cmd::logs::show_fw_events "$filter_ip" "$name" "$type" "$limit"
$fw_only || cmd::logs::show_wg_events "$filter_ip" "$name" "$type" "$limit"
}
function cmd::logs::follow() {
local filter_ip="${1:-}" filter_name="${2:-}" filter_type="${3:-}"
local fw_only="${4:-false}" wg_only="${5:-false}"
local filter_peers="${6:-}"
local clients_dir
clients_dir="$(ctx::clients)"
local wg_log="$WG_EVENTS_LOG"
local fw_log="$FW_EVENTS_LOG"
$fw_only && wg_log=""
$wg_only && fw_log=""
log::section "WireGuard Live Log (Ctrl+C to stop)"
printf "\n %-20s %-8s %-20s %-25s %s\n" \
"TIME" "SOURCE" "CLIENT" "DESTINATION/ENDPOINT" "EVENT"
printf " %s\n" "$(printf '─%.0s' {1..90})"
while IFS="|" read -r source ts client dst_or_endpoint event; do
if [[ "$source" == "fw" ]]; then
local colored_event
case "$event" in
tcp) colored_event="\033[1;33mtcp\033[0m" ;;
udp) colored_event="\033[0;36mudp\033[0m" ;;
icmp) colored_event="\033[0;37micmp\033[0m" ;;
*) colored_event="$event" ;;
esac
printf " %-20s %-8s %-20s %-25s %b\n" \
"$ts" "firewall" "$client" "$dst_or_endpoint" "$colored_event"
else
local colored_event
case "$event" in
attempt) colored_event="\033[1;31mattempt\033[0m" ;;
handshake) colored_event="\033[1;32mhandshake\033[0m" ;;
*) colored_event="$event" ;;
esac
printf " %-20s %-8s %-20s %-25s %b\n" \
"$ts" "wireguard" "$client" "$dst_or_endpoint" "$colored_event"
fi
done < <(json::follow_logs "$fw_log" "$wg_log" "$filter_ip" "$filter_type" "$clients_dir" "$filter_peers")
}
function cmd::logs::remove() {
local name="" type="" force=false
while [[ $# -gt 0 ]]; do
case "$1" in
--name) name="$2"; shift 2 ;;
--type) type="$2"; shift 2 ;;
--force) force=true; shift ;;
--help) cmd::logs::help; return ;;
*)
log::error "Unknown flag: $1"
return 1
;;
esac
done
if [[ -z "$name" ]]; then
log::error "Missing required flag: --name"
return 1
fi
name=$(peers::resolve_and_require "$name" "$type") || return 1
local client_ip
client_ip=$(peers::get_ip "$name")
local before_wg before_fw after_wg after_fw
before_wg=$(wc -l < "$WG_EVENTS_LOG" 2>/dev/null || echo 0)
before_fw=$(wc -l < "$FW_EVENTS_LOG" 2>/dev/null || echo 0)
json::remove_events "$WG_EVENTS_LOG" "$name"
json::remove_events "$FW_EVENTS_LOG" "$client_ip"
after_wg=$(wc -l < "$WG_EVENTS_LOG" 2>/dev/null || echo 0)
after_fw=$(wc -l < "$FW_EVENTS_LOG" 2>/dev/null || echo 0)
local removed=$(( (before_wg - after_wg) + (before_fw - after_fw) ))
if [[ "$removed" -eq 0 ]]; then
log::wg_warning "No log entries found for: ${name}"
return 0
fi
if ! $force; then
read -r -p "Remove all log entries for '${name}'? [y/N] " confirm
case "$confirm" in
[yY][eE][sS]|[yY]) ;;
*) log::info "Aborted"; return 0 ;;
esac
fi
log::wg_success "Removed ${removed} log entries for: ${name}"
json::remove_events "$WG_EVENTS_LOG" "$name"
json::remove_events "$FW_EVENTS_LOG" "$client_ip"
log::wg_success "Removed log entries for: ${name}"
}
function cmd::logs::show_wg_events() {
local filter_ip="$1" filter_name="$2" filter_type="$3" limit="$4"
[[ ! -f "$WG_EVENTS_LOG" ]] && return 0
printf " WireGuard Events:\n"
printf " %-20s %-20s %-18s %s\n" "TIME" "CLIENT" "ENDPOINT" "EVENT"
printf " %s\n" "$(printf '─%.0s' {1..75})"
local found=false
while IFS="|" read -r ts client endpoint event; do
[[ -z "$ts" ]] && continue
local colored_event
case "$event" in
attempt) colored_event="\033[1;31mattempt\033[0m" ;;
handshake) colored_event="\033[1;32mhandshake\033[0m" ;;
*) colored_event="$event" ;;
esac
printf " %-20s %-20s %-18s %b\n" "$ts" "$client" "$endpoint" "$colored_event"
found=true
done < <(json::wg_events "$WG_EVENTS_LOG" "$filter_name" "$filter_type" "$limit")
$found || printf " —\n"
printf "\n"
}
function cmd::logs::show_fw_events() {
local filter_ip="$1" filter_name="$2" filter_type="$3" limit="$4"
[[ ! -f "$FW_EVENTS_LOG" ]] && return 0
printf " Firewall Drops:\n"
printf " %-20s %-18s %-25s %s\n" "TIME" "CLIENT" "DESTINATION" "PROTOCOL"
printf " %s\n" "$(printf '─%.0s' {1..75})"
local found=false
while IFS="|" read -r ts client dst proto; do
[[ -z "$ts" ]] && continue
local colored_proto
case "$proto" in
tcp) colored_proto="\033[1;33mtcp\033[0m" ;;
udp) colored_proto="\033[1;36mudp\033[0m" ;;
icmp) colored_proto="\033[0;37micmp\033[0m" ;;
*) colored_proto="$proto" ;;
esac
printf " %-20s %-18s %-25s %b\n" "$ts" "$client" "$dst" "$colored_proto"
found=true
done < <(json::fw_events "$FW_EVENTS_LOG" "$filter_ip" "$filter_type" "$(ctx::clients)" "$limit")
$found || printf " —\n"
printf "\n"
}
| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | FW_EVENTS_LOG="$(ctx::fw_events_log)" |
| 4 | WG_EVENTS_LOG="$(ctx::events_log)" |
| 5 | |
| 6 | function cmd::logs::on_load() { |
| 7 | flag::register --name |
| 8 | flag::register --type |
| 9 | flag::register --since |
| 10 | flag::register --limit |
| 11 | flag::register --fw |
| 12 | flag::register --wg |
| 13 | flag::register --follow |
| 14 | } |
| 15 | |
| 16 | function cmd::logs::help() { |
| 17 | cat <<EOF |
| 18 | Usage: wgctl logs [options] |
| 19 | |
| 20 | Show WireGuard and firewall activity logs. |
| 21 | |
| 22 | Options: |
| 23 | --name <name> Filter by client name |
| 24 | --type <type> Filter by device type |
| 25 | --since <time> Time filter (e.g. 1h, 24h, 7d) |
| 26 | --limit <n> Max results per source (default 50) |
| 27 | --fw Show only firewall drops |
| 28 | --wg Show only WireGuard events |
| 29 | |
| 30 | Examples: |
| 31 | wgctl logs |
| 32 | wgctl logs --name guest-test |
| 33 | wgctl logs --type guest |
| 34 | wgctl logs --since 1h |
| 35 | wgctl logs --fw --limit 100 |
| 36 | EOF |
| 37 | } |
| 38 | |
| 39 | function cmd::logs::run() { |
| 40 | local subcmd="${1:-show}" |
| 41 | |
| 42 | # Check if first arg is a flag |
| 43 | if [[ "$subcmd" == --* ]]; then |
| 44 | subcmd="show" |
| 45 | else |
| 46 | shift || true |
| 47 | fi |
| 48 | |
| 49 | case "$subcmd" in |
| 50 | show) cmd::logs::show "$@" ;; |
| 51 | remove|rm|del) cmd::logs::remove "$@" ;; |
| 52 | help) cmd::logs::help ;; |
| 53 | *) |
| 54 | log::error "Unknown subcommand: '${subcmd}'" |
| 55 | cmd::logs::help |
| 56 | return 1 |
| 57 | ;; |
| 58 | esac |
| 59 | } |
| 60 | |
| 61 | function cmd::logs::show() { |
| 62 | local name="" type="" since="" limit=50 |
| 63 | local fw_only=false wg_only=false follow=false |
| 64 | |
| 65 | while [[ $# -gt 0 ]]; do |
| 66 | case "$1" in |
| 67 | --name) name="$2"; shift 2 ;; |
| 68 | --type) type="$2"; shift 2 ;; |
| 69 | --since) since="$2"; shift 2 ;; |
| 70 | --limit) limit="$2"; shift 2 ;; |
| 71 | --fw) fw_only=true; shift ;; |
| 72 | --wg) wg_only=true; shift ;; |
| 73 | --follow|-f) follow=true; shift ;; |
| 74 | --help) cmd::logs::help; return ;; |
| 75 | *) |
| 76 | log::error "Unknown flag: $1" |
| 77 | return 1 |
| 78 | ;; |
| 79 | esac |
| 80 | done |
| 81 | |
| 82 | if [[ -n "$name" && -n "$type" ]]; then |
| 83 | name=$(peers::resolve_and_require "$name" "$type") || return 1 |
| 84 | fi |
| 85 | |
| 86 | local filter_ip="" |
| 87 | if [[ -n "$name" ]]; then |
| 88 | filter_ip=$(peers::get_ip "$name") |
| 89 | [[ -z "$filter_ip" ]] && log::error "Could not find IP for: $name" && return 1 |
| 90 | fi |
| 91 | |
| 92 | if $follow; then |
| 93 | cmd::logs::follow "$filter_ip" "$name" "$type" "$fw_only" "$wg_only" |
| 94 | return |
| 95 | fi |
| 96 | |
| 97 | log::section "WireGuard Activity Log" |
| 98 | printf "\n" |
| 99 | $wg_only || cmd::logs::show_fw_events "$filter_ip" "$name" "$type" "$limit" |
| 100 | $fw_only || cmd::logs::show_wg_events "$filter_ip" "$name" "$type" "$limit" |
| 101 | } |
| 102 | |
| 103 | function cmd::logs::follow() { |
| 104 | local filter_ip="${1:-}" filter_name="${2:-}" filter_type="${3:-}" |
| 105 | local fw_only="${4:-false}" wg_only="${5:-false}" |
| 106 | local filter_peers="${6:-}" |
| 107 | |
| 108 | local clients_dir |
| 109 | clients_dir="$(ctx::clients)" |
| 110 | |
| 111 | local wg_log="$WG_EVENTS_LOG" |
| 112 | local fw_log="$FW_EVENTS_LOG" |
| 113 | $fw_only && wg_log="" |
| 114 | $wg_only && fw_log="" |
| 115 | |
| 116 | log::section "WireGuard Live Log (Ctrl+C to stop)" |
| 117 | printf "\n %-20s %-8s %-20s %-25s %s\n" \ |
| 118 | "TIME" "SOURCE" "CLIENT" "DESTINATION/ENDPOINT" "EVENT" |
| 119 | printf " %s\n" "$(printf '─%.0s' {1..90})" |
| 120 | |
| 121 | while IFS="|" read -r source ts client dst_or_endpoint event; do |
| 122 | if [[ "$source" == "fw" ]]; then |
| 123 | local colored_event |
| 124 | case "$event" in |
| 125 | tcp) colored_event="\033[1;33mtcp\033[0m" ;; |
| 126 | udp) colored_event="\033[0;36mudp\033[0m" ;; |
| 127 | icmp) colored_event="\033[0;37micmp\033[0m" ;; |
| 128 | *) colored_event="$event" ;; |
| 129 | esac |
| 130 | printf " %-20s %-8s %-20s %-25s %b\n" \ |
| 131 | "$ts" "firewall" "$client" "$dst_or_endpoint" "$colored_event" |
| 132 | else |
| 133 | local colored_event |
| 134 | case "$event" in |
| 135 | attempt) colored_event="\033[1;31mattempt\033[0m" ;; |
| 136 | handshake) colored_event="\033[1;32mhandshake\033[0m" ;; |
| 137 | *) colored_event="$event" ;; |
| 138 | esac |
| 139 | printf " %-20s %-8s %-20s %-25s %b\n" \ |
| 140 | "$ts" "wireguard" "$client" "$dst_or_endpoint" "$colored_event" |
| 141 | fi |
| 142 | done < <(json::follow_logs "$fw_log" "$wg_log" "$filter_ip" "$filter_type" "$clients_dir" "$filter_peers") |
| 143 | } |
| 144 | |
| 145 | function cmd::logs::remove() { |
| 146 | local name="" type="" force=false |
| 147 | |
| 148 | while [[ $# -gt 0 ]]; do |
| 149 | case "$1" in |
| 150 | --name) name="$2"; shift 2 ;; |
| 151 | --type) type="$2"; shift 2 ;; |
| 152 | --force) force=true; shift ;; |
| 153 | --help) cmd::logs::help; return ;; |
| 154 | *) |
| 155 | log::error "Unknown flag: $1" |
| 156 | return 1 |
| 157 | ;; |
| 158 | esac |
| 159 | done |
| 160 | |
| 161 | if [[ -z "$name" ]]; then |
| 162 | log::error "Missing required flag: --name" |
| 163 | return 1 |
| 164 | fi |
| 165 | |
| 166 | name=$(peers::resolve_and_require "$name" "$type") || return 1 |
| 167 | |
| 168 | local client_ip |
| 169 | client_ip=$(peers::get_ip "$name") |
| 170 | |
| 171 | local before_wg before_fw after_wg after_fw |
| 172 | before_wg=$(wc -l < "$WG_EVENTS_LOG" 2>/dev/null || echo 0) |
| 173 | before_fw=$(wc -l < "$FW_EVENTS_LOG" 2>/dev/null || echo 0) |
| 174 | |
| 175 | json::remove_events "$WG_EVENTS_LOG" "$name" |
| 176 | json::remove_events "$FW_EVENTS_LOG" "$client_ip" |
| 177 | |
| 178 | after_wg=$(wc -l < "$WG_EVENTS_LOG" 2>/dev/null || echo 0) |
| 179 | after_fw=$(wc -l < "$FW_EVENTS_LOG" 2>/dev/null || echo 0) |
| 180 | |
| 181 | local removed=$(( (before_wg - after_wg) + (before_fw - after_fw) )) |
| 182 | if [[ "$removed" -eq 0 ]]; then |
| 183 | log::wg_warning "No log entries found for: ${name}" |
| 184 | return 0 |
| 185 | fi |
| 186 | |
| 187 | if ! $force; then |
| 188 | read -r -p "Remove all log entries for '${name}'? [y/N] " confirm |
| 189 | case "$confirm" in |
| 190 | [yY][eE][sS]|[yY]) ;; |
| 191 | *) log::info "Aborted"; return 0 ;; |
| 192 | esac |
| 193 | fi |
| 194 | |
| 195 | log::wg_success "Removed ${removed} log entries for: ${name}" |
| 196 | |
| 197 | json::remove_events "$WG_EVENTS_LOG" "$name" |
| 198 | json::remove_events "$FW_EVENTS_LOG" "$client_ip" |
| 199 | |
| 200 | log::wg_success "Removed log entries for: ${name}" |
| 201 | } |
| 202 | |
| 203 | function cmd::logs::show_wg_events() { |
| 204 | local filter_ip="$1" filter_name="$2" filter_type="$3" limit="$4" |
| 205 | |
| 206 | [[ ! -f "$WG_EVENTS_LOG" ]] && return 0 |
| 207 | |
| 208 | printf " WireGuard Events:\n" |
| 209 | printf " %-20s %-20s %-18s %s\n" "TIME" "CLIENT" "ENDPOINT" "EVENT" |
| 210 | printf " %s\n" "$(printf '─%.0s' {1..75})" |
| 211 | |
| 212 | local found=false |
| 213 | while IFS="|" read -r ts client endpoint event; do |
| 214 | [[ -z "$ts" ]] && continue |
| 215 | local colored_event |
| 216 | case "$event" in |
| 217 | attempt) colored_event="\033[1;31mattempt\033[0m" ;; |
| 218 | handshake) colored_event="\033[1;32mhandshake\033[0m" ;; |
| 219 | *) colored_event="$event" ;; |
| 220 | esac |
| 221 | printf " %-20s %-20s %-18s %b\n" "$ts" "$client" "$endpoint" "$colored_event" |
| 222 | found=true |
| 223 | done < <(json::wg_events "$WG_EVENTS_LOG" "$filter_name" "$filter_type" "$limit") |
| 224 | |
| 225 | $found || printf " —\n" |
| 226 | printf "\n" |
| 227 | } |
| 228 | |
| 229 | function cmd::logs::show_fw_events() { |
| 230 | local filter_ip="$1" filter_name="$2" filter_type="$3" limit="$4" |
| 231 | |
| 232 | [[ ! -f "$FW_EVENTS_LOG" ]] && return 0 |
| 233 | |
| 234 | printf " Firewall Drops:\n" |
| 235 | printf " %-20s %-18s %-25s %s\n" "TIME" "CLIENT" "DESTINATION" "PROTOCOL" |
| 236 | printf " %s\n" "$(printf '─%.0s' {1..75})" |
| 237 | |
| 238 | local found=false |
| 239 | while IFS="|" read -r ts client dst proto; do |
| 240 | [[ -z "$ts" ]] && continue |
| 241 | local colored_proto |
| 242 | case "$proto" in |
| 243 | tcp) colored_proto="\033[1;33mtcp\033[0m" ;; |
| 244 | udp) colored_proto="\033[1;36mudp\033[0m" ;; |
| 245 | icmp) colored_proto="\033[0;37micmp\033[0m" ;; |
| 246 | *) colored_proto="$proto" ;; |
| 247 | esac |
| 248 | printf " %-20s %-18s %-25s %b\n" "$ts" "$client" "$dst" "$colored_proto" |
| 249 | found=true |
| 250 | done < <(json::fw_events "$FW_EVENTS_LOG" "$filter_ip" "$filter_type" "$(ctx::clients)" "$limit") |
| 251 | |
| 252 | $found || printf " —\n" |
| 253 | printf "\n" |
| 254 | } |