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