Browse Source

use sh, _post accroding Neilpang's request and add more check error

pull/682/head
hiska 8 years ago
parent
commit
1352a03c92
  1. 224
      dnsapi/dns_dnsever.sh

224
dnsapi/dns_dnsever.sh

@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh
#Here is a sample custom api script. #Here is a sample custom api script.
#This file name is "dns_myapi.sh" #This file name is "dns_myapi.sh"
@ -21,7 +21,7 @@ dns_dnsever_add() {
if [ -z "$DNSEVER_ID" ] || [ -z "$DNSEVER_PW" ]; then if [ -z "$DNSEVER_ID" ] || [ -z "$DNSEVER_PW" ]; then
DNSEVER_ID="" DNSEVER_ID=""
DNSEVER_ID=""
DNSEVER_PW=""
_err "You don't specify dnsever.com ID or PW yet." _err "You don't specify dnsever.com ID or PW yet."
_err "Please create you key and try again." _err "Please create you key and try again."
return 1 return 1
@ -31,8 +31,7 @@ dns_dnsever_add() {
_saveaccountconf DNSEVER_ID "$DNSEVER_ID" _saveaccountconf DNSEVER_ID "$DNSEVER_ID"
_saveaccountconf DNSEVER_PW "$DNSEVER_PW" _saveaccountconf DNSEVER_PW "$DNSEVER_PW"
dnsever_add_txt "$DNSEVER_ID" "$DNSEVER_PW" "$fulldomain" "$txtvalue"
dnsever_txt "add" "$DNSEVER_ID" "$DNSEVER_PW" "$fulldomain" "$txtvalue"
return $? return $?
} }
@ -44,78 +43,203 @@ dns_dnsever_rm() {
_info "Using dnsever" _info "Using dnsever"
_debug fulldomain "$fulldomain" _debug fulldomain "$fulldomain"
_debug txtvalue "$txtvalue" _debug txtvalue "$txtvalue"
dnsever_delete_txt "$DNSEVER_ID" "$DNSEVER_PW" "$fulldomain" "$txtvalue"
if [ -z "$DNSEVER_ID" ] || [ -z "$DNSEVER_PW" ]; then
DNSEVER_ID=""
DNSEVER_PW=""
_err "You don't specify dnsever.com ID or PW yet."
_err "Please create you key and try again."
return 1
fi
#save the api key and email to the account conf file.
_saveaccountconf DNSEVER_ID "$DNSEVER_ID"
_saveaccountconf DNSEVER_PW "$DNSEVER_PW"
dnsever_txt "delete" "$DNSEVER_ID" "$DNSEVER_PW" "$fulldomain" "$txtvalue"
return $? return $?
} }
#################### Private functions below ################################## #################### Private functions below ##################################
dnsever_add_txt(){
local login_id="$1"; local login_password="$2"; local fulldomain="$3"; local new_txt="$4"
local n=$(echo $fulldomain | grep -o '\.' | wc -l)
local f=$(seq 1 $(($n - 1)) | paste -d, -s -)
local subname=$(echo $fulldomain | cut -f $f -d .)
local user_domain=$(echo $fulldomain | cut -f $n,$(( $n + 1 )) -d .)
dnsever_txt(){
action="$1"; login_id="$2"; login_password="$3"; fulldomain="$4"; txt="$5"
_inithttp
curl -sS -k -c /tmp/dnsever.txt -d "login_id=$login_id" -d "login_password=$login_password" https://kr.dnsever.com/index.html > /tmp/dnsever.html
local skey=$(curl -s -k -b /tmp/dnsever.txt https://kr.dnsever.com/start.html | grep skey | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*/\1/p")
if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then
httpmethod_save="POST -c /tmp/dnsever.txt"
httpmethod_load="POST -b /tmp/dnsever.txt"
else
httpmethod_save="POST --load-cookies /tmp/dnsever.txt"
httpmethod_load="POST --save-cookies /tmp/dnsever.txt"
fi
_info "dnsever_add_txt skey=$skey user_domain=$user_domain selected_menu=edittxt command=add_txt subname=$subname new_txt=$new_txt"
response=$(_post "login_id=$login_id&login_password=$login_password" "https://kr.dnsever.com/index.html" "" "$httpmethod_save")
if [ $? != 0 -o -z "$response" ]; then
_err "dnsever_txt:$action ERROR login failed. Please check https://kr.dnsever.com/index.html with login_id=$login_id login_password=$login_password"
dnsever_logout "" "$httpmethod_load"
return 1
fi
if [ -z "$skey" -o -z "$user_domain" -o -z "$subname" -o -z "$new_txt" ]; then
_err "dnsever_add_txt ERROR skey or user_domain or subname or new_txt was empty"
response=$(_post "" "https://kr.dnsever.com/start.html" "" "$httpmethod_load")
if [ $? != 0 -o -z "$response" ]; then
_err "dnsever_txt:$action ERROR login failed. Please check https://kr.dnsever.com/start.html after login"
dnsever_logout "" "$httpmethod_load"
return 1
fi
skey=$(printf "%s\n" "$response" | grep skey | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*/\1/p")
if [ -z "$skey" ]; then
_err "dnsever_txt:$action ERROR login failed with login_id=$login_id login_password=$login_password"
dnsever_logout "$skey" "$httpmethod_load"
return 1 return 1
fi fi
curl -sS -k -b /tmp/dnsever.txt -d "skey=$skey" -d "user_domain=$user_domain" -d 'selected_menu=edittxt' \
-d "subname=$subname" -d "new_txt=$new_txt" -d 'command=add_txt' \
"https://kr.dnsever.com/start.html?user_domain=$user_domain&selected_menu=edittxt" > /tmp/dnsever.html
return $?
}
user_domain=$(dnsever_select_user_domain "$fulldomain" "$response")
dnsever_delete_txt(){
local login_id="$1"; local login_password="$2"; local fulldomain="$3"; local txtvalue="$4"
local n=$(echo $fulldomain | grep -o '\.' | wc -l)
local f=$(seq 1 $(($n - 1)) | paste -d, -s -)
local subname=$(echo $fulldomain | cut -f $f -d .)
local user_domain=$(echo $fulldomain | cut -f $n,$(( $n + 1 )) -d .)
if [ -z "$user_domain" ]; then
_err "dnsever_txt:$action ERROR no matching domain in DNSEver"
dnsever_logout "$skey" "$httpmethod_load"
return 1
fi
if [ "$action" = "add" ]; then
curl -sS -k -c /tmp/dnsever.txt -d "login_id=$login_id" -d "login_password=$login_password" https://kr.dnsever.com/index.html > /tmp/dnsever.html
curl -sS -k -b /tmp/dnsever.txt "https://kr.dnsever.com/start.html?user_domain=$user_domain&selected_menu=edittxt" > /tmp/dnsever.html
subname=$(echo $fulldomain | sed "s/\.$user_domain\$//")
local skey=$(cat /tmp/dnsever.html | grep skey | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*/\1/p")
if [ -z "$subname" -o -z "$txt" ]; then
_err "dnsever_txt ERROR subname=$subname or txt=$txt is empty"
dnsever_logout "$skey" "$httpmethod_load"
return 1
fi
_info "dnsever_delete_txt skey=$skey subname=$subname user_domain=$user_domain"
_info "dnsever_txt:$action skey=$skey user_domain=$user_domain selected_menu=edittxt command=add_txt subname=$subname txt=$txt"
if [ -z "$skey" -o -z "$user_domain" -o -z "$subname" ]; then
_err "dnsever_delete_txt ERROR skey or user_domain or subname was empty"
return 1
response=$(_post "skey=$skey&user_domain=$user_domain&selected_menu=edittxt" "https://kr.dnsever.com/start.html" "" "$httpmethod_load")
if [ $? != 0 -o -z "$response" ]; then
_err "dnsever_txt:$action ERROR failed to get TXT records from DNSEver"
dnsever_logout "$skey" "$httpmethod_load"
return 1
fi
check=$(dnsever_check "$fulldomain" "$txt" "$response")
if [ $? = 0 -o -n "$check" ]; then
_err "dnsever_txt:$action ERROR $fulldomain=$txt already exists"
dnsever_logout "$skey" "$httpmethod_load"
return 1
fi
response=$(_post "skey=$skey&user_domain=$user_domain&selected_menu=edittxt&command=add_txt&subname=$subname&new_txt=$txt" "https://kr.dnsever.com/start.html" "" "$httpmethod_load")
if [ $? != 0 -o -z "$response" ]; then
_err "dnsever_txt:$action ERROR failed to add_text $fulldomain=$txt"
dnsever_logout "$skey" "$httpmethod_load"
return 1
fi
check=$(dnsever_check "$fulldomain" "$txt" "$response")
if [ $? != 0 -o -z "$check" ]; then
_err "dnsever_txt:$action ERROR failed to get newly added $fulldomain=$txt"
dnsever_logout "$skey" "$httpmethod_load"
fi
elif [ "$action" = "delete" ]; then
response=$(_post "skey=$skey&user_domain=$user_domain&selected_menu=edittxt" "https://kr.dnsever.com/start.html" "" "$httpmethod_load")
if [ $? != 0 -o -z "$response" ]; then
_err "dnsever_txt:$action ERROR failed to get TXT records from DNSEver"
dnsever_logout "$skey" "$httpmethod_load"
return 1
fi
check=$(dnsever_check "$fulldomain" "$txt" "$response")
if [ $? != 0 -o -z "$check" ]; then
_err "dnsever_txt:$action ERROR $fulldomain=$txt does not exists"
dnsever_logout "$skey" "$httpmethod_load"
return 1
fi
_info "dnsever_txt:$action skey=$skey user_domain=$user_domain selected_menu=edittxt command=delete_txt$(echo $check|sed 's/\&/ /g')"
response=$(_post "skey=$skey&user_domain=$user_domain&selected_menu=edittxt&command=delete_txt&$check" "https://kr.dnsever.com/start.html" "" "$httpmethod_load")
if [ $? != 0 -o -z "&response" ]; then
_err "dnsever_txt:$action ERROR failed to delete $fulldomain=$txt from DNSEver"
dnsever_logout "$skey" "$httpmethod_load"
return 1
fi
response=$(_post "skey=$skey&user_domain=$user_domain&selected_menu=edittxt" "https://kr.dnsever.com/start.html" "" "$httpmethod_load")
if [ $? != 0 -o -z "&response" ]; then
_err "dnsever_txt:$action ERROR failed to get $fulldomain=$txt from DNSEver"
dnsever_logout "$skey" "$httpmethod_load"
return 1
fi
check=$(dnsever_check "$fulldomain" "$txt" "$response")
if [ $? = 0 -a -n "$check" ]; then
_err "dnsever_txt:$action ERROR $fulldomain=$txt still exists"
dnsever_logout "$skey" "$httpmethod_load"
return 1
fi
else
_err "dnsever_txt:$action action should be add or delete"
fi fi
local matched=$(grep "$fulldomain" /tmp/dnsever.html | sed -n -e "s/^.*name=['\"]\(.*\)['\"].*value.*$/\1/p" | sed 's/domain_for_txt_//g')
local n; local checked; local input;
dnsever_logout "$skey" "$httpmethod_load"
return 0
}
dnsever_logout(){
skey="$1"; httpmethod_load="$2"
if [ -n "$skey" -a -n "$httpmethod_load" ]; then
response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php" "" "$httpmethod_load")
fi
if [ -e /tmp/dnsever.txt ]; then
rm -f /tmp/dnsever.txt
fi
}
dnsever_select_user_domain(){
fulldomain="$1"; response="$2"
domains=$(printf "%s\n" "$response" | grep OPTION | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*/\1/p" | grep -v "^$")
nmax=0; selected=""
for domain in $domains; do
if echo "$fulldomain" | grep -q "$domain\$"; then
n=$(echo $domain | wc -c)
if [ $n -gt $nmax ]; then
nmax=$n
selected="$domain"
fi
fi
done
echo "$selected"
}
dnsever_check(){
fulldomain="$1"
old_txt="$1"
response="$3"
matched=$(printf "%s\n" "$response" | grep "$fulldomain" | sed -n -e "s/^.*name=['\"]\(.*\)['\"].*value.*$/\1/p" | sed 's/domain_for_txt_//g')
check=""
for n in $matched; do for n in $matched; do
local seq=$(cat /tmp/dnsever.html | grep seq_$n | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*/\1/p")
local old_txt=$(cat /tmp/dnsever.html | grep old_txt_$n | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*id=.*$/\1/p")
seq=$(printf "%s\n" "$response" | grep seq_$n | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*/\1/p")
old_txt=$(printf "%s\n" "$response" | grep old_txt_$n | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*id=.*$/\1/p")
if [ "$txtvalue" != "$old_txt" ]; then if [ "$txtvalue" != "$old_txt" ]; then
_info "dnsever_delete_txt skip deleting seq=$seq fulldomain=$fulldomain due to old_txt=$old_txt was different from txtvalue=$txtvalue skip"
_info "dnsever_check skip deleting seq=$seq fulldomain=$fulldomain due to old_txt=$old_txt was different from txtvalue=$txtvalue skip"
continue continue
fi fi
checked="$checked,$n"
input="$input -d domain_for_txt_$n=$fulldomain -d seq_$n=$seq -d old_txt_$n=$old_txt"
check="$input&check[]=$n&domain_for_txt_$n=$fulldomain&seq_$n=$seq&old_txt_$n=$old_txt"
done done
local check=$(echo "$checked" | sed 's/^,//')
_info "dnsever_delete_txt skey=$skey user_domain=$user_domain selected_menu=edittxt command=delete_txt check[]=$check $input"
if [ -z "$skey" -o -z "$user_domain" -o -z "$check" -o -z "$input" ]; then
_err "dnsever_delete_txt ERROR skey or user_domain or check[] or other parameters was empty. Maybe $fulldomain=$txtvalue was not found."
if [ -z "$check" ]; then
return 1 return 1
fi fi
curl -sS -k -b /tmp/dnsever.txt -d "skey=$skey" -d "user_domain=$user_domain" -d 'selected_menu=edittxt' \
-d "check[]=$check" $input -d 'command=delete_txt' \
"https://kr.dnsever.com/start.html?user_domain=$user_domain&selected_menu=edittxt" > /tmp/dnsever.html
return $?
echo "$check"
return 0
} }
Loading…
Cancel
Save