dns_la.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/usr/bin/env sh
  2. # LA_Id="123"
  3. # LA_Sk="456"
  4. # shellcheck disable=SC2034
  5. dns_la_info='dns.la
  6. Site: dns.la
  7. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_la
  8. Options:
  9. LA_Id APIID
  10. LA_Sk APISecret
  11. LA_Token 用冒号连接 APIID APISecret 再base64生成
  12. Issues: github.com/acmesh-official/acme.sh/issues/4257
  13. '
  14. LA_Api="https://api.dns.la/api"
  15. ######## Public functions #####################
  16. #Usage: dns_la_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  17. dns_la_add() {
  18. fulldomain=$1
  19. txtvalue=$2
  20. LA_Id="${LA_Id:-$(_readaccountconf_mutable LA_Id)}"
  21. LA_Sk="${LA_Sk:-$(_readaccountconf_mutable LA_Sk)}"
  22. _log "LA_Id=$LA_Id"
  23. _log "LA_Sk=$LA_Sk"
  24. if [ -z "$LA_Id" ] || [ -z "$LA_Sk" ]; then
  25. LA_Id=""
  26. LA_Sk=""
  27. _err "You didn't specify a dnsla api id and key yet."
  28. return 1
  29. fi
  30. #save the api key and email to the account conf file.
  31. _saveaccountconf_mutable LA_Id "$LA_Id"
  32. _saveaccountconf_mutable LA_Sk "$LA_Sk"
  33. # generate dnsla token
  34. _la_token
  35. _debug "First detect the root zone"
  36. if ! _get_root "$fulldomain"; then
  37. _err "invalid domain"
  38. return 1
  39. fi
  40. _debug _domain_id "$_domain_id"
  41. _debug _sub_domain "$_sub_domain"
  42. _debug _domain "$_domain"
  43. _info "Adding record"
  44. # record type is enum in new api, 16 for TXT
  45. if _la_post "{\"domainId\":\"$_domain_id\",\"type\":16,\"host\":\"$_sub_domain\",\"data\":\"$txtvalue\",\"ttl\":600}" "record"; then
  46. if _contains "$response" '"id":'; then
  47. _info "Added, OK"
  48. return 0
  49. elif _contains "$response" '"msg":"与已有记录冲突"'; then
  50. _info "Already exists, OK"
  51. return 0
  52. else
  53. _err "Add txt record error."
  54. return 1
  55. fi
  56. fi
  57. _err "Add txt record failed."
  58. return 1
  59. }
  60. #fulldomain txtvalue
  61. dns_la_rm() {
  62. fulldomain=$1
  63. txtvalue=$2
  64. LA_Id="${LA_Id:-$(_readaccountconf_mutable LA_Id)}"
  65. LA_Sk="${LA_Sk:-$(_readaccountconf_mutable LA_Sk)}"
  66. _la_token
  67. _debug "First detect the root zone"
  68. if ! _get_root "$fulldomain"; then
  69. _err "invalid domain"
  70. return 1
  71. fi
  72. _debug _domain_id "$_domain_id"
  73. _debug _sub_domain "$_sub_domain"
  74. _debug _domain "$_domain"
  75. _debug "Getting txt records"
  76. # record type is enum in new api, 16 for TXT
  77. if ! _la_get "recordList?pageIndex=1&pageSize=10&domainId=$_domain_id&host=$_sub_domain&type=16&data=$txtvalue"; then
  78. _err "Error"
  79. return 1
  80. fi
  81. if ! _contains "$response" '"id":'; then
  82. _info "Don't need to remove."
  83. return 0
  84. fi
  85. record_id=$(printf "%s" "$response" | grep '"id":' | _head_n 1 | sed 's/.*"id": *"\([^"]*\)".*/\1/')
  86. _debug "record_id" "$record_id"
  87. if [ -z "$record_id" ]; then
  88. _err "Can not get record id to remove."
  89. return 1
  90. fi
  91. # remove record in new api is RESTful
  92. if ! _la_post "" "record?id=$record_id" "DELETE"; then
  93. _err "Delete record error."
  94. return 1
  95. fi
  96. _contains "$response" '"code":200'
  97. }
  98. #################### Private functions below ##################################
  99. #_acme-challenge.www.domain.com
  100. #returns
  101. # _sub_domain=_acme-challenge.www
  102. # _domain=domain.com
  103. # _domain_id=sdjkglgdfewsdfg
  104. _get_root() {
  105. domain=$1
  106. i=1
  107. p=1
  108. while true; do
  109. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  110. if [ -z "$h" ]; then
  111. #not valid
  112. return 1
  113. fi
  114. if ! _la_get "domain?domain=$h"; then
  115. return 1
  116. fi
  117. if _contains "$response" '"domain":'; then
  118. _domain_id=$(echo "$response" | sed -n 's/.*"id":"\([^"]*\)".*/\1/p')
  119. _log "_domain_id" "$_domain_id"
  120. if [ "$_domain_id" ]; then
  121. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  122. _domain="$h"
  123. return 0
  124. fi
  125. return 1
  126. fi
  127. p="$i"
  128. i=$(_math "$i" + 1)
  129. done
  130. return 1
  131. }
  132. #Usage: URI
  133. _la_rest() {
  134. url="$LA_Api/$1"
  135. _debug "$url"
  136. if ! response="$(_get "$url" "Authorization: Basic $LA_Token" | tr -d ' ' | tr "}" ",")"; then
  137. _err "Error: $url"
  138. return 1
  139. fi
  140. _debug2 response "$response"
  141. return 0
  142. }
  143. _la_get() {
  144. url="$LA_Api/$1"
  145. _debug "$url"
  146. export _H1="Authorization: Basic $LA_Token"
  147. if ! response="$(_get "$url" | tr -d ' ' | tr "}" ",")"; then
  148. _err "Error: $url"
  149. return 1
  150. fi
  151. _debug2 response "$response"
  152. return 0
  153. }
  154. # Usage: _la_post body url [POST|PUT|DELETE]
  155. _la_post() {
  156. body=$1
  157. url="$LA_Api/$2"
  158. http_method=$3
  159. _debug "$body"
  160. _debug "$url"
  161. export _H1="Authorization: Basic $LA_Token"
  162. if ! response="$(_post "$body" "$url" "" "$http_method")"; then
  163. _err "Error: $url"
  164. return 1
  165. fi
  166. _debug2 response "$response"
  167. return 0
  168. }
  169. _la_token() {
  170. LA_Token=$(printf "%s:%s" "$LA_Id" "$LA_Sk" | _base64)
  171. _debug "$LA_Token"
  172. return 0
  173. }