Browse Source

Consolidate API request logic in dns_mgwm.sh

Refactor DNS API functions to use a unified request handler.
pull/6671/head
Markus G. 4 days ago
committed by neil
parent
commit
d8722c46d9
  1. 100
      dnsapi/dns_mgwm.sh

100
dnsapi/dns_mgwm.sh

@ -1,6 +1,5 @@
#!/usr/bin/env sh
# shellcheck disable=SC2034
# DNS provider information for acme.sh
dns_mgwm_info='mgw-media.de
Site: mgw-media.de
@ -10,87 +9,66 @@ Options:
MGWM_API_HASH Your API Hash
Issues: github.com/acmesh-official/acme.sh
'
# Base URL for the mgw-media.de API
MGWM_API_BASE="https://api.mgw-media.de/record"
######## Public functions #####################
# This function is called by acme.sh to add a TXT record.
dns_mgwm_add() {
fulldomain=$1
txtvalue=$2
_info "Using mgw-media.de DNS API for domain $fulldomain"
_info "Using mgw-media.de DNS API for domain $fulldomain (add record)"
_debug "fulldomain: $fulldomain"
_debug "txtvalue: $txtvalue"
# Call private function to load and save environment variables and set up the Basic Auth Header.
if ! _mgwm_init_env; then
return 1
fi
# Construct the API URL for adding a record.
#_add_url="${MGWM_API_BASE}/add/${fulldomain}/txt/${txtvalue}"
_add_url="${MGWM_API_BASE}.php?action=add&fulldomain=${fulldomain}&type=txt&content=${txtvalue}"
_debug "Calling MGWM ADD URL: ${_add_url}"
# Execute the HTTP GET request with the Authorization Header.
# The 5th parameter of _get is where acme.sh expects custom HTTP headers like Authorization.
response="$(_get "$_add_url")"
_debug "MGWM add response: $response"
# Check the API response for success. The API returns "OK" on success.
if [ "$response" = "OK" ]; then
# Call the new private function to handle the API request.
# The 'add' action, fulldomain, type 'txt' and txtvalue are passed.
if _mgwm_perform_api_request "add" "$fulldomain" "txt" "$txtvalue"; then
_info "TXT record for $fulldomain successfully added via MGWM API."
_sleep 10 # Wait briefly for DNS propagation, a common practice in DNS-01 hooks.
return 0
else
_err "mgwm_add: Failed to add TXT record for $fulldomain. Unexpected API Response: '$response'"
# Error message already logged by _mgwm_perform_api_request, but a specific one here helps.
_err "mgwm_add: Failed to add TXT record for $fulldomain."
return 1
fi
}
# This function is called by acme.sh to remove a TXT record after validation.
dns_mgwm_rm() {
fulldomain=$1
txtvalue=$2 # This txtvalue is now used to identify the specific record to be removed.
_info "Removing TXT record for $fulldomain using mgw-media.de DNS API"
_info "Removing TXT record for $fulldomain using mgw-media.de DNS API (remove record)"
_debug "fulldomain: $fulldomain"
_debug "txtvalue: $txtvalue"
# Call private function to load and save environment variables and set up the Basic Auth Header.
if ! _mgwm_init_env; then
return 1
fi
# Construct the API URL for removing a record.
# To delete a specific record by its value (as required by ACME v2 for multiple TXT records),
# the txtvalue must be part of the URL, similar to the add action.
#_rm_url="${MGWM_API_BASE}/rm/${fulldomain}/txt/${txtvalue}"
_rm_url="${MGWM_API_BASE}.php?action=rm&fulldomain=${fulldomain}&type=txt&content=${txtvalue}"
_debug "Calling MGWM RM URL: ${_rm_url}"
# Execute the HTTP GET request with the Authorization Header.
response="$(_get "$_rm_url")"
_debug "MGWM rm response: $response"
# Check the API response for success. The API returns "OK" on success.
if [ "$response" = "OK" ]; then
# Call the new private function to handle the API request.
# The 'rm' action, fulldomain, type 'txt' and txtvalue are passed.
if _mgwm_perform_api_request "rm" "$fulldomain" "txt" "$txtvalue"; then
_info "TXT record for $fulldomain successfully removed via MGWM API."
return 0
else
_err "mgwm_rm: Failed to remove TXT record for $fulldomain. Unexpected API Response: '$response'"
# Error message already logged by _mgwm_perform_api_request, but a specific one here helps.
_err "mgwm_rm: Failed to remove TXT record for $fulldomain."
return 1
fi
}
#################### Private functions below ##################################
# _mgwm_init_env() loads the mgw-media.de API credentials (customer number and hash)
# from environment variables or acme.sh's configuration, saves them, and
# prepares the global _H1 variable for Basic Authorization header.
_mgwm_init_env() {
# _mgwm_perform_api_request() encapsulates the API call logic, including
# loading credentials, setting the Authorization header, and executing the request.
# Arguments:
# $1: action (e.g., "add", "rm")
# $2: fulldomain
# $3: type (e.g., "txt")
# $4: content (the txtvalue)
_mgwm_perform_api_request() {
_action="$1"
_fulldomain="$2"
_type="$3"
_content="$4"
_debug "Calling _mgwm_perform_api_request for action: $_action, domain: $_fulldomain, type: $_type, content: $_content"
# --- Start of _mgwm_init_env logic (now embedded here) ---
# Load credentials from environment or acme.sh config
MGWM_CUSTOMER="${MGWM_CUSTOMER:-$(_readaccountconf_mutable MGWM_CUSTOMER)}"
MGWM_API_HASH="${MGWM_API_HASH:-$(_readaccountconf_mutable MGWM_API_HASH)}"
@ -110,5 +88,25 @@ _mgwm_init_env() {
_credentials="$(printf "%s:%s" "$MGWM_CUSTOMER" "$MGWM_API_HASH" | _base64)"
export _H1="Authorization: Basic $_credentials"
_debug "Set Authorization Header: Basic <credentials_encoded>" # Log debug message without sensitive credentials
return 0
# --- End of _mgwm_init_env logic ---
# Construct the API URL based on the action and provided parameters.
_request_url="${MGWM_API_BASE}.php?action=${_action}&fulldomain=${_fulldomain}&type=${_type}&content=${_content}"
_debug "Constructed MGWM API URL for action '$_action': ${_request_url}"
# Execute the HTTP GET request with the Authorization Header.
# The 5th parameter of _get is where acme.sh expects custom HTTP headers like Authorization.
response="$(_get "$_request_url")"
_debug "MGWM API response for action '$_action': $response"
# Check the API response for success. The API returns "OK" on success.
if [ "$response" = "OK" ]; then
_info "MGWM API action '$_action' for record '$_fulldomain' successful."
return 0
else
_err "mgwm_perform_api_request: Failed API action '$_action' for record '$_fulldomain'. Unexpected API Response: '$response'"
return 1
fi
}
# The original _mgwm_init_env function is now removed as its logic is integrated into _mgwm_perform_api_request.
Loading…
Cancel
Save