debian 开发,modiqi 修复 cf_load_profile 缺失问题。 9 个命令:cf-api/cf-zone/cf-dns/cf-cache/cf-ssl/cf-fw/cf-pagerule/cf-analytics/cf-hostname 支持多账号、域名自动解析 zone_id。 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
95 lines
3.7 KiB
Bash
Executable file
95 lines
3.7 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
# cf-cache — Cloudflare 缓存管理
|
|
# 用法: cf-cache purge <domain> 清除全部缓存
|
|
# cf-cache purge <domain> -u <url> [url...] 按 URL 清除
|
|
# cf-cache purge <domain> -t <tag> [tag...] 按 Cache-Tag 清除
|
|
# cf-cache purge <domain> -h <host> [host...] 按主机名清除
|
|
# cf-cache purge <domain> --prefix <prefix> 按前缀清除
|
|
# cf-cache status <domain> 缓存分析概览
|
|
# 选项: -p <profile> 指定 profile
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
source "$SCRIPT_DIR/cf-lib.sh"
|
|
|
|
cf_parse_profile "$@"
|
|
set -- "${CF_REMAINING_ARGS[@]}"
|
|
cf_load_profile
|
|
|
|
usage() {
|
|
cat >&2 <<'EOF'
|
|
用法: cf-cache <command> <domain> [args...]
|
|
命令:
|
|
purge <domain> 清除全部缓存
|
|
purge <domain> -u <url> [url...] 按 URL 清除
|
|
purge <domain> -t <tag> [tag...] 按 Cache-Tag 清除
|
|
purge <domain> -h <host> [host...] 按主机名清除
|
|
purge <domain> --prefix <prefix> 按前缀清除
|
|
status <domain> 缓存分析概览
|
|
EOF
|
|
exit 1
|
|
}
|
|
|
|
[[ $# -lt 1 ]] && usage
|
|
|
|
CMD="$1"; shift
|
|
|
|
case "$CMD" in
|
|
purge)
|
|
[[ $# -lt 1 ]] && { echo "用法: cf-cache purge <domain> [-u url|-t tag|-h host|--prefix pfx]" >&2; exit 1; }
|
|
DOMAIN="$1"; shift
|
|
ZONE_ID=$(cf_resolve_zone "$DOMAIN")
|
|
|
|
if [[ $# -eq 0 ]]; then
|
|
# 清除全部
|
|
resp=$(cf_call POST "/zones/$ZONE_ID/purge_cache" -d '{"purge_everything":true}')
|
|
cf_check_error "$resp"
|
|
echo "已清除 $DOMAIN 的全部缓存"
|
|
else
|
|
MODE="$1"; shift
|
|
case "$MODE" in
|
|
-u)
|
|
URLS=$(printf '%s\n' "$@" | jq -R . | jq -s .)
|
|
resp=$(cf_call POST "/zones/$ZONE_ID/purge_cache" -d "{\"files\":$URLS}")
|
|
;;
|
|
-t)
|
|
TAGS=$(printf '%s\n' "$@" | jq -R . | jq -s .)
|
|
resp=$(cf_call POST "/zones/$ZONE_ID/purge_cache" -d "{\"tags\":$TAGS}")
|
|
;;
|
|
-h)
|
|
HOSTS=$(printf '%s\n' "$@" | jq -R . | jq -s .)
|
|
resp=$(cf_call POST "/zones/$ZONE_ID/purge_cache" -d "{\"hosts\":$HOSTS}")
|
|
;;
|
|
--prefix)
|
|
PREFIXES=$(printf '%s\n' "$@" | jq -R . | jq -s .)
|
|
resp=$(cf_call POST "/zones/$ZONE_ID/purge_cache" -d "{\"prefixes\":$PREFIXES}")
|
|
;;
|
|
*)
|
|
echo "未知选项: $MODE" >&2; usage ;;
|
|
esac
|
|
cf_check_error "$resp"
|
|
echo "缓存清除请求已提交"
|
|
fi
|
|
;;
|
|
|
|
status)
|
|
[[ $# -lt 1 ]] && { echo "用法: cf-cache status <domain>" >&2; exit 1; }
|
|
DOMAIN="$1"
|
|
ZONE_ID=$(cf_resolve_zone "$DOMAIN")
|
|
# 获取最近 24h 缓存分析
|
|
SINCE=$(date -u -d '24 hours ago' '+%Y-%m-%dT%H:%M:%SZ' 2>/dev/null || date -u -v-24H '+%Y-%m-%dT%H:%M:%SZ')
|
|
UNTIL=$(date -u '+%Y-%m-%dT%H:%M:%SZ')
|
|
resp=$(cf_call GET "/zones/$ZONE_ID/analytics/dashboard?since=$SINCE&until=$UNTIL")
|
|
cf_check_error "$resp"
|
|
echo "$resp" | jq '.result.totals | {
|
|
requests_all: .requests.all,
|
|
requests_cached: .requests.cached,
|
|
requests_uncached: .requests.uncached,
|
|
cache_hit_rate: (if .requests.all > 0 then ((.requests.cached / .requests.all * 100) | round | tostring) + "%" else "N/A" end),
|
|
bandwidth_all: ((.bandwidth.all / 1048576) | round | tostring) + " MB",
|
|
bandwidth_cached: ((.bandwidth.cached / 1048576) | round | tostring) + " MB"
|
|
}'
|
|
;;
|
|
|
|
*) usage ;;
|
|
esac
|