From 9ef2d8b2721cc691af1f9aef09e86ac8d40ada09 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Fri, 24 Mar 2023 00:33:09 -0700 Subject: remove extension --- hetzner-ddns | 68 +++++++++++++++++++++++++++++++++++++++++++ hetzner-ddns.sh | 68 ------------------------------------------- systemd/hetzner-ddns@.service | 2 +- 3 files changed, 69 insertions(+), 69 deletions(-) create mode 100755 hetzner-ddns delete mode 100755 hetzner-ddns.sh diff --git a/hetzner-ddns b/hetzner-ddns new file mode 100755 index 0000000..6d70e69 --- /dev/null +++ b/hetzner-ddns @@ -0,0 +1,68 @@ +#!/usr/bin/env bash +# +# Copyright 2022 David Vazgenovich Shakaryan +# +# HETZNER_TOKEN= hetzner-ddns +# HETZNER_TOKEN_FILE=/path/to/token hetzner-ddns +# systemctl enable --now "hetzner-ddns@$(systemd-escape ).timer" + +IP_RESOLVER='https://ifconfig.co' +TARGET="${1}" + +shopt -s extglob + +die() { + [[ -n "${@}" ]] && echo "${@}" >&2 + exit 1 +} + +hetzcurl() { + curl -sfH "Auth-API-Token: ${HETZNER_TOKEN}" \ + "https://dns.hetzner.com/api/v1/${1}" \ + "${@:2}" +} + +if [[ -z "${HETZNER_TOKEN}" ]] && [[ -n "${HETZNER_TOKEN_FILE}" ]]; then + [[ -f "${HETZNER_TOKEN_FILE}" ]] || die 'Specified token file' \ + "(${HETZNER_TOKEN_FILE}) does not exist" + HETZNER_TOKEN="$(<"${HETZNER_TOKEN_FILE}")" +fi +[[ -n "${HETZNER_TOKEN}" ]] || die 'Missing token' + +ip="$(curl -sf4 "${IP_RESOLVER}")" || die 'IP lookup failed' + +zone_re="${TARGET}" +while [[ "${zone_re}" =~ ^([^\\]*)\.(.*)$ ]]; do + zone_re="(${BASH_REMATCH[1]}\\.)?${BASH_REMATCH[2]}" +done + +res="$(hetzcurl "zones")" || die 'Zones lookup failed' +IFS='|' read zone_id zone_name < <(jq -er --arg re "${zone_re}" \ + '[.zones[] | select(.name | test("\\A" + $re + "\\z"))] | + if . == [] then (null | halt_error) else . end | + max_by(.name | length) | [.id, .name] | join("|")' \ + <<< "${res}") || die 'Zone not found' +rec_name="${TARGET%%?(.)${zone_name}}" +rec_name="${rec_name:-@}" + +res="$(hetzcurl "records?zone_id=${zone_id}")" || die 'Records lookup failed' +rec_id="$(jq -er --arg name "${rec_name}" \ + 'first(.records[] | select(.type == "A" and .name == $name)) | .id' \ + <<< "${res}")" || die 'Record not found' + +res="$(hetzcurl "records/${rec_id}")" || die 'Record lookup failed' +old_ip="$(jq -r '.record.value' <<< "${res}")" + +if [[ "${old_ip}" == "${ip}" ]]; then + echo "IP unchanged from ${ip}" + exit +fi + +data="$(jq -c --arg ip "${ip}" \ + '.record | {zone_id, type, name, value: $ip }' <<< "${res}")" +res="$(hetzcurl "records/${rec_id}" \ + -H 'Content-Type: application/json' \ + -X PUT -d "${data}")" || die 'Record update failed' +new_ip="$(jq -r '.record.value' <<< "${res}")" + +echo "IP changed from ${old_ip} to ${new_ip}" diff --git a/hetzner-ddns.sh b/hetzner-ddns.sh deleted file mode 100755 index 5e6de74..0000000 --- a/hetzner-ddns.sh +++ /dev/null @@ -1,68 +0,0 @@ -#!/usr/bin/env bash -# -# Copyright 2022 David Vazgenovich Shakaryan -# -# HETZNER_TOKEN= hetzner-ddns.sh -# HETZNER_TOKEN_FILE=/path/to/token hetzner-ddns.sh -# systemctl enable --now "hetzner-ddns@$(systemd-escape ).timer" - -IP_RESOLVER='https://ifconfig.co' -TARGET="${1}" - -shopt -s extglob - -die() { - [[ -n "${@}" ]] && echo "${@}" >&2 - exit 1 -} - -hetzcurl() { - curl -sfH "Auth-API-Token: ${HETZNER_TOKEN}" \ - "https://dns.hetzner.com/api/v1/${1}" \ - "${@:2}" -} - -if [[ -z "${HETZNER_TOKEN}" ]] && [[ -n "${HETZNER_TOKEN_FILE}" ]]; then - [[ -f "${HETZNER_TOKEN_FILE}" ]] || die 'Specified token file' \ - "(${HETZNER_TOKEN_FILE}) does not exist" - HETZNER_TOKEN="$(<"${HETZNER_TOKEN_FILE}")" -fi -[[ -n "${HETZNER_TOKEN}" ]] || die 'Missing token' - -ip="$(curl -sf4 "${IP_RESOLVER}")" || die 'IP lookup failed' - -zone_re="${TARGET}" -while [[ "${zone_re}" =~ ^([^\\]*)\.(.*)$ ]]; do - zone_re="(${BASH_REMATCH[1]}\\.)?${BASH_REMATCH[2]}" -done - -res="$(hetzcurl "zones")" || die 'Zones lookup failed' -IFS='|' read zone_id zone_name < <(jq -er --arg re "${zone_re}" \ - '[.zones[] | select(.name | test("\\A" + $re + "\\z"))] | - if . == [] then (null | halt_error) else . end | - max_by(.name | length) | [.id, .name] | join("|")' \ - <<< "${res}") || die 'Zone not found' -rec_name="${TARGET%%?(.)${zone_name}}" -rec_name="${rec_name:-@}" - -res="$(hetzcurl "records?zone_id=${zone_id}")" || die 'Records lookup failed' -rec_id="$(jq -er --arg name "${rec_name}" \ - 'first(.records[] | select(.type == "A" and .name == $name)) | .id' \ - <<< "${res}")" || die 'Record not found' - -res="$(hetzcurl "records/${rec_id}")" || die 'Record lookup failed' -old_ip="$(jq -r '.record.value' <<< "${res}")" - -if [[ "${old_ip}" == "${ip}" ]]; then - echo "IP unchanged from ${ip}" - exit -fi - -data="$(jq -c --arg ip "${ip}" \ - '.record | {zone_id, type, name, value: $ip }' <<< "${res}")" -res="$(hetzcurl "records/${rec_id}" \ - -H 'Content-Type: application/json' \ - -X PUT -d "${data}")" || die 'Record update failed' -new_ip="$(jq -r '.record.value' <<< "${res}")" - -echo "IP changed from ${old_ip} to ${new_ip}" diff --git a/systemd/hetzner-ddns@.service b/systemd/hetzner-ddns@.service index 58e6e6f..5396d71 100644 --- a/systemd/hetzner-ddns@.service +++ b/systemd/hetzner-ddns@.service @@ -2,7 +2,7 @@ Description=Hetzner DDNS updater [Service] -ExecStart=hetzner-ddns.sh %I +ExecStart=hetzner-ddns %I DynamicUser=yes LoadCredentialEncrypted=hetzner_token.cred Environment=HETZNER_TOKEN_FILE=%d/hetzner_token.cred -- cgit v1.2.3-70-g09d2