This commit is contained in:
zer0init1 2024-06-17 20:15:47 -05:00 committed by GitHub
commit 9d67bbef2c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,17 +1,53 @@
#!/usr/bin/env sh #!/usr/bin/env sh
# shellcheck disable=SC2034
dns_fornex_info='Fornex.com
Site: Fornex.com
Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_fornex
Options:
FORNEX_API_KEY API Key
Issues: github.com/acmesh-official/acme.sh/issues/3998
Author: Timur Umarov <inbox@tumarov.com>
'
FORNEX_API_URL="https://fornex.com/api/dns/v0.1" # Author: @SBohomolov <noc@fornex.com>
# Site: Fornex.com
# Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_fornex
# Bugs: https://github.com/acmesh-official/acme.sh/issues/5161
######## Public functions #####################
## install jq ##
# Check the operating system
if [ "$(uname)" = "Darwin" ]; then
# macOS - install jq using Homebrew
if ! command -v brew >/dev/null 2>&1; then
echo "Error: Homebrew is not installed. Please install Homebrew first." >&2
exit 1
fi
brew install jq
elif [ -f "/etc/redhat-release" ] || [ -f "/etc/centos-release" ] || [ -f "/etc/fedora-release" ]; then
# RedHat/CentOS/Fedora - install jq using yum or dnf
if command -v dnf >/dev/null 2>&1; then
dnf install -y jq
elif command -v yum >/dev/null 2>&1; then
yum install -y jq
else
echo "Error: Neither yum nor dnf package manager found." >&2
exit 1
fi
elif [ -f "/etc/lsb-release" ] || [ -f "/etc/debian_version" ]; then
# Debian/Ubuntu - install jq using apt
if command -v apt >/dev/null 2>&1; then
apt update
apt install -y jq
else
echo "Error: apt package manager not found." >&2
exit 1
fi
else
echo "Error: Unsupported operating system." >&2
exit 1
fi
# jq installed successfully
echo "jq installed successfully."
#######################################################
FORNEX_API_URL="https://fornex.com/api/dns/domain"
######## Public functions ###########################
# Usage: dns_fornex_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" # Usage: dns_fornex_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
dns_fornex_add() { dns_fornex_add() {
@ -22,60 +58,63 @@ dns_fornex_add() {
return 1 return 1
fi fi
if ! _get_root "$fulldomain"; then domain=$(echo "$fulldomain" | sed 's/^\*\.//')
_err "Unable to determine root domain"
if ! _get_domain_id "$domain"; then
_err "Unable to determine domain ID"
return 1 return 1
else else
_debug _domain "$_domain" _debug _domain_id "$_domain_id"
fi fi
_info "Adding record" _info "Adding TXT record for $fulldomain"
if _rest POST "$_domain/entry_set/add/" "host=$fulldomain&type=TXT&value=$txtvalue&apikey=$FORNEX_API_KEY"; then # Add the TXT record
_debug _response "$response" if ! _rest POST "$domain/entry_set/" "type=TXT&host=_acme-challenge&value=$txtvalue"; then
if _contains "$response" '"ok": true' || _contains "$response" 'Такая запись уже существует.'; then _err "Failed to add TXT record"
_info "Added, OK"
return 0
fi
fi
_err "Add txt record error."
return 1 return 1
fi
_info "TXT record added successfully"
return 0
} }
#Usage: dns_fornex_rm _acme-challenge.www.domain.com
dns_fornex_rm() { dns_fornex_rm() {
fulldomain=$1 fulldomain=$1
txtvalue=$2
if ! _Fornex_API; then if ! _Fornex_API; then
return 1 return 1
fi fi
if ! _get_root "$fulldomain"; then domain=$(echo "$fulldomain" | sed 's/^\*\.//')
_err "Unable to determine root domain"
if ! _get_domain_id "$domain"; then
_err "Unable to determine domain ID"
return 1 return 1
else else
_debug _domain "$_domain" _debug _domain_id "$_domain_id"
fi fi
_debug "Getting txt records" _info "Removing TXT records for domain: _acme-challenge.$domain"
_rest GET "$_domain/entry_set.json?apikey=$FORNEX_API_KEY"
if ! _contains "$response" "$txtvalue"; then response=$(curl -X GET -H "Authorization: Api-Key $FORNEX_API_KEY" "https://fornex.com/api/dns/domain/$domain/entry_set/")
_err "Txt record not found"
return 1 # Extract TXT record IDs using jq
txt_ids=$(echo "$response" | jq -r '.[] | select(.type == "TXT") | .id')
if [ -z "$txt_ids" ]; then
_info "No TXT records found for domain: _acme-challenge.$domain"
return 0
fi fi
_record_id="$(echo "$response" | _egrep_o "{[^{]*\"value\"*:*\"$txtvalue\"[^}]*}" | sed -n -e 's#.*"id": \([0-9]*\).*#\1#p')" for txt_id in $txt_ids; do
_debug "_record_id" "$_record_id" _info "Removing TXT record with ID: $txt_id"
if [ -z "$_record_id" ]; then if ! curl -X DELETE -H "Authorization: Api-Key $FORNEX_API_KEY" "https://fornex.com/api/dns/domain/$domain/entry_set/$txt_id/"; then
_err "can not find _record_id" _err "Failed to remove TXT record with ID: $txt_id"
return 1 else
_info "TXT record with ID $txt_id removed successfully"
fi fi
done
if ! _rest POST "$_domain/entry_set/$_record_id/delete/" "apikey=$FORNEX_API_KEY"; then
_err "Delete record error."
return 1
fi
return 0 return 0
} }
@ -85,32 +124,24 @@ dns_fornex_rm() {
# returns # returns
# _sub_domain=_acme-challenge.www # _sub_domain=_acme-challenge.www
# _domain=domain.com # _domain=domain.com
_get_root() { _get_domain_id() {
domain=$1 domain=$1
i=1 _debug "Getting domain ID for $domain"
while true; do
h=$(printf "%s" "$domain" | cut -d . -f $i-100) if echo "$domain" | grep -q "_acme-challenge"; then
_debug h "$h" # If yes, remove "_acme-challenge" from the domain name
if [ -z "$h" ]; then domain=$(echo "$domain" | sed 's/_acme-challenge\.//')
#not valid fi
if ! _rest GET "$domain/entry_set/"; then
_err "Failed to get domain ID for $domain"
return 1 return 1
fi fi
if ! _rest GET "domain_list.json?q=$h&apikey=$FORNEX_API_KEY"; then _domain_id="$response"
return 1 _debug "Domain ID for $domain is $_domain_id"
fi
if _contains "$response" "\"$h\"" >/dev/null; then
_domain=$h
return 0 return 0
else
_debug "$h not found"
fi
i=$(_math "$i" + 1)
done
return 1
} }
_Fornex_API() { _Fornex_API() {
@ -135,12 +166,15 @@ _rest() {
_debug "$ep" _debug "$ep"
export _H1="Accept: application/json" export _H1="Accept: application/json"
export _H2="Authorization: Api-Key $FORNEX_API_KEY"
if [ "$m" != "GET" ]; then if [ "$m" != "GET" ]; then
_debug data "$data" _debug data "$data"
response="$(_post "$data" "$FORNEX_API_URL/$ep" "" "$m")" url="$FORNEX_API_URL/$ep"
response=$(curl -X "$m" -H "Authorization: Api-Key $FORNEX_API_KEY" -d "$data" "$url")
else else
response="$(_get "$FORNEX_API_URL/$ep" | _normalizeJson)" url="$FORNEX_API_URL/$ep"
response=$(curl -X GET -H "Authorization: Api-Key $FORNEX_API_KEY" "$url")
fi fi
_ret="$?" _ret="$?"