-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5284 from acmesh-official/dev
sync
- Loading branch information
Showing
11 changed files
with
1,147 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
name: DNS | ||
on: | ||
workflow_dispatch: | ||
push: | ||
paths: | ||
- 'dnsapi/*.sh' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
#!/usr/bin/env sh | ||
|
||
# Script to create certificate to Alibaba Cloud CDN | ||
# | ||
# This deployment required following variables | ||
# export Ali_Key="ALIACCESSKEY" | ||
# export Ali_Secret="ALISECRETKEY" | ||
# export DEPLOY_ALI_CDN_DOMAIN="cdn.example.com" | ||
# If you have more than one domain, just | ||
# export DEPLOY_ALI_CDN_DOMAIN="cdn1.example.com cdn2.example.com" | ||
# | ||
# The credentials are shared with all domains, also shared with dns_ali api | ||
|
||
Ali_API="https://cdn.aliyuncs.com/" | ||
|
||
ali_cdn_deploy() { | ||
_cdomain="$1" | ||
_ckey="$2" | ||
_ccert="$3" | ||
_cca="$4" | ||
_cfullchain="$5" | ||
|
||
_debug _cdomain "$_cdomain" | ||
_debug _ckey "$_ckey" | ||
_debug _ccert "$_ccert" | ||
_debug _cca "$_cca" | ||
_debug _cfullchain "$_cfullchain" | ||
|
||
Ali_Key="${Ali_Key:-$(_readaccountconf_mutable Ali_Key)}" | ||
Ali_Secret="${Ali_Secret:-$(_readaccountconf_mutable Ali_Secret)}" | ||
if [ -z "$Ali_Key" ] || [ -z "$Ali_Secret" ]; then | ||
Ali_Key="" | ||
Ali_Secret="" | ||
_err "You don't specify aliyun api key and secret yet." | ||
return 1 | ||
fi | ||
|
||
#save the api key and secret to the account conf file. | ||
_saveaccountconf_mutable Ali_Key "$Ali_Key" | ||
_saveaccountconf_mutable Ali_Secret "$Ali_Secret" | ||
|
||
_getdeployconf DEPLOY_ALI_CDN_DOMAIN | ||
if [ "$DEPLOY_ALI_CDN_DOMAIN" ]; then | ||
_savedeployconf DEPLOY_ALI_CDN_DOMAIN "$DEPLOY_ALI_CDN_DOMAIN" | ||
else | ||
DEPLOY_ALI_CDN_DOMAIN="$_cdomain" | ||
fi | ||
|
||
# read cert and key files and urlencode both | ||
_cert=$(_url_encode_upper <"$_cfullchain") | ||
_key=$(_url_encode_upper <"$_ckey") | ||
|
||
_debug2 _cert "$_cert" | ||
_debug2 _key "$_key" | ||
|
||
## update domain ssl config | ||
for domain in $DEPLOY_ALI_CDN_DOMAIN; do | ||
_set_cdn_domain_ssl_certificate_query "$domain" "$_cert" "$_key" | ||
if _ali_rest "Set CDN domain SSL certificate for $domain" "" POST; then | ||
_info "Domain $domain certificate has been deployed successfully" | ||
fi | ||
done | ||
|
||
return 0 | ||
} | ||
|
||
#################### Private functions below ################################## | ||
|
||
# act ign mtd | ||
_ali_rest() { | ||
act="$1" | ||
ign="$2" | ||
mtd="$3" | ||
|
||
signature=$(printf "%s" "$mtd&%2F&$(_ali_urlencode "$query")" | _hmac "sha1" "$(printf "%s" "$Ali_Secret&" | _hex_dump | tr -d " ")" | _base64) | ||
signature=$(_ali_urlencode "$signature") | ||
url="$Ali_API?$query&Signature=$signature" | ||
|
||
if [ "$mtd" = "GET" ]; then | ||
response="$(_get "$url")" | ||
else | ||
# post payload is not supported yet because of signature | ||
response="$(_post "" "$url")" | ||
fi | ||
|
||
_ret="$?" | ||
_debug2 response "$response" | ||
if [ "$_ret" != "0" ]; then | ||
_err "Error <$act>" | ||
return 1 | ||
fi | ||
|
||
if [ -z "$ign" ]; then | ||
message="$(echo "$response" | _egrep_o "\"Message\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")" | ||
if [ "$message" ]; then | ||
_err "$message" | ||
return 1 | ||
fi | ||
fi | ||
} | ||
|
||
_ali_urlencode() { | ||
_str="$1" | ||
_str_len=${#_str} | ||
_u_i=1 | ||
while [ "$_u_i" -le "$_str_len" ]; do | ||
_str_c="$(printf "%s" "$_str" | cut -c "$_u_i")" | ||
case $_str_c in [a-zA-Z0-9.~_-]) | ||
printf "%s" "$_str_c" | ||
;; | ||
*) | ||
printf "%%%02X" "'$_str_c" | ||
;; | ||
esac | ||
_u_i="$(_math "$_u_i" + 1)" | ||
done | ||
} | ||
|
||
_ali_nonce() { | ||
#_head_n 1 </dev/urandom | _digest "sha256" hex | cut -c 1-31 | ||
#Not so good... | ||
date +"%s%N" | sed 's/%N//g' | ||
} | ||
|
||
_timestamp() { | ||
date -u +"%Y-%m-%dT%H%%3A%M%%3A%SZ" | ||
} | ||
|
||
# stdin stdout | ||
_url_encode_upper() { | ||
encoded=$(_url_encode) | ||
|
||
for match in $(echo "$encoded" | _egrep_o '%..' | sort -u); do | ||
upper=$(echo "$match" | _upper_case) | ||
encoded=$(echo "$encoded" | sed "s/$match/$upper/g") | ||
done | ||
|
||
echo "$encoded" | ||
} | ||
|
||
# domain pub pri | ||
_set_cdn_domain_ssl_certificate_query() { | ||
query='' | ||
query=$query'AccessKeyId='$Ali_Key | ||
query=$query'&Action=SetCdnDomainSSLCertificate' | ||
query=$query'&CertType=upload' | ||
query=$query'&DomainName='$1 | ||
query=$query'&Format=json' | ||
query=$query'&SSLPri='$3 | ||
query=$query'&SSLProtocol=on' | ||
query=$query'&SSLPub='$2 | ||
query=$query'&SignatureMethod=HMAC-SHA1' | ||
query=$query"&SignatureNonce=$(_ali_nonce)" | ||
query=$query'&SignatureVersion=1.0' | ||
query=$query'&Timestamp='$(_timestamp) | ||
query=$query'&Version=2018-05-10' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
#!/usr/bin/env sh | ||
# Alviy domain api | ||
# | ||
# Get API key and secret from https://cloud.alviy.com/token | ||
# | ||
# Alviy_token="some-secret-key" | ||
# | ||
# Ex.: acme.sh --issue --staging --dns dns_alviy -d "*.s.example.com" -d "s.example.com" | ||
|
||
Alviy_Api="https://cloud.alviy.com/api/v1" | ||
|
||
######## Public functions ##################### | ||
|
||
#Usage: dns_alviy_add _acme-challenge.www.domain.com "content" | ||
dns_alviy_add() { | ||
fulldomain=$1 | ||
txtvalue=$2 | ||
|
||
Alviy_token="${Alviy_token:-$(_readaccountconf_mutable Alviy_token)}" | ||
if [ -z "$Alviy_token" ]; then | ||
Alviy_token="" | ||
_err "Please specify Alviy token." | ||
return 1 | ||
fi | ||
|
||
#save the api key and email to the account conf file. | ||
_saveaccountconf_mutable Alviy_token "$Alviy_token" | ||
|
||
_debug "First detect the root zone" | ||
if ! _get_root "$fulldomain"; then | ||
_err "invalid domain" | ||
return 1 | ||
fi | ||
|
||
_debug _sub_domain "$_sub_domain" | ||
_debug _domain "$_domain" | ||
|
||
_debug "Getting existing records" | ||
if _alviy_txt_exists "$_domain" "$fulldomain" "$txtvalue"; then | ||
_info "This record already exists, skipping" | ||
return 0 | ||
fi | ||
|
||
_add_data="{\"content\":\"$txtvalue\",\"type\":\"TXT\"}" | ||
_debug2 _add_data "$_add_data" | ||
_info "Adding record" | ||
if _alviy_rest POST "zone/$_domain/domain/$fulldomain/" "$_add_data"; then | ||
_debug "Checking updated records of '${fulldomain}'" | ||
|
||
if ! _alviy_txt_exists "$_domain" "$fulldomain" "$txtvalue"; then | ||
_err "TXT record '${txtvalue}' for '${fulldomain}', value wasn't set!" | ||
return 1 | ||
fi | ||
|
||
else | ||
_err "Add txt record error, value '${txtvalue}' for '${fulldomain}' was not set." | ||
return 1 | ||
fi | ||
|
||
_sleep 10 | ||
_info "Added TXT record '${txtvalue}' for '${fulldomain}'." | ||
return 0 | ||
} | ||
|
||
#fulldomain | ||
dns_alviy_rm() { | ||
fulldomain=$1 | ||
txtvalue=$2 | ||
|
||
Alviy_token="${Alviy_token:-$(_readaccountconf_mutable Alviy_token)}" | ||
|
||
_debug "First detect the root zone" | ||
if ! _get_root "$fulldomain"; then | ||
_err "invalid domain" | ||
return 1 | ||
fi | ||
|
||
_debug _sub_domain "$_sub_domain" | ||
_debug _domain "$_domain" | ||
|
||
if ! _alviy_txt_exists "$_domain" "$fulldomain" "$txtvalue"; then | ||
_info "The record does not exist, skip" | ||
return 0 | ||
fi | ||
|
||
_add_data="" | ||
uuid=$(echo "$response" | tr "{" "\n" | grep "$txtvalue" | tr "," "\n" | grep uuid | cut -d \" -f4) | ||
# delete record | ||
_debug "Delete TXT record for '${fulldomain}'" | ||
if ! _alviy_rest DELETE "zone/$_domain/record/$uuid" "{\"confirm\":1}"; then | ||
_err "Cannot delete empty TXT record for '$fulldomain'" | ||
return 1 | ||
fi | ||
_info "The record '$fulldomain'='$txtvalue' deleted" | ||
} | ||
|
||
#################### Private functions below ################################## | ||
#_acme-challenge.www.domain.com | ||
#returns | ||
# _sub_domain=_acme-challenge.www | ||
# _domain=domain.com | ||
_get_root() { | ||
domain=$1 | ||
i=3 | ||
a="init" | ||
while [ -n "$a" ]; do | ||
a=$(printf "%s" "$domain" | cut -d . -f $i-) | ||
i=$((i + 1)) | ||
done | ||
n=$((i - 3)) | ||
h=$(printf "%s" "$domain" | cut -d . -f $n-) | ||
if [ -z "$h" ]; then | ||
#not valid | ||
_alviy_rest GET "zone/$domain/" | ||
_debug "can't get host from $domain" | ||
return 1 | ||
fi | ||
|
||
if ! _alviy_rest GET "zone/$h/"; then | ||
return 1 | ||
fi | ||
|
||
if _contains "$response" '"code":"NOT_FOUND"'; then | ||
_debug "$h not found" | ||
else | ||
s=$((n - 1)) | ||
_sub_domain=$(printf "%s" "$domain" | cut -d . -f -$s) | ||
_domain="$h" | ||
return 0 | ||
fi | ||
return 1 | ||
} | ||
|
||
_alviy_txt_exists() { | ||
zone=$1 | ||
domain=$2 | ||
content_data=$3 | ||
_debug "Getting existing records" | ||
|
||
if ! _alviy_rest GET "zone/$zone/domain/$domain/TXT/"; then | ||
_info "The record does not exist" | ||
return 1 | ||
fi | ||
|
||
if ! _contains "$response" "$3"; then | ||
_info "The record has other value" | ||
return 1 | ||
fi | ||
# GOOD code return - TRUE function | ||
return 0 | ||
} | ||
|
||
_alviy_rest() { | ||
method=$1 | ||
path="$2" | ||
content_data="$3" | ||
_debug "$path" | ||
|
||
export _H1="Authorization: Bearer $Alviy_token" | ||
export _H2="Content-Type: application/json" | ||
|
||
if [ "$content_data" ] || [ "$method" = "DELETE" ]; then | ||
_debug "data ($method): " "$content_data" | ||
response="$(_post "$content_data" "$Alviy_Api/$path" "" "$method")" | ||
else | ||
response="$(_get "$Alviy_Api/$path")" | ||
fi | ||
_code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")" | ||
if [ "$_code" = "401" ]; then | ||
_err "It seems that your api key or secret is not correct." | ||
return 1 | ||
fi | ||
|
||
if [ "$_code" != "200" ]; then | ||
_err "API call error ($method): $path Response code $_code" | ||
fi | ||
if [ "$?" != "0" ]; then | ||
_err "error on rest call ($method): $path. Response:" | ||
_err "$response" | ||
return 1 | ||
fi | ||
_debug2 response "$response" | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.