Skip to content

Commit

Permalink
always request verbose response, detect and log IP changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aptalca authored Apr 1, 2024
1 parent 52f0e81 commit 3031632
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions root/app/duck.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/with-contenv bash
# shellcheck shell=bash

if [ "${LOG_FILE,,}" = "true" ]; then
if [[ "${LOG_FILE,,}" = "true" ]]; then
DUCK_LOG="/config/duck.log"
touch "${DUCK_LOG}"
touch /config/logrotate.status
Expand All @@ -12,37 +12,46 @@ fi

{
# Use cloudflare to autodetect IP if UPDATE_IP is set
if [ "${UPDATE_IP}" = "ipv4" ]; then
if [[ "${UPDATE_IP}" = "ipv4" ]]; then
echo "Detecting IPv4 via CloudFlare"
IPV4=$(dig +short ch txt whoami.cloudflare -4 @1.1.1.1 | sed 's/"//g')
RESPONSE=$(curl -sS --max-time 60 "https://www.duckdns.org/update?domains=${SUBDOMAINS}&token=${TOKEN}&ip=${IPV4}")
elif [ "${UPDATE_IP}" = "ipv6" ]; then
DRESPONSE=$(curl -sS --max-time 60 "https://www.duckdns.org/update?domains=${SUBDOMAINS}&token=${TOKEN}&ip=${IPV4}&verbose=true")
RESPONSE=$(echo "${DRESPONSE}" | awk 'NR==1')
IPCHANGE=$(echo "${DRESPONSE}" | awk 'NR==4')
elif [[ "${UPDATE_IP}" = "ipv6" ]]; then
echo "Detecting IPv6 via CloudFlare"
IPV6=$(dig +short ch txt whoami.cloudflare -6 @2606:4700:4700::1111 | sed 's/"//g')
RESPONSE=$(curl -sS --max-time 60 "https://www.duckdns.org/update?domains=${SUBDOMAINS}&token=${TOKEN}&ipv6=${IPV6}")
elif [ "${UPDATE_IP}" = "both" ]; then
DRESPONSE=$(curl -sS --max-time 60 "https://www.duckdns.org/update?domains=${SUBDOMAINS}&token=${TOKEN}&ipv6=${IPV6}&verbose=true")
RESPONSE=$(echo "${DRESPONSE}" | awk 'NR==1')
IPCHANGE=$(echo "${DRESPONSE}" | awk 'NR==4')
elif [[ "${UPDATE_IP}" = "both" ]]; then
echo "Detecting IPv4 and IPv6 via CloudFlare"
IPV4=$(dig +short ch txt whoami.cloudflare -4 @1.1.1.1 | sed 's/"//g')
IPV6=$(dig +short ch txt whoami.cloudflare -6 @2606:4700:4700::1111 | sed 's/"//g')
RESPONSE=$(curl -sS --max-time 60 "https://www.duckdns.org/update?domains=${SUBDOMAINS}&token=${TOKEN}&ip=${IPV4}&ipv6=${IPV6}")
DRESPONSE=$(curl -sS --max-time 60 "https://www.duckdns.org/update?domains=${SUBDOMAINS}&token=${TOKEN}&ip=${IPV4}&ipv6=${IPV6}&verbose=true")
RESPONSE=$(echo "${DRESPONSE}" | awk 'NR==1')
IPCHANGE=$(echo "${DRESPONSE}" | awk 'NR==4')
else
# Use DuckDns to autodetect IPv4 (default behaviour)
echo "Detecting IPv4 via DuckDNS"
DRESPONSE=$(curl -sS --max-time 60 "https://www.duckdns.org/update?domains=${SUBDOMAINS}&token=${TOKEN}&ip=&verbose")
DRESPONSE=$(curl -sS --max-time 60 "https://www.duckdns.org/update?domains=${SUBDOMAINS}&token=${TOKEN}&ip=&verbose=true")
IPV4=$(echo "${DRESPONSE}" | awk 'NR==2')
IPV6=$(echo "${DRESPONSE}" | awk 'NR==3')
RESPONSE=$(echo "${DRESPONSE}" | awk 'NR==1')
IPCHANGE=$(echo "${DRESPONSE}" | awk 'NR==4')
fi

if [ "${RESPONSE}" = "OK" ]; then
if [ "${IPV4}" != "" ] && [ "${IPV6}" == "" ]; then
if [[ "${RESPONSE}" = "OK" ]] && [[ "${IPCHANGE}" = "UPDATED" ]]; then
if [[ "${IPV4}" != "" ]] && [[ "${IPV6}" == "" ]]; then
echo "Your IP was updated at $(date) to IPv4: ${IPV4}"
elif [ "${IPV4}" == "" ] && [ "${IPV6}" != "" ]; then
elif [[ "${IPV4}" == "" ]] && [[ "${IPV6}" != "" ]]; then
echo "Your IP was updated at $(date) to IPv6: ${IPV6}"
else
echo "Your IP was updated at $(date) to IPv4: ${IPV4} & IPv6 to: {$IPV6}"
fi
elif [[ "${RESPONSE}" = "OK" ]] && [[ "${IPCHANGE}" = "NOCHANGE" ]]; then
echo "DuckDNS request at $(date) successful. IP(s) unchanged."
else
echo -e "Something went wrong, please check your settings $(date)\nThe response returned was:\n${RESPONSE}"
echo -e "Something went wrong, please check your settings $(date)\nThe response returned was:\n${DRESPONSE}\n"
fi
} | tee -a "${DUCK_LOG}"

0 comments on commit 3031632

Please sign in to comment.