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.

155 lines
3.9 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://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" "\"code\":300"; then
  71. _record_id=$(printf "%s" "$response" | grep '"resultid"' | cut -d : -f 2 | cut -d , -f 1 | tr -d '\r' | tr -d '\n')
  72. _debug _record_id "$_record_id"
  73. fi
  74. _contains "$response" "\"code\":300"
  75. }
  76. #################### Private functions below ##################################
  77. #_acme-challenge.www.domain.com
  78. #returns
  79. # _sub_domain=_acme-challenge.www
  80. # _domain=domain.com
  81. # _domain_id=sdjkglgdfewsdfg
  82. _get_root() {
  83. domain=$1
  84. i=2
  85. p=1
  86. while true; do
  87. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  88. if [ -z "$h" ]; then
  89. #not valid
  90. return 1
  91. fi
  92. if ! _rest GET "domain.ashx?cmd=get&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domain=$h"; then
  93. return 1
  94. fi
  95. if _contains "$response" "\"code\":300"; then
  96. _domain_id=$(printf "%s" "$response" | grep '"domainid"' | cut -d : -f 2 | cut -d , -f 1 | tr -d '\r' | tr -d '\n')
  97. _debug _domain_id "$_domain_id"
  98. if [ "$_domain_id" ]; then
  99. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  100. _debug _sub_domain "$_sub_domain"
  101. _domain="$h"
  102. _debug _domain "$_domain"
  103. return 0
  104. fi
  105. return 1
  106. fi
  107. p="$i"
  108. i=$(_math "$i" + 1)
  109. done
  110. return 1
  111. }
  112. #Usage: method URI data
  113. _rest() {
  114. m="$1"
  115. ep="$2"
  116. data="$3"
  117. _debug "$ep"
  118. url="$DNSLA_API$ep"
  119. _debug url "$url"
  120. if [ "$m" = "GET" ]; then
  121. response="$(_get "$url" | tr -d ' ' | tr "}" ",")"
  122. else
  123. _debug2 data "$data"
  124. response="$(_post "$data" "$url" | tr -d ' ' | tr "}" ",")"
  125. fi
  126. if [ "$?" != "0" ]; then
  127. _err "error $ep"
  128. return 1
  129. fi
  130. _debug2 response "$response"
  131. return 0
  132. }