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.

215 lines
5.8 KiB

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