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.

145 lines
3.4 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/api-3563900
  7. Options:
  8. MIJN_HOST_API_KEY API Key
  9. '
  10. ######## Public functions ###################### Constants for your mijn-host API
  11. MIJN_HOST_API="https://mijn.host/api/v2"
  12. # Add TXT record for domain verification
  13. dns_mijn_host_add() {
  14. fulldomain=$1
  15. txtvalue=$2
  16. MIJN_HOST_API_KEY="${MIJN_HOST_API_KEY:-$(_readaccountconf_mutable MIJN_HOST_API_KEY)}"
  17. if [ -z "$MIJN_HOST_API_KEY" ]; then
  18. MIJN_HOST_API_KEY=""
  19. _err "You haven't specified mijn-host API key yet."
  20. _err "Please set it and try again."
  21. return 1
  22. fi
  23. # Save the API key for future use
  24. _saveaccountconf_mutable MIJN_HOST_API_KEY "$MIJN_HOST_API_KEY"
  25. _debug "First detect the root zone"
  26. if ! _get_root "$fulldomain"; then
  27. _err "Invalid domain"
  28. return 1
  29. fi
  30. _debug "Add TXT record"
  31. # Build the payload for the API
  32. data="{\"type\":\"TXT\",\"name\":\"$subdomain\",\"value\":\"$txtvalue\",\"ttl\":120}"
  33. export _H1="API-Key: $MIJN_HOST_API_KEY"
  34. export _H2="Content-Type: application/json"
  35. extracted_domain="${fulldomain#*_acme-challenge.}"
  36. # Construct the API URL
  37. api_url="$MIJN_HOST_API/domains/$extracted_domain/dns"
  38. # Getting preivous records
  39. get_response="$(_get "$api_url")"
  40. records=$(echo "$get_response" | jq -r '.data.records')
  41. # Updating the records
  42. updated_records=$(echo "$records" | jq --argjson data "$data" '. += [$data]')
  43. # data
  44. data="{\"records\": $updated_records}"
  45. # Use the _post method to make the API request
  46. response="$(_post "$data" "$api_url" "" "PUT")"
  47. if _contains "$response" "error"; then
  48. _err "Error adding TXT record: $response"
  49. return 1
  50. fi
  51. _info "TXT record added successfully"
  52. return 0
  53. }
  54. # Remove TXT record after verification
  55. dns_mijn_host_rm() {
  56. fulldomain=$1
  57. txtvalue=$2
  58. MIJN_HOST_API_KEY="${MIJN_HOST_API_KEY:-$(_readaccountconf_mutable MIJN_HOST_API_KEY)}"
  59. if [ -z "$MIJN_HOST_API_KEY" ]; then
  60. MIJN_HOST_API_KEY=""
  61. _err "You haven't specified mijn-host API key yet."
  62. return 1
  63. fi
  64. _debug "First detect the root zone"
  65. if ! _get_root "$fulldomain"; then
  66. _err "Invalid domain"
  67. return 1
  68. fi
  69. _debug "Removing TXT record"
  70. # Build the payload for the API
  71. export _H1="API-Key: $MIJN_HOST_API_KEY"
  72. export _H2="Content-Type: application/json"
  73. extracted_domain="${fulldomain#*_acme-challenge.}"
  74. # Construct the API URL
  75. api_url="$MIJN_HOST_API/domains/$extracted_domain/dns"
  76. # Get current records
  77. response="$(_get "$api_url")"
  78. updated_records=$(echo "$response" | jq '.data.records')
  79. updated_records=$(echo "$updated_records" | jq --arg value "$txtvalue" 'map(select(.value != $value))')
  80. # Build the new payload
  81. data="{\"records\": $updated_records}"
  82. # Use the _put method to update the records
  83. response="$(_post "$data" "$api_url" "" "PUT")"
  84. if _contains "$response" "error"; then
  85. _err "Error updating TXT record: $response"
  86. return 1
  87. fi
  88. _info "TXT record removed successfully"
  89. return 0
  90. }
  91. # Helper function to detect the root zone
  92. _get_root() {
  93. domain=$1
  94. i=2
  95. p=1
  96. while true; do
  97. h=$(printf "%s" "$domain" | cut -d . -f "$i"-)
  98. if [ -z "$h" ]; then
  99. return 1
  100. fi
  101. if _contains "$(dig ns "$h")" "mijn.host"; then
  102. root_zone="$h"
  103. subdomain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  104. return 0
  105. fi
  106. p=$i
  107. i=$(_math "$i" + 1)
  108. done
  109. return 1
  110. }