Browse Source

fix(): changing logic and requests in the script for Fornex Public API (2.0.0)

pull/5081/head
Serb 7 months ago
committed by GitHub
parent
commit
0499e2dc93
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 101
      dnsapi/dns_fornex.sh

101
dnsapi/dns_fornex.sh

@ -1,8 +1,6 @@
#!/usr/bin/env sh
#Author: Timur Umarov <inbox@tumarov.com>
FORNEX_API_URL="https://fornex.com/api/dns/v0.1"
FORNEX_API_URL="https://fornex.com/api/dns/domain"
######## Public functions #####################
@ -15,60 +13,60 @@ dns_fornex_add() {
return 1
fi
if ! _get_root "$fulldomain"; then
_err "Unable to determine root domain"
domain=$(echo "$fulldomain" | sed 's/^\*\.//')
if ! _get_domain_id "$domain"; then
_err "Unable to determine domain ID"
return 1
else
_debug _domain "$_domain"
_debug _domain_id "$_domain_id"
fi
_info "Adding record"
if _rest POST "$_domain/entry_set/add/" "host=$fulldomain&type=TXT&value=$txtvalue&apikey=$FORNEX_API_KEY"; then
_debug _response "$response"
if _contains "$response" '"ok": true' || _contains "$response" 'Такая запись уже существует.'; then
_info "Added, OK"
return 0
fi
fi
_err "Add txt record error."
_info "Adding TXT record for $fulldomain"
# Add the TXT record
if ! _rest POST "$domain/entry_set/" "type=TXT&host=_acme-challenge&value=$txtvalue"; then
_err "Failed to add TXT record"
return 1
fi
_info "TXT record added successfully"
return 0
}
#Usage: dns_fornex_rm _acme-challenge.www.domain.com
dns_fornex_rm() {
fulldomain=$1
txtvalue=$2
if ! _Fornex_API; then
return 1
fi
if ! _get_root "$fulldomain"; then
_err "Unable to determine root domain"
domain=$(echo "$fulldomain" | sed 's/^\*\.//')
if ! _get_domain_id "$domain"; then
_err "Unable to determine domain ID"
return 1
else
_debug _domain "$_domain"
_debug _domain_id "$_domain_id"
fi
_debug "Getting txt records"
_rest GET "$_domain/entry_set.json?apikey=$FORNEX_API_KEY"
_info "Removing TXT records for domain: _acme-challenge.$domain"
if ! _contains "$response" "$txtvalue"; then
_err "Txt record not found"
return 1
fi
txt_ids=$(curl -X GET -H "Authorization: Api-Key $FORNEX_API_KEY" "https://fornex.com/api/dns/domain/$domain/entry_set/" | jq -r '.[] | select(.type == "TXT") | .id')
_record_id="$(echo "$response" | _egrep_o "{[^{]*\"value\"*:*\"$txtvalue\"[^}]*}" | sed -n -e 's#.*"id": \([0-9]*\).*#\1#p')"
_debug "_record_id" "$_record_id"
if [ -z "$_record_id" ]; then
_err "can not find _record_id"
return 1
if [ -z "$txt_ids" ]; then
_info "No TXT records found for domain: _acme-challenge.$domain"
return 0
fi
if ! _rest POST "$_domain/entry_set/$_record_id/delete/" "apikey=$FORNEX_API_KEY"; then
_err "Delete record error."
return 1
for txt_id in $txt_ids; do
_info "Removing TXT record with ID: $txt_id"
if ! _rest DELETE "$domain/entry_set/$txt_id"; then
_err "Failed to remove TXT record with ID: $txt_id"
else
_info "TXT record with ID $txt_id removed successfully"
fi
done
return 0
}
@ -78,34 +76,27 @@ dns_fornex_rm() {
#returns
# _sub_domain=_acme-challenge.www
# _domain=domain.com
_get_root() {
_get_domain_id() {
domain=$1
i=1
while true; do
h=$(printf "%s" "$domain" | cut -d . -f $i-100)
_debug h "$h"
if [ -z "$h" ]; then
#not valid
return 1
_debug "Getting domain ID for $domain"
if echo "$domain" | grep -q "_acme-challenge"; then
# If yes, remove "_acme-challenge" from the domain name
domain=$(echo "$domain" | sed 's/_acme-challenge\.//')
fi
if ! _rest GET "domain_list.json?q=$h&apikey=$FORNEX_API_KEY"; then
if ! _rest GET "$domain/entry_set/"; then
_err "Failed to get domain ID for $domain"
return 1
fi
if _contains "$response" "\"$h\"" >/dev/null; then
_domain=$h
_domain_id="$response"
_debug "Domain ID for $domain is $_domain_id"
return 0
else
_debug "$h not found"
fi
i=$(_math "$i" + 1)
done
return 1
}
_Fornex_API() {
FORNEX_API_KEY="${FORNEX_API_KEY:-$(_readaccountconf_mutable FORNEX_API_KEY)}"
if [ -z "$FORNEX_API_KEY" ]; then
@ -128,11 +119,15 @@ _rest() {
_debug "$ep"
export _H1="Accept: application/json"
export _H2="Authorization: Api-Key $FORNEX_API_KEY"
if [ "$m" != "GET" ]; then
_debug data "$data"
response="$(_post "$data" "$FORNEX_API_URL/$ep" "" "$m")"
url="$FORNEX_API_URL/$ep"
echo "curl -X $m -H 'Authorization: Api-Key $FORNEX_API_KEY' -d '$data' \"$url\""
response="$(_post "$data" "$url" "" "$m")"
else
echo "curl -X GET -H 'Authorization: Api-Key $FORNEX_API_KEY' $FORNEX_API_URL/$ep"
response="$(_get "$FORNEX_API_URL/$ep" | _normalizeJson)"
fi

Loading…
Cancel
Save