diff --git a/README.pod b/README.pod index 1ce2f96..cbef50e 100644 --- a/README.pod +++ b/README.pod @@ -13,13 +13,15 @@ L; the software goals are for simplicity, flexibility and configurability while using the minimal system tools installed with most distributions. - Version 1.x = FreeDNS API v1 - Version 2.x = FreeDNS API v2 - If upgrading from API v1 to v2, a manual action is required on the FreeDNS website to generate a new API key for each domain name. See L for more information. +By default, petrified will use the length of the API key to automatically +choose the APIv1 or APIv2 URL; this can be overridden in the configuration +file to force a specific version, as the v1 key length is variable but the +v2 key length is fixed at 24 chars. + =head1 OPTIONS =over 4 diff --git a/petrified b/petrified index f5ff1ee..77fccf2 100755 --- a/petrified +++ b/petrified @@ -38,9 +38,18 @@ while getopts ":c:" opt; do done ## v2 API - How to update DNS - DDNS_KEY is unique per domain -DDNS_URL=https://sync.afraid.org/u/ +DDNS_URL="" DDNS_KEY="" +## From Josh, v1 key is variable but v2 is fixed length @24char. When set +## to 0 (auto) the length of the DDNS_KEY will be used to choose which API +## endpoint, otherwise specify 1 (API v1) or 2 (API v2) directly +DDNS_API=0 + +## API URL endpoints, no trailing slashes +DDNS_V1U=https://freedns.afraid.org/dynamic/update.php +DDNS_V2U=https://sync.afraid.org/u + ## Which URL to use to get an IP # IPv4 icanhazip (http://major.io/icanhazip-com-faq/) DDNS_CHECK="http://4.icanhazip.com" @@ -106,6 +115,28 @@ fi if [[ -z "${DDNS_KEY}" ]]; then echo "DDNS_KEY must be configured, exiting." exit 1 +else + # Set the API version endpoint, IP added later + case ${DDNS_API} in + 0) + # From Josh, v2 is fixed @24 but v1 is variable (typically >24) + if [[ ${#DDNS_KEY} -ne 24 ]]; then + DDNS_URL="${DDNS_V1U}?${DDNS_KEY}&address=" + else + DDNS_URL="${DDNS_V2U}/${DDNS_KEY}/?ip=" + fi + ;; + 1) + DDNS_URL="${DDNS_V1U}?${DDNS_KEY}&address=" + ;; + 2) + DDNS_URL="${DDNS_V2U}/${DDNS_KEY}/?ip=" + ;; + *) + echo "DDNS_API must be 0, 1 or 2; exiting." + exit 1 + ;; + esac fi # Make sure that PET_LOG will work if required @@ -219,7 +250,7 @@ fi # If they don't match, tell upstream __UPDATED=0 if [[ "${OLDIP}" != "${NEWIP}" ]]; then - UPDURL="${DDNS_URL}${DDNS_KEY}/?ip=${NEWIP}" + UPDURL="${DDNS_URL}${NEWIP}" RESULT=$(curl -m ${CURL_WAIT} -sk "${UPDURL}" 2>/dev/null) logmsg "${RESULT}" # "Updated foo from 1.2.3.4 to 5.6.7.8" diff --git a/petrified.1 b/petrified.1 index 9285d2c..2aaff62 100644 --- a/petrified.1 +++ b/petrified.1 @@ -129,7 +129,7 @@ .\" ======================================================================== .\" .IX Title "PETRIFIED 1" -.TH PETRIFIED 1 "2017-09-16" "petrified 2.0.0" "Petrified Manual" +.TH PETRIFIED 1 "2017-09-29" "petrified 2.0.0" "Petrified Manual" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l @@ -146,14 +146,14 @@ Usage: \fBpetrified\fR [ \fB\-c\fR \fIconfig\fR ] flexibility and configurability while using the minimal system tools installed with most distributions. .PP -.Vb 2 -\& Version 1.x = FreeDNS API v1 -\& Version 2.x = FreeDNS API v2 -.Ve -.PP If upgrading from \s-1API\s0 v1 to v2, a manual action is required on the FreeDNS website to generate a new \s-1API\s0 key for each domain name. See for more information. +.PP +By default, petrified will use the length of the \s-1API\s0 key to automatically +choose the APIv1 or APIv2 \s-1URL\s0; this can be overridden in the configuration +file to force a specific version, as the v1 key length is variable but the +v2 key length is fixed at 24 chars. .SH "OPTIONS" .IX Header "OPTIONS" .IP "\fB\-c\fR \fIconfig\fR" 4 diff --git a/petrified.conf b/petrified.conf index a5ac4ad..ad689c2 100644 --- a/petrified.conf +++ b/petrified.conf @@ -13,14 +13,26 @@ # See petrified.crontab for examples on how to run via cron/fcron. -# This is the unique string for the dynamic DNS entry to update; visit -# https://freedns.afraid.org/dynamic/v2/ - next to the DNS entries towards -# the bottom is a link to sync.afraid.org; copy that and extract the string -# after the '.../u/' part and put here. +# This is the unique key for the dynamic DNS entry to update; visit: +# https://freedns.afraid.org/dynamic/ (v1 API) or +# https://freedns.afraid.org/dynamic/v2/ (v2 API) +# Next to the DNS entries towards the bottom is a link to afraid.org; +# copy that and extract the string and put here, this is your API key. # -# Example: DDNS_KEY=ZIfSWnXKuDqMpNEsTXBA852 +# Example v1: DDNS_KEY=9C4yjq3kaekkYNbYsKRqcosTmouMYbfVMLkJtR== +# Example v2: DDNS_KEY=ZIfSWnXKuDqMpNEsTXBA852 DDNS_KEY= +# The migration from API v1 to API v2 requires the user to visit the +# website and click a few buttons to generate a new API key for each +# domain; the API v1 length is variable but API v2 is a fixed length +# at 24 chars. This setting will try and autodetect the API version by +# the length of DDNS_KEY - if 24, use v2 else use v1. +# 0 = auto +# 1 = force API v1 +# 2 = force API v2 +DDNS_API=0 + # Which URL to use to get an IP - the expectation is output from this # host in plain text, just the IP - no parsing will be done. The # icanhazip service returns the IPv4 and IPv6 nice and clean.