cf-cli-tools/cf-hostname
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

123 lines
4.5 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-hostname — Cloudflare for SaaS / Custom Hostname 管理
# 用法: cf-hostname list <domain> [-s status] 列出自定义主机名
# cf-hostname add <domain> <hostname> 添加自定义主机名
# cf-hostname info <domain> <hostname_id> 主机名详情
# cf-hostname delete <domain> <hostname_id> 删除
# cf-hostname fallback <domain> 查看 fallback origin
# cf-hostname set-fallback <domain> <origin> 设置 fallback origin
# 选项: -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-hostname <command> <domain> [args...]
命令:
list <domain> [-s status] 自定义主机名列表
status: active/pending/moved/deleted
add <domain> <hostname> [--ssl method] 添加ssl: dv/http
info <domain> <hostname_id> 详情
delete <domain> <hostname_id> 删除
fallback <domain> 查看 fallback origin
set-fallback <domain> <origin> 设置 fallback origin
EOF
exit 1
}
[[ $# -lt 1 ]] && usage
CMD="$1"; shift
case "$CMD" in
list)
[[ $# -lt 1 ]] && { echo "用法: cf-hostname list <domain> [-s status]" >&2; exit 1; }
DOMAIN="$1"; shift
ZONE_ID=$(cf_resolve_zone "$DOMAIN")
QUERY="/zones/$ZONE_ID/custom_hostnames?per_page=50"
while [[ $# -gt 0 ]]; do
case "$1" in
-s) QUERY+="&status=$2"; shift 2 ;;
*) shift ;;
esac
done
resp=$(cf_call GET "$QUERY")
cf_check_error "$resp"
count=$(echo "$resp" | jq '.result | length')
if [[ "$count" == "0" ]]; then
echo "无自定义主机名"
else
echo "$resp" | jq -r '
["主机名", "状态", "SSL状态", "SSL方法", "创建时间", "ID"],
(.result[] | [.hostname, .status, .ssl.status, .ssl.method, .created_at[:10], .id]) |
@tsv' | column -t -s $'\t'
total=$(echo "$resp" | jq '.result_info.total_count')
echo -e "\n共 $total 个自定义主机名"
fi
;;
add)
[[ $# -lt 2 ]] && { echo "用法: cf-hostname add <domain> <hostname> [--ssl dv|http]" >&2; exit 1; }
DOMAIN="$1" HOSTNAME="$2"; shift 2
ZONE_ID=$(cf_resolve_zone "$DOMAIN")
SSL_METHOD="http"
while [[ $# -gt 0 ]]; do
case "$1" in
--ssl) SSL_METHOD="$2"; shift 2 ;;
*) shift ;;
esac
done
DATA=$(jq -n --arg h "$HOSTNAME" --arg m "$SSL_METHOD" '{
hostname: $h,
ssl: {method: $m, type: "dv", settings: {min_tls_version: "1.2"}}
}')
resp=$(cf_call POST "/zones/$ZONE_ID/custom_hostnames" -d "$DATA")
cf_check_error "$resp"
echo "$resp" | jq '.result | {hostname, status, id, ssl_status: .ssl.status, ownership_verification}'
;;
info)
[[ $# -lt 2 ]] && { echo "用法: cf-hostname info <domain> <hostname_id>" >&2; exit 1; }
ZONE_ID=$(cf_resolve_zone "$1")
resp=$(cf_call GET "/zones/$ZONE_ID/custom_hostnames/$2")
cf_check_error "$resp"
echo "$resp" | jq '.result | {
hostname, status, id,
ssl: {status: .ssl.status, method: .ssl.method, type: .ssl.type},
ownership_verification,
created_at
}'
;;
delete)
[[ $# -lt 2 ]] && { echo "用法: cf-hostname delete <domain> <hostname_id>" >&2; exit 1; }
ZONE_ID=$(cf_resolve_zone "$1")
resp=$(cf_call DELETE "/zones/$ZONE_ID/custom_hostnames/$2")
cf_check_error "$resp"
echo "已删除自定义主机名: $2"
;;
fallback)
[[ $# -lt 1 ]] && { echo "用法: cf-hostname fallback <domain>" >&2; exit 1; }
ZONE_ID=$(cf_resolve_zone "$1")
resp=$(cf_call GET "/zones/$ZONE_ID/custom_hostnames/fallback_origin")
cf_check_error "$resp"
echo "$resp" | jq '.result | {origin, status}'
;;
set-fallback)
[[ $# -lt 2 ]] && { echo "用法: cf-hostname set-fallback <domain> <origin>" >&2; exit 1; }
ZONE_ID=$(cf_resolve_zone "$1")
resp=$(cf_call PUT "/zones/$ZONE_ID/custom_hostnames/fallback_origin" -d "{\"origin\":\"$2\"}")
cf_check_error "$resp"
echo "Fallback origin 已设为: $2"
;;
*) usage ;;
esac