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.

143 lines
3.4 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
  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. MIJN_HOST_ENDPOINT_API API Endpoint URL. E.g. "https://mijn.host/api/v2"
  10. '
  11. ######## Public functions ###################### Constants for your mijn-host API
  12. MIJN_HOST_API="https://mijn.host/api/v2"
  13. # Add TXT record for domain verification
  14. dns_mijn_host_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. MIJN_HOST_API_KEY="${MIJN_HOST_API_KEY:-$(_readaccountconf_mutable MIJN_HOST_API_KEY)}"
  18. if [ -z "$MIJN_HOST_API_KEY" ]; then
  19. MIJN_HOST_API_KEY=""
  20. _err "You haven't specified mijn-host API key yet."
  21. _err "Please set it and try again."
  22. return 1
  23. fi
  24. # Save the API key for future use
  25. _saveaccountconf_mutable MIJN_HOST_API_KEY "$MIJN_HOST_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. _debug "Add TXT record"
  32. # Build the payload for the API
  33. data="{\"type\":\"TXT\",\"name\":\"$subdomain\",\"value\":\"$txtvalue\",\"ttl\":120}"
  34. export _H1="API-Key: $MIJN_HOST_API_KEY"
  35. export _H2="Content-Type: application/json"
  36. # Construct the API URL
  37. api_url="$MIJN_HOST_API/domains/$_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. # Construct the API URL
  74. api_url="$MIJN_HOST_API/domains/$_domain/dns"
  75. # Get current records
  76. response="$(_get "$api_url")"
  77. updated_records=$(echo "$response" | jq '.data.records')
  78. updated_records=$(echo "$updated_records" | jq --arg value "$txtvalue" 'map(select(.value != $value))')
  79. # Build the new payload
  80. data="{\"records\": $updated_records}"
  81. # Use the _put method to update the records
  82. response="$(_post "$data" "$api_url" "" "PUT")"
  83. if _contains "$response" "error"; then
  84. _err "Error updating TXT record: $response"
  85. return 1
  86. fi
  87. _info "TXT record removed successfully"
  88. return 0
  89. }
  90. # Helper function to detect the root zone
  91. _get_root() {
  92. domain=$1
  93. i=2
  94. p=1
  95. while true; do
  96. h=$(printf "%s" "$domain" | cut -d . -f $i-)
  97. if [ -z "$h" ]; then
  98. return 1
  99. fi
  100. if _contains "$(dig ns "$h")" "mijn.host"; then
  101. root_zone="$h"
  102. subdomain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  103. return 0
  104. fi
  105. p=$i
  106. i=$(_math "$i" + 1)
  107. done
  108. return 1
  109. }