Browse Source

Added support for Hostway API

pull/750/head
Cyrus Joudieh 4 years ago
parent
commit
c4fdf6c1db
2 changed files with 152 additions and 0 deletions
  1. +86
    -0
      dns_scripts/dns_add_hostway
  2. +66
    -0
      dns_scripts/dns_del_hostway

+ 86
- 0
dns_scripts/dns_add_hostway View File

@ -0,0 +1,86 @@
#!/usr/bin/env bash
# Need to add your API key below or set as env variable
apikey="$HOSTWAY_API_KEY"
# This script adds a token to dynu.com DNS for the ACME challenge
# usage dns_add_dynu "domain name" "token"
# return codes are;
# 0 - success
# 1 - error in input
# 2 - error within internal processing
# 3 - error in result ( domain not found in dynu.com etc)
fulldomain="${1}"
token="${2}"
API='https://api.hostway.com/dns'
# Check initial parameters
if [[ -z "$fulldomain" ]]; then
echo "DNS script requires full domain name as first parameter"
exit 1
fi
if [[ -z "$token" ]]; then
echo "DNS script requires challenge token as second parameter"
exit 1
fi
curl_params=( -H "accept: application/json" -H "Authorization: Basic $apikey" -H 'Content-Type: application/json charset=utf-8')
# Get domain id
# curl -X GET "https://api.hostway.com/dns/domain/"
resp=$(curl --silent "${curl_params[@]}" -X GET "$API/${fulldomain}")
# Match domain id
re="\"serial\":\s?([^}]*)"
if [[ "$resp" =~ $re ]]; then
domain_id="${BASH_REMATCH[1]}"
fi
if [[ -z "$domain_id" ]]; then
echo 'Domain name not found on your Hostway account'
exit 3
fi
# Check for existing _acme-challenge TXT record
# curl -X GET "https://api.hostway.com/dns/domain/records?filterType=TXT&page=1&pageSize=100"
resp=$(curl --silent "${curl_params[@]}" -X GET "$API/${fulldomain}/records?filterType=TXT")
re="\"id\":\s?([^}]*)"
if [[ "$resp" =~ $re ]]; then
record_id="${BASH_REMATCH[1]}"
fi
if [[ -z "$record_id" ]]; then
# Add new TXT challenge record
# curl -X POST https://api.hostway.com/dns/{domain}/records/{record_id} -d "{\"name\":\"_acme-challenge.{domain}\",\"type\":\"TXT\",\"ttl\":\"300\",\"data\":\"Test2\"}"
# Response is empty when successful
echo "Adding record for ${fulldomain}"
resp=$(curl --silent \
"${curl_params[@]}" \
-X POST "${API}/${fulldomain}/records" \
--data "{\"name\":\"_acme-challenge.${fulldomain}\",\"type\":\"TXT\",\"ttl\":\"300\",\"data\":\"$token\"}")
else
# Update existing record
# curl -X PUT https://api.hostway.com/dns/{domain}/records/{record_id} -d "{\"name\":\"_acme-challenge.{domain}\", \"data\":\"Test2\"}"
echo "Updating record for ${fulldomain}"
resp=$(curl --silent \
"${curl_params[@]}" \
-X PUT "${API}/${fulldomain}/records/${record_id}" \
--data "{\"name\":\"_acme-challenge.${fulldomain}\", \"data\":\"$token\"}")
fi
# Check if response data matches token
re="\"data\":\s?\"([^,]*)\""
if [[ "$resp" =~ $re ]]; then
if [[ ${BASH_REMATCH[1]} == "$token" ]]; then
token_match="$token ${BASH_REMATCH[1]}"
fi
fi
# If adding record failed (exception:) then print error message
if [[ -z "$token_match" && "$resp" != "" ]]; then
echo "Error: DNS challenge not added: unknown error - ${resp}"
exit 3
else
echo "Record added successfully for ${fulldomain}"
fi

+ 66
- 0
dns_scripts/dns_del_hostway View File

@ -0,0 +1,66 @@
#!/usr/bin/env bash
# Need to add your API key below or set as env variable
apikey="$HOSTWAY_API_KEY"
# This script adds a token to dynu.com DNS for the ACME challenge
# usage dns_add_dynu "domain name" "token"
# return codes are;
# 0 - success
# 1 - error in input
# 2 - error within internal processing
# 3 - error in result ( domain not found in dynu.com etc)
fulldomain="${1}"
token="${2}"
API='https://api.hostway.com/dns'
# Check initial parameters
if [[ -z "$fulldomain" ]]; then
echo "DNS script requires full domain name as first parameter"
exit 1
fi
if [[ -z "$token" ]]; then
echo "DNS script requires challenge token as second parameter"
exit 1
fi
curl_params=( -H "accept: application/json" -H "Authorization: Basic $apikey" -H 'Content-Type: application/json charset=utf-8')
# Get domain id
# curl -X GET "https://api.hostway.com/dns/domain/"
resp=$(curl --silent "${curl_params[@]}" -X GET "$API/${fulldomain}")
# Match domain id
re="\"serial\":\s?([^}]*)"
if [[ "$resp" =~ $re ]]; then
domain_id="${BASH_REMATCH[1]}"
fi
if [[ -z "$domain_id" ]]; then
echo 'Domain name not found on your Hostway account'
exit 3
fi
# Check for existing _acme-challenge TXT record
# curl -X GET "https://api.hostway.com/dns/domain/records?filterType=TXT&page=1&pageSize=100"
resp=$(curl --silent "${curl_params[@]}" -X GET "$API/${fulldomain}/records?filterType=TXT")
#re="\"id\":\s?([^}]*)"
re="(?<=_acme(.*)\"id\":\s?)[0-9]+(?=\})"
if [[ "$resp" =~ $re ]]; then
record_id="${BASH_REMATCH[1]}"
fi
if [[ -z "$record_id" ]]; then
echo "Not able to find a record to delete"
else
# Delete existing record
# curl -X DELETE https://api.hostway.com/dns/{domain}/records/{record_id}
resp=$(curl --silent \
"${curl_params[@]}" \
-X DELETE "${API}/${fulldomain}/records/${record_id}")
if [[ "$resp" == "" ]]; then
echo "Record deleted successfully for ${fulldomain}"
fi
fi

Loading…
Cancel
Save