Changed API a little to split and pass domain in a compatible way

This commit is contained in:
Eric Renfro 2023-10-31 15:50:52 -04:00
parent b2baa656b9
commit 0353a19ded
Signed by: psi-jack
GPG key ID: 14977F3A50D9A5BF
2 changed files with 47 additions and 21 deletions

View file

@ -12,8 +12,8 @@ if [[ -r "${DIP_BASE_DIR}/conf.d/${DIP_AGENT_EXEC}.conf" ]]; then
source "${DIP_BASE_DIR}/conf.d/${DIP_AGENT_EXEC}.conf"
fi
if [[ -z "$DIP_CUR_IP" ]] || [[ -z "$DIP_OLD_IP" ]] || [[ -z "$DIP_RECORD" ]]; then
logerr "ERROR: Agent expects currentip, existingip, and record."
if [[ -z "$DIP_CUR_IP" ]] || [[ -z "$DIP_OLD_IP" ]] || [[ -z "$DIP_RECORD" ]] || [[ -z "$DIP_DOMAIN" ]]; then
logerr "ERROR: Agent expects currentip, existingip, record and domain."
exit 98
fi
@ -21,15 +21,30 @@ fi
_get_host_and_domain() {
local record=$1
local domain=$2
local hostname
hostname=${record%%.*}
domain=${record#*.}
if [[ -z "$record" ]] || [[ -z "$domain" ]]; then
return 1
fi
hostname="${record%%."$domain"}"
if [[ -n "$hostname" ]]; then
DIP_RECORD="$hostname"
else
return 1
fi
#hostname=${record%%.*}
#hostname="${record%%."$domain"}"
#domain=${record#*.}
if [[ -n "${agent_update_dns_namecheap_token[$domain]}" ]]; then
token="${agent_update_dns_namecheap_token[$domain]}"
DIP_TOKEN="${agent_update_dns_namecheap_token["$domain"]}"
return 0
else
logerr "ERROR: Password for $domain not found in agent configuration."
logerr "ERROR: Password for '$domain' not found in agent configuration."
return 1
fi
}
@ -37,21 +52,21 @@ _get_host_and_domain() {
# Update and Check Response
_update_dns_namecheap() {
local dnsHostname=$1
local dnsDomain=$2
local dnsToken=$3
local dnsIP=$4
local rec=$1
local dom=$2
local pas=$3
local ip=$4
local result
local errors
if result=$(curl -qs "https://dynamicdns.park-your-domain.com/update?host=${dnsHostname}&domain=${dnsDomain}&password=${dnsToken}&ip=${dnsIP}" | grep ErrCount); then
if result=$(curl -qs "https://dynamicdns.park-your-domain.com/update?host=${rec}&domain=${dom}&password=${pas}&ip=${ip}" | grep ErrCount); then
errors=$(grep -oPm1 "(?<=<ErrCount>)[^<]+" <<< "$result")
if [[ "$errors" -eq 0 ]]; then
echo "DNS record ${dnsHostname}.${dnsDomain} updated successfully."
echo "DNS record ${rec}.${dom} updated successfully."
return 0
else
echo "DNS record ${dnsHostname}.${dnsDomain} failed to update."
echo "DNS record ${rec}.${dom} failed to update."
return 96
fi
else
@ -62,16 +77,18 @@ _update_dns_namecheap() {
# Main
_get_host_and_domain "$DIP_RECORD" || return 99
_get_host_and_domain "$DIP_RECORD" "$DIP_DOMAIN" || exit 99
if valid_ipv4 "$DIP_CUR_IP"; then
log "Updating Namecheap DNS IPv4 Record for ${hostname}.${domain}"
_update_dns_namecheap "$hostname" "$domain" "$token" "${DIP_CUR_IP}"
log "Updating Namecheap DNS IPv4 Record for ${DIP_RECORD}.${DIP_DOMAIN}"
_update_dns_namecheap "$DIP_RECORD" "$DIP_DOMAIN" "$DIP_TOKEN" "${DIP_CUR_IP}"
unset DIP_TOKEN
exit $?
fi
if valid_ipv6 "$DIP_CUR_IP"; then
log "Updating Namecheap DNS IPv6 Record for ${hostname}.${domain}"
_update_dns_namecheap "$hostname" "$domain" "$token" "${DIP_CUR_IP}"
log "Updating Namecheap DNS IPv6 Record for ${DIP_RECORD}.${DIP_DOMAIN}"
_update_dns_namecheap "$DIP_RECORD" "$DIP_DOMAIN" "$DIP_TOKEN" "${DIP_CUR_IP}"
unset DIP_TOKEN
exit $?
fi

View file

@ -271,6 +271,7 @@ run-hook() {
DIP_CUR_IP="$1" \
DIP_OLD_IP="$2" \
DIP_RECORD="$3" \
DIP_DOMAIN="$4" \
"$hook_script" "$@"
then
logerr "WARNING: Agent $(basename "$hook_script") had errors"
@ -285,9 +286,10 @@ run-update() {
local cip=$1
local eip=$2
local rec=$3
local dom=$4
if [[ "$cip" != "$eip" ]]; then
run-hook "${script_dir}/update.d" "$cip" "$eip" "$rec" || return $?
run-hook "${script_dir}/update.d" "$cip" "$eip" "$rec" "$dom" || return $?
else
log "No change detected"
fi
@ -342,18 +344,25 @@ getCurrentIP() {
check-update() {
local iptype=$1
local record=$2
local domain
[[ -z "$iptype" ]] && return 1
[[ -z "$record" ]] && return 1
if [[ "$record" == *":"* ]]; then
domain="${record##*:}"
#record="${record%%."$domain"}"
record="${record%%:*}"
fi
getCurrentIP "$iptype" "$record"
local status=$?
case $status in
0) if [[ "$currentip" != "$externalip" ]]; then
log "Updates found: $externalip is not $currentip"
log "Running Agents for $record"
if run-update "$currentip" "$externalip" "$record"; then
log "Running Agents for $recordi${domain:+."$domain"}"
if run-update "$currentip" "$externalip" "$record" "$domain"; then
log "Agents ran successfully"
else
logerr "WARNING: Agents had $? errors"