You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
3.8 KiB

2 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_la_info='dns.la
  4. Site: dns.la
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_la
  6. Options:
  7. LA_Id API ID
  8. LA_Key API key
  9. Issues: github.com/acmesh-official/acme.sh/issues/4257
  10. '
  11. LA_Api="https://api.dns.la/api"
  12. ######## Public functions #####################
  13. #Usage: dns_la_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_la_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. LA_Id="${LA_Id:-$(_readaccountconf_mutable LA_Id)}"
  18. LA_Key="${LA_Key:-$(_readaccountconf_mutable LA_Key)}"
  19. if [ -z "$LA_Id" ] || [ -z "$LA_Key" ]; then
  20. LA_Id=""
  21. LA_Key=""
  22. _err "You didn't specify a dnsla api id and key yet."
  23. return 1
  24. fi
  25. #save the api key and email to the account conf file.
  26. _saveaccountconf_mutable LA_Id "$LA_Id"
  27. _saveaccountconf_mutable LA_Key "$LA_Key"
  28. _debug "First detect the root zone"
  29. if ! _get_root "$fulldomain"; then
  30. _err "invalid domain"
  31. return 1
  32. fi
  33. _debug _domain_id "$_domain_id"
  34. _debug _sub_domain "$_sub_domain"
  35. _debug _domain "$_domain"
  36. _info "Adding record"
  37. 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
  38. if _contains "$response" '"resultid":'; then
  39. _info "Added, OK"
  40. return 0
  41. elif _contains "$response" '"code":532'; then
  42. _info "Already exists, OK"
  43. return 0
  44. else
  45. _err "Add txt record error."
  46. return 1
  47. fi
  48. fi
  49. _err "Add txt record error."
  50. return 1
  51. }
  52. #fulldomain txtvalue
  53. dns_la_rm() {
  54. fulldomain=$1
  55. txtvalue=$2
  56. LA_Id="${LA_Id:-$(_readaccountconf_mutable LA_Id)}"
  57. LA_Key="${LA_Key:-$(_readaccountconf_mutable LA_Key)}"
  58. _debug "First detect the root zone"
  59. if ! _get_root "$fulldomain"; then
  60. _err "invalid domain"
  61. return 1
  62. fi
  63. _debug _domain_id "$_domain_id"
  64. _debug _sub_domain "$_sub_domain"
  65. _debug _domain "$_domain"
  66. _debug "Getting txt records"
  67. 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
  68. _err "Error"
  69. return 1
  70. fi
  71. if ! _contains "$response" '"recordid":'; then
  72. _info "Don't need to remove."
  73. return 0
  74. fi
  75. record_id=$(printf "%s" "$response" | grep '"recordid":' | cut -d : -f 2 | cut -d , -f 1 | tr -d '\r' | tr -d '\n')
  76. _debug "record_id" "$record_id"
  77. if [ -z "$record_id" ]; then
  78. _err "Can not get record id to remove."
  79. return 1
  80. fi
  81. if ! _la_rest "record.ashx?cmd=remove&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domainid=$_domain_id&domain=$_domain&recordid=$record_id"; then
  82. _err "Delete record error."
  83. return 1
  84. fi
  85. _contains "$response" '"code":300'
  86. }
  87. #################### Private functions below ##################################
  88. #_acme-challenge.www.domain.com
  89. #returns
  90. # _sub_domain=_acme-challenge.www
  91. # _domain=domain.com
  92. # _domain_id=sdjkglgdfewsdfg
  93. _get_root() {
  94. domain=$1
  95. i=1
  96. p=1
  97. while true; do
  98. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  99. if [ -z "$h" ]; then
  100. #not valid
  101. return 1
  102. fi
  103. if ! _la_rest "domain.ashx?cmd=get&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domain=$h"; then
  104. return 1
  105. fi
  106. if _contains "$response" '"domainid":'; then
  107. _domain_id=$(printf "%s" "$response" | grep '"domainid":' | cut -d : -f 2 | cut -d , -f 1 | tr -d '\r' | tr -d '\n')
  108. if [ "$_domain_id" ]; then
  109. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  110. _domain="$h"
  111. return 0
  112. fi
  113. return 1
  114. fi
  115. p="$i"
  116. i=$(_math "$i" + 1)
  117. done
  118. return 1
  119. }
  120. #Usage: URI
  121. _la_rest() {
  122. url="$LA_Api/$1"
  123. _debug "$url"
  124. if ! response="$(_get "$url" | tr -d ' ' | tr "}" ",")"; then
  125. _err "Error: $url"
  126. return 1
  127. fi
  128. _debug2 response "$response"
  129. return 0
  130. }