miaopanel
1#!/usr/bin/env bash
2# Verify live metrics while traffic is actually flowing.
3set -uo pipefail
4ENTRY="${1:?usage: check-waf-live.sh <entry>}"
5BASE="http://127.0.0.1:8888/$ENTRY"
6PASS=$(awk -F': ' '/^password:/{print $2}' /www/server/panel/data/initial_password.txt)
7ORIGIN="http://127.0.0.1:8888"
8
9curl -s -c /tmp/ck.txt -H 'Content-Type: application/json' -H "Origin: $ORIGIN" \
10 -d "{\"username\":\"admin\",\"password\":\"$PASS\"}" "$BASE/api/v1/auth/login" >/dev/null
11
12# 8 seconds of mixed traffic in the background.
13(
14 for i in $(seq 1 40); do
15 curl -s -o /dev/null -H 'Host: demo.test' "http://127.0.0.1/check-$i"
16 sleep 0.2
17 done
18 for i in $(seq 1 10); do
19 curl -s -o /dev/null -A 'sqlmap/1.5' -H 'Host: demo.test' http://127.0.0.1/
20 sleep 0.4
21 done
22) &
23TRAFFIC=$!
24
25sleep 6
26echo "--- 实时(流量进行中) ---"
27curl -s -b /tmp/ck.txt "$BASE/api/v1/waf/overview" | python3 -c "
28import json,sys
29d=json.load(sys.stdin)['data']
30print('qps=%.2f traffic=%.0fB/s rt=%.2fms requests=%d blocked=%d' % (
31 d['qps'], d['traffic'], d['rt'], d['requests'], d['blocked']))"
32
33curl -s -b /tmp/ck.txt "$BASE/api/v1/waf/series?window=5m" | python3 -c "
34import json,sys
35s=json.load(sys.stdin)['data']['series']
36print('5m max qps=%.2f max traffic=%.0f max rt=%.2f' % (
37 max(p['value'] for p in s['requests']), max(p['value'] for p in s['traffic']), max(p['value'] for p in s['rt'])))
38
39wait $TRAFFIC 2>/dev/null || true
40echo "--- 事件 ---"
41curl -s -b /tmp/ck.txt "$BASE/api/v1/waf/events?limit=3" | python3 -c "
42import json,sys
43for e in json.load(sys.stdin)['data'][:3]:
44 print(e['status'], e['rule'], e['uri'][:30], e['ua'][:20])"