#!/bin/bash
province=
"省份"
locality=
"城市"
username=
"姓名"
phone=
"手机号"
email=
"邮箱"
domain_root=
"baidu.com"
today=$(
date
+%Y-%m-%d)
domain_path=
"/user/local/ssl/"
domain_array=(
"www.baidu.com"
"www.aliyun.com"
"www.52pojie.cn"
)
aliyun_region=
"cn-hangzhou"
perform_domain_verification() {
local
res=
"$1"
local
record_domain=$(
echo
"$res"
| jq -r
'.RecordDomain'
)
local
record_value=$(
echo
"$res"
| jq -r
'.RecordValue'
)
local
rr_key_word=
"${record_domain/.$domain_root/}"
echo
"校验 DNS 记录:"
echo
"域名: $record_domain"
echo
"记录值: $record_value"
echo
"RRKeyWord: $rr_key_word"
local
list_res=$(aliyun alidns DescribeDomainRecords --region
"$aliyun_region"
--DomainName
"$domain_root"
--RRKeyWord
"$rr_key_word"
--ValueKeyWord
"$record_value"
--SearchMode ADVANCED --Type TXT)
local
total_count=$(
echo
"$list_res"
| jq -r
'.TotalCount'
)
if
[
"$total_count"
-
eq
0 ];
then
aliyun alidns AddDomainRecord --region
"$aliyun_region"
--DomainName
"$domain_root"
--Type TXT --RR
"$rr_key_word"
--Value
"$record_value"
fi
}
generate_certificate_files() {
local
res=
"$1"
local
domain=
"$2"
local
ssl_key=$(
echo
"$res"
| jq -r
'.PrivateKey'
)
local
ssl_pem=$(
echo
"$res"
| jq -r
'.Certificate'
)
echo
"$ssl_key"
>
"$domain_path$domain.key"
echo
"$ssl_pem"
>
"$domain_path$domain.pem"
}
monitor_order() {
local
order_id=
"$1"
local
domain=
"$2"
while
true
;
do
res=$(aliyun cas DescribeCertificateState --region
"$aliyun_region"
--OrderId
"$order_id"
)
order_type=$(
echo
"$res"
| jq -r
'.Type'
)
if
[[
"$order_type"
==
"domain_verify"
]];
then
echo
"证书状态为 domain_verify,进行域名解析。"
perform_domain_verification
"$res"
elif
[[
"$order_type"
==
"certificate"
]];
then
echo
"证书状态为 certificate,生成 key 和 pem 文件。"
generate_certificate_files
"$res"
"$domain"
break
else
echo
"证书状态尚未完成"
fi
echo
"等待 30 秒后重试..."
sleep
30
done
}
create_certificate() {
local
domain=
"$1"
res=$(aliyun cas CreateCertificateRequest --region
"$aliyun_region"
--ProductCode
'digicert-free-1-free'
--Username
"$username"
--Phone
"$phone"
--Email
"$email"
--Domain
"$domain"
--ValidateType DNS)
echo
"创建证书返回 $res"
order_id=$(
echo
"$res"
| jq -r
'.OrderId'
)
monitor_order
"$order_id"
"$domain"
}
check_certificates() {
response=$(aliyun cas ListUserCertificateOrder --region
"$aliyun_region"
--OrderType CERT)
echo
"命令响应: $response"
local
domains=$(
echo
"$response"
| jq -r --arg today
"$today"
'
.CertificateOrderList[] |
select
(.EndDate == $today) |
.CommonName
')
if
[ -z
"$domains"
];
then
echo
"没有证书的结束日期是 $today。"
else
for
domain
in
$domains;
do
if
[[
" ${domain_array[@]} "
=~
" ${domain} "
]];
then
echo
"域名 $domain 存在于数组中"
create_certificate
"$domain"
else
echo
"域名 $domain 不在数组中,跳过。"
fi
done
fi
}
while
[[ $
case
"$1"
in
--
date
)
today=
"$2"
shift
2
;;
--domain)
domain_param=
"$2"
shift
2
;;
--orderId)
order_id_param=
"$2"
shift
2
;;
*)
echo
"未知参数: $1"
exit
1
;;
esac
done
if
[ -n
"$order_id_param"
];
then
echo
"轮询校验订单是否完成"
monitor_order
"$order_id_param"
"$domain_param"
elif
[ -n
"$domain_param"
];
then
echo
"指定域名:$domain_param,直接调用 create_certificate。"
create_certificate
"$domain_param"
else
echo
"没有指定域名,调用 check_certificates 处理所有域名。"
check_certificates
fi