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.

161 lines
4.0 KiB

  1. #!/usr/bin/env sh
  2. # dns.la Domain api
  3. #
  4. #LA_Id="test123"
  5. #
  6. #LA_Key="d1j2fdo4dee3948"
  7. DNSLA_API="https://www.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. _fullkey=$(printf "%s" ${fulldomain:16} | tr '.' '_' )
  37. LA_Id="${LA_Id:-$(_readaccountconf_mutable LA_Id)}"
  38. LA_Key="${LA_Key:-$(_readaccountconf_mutable LA_Key)}"
  39. _debug fullkey $_fullkey
  40. RM_recordid="$(_readaccountconf $_fullkey)"
  41. _debug rm_recordid "$RM_recordid"
  42. _debug "detect the root zone"
  43. if ! _get_root "$fulldomain"; then
  44. _err "invalid domain"
  45. return 1
  46. fi
  47. if ! _rest GET "record.ashx?cmd=get&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domainid=$_domain_id&domain=$_domain&recordid=$RM_recordid"; then
  48. _err "get record lis error."
  49. return 1
  50. fi
  51. if ! _contains "$response" "$RM_recordid"; then
  52. _info "no need to remove record."
  53. return 0
  54. fi
  55. if ! _rest GET "record.ashx?cmd=remove&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domainid=$_domain_id&domain=$_domain&recordid=$RM_recordid"; then
  56. _err "record remove error."
  57. return 1
  58. fi
  59. _clearaccountconf $_fullkey
  60. _contains "$response" "\"code\":300"
  61. }
  62. #add the txt record.
  63. #usage: root sub txtvalue
  64. add_record() {
  65. root=$1
  66. sub=$2
  67. txtvalue=$3
  68. fulldomain="$sub.$root"
  69. _info "adding txt record"
  70. 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
  71. return 1
  72. fi
  73. if _contains "$response" "\"code\":300"; then
  74. _record_id=$(printf "%s" "$response" | grep '"resultid"' | cut -d : -f 2 | cut -d , -f 1 | tr -d '\r' | tr -d '\n' )
  75. _fullkey=$(printf "%s" ${fulldomain:16} | tr '.' '_' )
  76. _debug fullkey $_fullkey
  77. _saveaccountconf $_fullkey "$_record_id"
  78. _debug _record_id "$_record_id"
  79. fi
  80. _contains "$response" "\"code\":300"
  81. }
  82. #################### Private functions below ##################################
  83. #_acme-challenge.www.domain.com
  84. #returns
  85. # _sub_domain=_acme-challenge.www
  86. # _domain=domain.com
  87. # _domain_id=sdjkglgdfewsdfg
  88. _get_root() {
  89. domain=$1
  90. i=2
  91. p=1
  92. while true; do
  93. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  94. if [ -z "$h" ]; then
  95. #not valid
  96. return 1
  97. fi
  98. if ! _rest GET "domain.ashx?cmd=get&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domain=$h"; then
  99. return 1
  100. fi
  101. if _contains "$response" "\"code\":300"; then
  102. _domain_id=$(printf "%s" "$response" | grep '"domainid"' | cut -d : -f 2 | cut -d , -f 1 | tr -d '\r' | tr -d '\n' )
  103. _debug _domain_id "$_domain_id"
  104. if [ "$_domain_id" ]; then
  105. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  106. _debug _sub_domain "$_sub_domain"
  107. _domain="$h"
  108. _debug _domain "$_domain"
  109. return 0
  110. fi
  111. return 1
  112. fi
  113. p="$i"
  114. i=$(_math "$i" + 1)
  115. done
  116. return 1
  117. }
  118. #Usage: method URI data
  119. _rest() {
  120. m="$1"
  121. ep="$2"
  122. data="$3"
  123. _debug "$ep"
  124. url="$DNSLA_API$ep"
  125. _debug url "$url"
  126. if [ "$m" = "GET" ]; then
  127. response="$(_get "$url" | tr -d ' ' | tr "}" ",")"
  128. else
  129. _debug2 data "$data"
  130. response="$(_post "$data" "$url" | tr -d ' ' | tr "}" ",")"
  131. fi
  132. if [ "$?" != "0" ]; then
  133. _err "error $ep"
  134. return 1
  135. fi
  136. _debug2 response "$response"
  137. return 0
  138. }