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.

116 lines
2.4 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 _domain_id "$_domain_id"
  27. _debug _sub_domain "$_sub_domain"
  28. _debug _domain "$_domain"
  29. _info "Adding record"
  30. if _gd_rest PUT "domains/$_domain/records/TXT/$_sub_domain" "[{\"data\":\"$txtvalue\"}]"; then
  31. if [ "$response" = "{}" ]; then
  32. _info "Added, sleeping 10 seconds"
  33. sleep 10
  34. #todo: check if the record takes effect
  35. return 0
  36. else
  37. _err "Add txt record error."
  38. _err "$response"
  39. return 1
  40. fi
  41. fi
  42. _err "Add txt record error."
  43. }
  44. #fulldomain
  45. dns_gd_rm() {
  46. fulldomain=$1
  47. }
  48. #################### Private functions bellow ##################################
  49. #_acme-challenge.www.domain.com
  50. #returns
  51. # _sub_domain=_acme-challenge.www
  52. # _domain=domain.com
  53. # _domain_id=sdjkglgdfewsdfg
  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=$(expr $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. }