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.

154 lines
3.8 KiB

3 years ago
  1. #!/usr/bin/env sh
  2. # dns.la Domain api
  3. #
  4. #LA_Id="test123"
  5. #
  6. #LA_Key="d1j2fdo4dee3948"
  7. DNSLA_API="https://api.dns.la/api/"
  8. ######## Public functions #####################
  9. #Usage: dns_la_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  10. dns_la_add() {
  11. fulldomain=$1
  12. txtvalue=$2
  13. LA_Id="${LA_Id:-$(_readaccountconf_mutable LA_Id)}"
  14. LA_Key="${LA_Key:-$(_readaccountconf_mutable LA_Key)}"
  15. if [ -z "$LA_Id" ] || [ -z "$LA_Key" ]; then
  16. LA_Id=""
  17. LA_Key=""
  18. _err "You don't specify dnsla api id and key yet."
  19. _err "Please create your key and try again."
  20. return 1
  21. fi
  22. #save the api key and email to the account conf file.
  23. _saveaccountconf_mutable LA_Id "$LA_Id"
  24. _saveaccountconf_mutable LA_Key "$LA_Key"
  25. _debug "detect the root zone"
  26. if ! _get_root "$fulldomain"; then
  27. _err "invalid domain"
  28. return 1
  29. fi
  30. add_record "$_domain" "$_sub_domain" "$txtvalue"
  31. }
  32. #fulldomain txtvalue
  33. dns_la_rm() {
  34. fulldomain=$1
  35. txtvalue=$2
  36. LA_Id="${LA_Id:-$(_readaccountconf_mutable LA_Id)}"
  37. LA_Key="${LA_Key:-$(_readaccountconf_mutable LA_Key)}"
  38. _debug "First detect the root zone"
  39. if ! _get_root "$fulldomain"; then
  40. _err "invalid domain"
  41. return 1
  42. fi
  43. if ! _rest GET "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
  44. _err "get record list error."
  45. return 1
  46. fi
  47. if ! _contains "$response" "recordid"; then
  48. _info "no need to remove record."
  49. return 0
  50. fi
  51. _record_id=$(printf "%s" "$response" | grep '"recordid":' | cut -d : -f 2 | cut -d , -f 1 | tr -d '\r' | tr -d '\n')
  52. _debug delete_rid "$_record_id"
  53. if ! _rest GET "record.ashx?cmd=remove&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domainid=$_domain_id&domain=$_domain&recordid=$_record_id"; then
  54. _err "record remove error."
  55. return 1
  56. fi
  57. _contains "$response" "\"code\":300"
  58. }
  59. #add the txt record.
  60. #usage: root sub txtvalue
  61. add_record() {
  62. root=$1
  63. sub=$2
  64. txtvalue=$3
  65. fulldomain="$sub.$root"
  66. _info "adding txt record"
  67. if ! _rest GET "record.ashx?cmd=create&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domainid=$_domain_id&host=$_sub_domain&recordtype=TXT&recorddata=$txtvalue&recordline="; then
  68. return 1
  69. fi
  70. if _contains "$response" "resultid" || _contains "$response" "\"code\":532"; then
  71. return 0
  72. fi
  73. return 1
  74. }
  75. #################### Private functions below ##################################
  76. #_acme-challenge.www.domain.com
  77. #returns
  78. # _sub_domain=_acme-challenge.www
  79. # _domain=domain.com
  80. # _domain_id=sdjkglgdfewsdfg
  81. _get_root() {
  82. domain=$1
  83. i=1
  84. p=1
  85. while true; do
  86. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  87. if [ -z "$h" ]; then
  88. #not valid
  89. return 1
  90. fi
  91. if ! _rest GET "domain.ashx?cmd=get&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domain=$h"; then
  92. return 1
  93. fi
  94. if _contains "$response" "\"code\":300"; then
  95. _domain_id=$(printf "%s" "$response" | grep '"domainid"' | cut -d : -f 2 | cut -d , -f 1 | tr -d '\r' | tr -d '\n')
  96. _debug _domain_id "$_domain_id"
  97. if [ "$_domain_id" ]; then
  98. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  99. _debug _sub_domain "$_sub_domain"
  100. _domain="$h"
  101. _debug _domain "$_domain"
  102. return 0
  103. fi
  104. return 1
  105. fi
  106. p="$i"
  107. i=$(_math "$i" + 1)
  108. done
  109. return 1
  110. }
  111. #Usage: method URI data
  112. _rest() {
  113. m="$1"
  114. ep="$2"
  115. data="$3"
  116. _debug "$ep"
  117. url="$DNSLA_API$ep"
  118. _debug url "$url"
  119. if [ "$m" = "GET" ]; then
  120. response="$(_get "$url" | tr -d ' ' | tr "}" ",")"
  121. else
  122. _debug2 data "$data"
  123. response="$(_post "$data" "$url" | tr -d ' ' | tr "}" ",")"
  124. fi
  125. if [ "$?" != "0" ]; then
  126. _err "error $ep"
  127. return 1
  128. fi
  129. _debug2 response "$response"
  130. return 0
  131. }