dns_gd.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/usr/bin/env sh
  2. #Godaddy domain api
  3. #
  4. #GD_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
  5. #
  6. #GD_Secret="asdfsdfsfsdfsdfdfsdf"
  7. GD_Api="https://api.godaddy.com/v1"
  8. ######## Public functions #####################
  9. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  10. dns_gd_add() {
  11. fulldomain=$1
  12. txtvalue=$2
  13. GD_Key="${GD_Key:-$(_readaccountconf_mutable GD_Key)}"
  14. GD_Secret="${GD_Secret:-$(_readaccountconf_mutable GD_Secret)}"
  15. if [ -z "$GD_Key" ] || [ -z "$GD_Secret" ]; then
  16. GD_Key=""
  17. GD_Secret=""
  18. _err "You don't specify godaddy api key and secret yet."
  19. _err "Please create you key and try again."
  20. return 1
  21. fi
  22. #save the api key and email to the account conf file.
  23. _saveaccountconf_mutable GD_Key "$GD_Key"
  24. _saveaccountconf_mutable GD_Secret "$GD_Secret"
  25. _debug "First detect the root zone"
  26. if ! _get_root "$fulldomain"; then
  27. _err "invalid domain"
  28. return 1
  29. fi
  30. _debug _sub_domain "$_sub_domain"
  31. _debug _domain "$_domain"
  32. _debug "Getting existing records"
  33. if ! _gd_rest GET "domains/$_domain/records/TXT/$_sub_domain"; then
  34. return 1
  35. fi
  36. if _contains "$response" "$txtvalue"; then
  37. _info "The record is existing, skip"
  38. return 0
  39. fi
  40. _add_data="{\"data\":\"$txtvalue\"}"
  41. for t in $(echo "$response" | tr '{' "\n" | grep "\"name\":\"$_sub_domain\"" | tr ',' "\n" | grep '"data"' | cut -d : -f 2); do
  42. _debug2 t "$t"
  43. if [ "$t" ]; then
  44. _add_data="$_add_data,{\"data\":$t}"
  45. fi
  46. done
  47. _debug2 _add_data "$_add_data"
  48. _info "Adding record"
  49. if _gd_rest PUT "domains/$_domain/records/TXT/$_sub_domain" "[$_add_data]"; then
  50. _info "Added, sleeping 10 seconds"
  51. _sleep 10
  52. #todo: check if the record takes effect
  53. return 0
  54. fi
  55. _err "Add txt record error."
  56. return 1
  57. }
  58. #fulldomain
  59. dns_gd_rm() {
  60. fulldomain=$1
  61. txtvalue=$2
  62. GD_Key="${GD_Key:-$(_readaccountconf_mutable GD_Key)}"
  63. GD_Secret="${GD_Secret:-$(_readaccountconf_mutable GD_Secret)}"
  64. _debug "First detect the root zone"
  65. if ! _get_root "$fulldomain"; then
  66. _err "invalid domain"
  67. return 1
  68. fi
  69. _debug _sub_domain "$_sub_domain"
  70. _debug _domain "$_domain"
  71. _debug "Getting existing records"
  72. if ! _gd_rest GET "domains/$_domain/records/TXT/$_sub_domain"; then
  73. return 1
  74. fi
  75. if ! _contains "$response" "$txtvalue"; then
  76. _info "The record is not existing, skip"
  77. return 0
  78. fi
  79. _add_data=""
  80. for t in $(echo "$response" | tr '{' "\n" | grep "\"name\":\"$_sub_domain\"" | tr ',' "\n" | grep '"data"' | cut -d : -f 2); do
  81. _debug2 t "$t"
  82. if [ "$t" ] && [ "$t" != "\"$txtvalue\"" ]; then
  83. if [ "$_add_data" ]; then
  84. _add_data="$_add_data,{\"data\":$t}"
  85. else
  86. _add_data="{\"data\":$t}"
  87. fi
  88. fi
  89. done
  90. if [ -z "$_add_data" ]; then
  91. _add_data="{\"data\":\"\"}"
  92. fi
  93. _debug2 _add_data "$_add_data"
  94. _gd_rest PUT "domains/$_domain/records/TXT/$_sub_domain" "[$_add_data]"
  95. }
  96. #################### Private functions below ##################################
  97. #_acme-challenge.www.domain.com
  98. #returns
  99. # _sub_domain=_acme-challenge.www
  100. # _domain=domain.com
  101. _get_root() {
  102. domain=$1
  103. i=2
  104. p=1
  105. while true; do
  106. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  107. if [ -z "$h" ]; then
  108. #not valid
  109. return 1
  110. fi
  111. if ! _gd_rest GET "domains/$h"; then
  112. return 1
  113. fi
  114. if _contains "$response" '"code":"NOT_FOUND"'; then
  115. _debug "$h not found"
  116. else
  117. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  118. _domain="$h"
  119. return 0
  120. fi
  121. p="$i"
  122. i=$(_math "$i" + 1)
  123. done
  124. return 1
  125. }
  126. _gd_rest() {
  127. m=$1
  128. ep="$2"
  129. data="$3"
  130. _debug "$ep"
  131. export _H1="Authorization: sso-key $GD_Key:$GD_Secret"
  132. export _H2="Content-Type: application/json"
  133. if [ "$data" ]; then
  134. _debug data "$data"
  135. response="$(_post "$data" "$GD_Api/$ep" "" "$m")"
  136. else
  137. response="$(_get "$GD_Api/$ep")"
  138. fi
  139. if [ "$?" != "0" ]; then
  140. _err "error $ep"
  141. return 1
  142. fi
  143. _debug2 response "$response"
  144. if _contains "$response" "UNABLE_TO_AUTHENTICATE"; then
  145. _err "It seems that your api key or secret is not correct."
  146. return 1
  147. fi
  148. return 0
  149. }