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.

115 lines
2.3 KiB

  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. if [ -z "$GD_Key" ] || [ -z "$GD_Secret" ]; then
  14. _err "You don't specify godaddy api key and secret yet."
  15. _err "Please create you key and try again."
  16. return 1
  17. fi
  18. #save the api key and email to the account conf file.
  19. _saveaccountconf GD_Key "$GD_Key"
  20. _saveaccountconf GD_Secret "$GD_Secret"
  21. _debug "First detect the root zone"
  22. if ! _get_root "$fulldomain"; then
  23. _err "invalid domain"
  24. return 1
  25. fi
  26. _debug _sub_domain "$_sub_domain"
  27. _debug _domain "$_domain"
  28. _info "Adding record"
  29. if _gd_rest PUT "domains/$_domain/records/TXT/$_sub_domain" "[{\"data\":\"$txtvalue\"}]"; then
  30. if [ "$response" = "{}" ]; then
  31. _info "Added, sleeping 10 seconds"
  32. sleep 10
  33. #todo: check if the record takes effect
  34. return 0
  35. else
  36. _err "Add txt record error."
  37. _err "$response"
  38. return 1
  39. fi
  40. fi
  41. _err "Add txt record error."
  42. }
  43. #fulldomain
  44. dns_gd_rm() {
  45. fulldomain=$1
  46. }
  47. #################### Private functions bellow ##################################
  48. #_acme-challenge.www.domain.com
  49. #returns
  50. # _sub_domain=_acme-challenge.www
  51. # _domain=domain.com
  52. _get_root() {
  53. domain=$1
  54. i=2
  55. p=1
  56. while true; do
  57. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  58. if [ -z "$h" ]; then
  59. #not valid
  60. return 1
  61. fi
  62. if ! _gd_rest GET "domains/$h"; then
  63. return 1
  64. fi
  65. if _contains "$response" '"code":"NOT_FOUND"'; then
  66. _debug "$h not found"
  67. else
  68. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  69. _domain="$h"
  70. return 0
  71. fi
  72. p="$i"
  73. i=$(_math "$i" + 1)
  74. done
  75. return 1
  76. }
  77. _gd_rest() {
  78. m=$1
  79. ep="$2"
  80. data="$3"
  81. _debug "$ep"
  82. _H1="Authorization: sso-key $GD_Key:$GD_Secret"
  83. _H2="Content-Type: application/json"
  84. if [ "$data" ]; then
  85. _debug data "$data"
  86. response="$(_post "$data" "$GD_Api/$ep" "" "$m")"
  87. else
  88. response="$(_get "$GD_Api/$ep")"
  89. fi
  90. if [ "$?" != "0" ]; then
  91. _err "error $ep"
  92. return 1
  93. fi
  94. _debug2 response "$response"
  95. return 0
  96. }