From 5712546380985af0402c711603d8b071243cabb0 Mon Sep 17 00:00:00 2001 From: Darry Shen Date: Mon, 11 Sep 2023 10:57:43 +0800 Subject: [PATCH 1/4] Update dns_la.sh Modify for api update --- dnsapi/dns_la.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dnsapi/dns_la.sh b/dnsapi/dns_la.sh index 674df410a9..49b352462e 100644 --- a/dnsapi/dns_la.sh +++ b/dnsapi/dns_la.sh @@ -118,7 +118,7 @@ _get_root() { fi if _contains "$response" '"domainid":'; then - _domain_id=$(printf "%s" "$response" | grep '"domainid":' | cut -d : -f 2 | cut -d , -f 1 | tr -d '\r' | tr -d '\n') + _domain_id=$(printf "%s" "$response" | grep '"domainid":' | cut -d : -f 10 | cut -d , -f 1 | tr -d '\r' | tr -d '\n') if [ "$_domain_id" ]; then _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) _domain="$h" From 97af9284c482e14137d3ded88708dd3b9d30b922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E4=BC=9F=E5=BC=BA?= <453122747@qq.com> Date: Tue, 17 Oct 2023 18:00:43 +0800 Subject: [PATCH 2/4] dns_la adapter new api --- dnsapi/dns_la.sh | 94 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 73 insertions(+), 21 deletions(-) diff --git a/dnsapi/dns_la.sh b/dnsapi/dns_la.sh index 49b352462e..47b314dad1 100644 --- a/dnsapi/dns_la.sh +++ b/dnsapi/dns_la.sh @@ -1,7 +1,8 @@ #!/usr/bin/env sh -#LA_Id="test123" -#LA_Key="d1j2fdo4dee3948" +# LA_Ak="123" +# LA_Sk="456" +# LA_Token="" LA_Api="https://api.dns.la/api" @@ -12,19 +13,24 @@ dns_la_add() { fulldomain=$1 txtvalue=$2 - LA_Id="${LA_Id:-$(_readaccountconf_mutable LA_Id)}" - LA_Key="${LA_Key:-$(_readaccountconf_mutable LA_Key)}" + LA_Ak="${LA_Ak:-$(_readaccountconf_mutable LA_Ak)}" + LA_Sk="${LA_Sk:-$(_readaccountconf_mutable LA_Sk)}" + _log "LA_Ak=$LA_Ak" + _log "LA_Sk=$LA_Sk" - if [ -z "$LA_Id" ] || [ -z "$LA_Key" ]; then - LA_Id="" - LA_Key="" + if [ -z "$LA_Ak" ] || [ -z "$LA_Sk" ]; then + LA_Ak="" + LA_Sk="" _err "You didn't specify a dnsla api id and key yet." return 1 fi #save the api key and email to the account conf file. - _saveaccountconf_mutable LA_Id "$LA_Id" - _saveaccountconf_mutable LA_Key "$LA_Key" + _saveaccountconf_mutable LA_Ak "$LA_Ak" + _saveaccountconf_mutable LA_Sk "$LA_Sk" + + # generate dnsla token + _la_token _debug "First detect the root zone" if ! _get_root "$fulldomain"; then @@ -36,11 +42,13 @@ dns_la_add() { _debug _domain "$_domain" _info "Adding record" - if _la_rest "record.ashx?cmd=create&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domainid=$_domain_id&host=$_sub_domain&recordtype=TXT&recorddata=$txtvalue&recordline="; then - if _contains "$response" '"resultid":'; then + + # record type is enum in new api, 16 for TXT + if _la_post "{\"domainId\":\"$_domain_id\",\"type\":16,\"host\":\"$_sub_domain\",\"data\":\"$txtvalue\",\"ttl\":600}" "record"; then + if _contains "$response" '"id":'; then _info "Added, OK" return 0 - elif _contains "$response" '"code":532'; then + elif _contains "$response" '"msg":"与已有记录冲突"'; then _info "Already exists, OK" return 0 else @@ -48,7 +56,7 @@ dns_la_add() { return 1 fi fi - _err "Add txt record error." + _err "Add txt record failed." return 1 } @@ -58,8 +66,8 @@ dns_la_rm() { fulldomain=$1 txtvalue=$2 - LA_Id="${LA_Id:-$(_readaccountconf_mutable LA_Id)}" - LA_Key="${LA_Key:-$(_readaccountconf_mutable LA_Key)}" + LA_Ak="${LA_Ak:-$(_readaccountconf_mutable LA_Ak)}" + LA_Sk="${LA_Sk:-$(_readaccountconf_mutable LA_Sk)}" _debug "First detect the root zone" if ! _get_root "$fulldomain"; then @@ -71,7 +79,8 @@ dns_la_rm() { _debug _domain "$_domain" _debug "Getting txt records" - if ! _la_rest "record.ashx?cmd=listn&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domainid=$_domain_id&domain=$_domain&host=$_sub_domain&recordtype=TXT&recorddata=$txtvalue"; then + # record type is enum in new api, 16 for TXT + if ! _la_get "recordList?pageIndex=1&pageSize=10&domainid=$_domain_id&domain=$_domain&host=$_sub_domain&type=16&recorddata=$txtvalue"; then _err "Error" return 1 fi @@ -87,7 +96,8 @@ dns_la_rm() { _err "Can not get record id to remove." return 1 fi - if ! _la_rest "record.ashx?cmd=remove&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domainid=$_domain_id&domain=$_domain&recordid=$record_id"; then + # remove record in new api is RESTful + if ! _la_post "" "record?id=$record_id" "DELETE"; then _err "Delete record error." return 1 fi @@ -113,12 +123,13 @@ _get_root() { return 1 fi - if ! _la_rest "domain.ashx?cmd=get&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domain=$h"; then + if ! _la_get "domain?domain=$h"; then return 1 fi - if _contains "$response" '"domainid":'; then - _domain_id=$(printf "%s" "$response" | grep '"domainid":' | cut -d : -f 10 | cut -d , -f 1 | tr -d '\r' | tr -d '\n') + if _contains "$response" '"domain":'; then + _domain_id=$(printf "%s" "$response" | grep '"domain":' | cut -d : -f 5 | cut -d , -f 1 | tr -d '\r' | tr -d '\n' | sed 's/\"//g') + _log "_domain_id" "$_domain_id" if [ "$_domain_id" ]; then _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) _domain="$h" @@ -137,7 +148,22 @@ _la_rest() { url="$LA_Api/$1" _debug "$url" - if ! response="$(_get "$url" | tr -d ' ' | tr "}" ",")"; then + if ! response="$(_get "$url" "Authorization: Basic $LA_Token" | tr -d ' ' | tr "}" ",")"; then + _err "Error: $url" + return 1 + fi + + _debug2 response "$response" + return 0 +} + +_la_get() { + url="$LA_Api/$1" + _debug "$url" + + export _H1="Authorization: Basic $LA_Token" + + if ! response="$(_get "$url" | tr -d ' ' | tr "}" ",")"; then _err "Error: $url" return 1 fi @@ -145,3 +171,29 @@ _la_rest() { _debug2 response "$response" return 0 } + +# Usage: _la_post body url [POST|PUT|DELETE] +_la_post() { + body=$1 + url="$LA_Api/$2" + http_method=$3 + _debug "$body" + _debug "$url" + + export _H1="Authorization: Basic $LA_Token" + + if ! response="$(_post "$body" "$url" "" "$http_method")"; then + _err "Error: $url" + return 1 + fi + + _debug2 response "$response" + return 0 +} + +_la_token() { + LA_Token=$(echo -n "$LA_Ak:$LA_Sk" | base64 -w 0) + _debug "$LA_Token" + + return 0 +} \ No newline at end of file From ec2c9c23d91dd05b7e6345a7fe62dad9f011511b Mon Sep 17 00:00:00 2001 From: cnssl Date: Wed, 18 Oct 2023 15:04:48 +0800 Subject: [PATCH 3/4] dns_la adapter new api --- dnsapi/dns_la.sh | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/dnsapi/dns_la.sh b/dnsapi/dns_la.sh index 47b314dad1..a6ca9bead1 100644 --- a/dnsapi/dns_la.sh +++ b/dnsapi/dns_la.sh @@ -42,7 +42,7 @@ dns_la_add() { _debug _domain "$_domain" _info "Adding record" - + # record type is enum in new api, 16 for TXT if _la_post "{\"domainId\":\"$_domain_id\",\"type\":16,\"host\":\"$_sub_domain\",\"data\":\"$txtvalue\",\"ttl\":600}" "record"; then if _contains "$response" '"id":'; then @@ -163,7 +163,7 @@ _la_get() { export _H1="Authorization: Basic $LA_Token" - if ! response="$(_get "$url" | tr -d ' ' | tr "}" ",")"; then + if ! response="$(_get "$url" | tr -d ' ' | tr "}" ",")"; then _err "Error: $url" return 1 fi @@ -185,14 +185,19 @@ _la_post() { if ! response="$(_post "$body" "$url" "" "$http_method")"; then _err "Error: $url" return 1 - fi - + fi + _debug2 response "$response" return 0 } _la_token() { - LA_Token=$(echo -n "$LA_Ak:$LA_Sk" | base64 -w 0) + LA_Token=$(printf "$LA_Ak:$LA_Sk" | base64 -w 0) + _debug "$LA_Token" + + return 0 +} + -w 0) _debug "$LA_Token" return 0 From 9b691bebe11a8e7ae7e58076617d1b545a317019 Mon Sep 17 00:00:00 2001 From: cnssl Date: Wed, 18 Oct 2023 15:12:31 +0800 Subject: [PATCH 4/4] dns_la adapter new api --- dnsapi/dns_la.sh | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/dnsapi/dns_la.sh b/dnsapi/dns_la.sh index a6ca9bead1..b648f1cba6 100644 --- a/dnsapi/dns_la.sh +++ b/dnsapi/dns_la.sh @@ -42,7 +42,7 @@ dns_la_add() { _debug _domain "$_domain" _info "Adding record" - + # record type is enum in new api, 16 for TXT if _la_post "{\"domainId\":\"$_domain_id\",\"type\":16,\"host\":\"$_sub_domain\",\"data\":\"$txtvalue\",\"ttl\":600}" "record"; then if _contains "$response" '"id":'; then @@ -163,7 +163,7 @@ _la_get() { export _H1="Authorization: Basic $LA_Token" - if ! response="$(_get "$url" | tr -d ' ' | tr "}" ",")"; then + if ! response="$(_get "$url" | tr -d ' ' | tr "}" ",")"; then _err "Error: $url" return 1 fi @@ -185,19 +185,14 @@ _la_post() { if ! response="$(_post "$body" "$url" "" "$http_method")"; then _err "Error: $url" return 1 - fi - + fi + _debug2 response "$response" return 0 } _la_token() { - LA_Token=$(printf "$LA_Ak:$LA_Sk" | base64 -w 0) - _debug "$LA_Token" - - return 0 -} - -w 0) + LA_Token=$(printf "%s:%s" "$LA_Ak" "$LA_Sk" | base64 -w 0) _debug "$LA_Token" return 0