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.

118 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. #################### Private functions bellow ##################################
  45. #_acme-challenge.www.domain.com
  46. #returns
  47. # _sub_domain=_acme-challenge.www
  48. # _domain=domain.com
  49. # _domain_id=sdjkglgdfewsdfg
  50. _get_root() {
  51. domain=$1
  52. i=2
  53. p=1
  54. while [ '1' ] ; do
  55. h=$(printf $domain | cut -d . -f $i-100)
  56. if [ -z "$h" ] ; then
  57. #not valid
  58. return 1;
  59. fi
  60. if ! _gd_rest GET "domains/$h" ; then
  61. return 1
  62. fi
  63. if printf "$response" | grep '"code":"NOT_FOUND"' >/dev/null ; then
  64. _debug "$h not found"
  65. else
  66. _sub_domain=$(printf $domain | cut -d . -f 1-$p)
  67. _domain=$h
  68. return 0
  69. fi
  70. p=$i
  71. i=$(expr $i + 1)
  72. done
  73. return 1
  74. }
  75. _gd_rest() {
  76. m=$1
  77. ep="$2"
  78. data="$3"
  79. _debug $ep
  80. _H1="Authorization: sso-key $GD_Key:$GD_Secret"
  81. _H2="Content-Type: application/json"
  82. if [ "$data" ] ; then
  83. _debug data "$data"
  84. response="$(_post "$data" "$GD_Api/$ep" "" $m)"
  85. else
  86. response="$(_get "$GD_Api/$ep")"
  87. fi
  88. if [ "$?" != "0" ] ; then
  89. _err "error $ep"
  90. return 1
  91. fi
  92. _debug2 response "$response"
  93. return 0
  94. }