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.

162 lines
4.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #!/usr/bin/env sh
  2. #NETLIFY_ACCESS_TOKEN="xxxx"
  3. NETLIFY_HOST="api.netlify.com/api/v1/"
  4. NETLIFY_URL="https://$NETLIFY_HOST"
  5. ######## Public functions #####################
  6. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  7. dns_netlify_add() {
  8. fulldomain=$1
  9. txtvalue=$2
  10. NETLIFY_ACCESS_TOKEN="${NETLIFY_ACCESS_TOKEN:-$(_readaccountconf_mutable NETLIFY_ACCESS_TOKEN)}"
  11. if [ -z "$NETLIFY_ACCESS_TOKEN" ]; then
  12. NETLIFY_ACCESS_TOKEN=""
  13. _err "Please specify your Netlify Access Token and try again."
  14. return 1
  15. else
  16. _saveaccountconf_mutable NETLIFY_ACCESS_TOKEN "$NETLIFY_ACCESS_TOKEN"
  17. fi
  18. _info "Using Netlify"
  19. _debug fulldomain "$fulldomain"
  20. _debug txtvalue "$txtvalue"
  21. if ! _get_root "$fulldomain"; then
  22. _err "invalid domain"
  23. return 1
  24. fi
  25. _debug _domain_id "$_domain_id"
  26. _debug _sub_domain "$_sub_domain"
  27. _debug _domain "$_domain"
  28. dnsRecordURI="dns_zones/$_domain_id/dns_records"
  29. body="{\"type\":\"TXT\", \"hostname\":\"$_sub_domain\", \"value\":\"$txtvalue\", \"ttl\":\"10\"}"
  30. _netlify_rest POST "$dnsRecordURI" "$body" "$NETLIFY_ACCESS_TOKEN"
  31. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  32. if [ "$_code" = "200" ] || [ "$_code" = '201' ]; then
  33. _info "validation value added"
  34. return 0
  35. else
  36. _err "error adding validation value ($_code)"
  37. return 1
  38. fi
  39. _err "Not fully implemented!"
  40. return 1
  41. }
  42. #Usage: dns_myapi_rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  43. #Remove the txt record after validation.
  44. dns_netlify_rm() {
  45. _info "Using Netlify"
  46. txtdomain="$1"
  47. txt="$2"
  48. _debug txtdomain "$txtdomain"
  49. _debug txt "$txt"
  50. NETLIFY_ACCESS_TOKEN="${NETLIFY_ACCESS_TOKEN:-$(_readaccountconf_mutable NETLIFY_ACCESS_TOKEN)}"
  51. if ! _get_root "$txtdomain"; then
  52. _err "invalid domain"
  53. return 1
  54. fi
  55. _debug _domain_id "$_domain_id"
  56. _debug _sub_domain "$_sub_domain"
  57. _debug _domain "$_domain"
  58. dnsRecordURI="dns_zones/$_domain_id/dns_records"
  59. _netlify_rest GET "$dnsRecordURI" "" "$NETLIFY_ACCESS_TOKEN"
  60. _record_id=$(echo "$response" | _egrep_o "\"type\":\"TXT\",[^\}]*\"value\":\"$txt\"" | head -n 1 | _egrep_o "\"id\":\"[^\"\}]*\"" | cut -d : -f 2 | tr -d \")
  61. _debug _record_id "$_record_id"
  62. if [ "$_record_id" ]; then
  63. _netlify_rest DELETE "$dnsRecordURI/$_record_id" "" "$NETLIFY_ACCESS_TOKEN"
  64. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  65. if [ "$_code" = "200" ] || [ "$_code" = '204' ]; then
  66. _info "validation value removed"
  67. return 0
  68. else
  69. _err "error removing validation value ($_code)"
  70. return 1
  71. fi
  72. return 0
  73. fi
  74. return 1
  75. }
  76. #################### Private functions below ##################################
  77. _get_root() {
  78. domain=$1
  79. accesstoken=$2
  80. i=1
  81. p=1
  82. _netlify_rest GET "dns_zones" "" "$accesstoken"
  83. while true; do
  84. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  85. _debug2 "Checking domain: $h"
  86. if [ -z "$h" ]; then
  87. #not valid
  88. _err "Invalid domain"
  89. return 1
  90. fi
  91. if _contains "$response" "\"name\":\"$h\"" >/dev/null; then
  92. _domain_id=$(echo "$response" | _egrep_o "\"[^\"]*\",\"name\":\"$h\"" | cut -d , -f 1 | tr -d \")
  93. if [ "$_domain_id" ]; then
  94. if [ "$i" = 1 ]; then
  95. #create the record at the domain apex (@) if only the domain name was provided as --domain-alias
  96. _sub_domain="@"
  97. else
  98. _sub_domain=$(echo "$domain" | cut -d . -f 1-$p)
  99. fi
  100. _domain=$h
  101. return 0
  102. fi
  103. return 1
  104. fi
  105. p=$i
  106. i=$(_math "$i" + 1)
  107. done
  108. return 1
  109. }
  110. _netlify_rest() {
  111. m=$1
  112. ep="$2"
  113. data="$3"
  114. _debug "$ep"
  115. token_trimmed=$(echo "$NETLIFY_ACCESS_TOKEN" | tr -d '"')
  116. export _H1="Content-Type: application/json"
  117. export _H2="Authorization: Bearer $token_trimmed"
  118. : >"$HTTP_HEADER"
  119. if [ "$m" != "GET" ]; then
  120. _debug data "$data"
  121. response="$(_post "$data" "$NETLIFY_URL$ep" "" "$m")"
  122. else
  123. response="$(_get "$NETLIFY_URL$ep")"
  124. fi
  125. if [ "$?" != "0" ]; then
  126. _err "error $ep"
  127. return 1
  128. fi
  129. _debug2 response "$response"
  130. return 0
  131. }