|
| 1 | +#!/usr/bin/env sh |
| 2 | +# shellcheck disable=SC2034 |
| 3 | +dns_spaceship_info='Spaceship.com |
| 4 | +Site: Spaceship.com |
| 5 | +Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_spaceship |
| 6 | +Options: |
| 7 | + SPACESHIP_API_KEY Spaceship API Key |
| 8 | + SPACESHIP_API_SECRET Spaceship API Secret |
| 9 | + SPACESHIP_ROOT_DOMAIN (Optional) Manually specify the root domain if auto-detection fails |
| 10 | +Issues: github.com/acmesh-official/acme.sh/issues/6304 |
| 11 | +Author: Meow <https://github.com/Meo597> |
| 12 | +' |
| 13 | + |
| 14 | +# Spaceship API |
| 15 | +# https://docs.spaceship.dev/ |
| 16 | + |
| 17 | +######## Public functions ##################### |
| 18 | + |
| 19 | +SPACESHIP_API_BASE="https://spaceship.dev/api/v1" |
| 20 | + |
| 21 | +# Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" |
| 22 | +# Used to add txt record |
| 23 | +dns_spaceship_add() { |
| 24 | + fulldomain="$1" |
| 25 | + txtvalue="$2" |
| 26 | + |
| 27 | + _info "Adding TXT record for $fulldomain with value $txtvalue" |
| 28 | + |
| 29 | + # Initialize API credentials and headers |
| 30 | + if ! _spaceship_init; then |
| 31 | + return 1 |
| 32 | + fi |
| 33 | + |
| 34 | + # Detect root zone |
| 35 | + if ! _get_root "$fulldomain"; then |
| 36 | + return 1 |
| 37 | + fi |
| 38 | + |
| 39 | + # Extract subdomain part relative to root domain |
| 40 | + subdomain=$(echo "$fulldomain" | sed "s/\.$_domain$//") |
| 41 | + if [ "$subdomain" = "$fulldomain" ]; then |
| 42 | + _err "Failed to extract subdomain from $fulldomain relative to root domain $_domain" |
| 43 | + return 1 |
| 44 | + fi |
| 45 | + _debug "Extracted subdomain: $subdomain for root domain: $_domain" |
| 46 | + |
| 47 | + # Escape txtvalue to prevent JSON injection (e.g., quotes in txtvalue) |
| 48 | + escaped_txtvalue=$(echo "$txtvalue" | sed 's/"/\\"/g') |
| 49 | + |
| 50 | + # Prepare payload and URL for adding TXT record |
| 51 | + # Note: 'name' in payload uses subdomain (e.g., _acme-challenge.sub) as required by Spaceship API |
| 52 | + payload="{\"force\": true, \"items\": [{\"type\": \"TXT\", \"name\": \"$subdomain\", \"value\": \"$escaped_txtvalue\", \"ttl\": 600}]}" |
| 53 | + url="$SPACESHIP_API_BASE/dns/records/$_domain" |
| 54 | + |
| 55 | + # Send API request |
| 56 | + if _spaceship_api_request "PUT" "$url" "$payload"; then |
| 57 | + _info "Successfully added TXT record for $fulldomain" |
| 58 | + return 0 |
| 59 | + else |
| 60 | + _err "Failed to add TXT record. If the domain $_domain is incorrect, set SPACESHIP_ROOT_DOMAIN to the correct root domain." |
| 61 | + return 1 |
| 62 | + fi |
| 63 | +} |
| 64 | + |
| 65 | +# Usage: fulldomain txtvalue |
| 66 | +# Used to remove the txt record after validation |
| 67 | +dns_spaceship_rm() { |
| 68 | + fulldomain="$1" |
| 69 | + txtvalue="$2" |
| 70 | + |
| 71 | + _info "Removing TXT record for $fulldomain with value $txtvalue" |
| 72 | + |
| 73 | + # Initialize API credentials and headers |
| 74 | + if ! _spaceship_init; then |
| 75 | + return 1 |
| 76 | + fi |
| 77 | + |
| 78 | + # Detect root zone |
| 79 | + if ! _get_root "$fulldomain"; then |
| 80 | + return 1 |
| 81 | + fi |
| 82 | + |
| 83 | + # Extract subdomain part relative to root domain |
| 84 | + subdomain=$(echo "$fulldomain" | sed "s/\.$_domain$//") |
| 85 | + if [ "$subdomain" = "$fulldomain" ]; then |
| 86 | + _err "Failed to extract subdomain from $fulldomain relative to root domain $_domain" |
| 87 | + return 1 |
| 88 | + fi |
| 89 | + _debug "Extracted subdomain: $subdomain for root domain: $_domain" |
| 90 | + |
| 91 | + # Escape txtvalue to prevent JSON injection |
| 92 | + escaped_txtvalue=$(echo "$txtvalue" | sed 's/"/\\"/g') |
| 93 | + |
| 94 | + # Prepare payload and URL for deleting TXT record |
| 95 | + # Note: 'name' in payload uses subdomain (e.g., _acme-challenge.sub) as required by Spaceship API |
| 96 | + payload="[{\"type\": \"TXT\", \"name\": \"$subdomain\", \"value\": \"$escaped_txtvalue\"}]" |
| 97 | + url="$SPACESHIP_API_BASE/dns/records/$_domain" |
| 98 | + |
| 99 | + # Send API request |
| 100 | + if _spaceship_api_request "DELETE" "$url" "$payload"; then |
| 101 | + _info "Successfully deleted TXT record for $fulldomain" |
| 102 | + return 0 |
| 103 | + else |
| 104 | + _err "Failed to delete TXT record. If the domain $_domain is incorrect, set SPACESHIP_ROOT_DOMAIN to the correct root domain." |
| 105 | + return 1 |
| 106 | + fi |
| 107 | +} |
| 108 | + |
| 109 | +#################### Private functions below ################################## |
| 110 | + |
| 111 | +_spaceship_init() { |
| 112 | + SPACESHIP_API_KEY="${SPACESHIP_API_KEY:-$(_readaccountconf_mutable SPACESHIP_API_KEY)}" |
| 113 | + SPACESHIP_API_SECRET="${SPACESHIP_API_SECRET:-$(_readaccountconf_mutable SPACESHIP_API_SECRET)}" |
| 114 | + |
| 115 | + if [ -z "$SPACESHIP_API_KEY" ] || [ -z "$SPACESHIP_API_SECRET" ]; then |
| 116 | + _err "Spaceship API credentials are not set. Please set SPACESHIP_API_KEY and SPACESHIP_API_SECRET." |
| 117 | + _err "Ensure \"$LE_CONFIG_HOME\" directory has restricted permissions (chmod 700 \"$LE_CONFIG_HOME\") to protect credentials." |
| 118 | + return 1 |
| 119 | + fi |
| 120 | + |
| 121 | + # Save credentials to account config for future renewals |
| 122 | + _saveaccountconf_mutable SPACESHIP_API_KEY "$SPACESHIP_API_KEY" |
| 123 | + _saveaccountconf_mutable SPACESHIP_API_SECRET "$SPACESHIP_API_SECRET" |
| 124 | + |
| 125 | + # Set common headers for API requests |
| 126 | + export _H1="X-API-Key: $SPACESHIP_API_KEY" |
| 127 | + export _H2="X-API-Secret: $SPACESHIP_API_SECRET" |
| 128 | + export _H3="Content-Type: application/json" |
| 129 | + return 0 |
| 130 | +} |
| 131 | + |
| 132 | +_get_root() { |
| 133 | + domain="$1" |
| 134 | + |
| 135 | + # Check manual override |
| 136 | + SPACESHIP_ROOT_DOMAIN="${SPACESHIP_ROOT_DOMAIN:-$(_readdomainconf SPACESHIP_ROOT_DOMAIN)}" |
| 137 | + if [ -n "$SPACESHIP_ROOT_DOMAIN" ]; then |
| 138 | + _domain="$SPACESHIP_ROOT_DOMAIN" |
| 139 | + _debug "Using manually specified or saved root domain: $_domain" |
| 140 | + _savedomainconf SPACESHIP_ROOT_DOMAIN "$SPACESHIP_ROOT_DOMAIN" |
| 141 | + return 0 |
| 142 | + fi |
| 143 | + |
| 144 | + _debug "Detecting root zone for '$domain'" |
| 145 | + |
| 146 | + i=1 |
| 147 | + p=1 |
| 148 | + while true; do |
| 149 | + _cutdomain=$(printf "%s" "$domain" | cut -d . -f "$i"-100) |
| 150 | + |
| 151 | + _debug "Attempt i=$i: Checking if '$_cutdomain' is root zone (cut ret=$?)" |
| 152 | + |
| 153 | + if [ -z "$_cutdomain" ]; then |
| 154 | + _debug "Cut resulted in empty string, root zone not found." |
| 155 | + break |
| 156 | + fi |
| 157 | + |
| 158 | + # Call the API to check if this _cutdomain is a manageable zone |
| 159 | + if _spaceship_api_request "GET" "$SPACESHIP_API_BASE/dns/records/$_cutdomain?take=1&skip=0"; then |
| 160 | + # API call succeeded (HTTP 200 OK for GET /dns/records) |
| 161 | + _domain="$_cutdomain" |
| 162 | + _debug "Root zone found: '$_domain'" |
| 163 | + |
| 164 | + # Save the detected root domain |
| 165 | + _savedomainconf SPACESHIP_ROOT_DOMAIN "$_domain" |
| 166 | + _info "Root domain '$_domain' saved to configuration for future use." |
| 167 | + |
| 168 | + return 0 |
| 169 | + fi |
| 170 | + |
| 171 | + _debug "API check failed for '$_cutdomain'. Continuing search." |
| 172 | + |
| 173 | + p=$i |
| 174 | + i=$((i + 1)) |
| 175 | + done |
| 176 | + |
| 177 | + _err "Could not detect root zone for '$domain'. Please set SPACESHIP_ROOT_DOMAIN manually." |
| 178 | + return 1 |
| 179 | +} |
| 180 | + |
| 181 | +_spaceship_api_request() { |
| 182 | + method="$1" |
| 183 | + url="$2" |
| 184 | + payload="$3" |
| 185 | + |
| 186 | + _debug2 "Sending $method request to $url with payload $payload" |
| 187 | + if [ "$method" = "GET" ]; then |
| 188 | + response="$(_get "$url")" |
| 189 | + else |
| 190 | + response="$(_post "$payload" "$url" "" "$method")" |
| 191 | + fi |
| 192 | + |
| 193 | + if [ "$?" != "0" ]; then |
| 194 | + _err "API request failed. Response: $response" |
| 195 | + return 1 |
| 196 | + fi |
| 197 | + |
| 198 | + _debug2 "API response body: $response" |
| 199 | + |
| 200 | + if [ "$method" = "GET" ]; then |
| 201 | + if _contains "$(_head_n 1 <"$HTTP_HEADER")" '200'; then |
| 202 | + return 0 |
| 203 | + fi |
| 204 | + else |
| 205 | + if _contains "$(_head_n 1 <"$HTTP_HEADER")" '204'; then |
| 206 | + return 0 |
| 207 | + fi |
| 208 | + fi |
| 209 | + |
| 210 | + _debug2 "API response header: $HTTP_HEADER" |
| 211 | + return 1 |
| 212 | +} |
0 commit comments