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.

147 lines
3.7 KiB

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