11#! /usr/bin/env sh
2+
3+ # LA_Id="123"
4+ # LA_Sk="456"
25# shellcheck disable=SC2034
36dns_la_info=' dns.la
47Site: dns.la
58Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_la
69Options:
7- LA_Id API ID
8- LA_Key API key
10+ LA_Id APIID
11+ LA_Sk APISecret
12+ LA_Token 用冒号连接 APIID APISecret 再base64生成
913Issues: github.com/acmesh-official/acme.sh/issues/4257
1014'
11-
1215LA_Api=" https://api.dns.la/api"
1316
1417# ####### Public functions #####################
@@ -19,18 +22,23 @@ dns_la_add() {
1922 txtvalue=$2
2023
2124 LA_Id=" ${LA_Id:- $(_readaccountconf_mutable LA_Id)} "
22- LA_Key=" ${LA_Key:- $(_readaccountconf_mutable LA_Key)} "
25+ LA_Sk=" ${LA_Sk:- $(_readaccountconf_mutable LA_Sk)} "
26+ _log " LA_Id=$LA_Id "
27+ _log " LA_Sk=$LA_Sk "
2328
24- if [ -z " $LA_Id " ] || [ -z " $LA_Key " ]; then
29+ if [ -z " $LA_Id " ] || [ -z " $LA_Sk " ]; then
2530 LA_Id=" "
26- LA_Key =" "
31+ LA_Sk =" "
2732 _err " You didn't specify a dnsla api id and key yet."
2833 return 1
2934 fi
3035
3136 # save the api key and email to the account conf file.
3237 _saveaccountconf_mutable LA_Id " $LA_Id "
33- _saveaccountconf_mutable LA_Key " $LA_Key "
38+ _saveaccountconf_mutable LA_Sk " $LA_Sk "
39+
40+ # generate dnsla token
41+ _la_token
3442
3543 _debug " First detect the root zone"
3644 if ! _get_root " $fulldomain " ; then
@@ -42,19 +50,21 @@ dns_la_add() {
4250 _debug _domain " $_domain "
4351
4452 _info " Adding record"
45- 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
46- if _contains " $response " ' "resultid":' ; then
53+
54+ # record type is enum in new api, 16 for TXT
55+ if _la_post " {\" domainId\" :\" $_domain_id \" ,\" type\" :16,\" host\" :\" $_sub_domain \" ,\" data\" :\" $txtvalue \" ,\" ttl\" :600}" " record" ; then
56+ if _contains " $response " ' "id":' ; then
4757 _info " Added, OK"
4858 return 0
49- elif _contains " $response " ' "code":532 ' ; then
59+ elif _contains " $response " ' "msg":"与已有记录冲突" ' ; then
5060 _info " Already exists, OK"
5161 return 0
5262 else
5363 _err " Add txt record error."
5464 return 1
5565 fi
5666 fi
57- _err " Add txt record error ."
67+ _err " Add txt record failed ."
5868 return 1
5969
6070}
@@ -65,7 +75,9 @@ dns_la_rm() {
6575 txtvalue=$2
6676
6777 LA_Id=" ${LA_Id:- $(_readaccountconf_mutable LA_Id)} "
68- LA_Key=" ${LA_Key:- $(_readaccountconf_mutable LA_Key)} "
78+ LA_Sk=" ${LA_Sk:- $(_readaccountconf_mutable LA_Sk)} "
79+
80+ _la_token
6981
7082 _debug " First detect the root zone"
7183 if ! _get_root " $fulldomain " ; then
@@ -77,27 +89,29 @@ dns_la_rm() {
7789 _debug _domain " $_domain "
7890
7991 _debug " Getting txt records"
80- 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
92+ # record type is enum in new api, 16 for TXT
93+ if ! _la_get " recordList?pageIndex=1&pageSize=10&domainId=$_domain_id &host=$_sub_domain &type=16&data=$txtvalue " ; then
8194 _err " Error"
8295 return 1
8396 fi
8497
85- if ! _contains " $response " ' "recordid ":' ; then
98+ if ! _contains " $response " ' "id ":' ; then
8699 _info " Don't need to remove."
87100 return 0
88101 fi
89102
90- record_id=$( printf " %s" " $response " | grep ' "recordid ":' | cut -d : -f 2 | cut -d , -f 1 | tr -d ' \r ' | tr -d ' \n ' )
103+ record_id=$( printf " %s" " $response " | grep ' "id ":' | _head_n 1 | sed ' s/.*"id": *"\([^"]*\)".*/\1/ ' )
91104 _debug " record_id" " $record_id "
92105 if [ -z " $record_id " ]; then
93106 _err " Can not get record id to remove."
94107 return 1
95108 fi
96- if ! _la_rest " record.ashx?cmd=remove&apiid=$LA_Id &apipass=$LA_Key &rtype=json&domainid=$_domain_id &domain=$_domain &recordid=$record_id " ; then
109+ # remove record in new api is RESTful
110+ if ! _la_post " " " record?id=$record_id " " DELETE" ; then
97111 _err " Delete record error."
98112 return 1
99113 fi
100- _contains " $response " ' "code":300 '
114+ _contains " $response " ' "code":200 '
101115
102116}
103117
@@ -119,12 +133,13 @@ _get_root() {
119133 return 1
120134 fi
121135
122- if ! _la_rest " domain.ashx?cmd=get&apiid= $LA_Id &apipass= $LA_Key &rtype=json& domain=$h " ; then
136+ if ! _la_get " domain? domain=$h " ; then
123137 return 1
124138 fi
125139
126- if _contains " $response " ' "domainid":' ; then
127- _domain_id=$( printf " %s" " $response " | grep ' "domainid":' | cut -d : -f 2 | cut -d , -f 1 | tr -d ' \r' | tr -d ' \n' )
140+ if _contains " $response " ' "domain":' ; then
141+ _domain_id=$( echo " $response " | sed -n ' s/.*"id":"\([^"]*\)".*/\1/p' )
142+ _log " _domain_id" " $_domain_id "
128143 if [ " $_domain_id " ]; then
129144 _sub_domain=$( printf " %s" " $domain " | cut -d . -f 1-" $p " )
130145 _domain=" $h "
@@ -143,6 +158,21 @@ _la_rest() {
143158 url=" $LA_Api /$1 "
144159 _debug " $url "
145160
161+ if ! response=" $( _get " $url " " Authorization: Basic $LA_Token " | tr -d ' ' | tr " }" " ," ) " ; then
162+ _err " Error: $url "
163+ return 1
164+ fi
165+
166+ _debug2 response " $response "
167+ return 0
168+ }
169+
170+ _la_get () {
171+ url=" $LA_Api /$1 "
172+ _debug " $url "
173+
174+ export _H1=" Authorization: Basic $LA_Token "
175+
146176 if ! response=" $( _get " $url " | tr -d ' ' | tr " }" " ," ) " ; then
147177 _err " Error: $url "
148178 return 1
@@ -151,3 +181,29 @@ _la_rest() {
151181 _debug2 response " $response "
152182 return 0
153183}
184+
185+ # Usage: _la_post body url [POST|PUT|DELETE]
186+ _la_post () {
187+ body=$1
188+ url=" $LA_Api /$2 "
189+ http_method=$3
190+ _debug " $body "
191+ _debug " $url "
192+
193+ export _H1=" Authorization: Basic $LA_Token "
194+
195+ if ! response=" $( _post " $body " " $url " " " " $http_method " ) " ; then
196+ _err " Error: $url "
197+ return 1
198+ fi
199+
200+ _debug2 response " $response "
201+ return 0
202+ }
203+
204+ _la_token () {
205+ LA_Token=$( printf " %s:%s" " $LA_Id " " $LA_Sk " | _base64)
206+ _debug " $LA_Token "
207+
208+ return 0
209+ }
0 commit comments