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.

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