fix wget support in dns api

This commit is contained in:
neil 2016-05-07 23:33:42 +08:00
parent 94dc5f330c
commit a4270efac9
4 changed files with 49 additions and 34 deletions

31
acme.sh
View File

@ -1,6 +1,6 @@
#!/usr/bin/env sh
VER=2.2.3
VER=2.2.4
PROJECT_NAME="acme.sh"
@ -567,24 +567,29 @@ _calcjwk() {
_debug2 HEADER "$HEADER"
}
# body url [needbase64]
# body url [needbase64] [POST]
_post() {
body="$1"
url="$2"
needbase64="$3"
httpmethod="$4"
if [ -z "$httpmethod" ] ; then
httpmethod="POST"
fi
_debug $httpmethod
if _exists "curl" ; then
CURL="$CURL --dump-header $HTTP_HEADER "
if [ "$needbase64" ] ; then
response="$($CURL -A "User-Agent: $USER_AGENT" -X POST --data "$body" $url | _base64)"
response="$($CURL -A "User-Agent: $USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" --data "$body" $url | _base64)"
else
response="$($CURL -A "User-Agent: $USER_AGENT" -X POST --data "$body" $url)"
response="$($CURL -A "User-Agent: $USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" --data "$body" $url)"
fi
else
if [ "$needbase64" ] ; then
response="$($WGET -S -O - --user-agent="$USER_AGENT" --post-data="$body" $url 2>"$HTTP_HEADER" | _base64)"
response="$($WGET -S -O - --user-agent="$USER_AGENT" --method $httpmethod --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --body-data="$body" $url 2>"$HTTP_HEADER" | _base64)"
else
response="$($WGET -S -O - --user-agent="$USER_AGENT" --post-data="$body" $url 2>"$HTTP_HEADER")"
response="$($WGET -S -O - --user-agent="$USER_AGENT" --method $httpmethod --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --body-data="$body" $url 2>"$HTTP_HEADER")"
fi
_sed_i "s/^ *//g" "$HTTP_HEADER"
fi
@ -594,21 +599,22 @@ _post() {
# url getheader
_get() {
_debug GET
url="$1"
onlyheader="$2"
_debug url $url
if _exists "curl" ; then
if [ "$onlyheader" ] ; then
$CURL -I -A "User-Agent: $USER_AGENT" $url
$CURL -I -A "User-Agent: $USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" $url
else
$CURL -A "User-Agent: $USER_AGENT" $url
$CURL -A "User-Agent: $USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" $url
fi
else
_debug "WGET" "$WGET"
if [ "$onlyheader" ] ; then
eval $WGET --user-agent=\"$USER_AGENT\" -S -O /dev/null $url 2>&1 | sed 's/^[ ]*//g'
$WGET --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -S -O /dev/null $url 2>&1 | sed 's/^[ ]*//g'
else
eval $WGET --user-agent=\"$USER_AGENT\" -O - $url
$WGET --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -O - $url
fi
fi
ret=$?
@ -1061,8 +1067,9 @@ issue() {
_initpath $Le_Domain
if [ -f "$DOMAIN_CONF" ] ; then
Le_NextRenewTime=$(grep "^Le_NextRenewTime=" "$DOMAIN_CONF" | cut -d '=' -f 2)
if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ] ; then
Le_NextRenewTime=$(grep "^Le_NextRenewTime=" "$DOMAIN_CONF" | cut -d '=' -f 2 | tr -d "'\"")
_debug Le_NextRenewTime "$Le_NextRenewTime"
if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ $(date -u "+%s" ) -lt $Le_NextRenewTime ] ; then
_info "Skip, Next renewal time is: $(grep "^Le_NextRenewTimeStr" "$DOMAIN_CONF" | cut -d '=' -f 2)"
return 2
fi

View File

@ -7,7 +7,7 @@
#CF_Email="xxxx@sss.com"
CF_Api="https://api.cloudflare.com/client/v4/"
CF_Api="https://api.cloudflare.com/client/v4"
######## Public functions #####################
@ -36,18 +36,18 @@ dns_cf_add(){
_debug _domain "$_domain"
_debug "Getting txt records"
_cf_rest GET "/zones/${_domain_id}/dns_records?type=TXT&name=$fulldomain"
_cf_rest GET "zones/${_domain_id}/dns_records?type=TXT&name=$fulldomain"
if [ "$?" != "0" ] || ! printf $response | grep \"success\":true > /dev/null ; then
if ! printf "$response" | grep \"success\":true > /dev/null ; then
_err "Error"
return 1
fi
count=$(printf $response | grep -o \"count\":[^,]* | cut -d : -f 2)
count=$(printf "$response" | grep -o \"count\":[^,]* | cut -d : -f 2)
_debug count "$count"
if [ "$count" == "0" ] ; then
_info "Adding record"
if _cf_rest POST "/zones/$_domain_id/dns_records" "{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
if _cf_rest POST "zones/$_domain_id/dns_records" "{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
if printf $response | grep $fulldomain > /dev/null ; then
_info "Added, sleeping 10 seconds"
sleep 10
@ -61,10 +61,10 @@ dns_cf_add(){
_err "Add txt record error."
else
_info "Updating record"
record_id=$(printf $response | grep -o \"id\":\"[^\"]*\" | cut -d : -f 2 | tr -d \")
record_id=$(printf "$response" | grep -o \"id\":\"[^\"]*\" | cut -d : -f 2 | tr -d \"| head -1)
_debug "record_id" $record_id
_cf_rest PUT "/zones/$_domain_id/dns_records/$record_id" "{\"id\":\"$record_id\",\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"zone_id\":\"$_domain_id\",\"zone_name\":\"$_domain\"}"
_cf_rest PUT "zones/$_domain_id/dns_records/$record_id" "{\"id\":\"$record_id\",\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"zone_id\":\"$_domain_id\",\"zone_name\":\"$_domain\"}"
if [ "$?" == "0" ]; then
_info "Updated, sleeping 10 seconds"
sleep 10
@ -117,17 +117,21 @@ _get_root() {
return 1
}
_cf_rest() {
m=$1
ep="$2"
data="$3"
_debug $ep
if [ "$3" ] ; then
data="$3"
_H1="X-Auth-Email: $CF_Email"
_H2="X-Auth-Key: $CF_Key"
_H3="Content-Type: application/json"
if [ "$data" ] ; then
_debug data "$data"
response="$(curl --silent -X $m "$CF_Api/$ep" -H "X-Auth-Email: $CF_Email" -H "X-Auth-Key: $CF_Key" -H "Content-Type: application/json" --data $data)"
response="$(_post "$data" "$CF_Api/$ep" "" $m)"
else
response="$(curl --silent -X $m "$CF_Api/$ep" -H "X-Auth-Email: $CF_Email" -H "X-Auth-Key: $CF_Key" -H "Content-Type: application/json")"
response="$(_get "$CF_Api/$ep")"
fi
if [ "$?" != "0" ] ; then

View File

@ -183,11 +183,16 @@ _rest() {
_debug sec "$sec"
hmac=$(printf "$sec"| openssl md5 |cut -d " " -f 2)
_debug hmac "$hmac"
if [ "$3" ] ; then
response="$(curl --silent -X $m "$url" -H "API-KEY: $CX_Key" -H "API-REQUEST-DATE: $cdate" -H "API-HMAC: $hmac" -H 'Content-Type: application/json' -d "$data")"
_H1="API-KEY: $CX_Key"
_H2="API-REQUEST-DATE: $cdate"
_H3="API-HMAC: $hmac"
_H4="Content-Type: application/json"
if [ "$data" ] ; then
response="$(_post "$data" "$url" "" $m)"
else
response="$(curl --silent -X $m "$url" -H "API-KEY: $CX_Key" -H "API-REQUEST-DATE: $cdate" -H "API-HMAC: $hmac" -H 'Content-Type: application/json')"
response="$(_get "$url")"
fi
if [ "$?" != "0" ] ; then

View File

@ -71,7 +71,6 @@ existing_records() {
if printf "$response" | grep "Action completed successful" >/dev/null ; then
count=$(printf "$response" | grep '<type>TXT</type>' | wc -l)
record_id=$(printf "$response" | grep '^<id>' | tail -1 | cut -d '>' -f 2 | cut -d '<' -f 1)
return 0
else
@ -175,17 +174,17 @@ _get_root() {
_rest() {
m=$1
ep="$2"
data="$3"
_debug $ep
url="$REST_API/$ep"
_debug url "$url"
if [ "$3" ] ; then
data="$3"
if [ "$data" ] ; then
_debug2 data "$data"
response="$(curl --silent -X $m "$url" -d $data)"
response="$(_post $data "$url")"
else
response="$(curl --silent -X $m "$url" )"
response="$(_get "$url")"
fi
if [ "$?" != "0" ] ; then