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.

146 lines
3.5 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 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. extracted_domain="${fulldomain#*_acme-challenge.}"
  37. # Construct the API URL
  38. api_url="$MIJN_HOST_API/domains/$extracted_domain/dns"
  39. # Getting preivous records
  40. get_response="$(_get "$api_url")"
  41. records=$(echo "$get_response" | jq -r '.data.records')
  42. # Updating the records
  43. updated_records=$(echo "$records" | jq --argjson data "$data" '. += [$data]')
  44. # data
  45. data="{\"records\": $updated_records}"
  46. # Use the _post method to make the API request
  47. response="$(_post "$data" "$api_url" "" "PUT")"
  48. if _contains "$response" "error"; then
  49. _err "Error adding TXT record: $response"
  50. return 1
  51. fi
  52. _info "TXT record added successfully"
  53. return 0
  54. }
  55. # Remove TXT record after verification
  56. dns_mijn_host_rm() {
  57. fulldomain=$1
  58. txtvalue=$2
  59. MIJN_HOST_API_KEY="${MIJN_HOST_API_KEY:-$(_readaccountconf_mutable MIJN_HOST_API_KEY)}"
  60. if [ -z "$MIJN_HOST_API_KEY" ]; then
  61. MIJN_HOST_API_KEY=""
  62. _err "You haven't specified mijn-host API key yet."
  63. return 1
  64. fi
  65. _debug "First detect the root zone"
  66. if ! _get_root "$fulldomain"; then
  67. _err "Invalid domain"
  68. return 1
  69. fi
  70. _debug "Removing TXT record"
  71. # Build the payload for the API
  72. export _H1="API-Key: $MIJN_HOST_API_KEY"
  73. export _H2="Content-Type: application/json"
  74. extracted_domain="${fulldomain#*_acme-challenge.}"
  75. # Construct the API URL
  76. api_url="$MIJN_HOST_API/domains/$extracted_domain/dns"
  77. # Get current records
  78. response="$(_get "$api_url")"
  79. updated_records=$(echo "$response" | jq '.data.records')
  80. updated_records=$(echo "$updated_records" | jq --arg value "$txtvalue" 'map(select(.value != $value))')
  81. # Build the new payload
  82. data="{\"records\": $updated_records}"
  83. # Use the _put method to update the records
  84. response="$(_post "$data" "$api_url" "" "PUT")"
  85. if _contains "$response" "error"; then
  86. _err "Error updating TXT record: $response"
  87. return 1
  88. fi
  89. _info "TXT record removed successfully"
  90. return 0
  91. }
  92. # Helper function to detect the root zone
  93. _get_root() {
  94. domain=$1
  95. i=2
  96. p=1
  97. while true; do
  98. h=$(printf "%s" "$domain" | cut -d . -f $i-)
  99. if [ -z "$h" ]; then
  100. return 1
  101. fi
  102. if _contains "$(dig ns "$h")" "mijn.host"; then
  103. root_zone="$h"
  104. subdomain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  105. return 0
  106. fi
  107. p=$i
  108. i=$(_math "$i" + 1)
  109. done
  110. return 1
  111. }