dns_mijnhost.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_mijnhost_info='mijn.host
  4. Site: mijn.host
  5. Docs: https://github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_mijnhost
  6. Options:
  7. MIJNHOST_API_KEY API Key
  8. Issues: github.com/acmesh-official/acme.sh/issues/6177
  9. Author: @peterv99
  10. '
  11. ######## Public functions ######################
  12. MIJNHOST_API="https://mijn.host/api/v2"
  13. # Add TXT record for domain verification
  14. dns_mijnhost_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. MIJNHOST_API_KEY="${MIJNHOST_API_KEY:-$(_readaccountconf_mutable MIJNHOST_API_KEY)}"
  18. if [ -z "$MIJNHOST_API_KEY" ]; then
  19. MIJNHOST_API_KEY=""
  20. _err "You haven't specified your mijn-host API key yet."
  21. _err "Please add MIJNHOST_API_KEY to the env."
  22. return 1
  23. fi
  24. # Save the API key for future use
  25. _saveaccountconf_mutable MIJNHOST_API_KEY "$MIJNHOST_API_KEY"
  26. _debug "First detect the root zone"
  27. if ! _get_root "$fulldomain"; then
  28. _err "Invalid domain"
  29. return 1
  30. fi
  31. _debug2 _sub_domain "$_sub_domain"
  32. _debug2 _domain "$_domain"
  33. _debug "Adding DNS record" "${fulldomain}."
  34. # Construct the API URL
  35. api_url="$MIJNHOST_API/domains/$_domain/dns"
  36. # Getting previous records
  37. _mijnhost_rest GET "$api_url" ""
  38. if [ "$_code" != "200" ]; then
  39. _err "Error getting current DNS enties ($_code)"
  40. return 1
  41. fi
  42. records=$(echo "$response" | _egrep_o '"records":\[.*\]' | sed 's/"records"://')
  43. _debug2 "Current records" "$records"
  44. # Build the payload for the API
  45. data="{\"type\":\"TXT\",\"name\":\"$fulldomain.\",\"value\":\"$txtvalue\",\"ttl\":300}"
  46. _debug2 "Record to add" "$data"
  47. # Updating the records
  48. updated_records=$(echo "$records" | sed -E "s/\]( *$)/,$data\]/")
  49. _debug2 "Updated records" "$updated_records"
  50. # data
  51. data="{\"records\": $updated_records}"
  52. _mijnhost_rest PUT "$api_url" "$data"
  53. if [ "$_code" = "200" ]; then
  54. _info "DNS record succesfully added."
  55. return 0
  56. else
  57. _err "Error adding DNS record ($_code)."
  58. return 1
  59. fi
  60. }
  61. # Remove TXT record after verification
  62. dns_mijnhost_rm() {
  63. fulldomain=$1
  64. txtvalue=$2
  65. MIJNHOST_API_KEY="${MIJNHOST_API_KEY:-$(_readaccountconf_mutable MIJNHOST_API_KEY)}"
  66. if [ -z "$MIJNHOST_API_KEY" ]; then
  67. MIJNHOST_API_KEY=""
  68. _err "You haven't specified your mijn-host API key yet."
  69. _err "Please add MIJNHOST_API_KEY to the env."
  70. return 1
  71. fi
  72. _debug "Detecting root zone for" "${fulldomain}."
  73. if ! _get_root "$fulldomain"; then
  74. _err "Invalid domain"
  75. return 1
  76. fi
  77. _debug "Removing DNS record for TXT value" "${txtvalue}."
  78. # Construct the API URL
  79. api_url="$MIJNHOST_API/domains/$_domain/dns"
  80. # Get current records
  81. _mijnhost_rest GET "$api_url" ""
  82. if [ "$_code" != "200" ]; then
  83. _err "Error getting current DNS enties ($_code)"
  84. return 1
  85. fi
  86. _debug2 "Get current records response:" "$response"
  87. records=$(echo "$response" | _egrep_o '"records":\[.*\]' | sed 's/"records"://')
  88. _debug2 "Current records:" "$records"
  89. updated_records=$(echo "$records" | sed -E "s/\{[^}]*\"value\":\"$txtvalue\"[^}]*\},?//g" | sed 's/,]/]/g')
  90. _debug2 "Updated records:" "$updated_records"
  91. # Build the new payload
  92. data="{\"records\": $updated_records}"
  93. # Use the _put method to update the records
  94. _mijnhost_rest PUT "$api_url" "$data"
  95. if [ "$_code" = "200" ]; then
  96. _info "DNS record removed successfully."
  97. return 0
  98. else
  99. _err "Error removing DNS record ($_code)."
  100. return 1
  101. fi
  102. }
  103. # Helper function to detect the root zone
  104. _get_root() {
  105. domain=$1
  106. # Get current records
  107. _debug "Getting current domains"
  108. _mijnhost_rest GET "$MIJNHOST_API/domains" ""
  109. if [ "$_code" != "200" ]; then
  110. _err "error getting current domains ($_code)"
  111. return 1
  112. fi
  113. # Extract root domains from response
  114. rootDomains=$(echo "$response" | _egrep_o '"domain":"[^"]*"' | sed -E 's/"domain":"([^"]*)"/\1/')
  115. _debug "Root domains:" "$rootDomains"
  116. for rootDomain in $rootDomains; do
  117. if _contains "$domain" "$rootDomain"; then
  118. _domain="$rootDomain"
  119. _sub_domain=$(echo "$domain" | sed "s/.$rootDomain//g")
  120. _debug "Found root domain" "$_domain" "and subdomain" "$_sub_domain" "for" "$domain"
  121. return 0
  122. fi
  123. done
  124. return 1
  125. }
  126. # Helper function for rest calls
  127. _mijnhost_rest() {
  128. m=$1
  129. ep="$2"
  130. data="$3"
  131. MAX_REQUEST_RETRY_TIMES=15
  132. _request_retry_times=0
  133. _retry_sleep=5 #Initial sleep time in seconds.
  134. while [ "${_request_retry_times}" -lt "$MAX_REQUEST_RETRY_TIMES" ]; do
  135. _debug2 _request_retry_times "$_request_retry_times"
  136. export _H1="API-Key: $MIJNHOST_API_KEY"
  137. export _H2="Content-Type: application/json"
  138. # clear headers from previous request to avoid getting wrong http code on timeouts
  139. : >"$HTTP_HEADER"
  140. _debug "$ep"
  141. if [ "$m" != "GET" ]; then
  142. _debug2 "data $data"
  143. response="$(_post "$data" "$ep" "" "$m")"
  144. else
  145. response="$(_get "$ep")"
  146. fi
  147. _ret="$?"
  148. _debug2 "response $response"
  149. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  150. _debug "http response code $_code"
  151. if [ "$_code" = "401" ]; then
  152. # we have an invalid API token, maybe it is expired?
  153. _err "Access denied. Invalid API token."
  154. return 1
  155. fi
  156. if [ "$_ret" != "0" ] || [ -z "$_code" ] || [ "$_code" = "400" ] || _contains "$response" "DNS records not managed by mijn.host"; then #Sometimes API errors out
  157. _request_retry_times="$(_math "$_request_retry_times" + 1)"
  158. _info "REST call error $_code retrying $ep in ${_retry_sleep}s"
  159. _sleep "$_retry_sleep"
  160. _retry_sleep="$(_math "$_retry_sleep" \* 2)"
  161. continue
  162. fi
  163. break
  164. done
  165. if [ "$_request_retry_times" = "$MAX_REQUEST_RETRY_TIMES" ]; then
  166. _err "Error mijn.host API call was retried $MAX_REQUEST_RETRY_TIMES times."
  167. _err "Calling $ep failed."
  168. return 1
  169. fi
  170. response="$(echo "$response" | _normalizeJson)"
  171. return 0
  172. }