cf-cli-tools/cf-pagerule
feibisi 1be3d8ef8d 初始提交:Cloudflare CLI 工具集
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>
2026-02-24 02:39:31 +08:00

107 lines
4.4 KiB
Bash
Executable file
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
set -euo pipefail
# cf-pagerule — Cloudflare Page Rules / Cache Rules / Redirect Rules 管理
# 用法: cf-pagerule list <domain> 列出 Page Rules
# cf-pagerule add <domain> <url> <action> 添加 Page Rule
# cf-pagerule delete <domain> <rule_id> 删除
# cf-pagerule redirects <domain> Redirect RulesBulk Redirects
# cf-pagerule cache-rules <domain> Cache Rules
# 选项: -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-pagerule <command> <domain> [args...]
命令:
list <domain> Page Rules 列表
add <domain> <url> <setting> <value> 添加 Page Rule
示例: cf-pagerule add example.com "*.example.com/api/*" cache_level bypass
示例: cf-pagerule add example.com "example.com/*" ssl flexible
delete <domain> <rule_id> 删除 Page Rule
redirects <domain> Redirect Rules 列表
cache-rules <domain> Cache Rules 列表Rulesets
EOF
exit 1
}
[[ $# -lt 1 ]] && usage
CMD="$1"; shift
case "$CMD" in
list)
[[ $# -lt 1 ]] && { echo "用法: cf-pagerule list <domain>" >&2; exit 1; }
ZONE_ID=$(cf_resolve_zone "$1")
resp=$(cf_call GET "/zones/$ZONE_ID/pagerules?status=active&order=priority&direction=asc")
cf_check_error "$resp"
count=$(echo "$resp" | jq '.result | length')
if [[ "$count" == "0" ]]; then
echo "无 Page Rules"
else
echo "$resp" | jq -r '.result[] | "[\(.priority)] \(.status) | \(.targets[0].constraint.value)\n \(.actions | map("\(.id)=\(.value // "on")") | join(", "))\n ID: \(.id)\n"'
fi
;;
add)
[[ $# -lt 4 ]] && { echo "用法: cf-pagerule add <domain> <url_pattern> <setting> <value>" >&2; exit 1; }
DOMAIN="$1" URL="$2" SETTING="$3" VALUE="$4"
ZONE_ID=$(cf_resolve_zone "$DOMAIN")
DATA=$(jq -n --arg url "$URL" --arg s "$SETTING" --arg v "$VALUE" '{
targets: [{target:"url", constraint:{operator:"matches", value:$url}}],
actions: [{id:$s, value:$v}],
status: "active"
}')
resp=$(cf_call POST "/zones/$ZONE_ID/pagerules" -d "$DATA")
cf_check_error "$resp"
echo "$resp" | jq -r '.result | "已添加 Page Rule: \(.targets[0].constraint.value) (ID: \(.id))"'
;;
delete)
[[ $# -lt 2 ]] && { echo "用法: cf-pagerule delete <domain> <rule_id>" >&2; exit 1; }
ZONE_ID=$(cf_resolve_zone "$1")
resp=$(cf_call DELETE "/zones/$ZONE_ID/pagerules/$2")
cf_check_error "$resp"
echo "已删除 Page Rule: $2"
;;
redirects)
[[ $# -lt 1 ]] && { echo "用法: cf-pagerule redirects <domain>" >&2; exit 1; }
ZONE_ID=$(cf_resolve_zone "$1")
# 获取 redirect rules (phase: http_request_dynamic_redirect)
resp=$(cf_call GET "/zones/$ZONE_ID/rulesets/phases/http_request_dynamic_redirect/entrypoint")
if echo "$resp" | jq -e '.success' &>/dev/null; then
count=$(echo "$resp" | jq '.result.rules | length')
if [[ "$count" == "0" ]]; then
echo "无 Redirect Rules"
else
echo "$resp" | jq -r '.result.rules[] | "[\(if .enabled then "启用" else "禁用" end)] \(.description // "无描述")\n 表达式: \(.expression[:80])\n 动作: \(.action)\n"'
fi
else
echo "无 Redirect Rules或无权限访问"
fi
;;
cache-rules)
[[ $# -lt 1 ]] && { echo "用法: cf-pagerule cache-rules <domain>" >&2; exit 1; }
ZONE_ID=$(cf_resolve_zone "$1")
resp=$(cf_call GET "/zones/$ZONE_ID/rulesets/phases/http_request_cache_settings/entrypoint")
if echo "$resp" | jq -e '.success' &>/dev/null; then
count=$(echo "$resp" | jq '.result.rules | length')
if [[ "$count" == "0" ]]; then
echo "无 Cache Rules"
else
echo "$resp" | jq -r '.result.rules[] | "[\(if .enabled then "启用" else "禁用" end)] \(.description // "无描述")\n 表达式: \(.expression[:80])\n 动作: \(.action)\n"'
fi
else
echo "无 Cache Rules或无权限访问"
fi
;;
*) usage ;;
esac