autodetect API by length of API key

This commit is contained in:
tengel 2024-03-20 09:32:36 -05:00
parent 1bb3ed28ef
commit 12caa51fb5
4 changed files with 61 additions and 16 deletions

View file

@ -13,13 +13,15 @@ L<https://freedns.afraid.org>; the software goals are for simplicity,
flexibility and configurability while using the minimal system tools flexibility and configurability while using the minimal system tools
installed with most distributions. 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 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 FreeDNS website to generate a new API key for each domain name. See
L<https://freedns.afraid.org/dynamic/v2/> for more information. L<https://freedns.afraid.org/dynamic/v2/> 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 =head1 OPTIONS
=over 4 =over 4

View file

@ -38,9 +38,18 @@ while getopts ":c:" opt; do
done done
## v2 API - How to update DNS - DDNS_KEY is unique per domain ## v2 API - How to update DNS - DDNS_KEY is unique per domain
DDNS_URL=https://sync.afraid.org/u/ DDNS_URL=""
DDNS_KEY="" 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 ## Which URL to use to get an IP
# IPv4 icanhazip (http://major.io/icanhazip-com-faq/) # IPv4 icanhazip (http://major.io/icanhazip-com-faq/)
DDNS_CHECK="http://4.icanhazip.com" DDNS_CHECK="http://4.icanhazip.com"
@ -106,6 +115,28 @@ fi
if [[ -z "${DDNS_KEY}" ]]; then if [[ -z "${DDNS_KEY}" ]]; then
echo "DDNS_KEY must be configured, exiting." echo "DDNS_KEY must be configured, exiting."
exit 1 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 fi
# Make sure that PET_LOG will work if required # Make sure that PET_LOG will work if required
@ -219,7 +250,7 @@ fi
# If they don't match, tell upstream # If they don't match, tell upstream
__UPDATED=0 __UPDATED=0
if [[ "${OLDIP}" != "${NEWIP}" ]]; then 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) RESULT=$(curl -m ${CURL_WAIT} -sk "${UPDURL}" 2>/dev/null)
logmsg "${RESULT}" logmsg "${RESULT}"
# "Updated foo from 1.2.3.4 to 5.6.7.8" # "Updated foo from 1.2.3.4 to 5.6.7.8"

View file

@ -129,7 +129,7 @@
.\" ======================================================================== .\" ========================================================================
.\" .\"
.IX Title "PETRIFIED 1" .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 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents. .\" way too many mistakes in technical documents.
.if n .ad l .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 flexibility and configurability while using the minimal system tools
installed with most distributions. installed with most distributions.
.PP .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 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 FreeDNS website to generate a new \s-1API\s0 key for each domain name. See
<https://freedns.afraid.org/dynamic/v2/> for more information. <https://freedns.afraid.org/dynamic/v2/> 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" .SH "OPTIONS"
.IX Header "OPTIONS" .IX Header "OPTIONS"
.IP "\fB\-c\fR \fIconfig\fR" 4 .IP "\fB\-c\fR \fIconfig\fR" 4

View file

@ -13,14 +13,26 @@
# See petrified.crontab for examples on how to run via cron/fcron. # 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 # This is the unique key for the dynamic DNS entry to update; visit:
# https://freedns.afraid.org/dynamic/v2/ - next to the DNS entries towards # https://freedns.afraid.org/dynamic/ (v1 API) or
# the bottom is a link to sync.afraid.org; copy that and extract the string # https://freedns.afraid.org/dynamic/v2/ (v2 API)
# after the '.../u/' part and put here. # 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= 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 # 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 # host in plain text, just the IP - no parsing will be done. The
# icanhazip service returns the IPv4 and IPv6 nice and clean. # icanhazip service returns the IPv4 and IPv6 nice and clean.