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.

178 lines
4.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
  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. if [ "$response" = "{}" ]; then
  51. _info "Added, sleeping 10 seconds"
  52. _sleep 10
  53. #todo: check if the record takes effect
  54. return 0
  55. else
  56. _err "Add txt record error."
  57. _err "$response"
  58. return 1
  59. fi
  60. fi
  61. _err "Add txt record error."
  62. }
  63. #fulldomain
  64. dns_gd_rm() {
  65. fulldomain=$1
  66. txtvalue=$2
  67. GD_Key="${GD_Key:-$(_readaccountconf_mutable GD_Key)}"
  68. GD_Secret="${GD_Secret:-$(_readaccountconf_mutable GD_Secret)}"
  69. _debug "First detect the root zone"
  70. if ! _get_root "$fulldomain"; then
  71. _err "invalid domain"
  72. return 1
  73. fi
  74. _debug _sub_domain "$_sub_domain"
  75. _debug _domain "$_domain"
  76. _debug "Getting existing records"
  77. if ! _gd_rest GET "domains/$_domain/records/TXT/$_sub_domain"; then
  78. return 1
  79. fi
  80. if ! _contains "$response" "$txtvalue"; then
  81. _info "The record is not existing, skip"
  82. return 0
  83. fi
  84. _add_data=""
  85. for t in $(echo "$response" | tr '{' "\n" | grep "\"name\":\"$_sub_domain\"" | tr ',' "\n" | grep '"data"' | cut -d : -f 2); do
  86. _debug2 t "$t"
  87. if [ "$t" ] && [ "$t" != "\"$txtvalue\"" ]; then
  88. if [ "$_add_data" ]; then
  89. _add_data="$_add_data,{\"data\":$t}"
  90. else
  91. _add_data="{\"data\":$t}"
  92. fi
  93. fi
  94. done
  95. if [ -z "$_add_data" ]; then
  96. _add_data="{\"data\":\"\"}"
  97. fi
  98. _debug2 _add_data "$_add_data"
  99. _gd_rest PUT "domains/$_domain/records/TXT/$_sub_domain" "[$_add_data]"
  100. }
  101. #################### Private functions below ##################################
  102. #_acme-challenge.www.domain.com
  103. #returns
  104. # _sub_domain=_acme-challenge.www
  105. # _domain=domain.com
  106. _get_root() {
  107. domain=$1
  108. i=2
  109. p=1
  110. while true; do
  111. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  112. if [ -z "$h" ]; then
  113. #not valid
  114. return 1
  115. fi
  116. if ! _gd_rest GET "domains/$h"; then
  117. return 1
  118. fi
  119. if _contains "$response" '"code":"NOT_FOUND"'; then
  120. _debug "$h not found"
  121. else
  122. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  123. _domain="$h"
  124. return 0
  125. fi
  126. p="$i"
  127. i=$(_math "$i" + 1)
  128. done
  129. return 1
  130. }
  131. _gd_rest() {
  132. m=$1
  133. ep="$2"
  134. data="$3"
  135. _debug "$ep"
  136. export _H1="Authorization: sso-key $GD_Key:$GD_Secret"
  137. export _H2="Content-Type: application/json"
  138. if [ "$data" ]; then
  139. _debug data "$data"
  140. response="$(_post "$data" "$GD_Api/$ep" "" "$m")"
  141. else
  142. response="$(_get "$GD_Api/$ep")"
  143. fi
  144. if [ "$?" != "0" ]; then
  145. _err "error $ep"
  146. return 1
  147. fi
  148. _debug2 response "$response"
  149. return 0
  150. }