Browse Source

Update dns_czechia.sh

fix: handle root hostName and already existing records for better stability
pull/6764/head
CZECHIA-COM 1 week ago
committed by GitHub
parent
commit
02fb0de52f
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 22
      dnsapi/dns_czechia.sh

22
dnsapi/dns_czechia.sh

@ -23,28 +23,42 @@ dns_czechia_add() {
_cz=$(printf "%s" "$_current_zone" | _lower_case | sed 's/ //g') _cz=$(printf "%s" "$_current_zone" | _lower_case | sed 's/ //g')
_tk=$(printf "%s" "$CZ_AuthorizationToken" | sed 's/ //g') _tk=$(printf "%s" "$CZ_AuthorizationToken" | sed 's/ //g')
if [ -z "$_cz" ] || [ -z "$_tk" ]; then if [ -z "$_cz" ] || [ -z "$_tk" ]; then
_err "Missing zone or AuthorizationToken (CZ_Zones/CZ_AuthorizationToken)."
_err "Missing zone or Token."
return 1 return 1
fi fi
_url="$CZ_API_BASE/api/DNS/$_cz/TXT" _url="$CZ_API_BASE/api/DNS/$_cz/TXT"
_fd=$(printf "%s" "$fulldomain" | _lower_case | sed 's/\.$//') _fd=$(printf "%s" "$fulldomain" | _lower_case | sed 's/\.$//')
# Robustní určení hostname - pokud se shoduje se zónou, použije @
if [ "$_fd" = "$_cz" ]; then if [ "$_fd" = "$_cz" ]; then
_h="@" _h="@"
else else
_h=$(printf "%s" "$_fd" | sed "s/\.$_cz$//") _h=$(printf "%s" "$_fd" | sed "s/\.$_cz$//")
[ "$_h" = "$_fd" ] && _h="@"
fi fi
# Pojistka: hostName nesmí být nikdy prázdný řetězec
[ -z "$_h" ] && _h="@"
_info "Adding TXT record for $_h in zone $_cz" _info "Adding TXT record for $_h in zone $_cz"
_h_esc=$(printf "%s" "$_h" | sed 's/\\/\\\\/g; s/"/\\"/g') _h_esc=$(printf "%s" "$_h" | sed 's/\\/\\\\/g; s/"/\\"/g')
_txt_esc=$(printf "%s" "$txtvalue" | sed 's/\\/\\\\/g; s/"/\\"/g') _txt_esc=$(printf "%s" "$txtvalue" | sed 's/\\/\\\\/g; s/"/\\"/g')
_body="{\"hostName\":\"$_h_esc\",\"text\":\"$_txt_esc\",\"ttl\":60,\"publishZone\":1}" _body="{\"hostName\":\"$_h_esc\",\"text\":\"$_txt_esc\",\"ttl\":60,\"publishZone\":1}"
export _H1="Content-Type: application/json" export _H1="Content-Type: application/json"
export _H2="AuthorizationToken: $_tk" export _H2="AuthorizationToken: $_tk"
if ! _res="$(_post "$_body" "$_url" "" "POST")"; then if ! _res="$(_post "$_body" "$_url" "" "POST")"; then
_err "API request failed." _err "API request failed."
return 1 return 1
fi fi
# Ignorujeme chybu, pokud záznam již existuje (časté při testech)
if _contains "$_res" "already exists"; then
_info "Record already exists, skipping."
return 0
fi
if _contains "$_res" "\"status\":4" || _contains "$_res" "\"status\":5" || _contains "$_res" "\"errors\""; then if _contains "$_res" "\"status\":4" || _contains "$_res" "\"status\":5" || _contains "$_res" "\"errors\""; then
_err "API error details: $_res"
_err "API error: $_res"
return 1 return 1
fi fi
return 0 return 0
@ -64,12 +78,16 @@ dns_czechia_rm() {
_h="@" _h="@"
else else
_h=$(printf "%s" "$_fd" | sed "s/\.$_cz$//") _h=$(printf "%s" "$_fd" | sed "s/\.$_cz$//")
[ "$_h" = "$_fd" ] && _h="@"
fi fi
[ -z "$_h" ] && _h="@"
_h_esc=$(printf "%s" "$_h" | sed 's/\\/\\\\/g; s/"/\\"/g') _h_esc=$(printf "%s" "$_h" | sed 's/\\/\\\\/g; s/"/\\"/g')
_txt_esc=$(printf "%s" "$txtvalue" | sed 's/\\/\\\\/g; s/"/\\"/g') _txt_esc=$(printf "%s" "$txtvalue" | sed 's/\\/\\\\/g; s/"/\\"/g')
_body="{\"hostName\":\"$_h_esc\",\"text\":\"$_txt_esc\",\"ttl\":60,\"publishZone\":1}" _body="{\"hostName\":\"$_h_esc\",\"text\":\"$_txt_esc\",\"ttl\":60,\"publishZone\":1}"
export _H1="Content-Type: application/json" export _H1="Content-Type: application/json"
export _H2="AuthorizationToken: $_tk" export _H2="AuthorizationToken: $_tk"
# Při mazání ignorujeme výsledek (pokud neexistuje, je to v pořádku)
_post "$_body" "$_url" "" "DELETE" >/dev/null _post "$_body" "$_url" "" "DELETE" >/dev/null
return 0 return 0
} }

Loading…
Cancel
Save