Files
addons/duckdns/data/run.sh
Ville Skyttä 5650ceaa16 duckdns: add option to grab IP addresses from URLs (#1077)
* duckdns: add option to grab IP addresses from URLs

Enables use of external services instead of Duck DNS' automatic
detection, which at time of writing doesn't work with IPv6.

* Update config.json

* Update CHANGELOG.md

* Update README.md

Co-authored-by: Pascal Vizeli <pascal.vizeli@syshack.ch>
2020-02-21 12:16:42 +01:00

70 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bashio
CERT_DIR=/data/letsencrypt
WORK_DIR=/data/workdir
# Let's encrypt
LE_UPDATE="0"
# DuckDNS
IPV4=$(bashio::config 'ipv4 // empty')
IPV6=$(bashio::config 'ipv6 // empty')
TOKEN=$(bashio::config 'token')
DOMAINS=$(bashio::config 'domains | join(",")')
WAIT_TIME=$(bashio::config 'seconds')
# Function that performe a renew
function le_renew() {
local domain_args=()
local domains
domains=$(bashio::config 'domains')
# Prepare domain for Let's Encrypt
for domain in ${domains}; do
domain_args+=("--domain" "${domain}")
done
dehydrated --cron --hook ./hooks.sh --challenge dns-01 "${domain_args[@]}" --out "${CERT_DIR}" --config "${WORK_DIR}/config" || true
LE_UPDATE="$(date +%s)"
}
# Register/generate certificate if terms accepted
if bashio::config.true 'lets_encrypt.accept_terms'; then
# Init folder structs
mkdir -p "${CERT_DIR}"
mkdir -p "${WORK_DIR}"
# Generate new certs
if [ ! -d "${CERT_DIR}/live" ]; then
# Create empty dehydrated config file so that this dir will be used for storage
touch "${WORK_DIR}/config"
dehydrated --register --accept-terms --config "${WORK_DIR}/config"
elif [ -e "${WORK_DIR}/lock" ]; then
# Some user reports issue with lock files/cleanup
rm -rf "${WORK_DIR}/lock"
bashio::log.warning "Reset dehydrated lock file"
fi
fi
# Run duckdns
while true; do
[[ ${IPV4} != *:/* ]] && ipv4=${IPV4} || ipv4=$(curl -s -m 10 "${IPV4}")
[[ ${IPV6} != *:/* ]] && ipv6=${IPV6} || ipv6=$(curl -s -m 10 "${IPV6}")
if answer="$(curl -s "https://www.duckdns.org/update?domains=${DOMAINS}&token=${TOKEN}&ip=${ipv4}&ipv6=${ipv6}&verbose=true")"; then
bashio::log.info "${answer}"
else
bashio::log.warning "${answer}"
fi
now="$(date +%s)"
if bashio::config.true 'lets_encrypt.accept_terms' && [ $((now - LE_UPDATE)) -ge 43200 ]; then
le_renew
fi
sleep "${WAIT_TIME}"
done