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.

123 lines
2.9 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 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://api.mijn-host.com/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\", \"content\": \"$txtvalue\", \"ttl\": 120}"
  34. export _H1="Authorization: Bearer $MIJN_HOST_API_KEY"
  35. export _H2="Content-Type: application/json"
  36. # Use the _post method to make the API request
  37. response="$(_post "$data" "$MIJN_HOST_API/zones/$root_zone/records")"
  38. if _contains "$response" "error"; then
  39. _err "Error adding TXT record: $response"
  40. return 1
  41. fi
  42. _info "TXT record added successfully"
  43. return 0
  44. }
  45. # Remove TXT record after verification
  46. dns_mijn_host_rm() {
  47. fulldomain=$1
  48. txtvalue=$2
  49. MIJN_HOST_API_KEY="${MIJN_HOST_API_KEY:-$(_readaccountconf_mutable MIJN_HOST_API_KEY)}"
  50. if [ -z "$MIJN_HOST_API_KEY" ]; then
  51. MIJN_HOST_API_KEY=""
  52. _err "You haven't specified mijn-host API key yet."
  53. return 1
  54. fi
  55. _debug "First detect the root zone"
  56. if ! _get_root "$fulldomain"; then
  57. _err "Invalid domain"
  58. return 1
  59. fi
  60. _debug "Removing TXT record"
  61. # Build the payload for the API
  62. export _H1="Authorization: Bearer $MIJN_HOST_API_KEY"
  63. # Use the _get method to find the record ID for deletion
  64. record_id="$(_get "$MIJN_HOST_API/zones/$root_zone/records?type=TXT&name=$subdomain")"
  65. if [ -z "$record_id" ]; then
  66. _err "TXT record not found"
  67. return 1
  68. fi
  69. # Delete the TXT record
  70. response="$(_post "" "$MIJN_HOST_API/zones/$root_zone/records/$record_id" "DELETE")"
  71. if _contains "$response" "error"; then
  72. _err "Error removing TXT record: $response"
  73. return 1
  74. fi
  75. _info "TXT record removed successfully"
  76. return 0
  77. }
  78. # Helper function to detect the root zone
  79. _get_root() {
  80. domain=$1
  81. i=2
  82. p=1
  83. while true; do
  84. h=$(printf "%s" "$domain" | cut -d . -f $i-)
  85. if [ -z "$h" ]; then
  86. return 1
  87. fi
  88. if _contains "$(dig ns "$h")" "mijn.host"; then
  89. root_zone="$h"
  90. subdomain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  91. return 0
  92. fi
  93. p=$i
  94. i=$(_math "$i" + 1)
  95. done
  96. return 1
  97. }