multiple bug fixes

fix script premature termination as bashio set -e?
fix parsing of the DNS servers
improve ip/host checks
This commit is contained in:
litinoveweedle
2026-02-03 22:36:46 +01:00
parent 8c5171b027
commit 5647aac528
2 changed files with 21 additions and 26 deletions

View File

@@ -112,7 +112,7 @@ RUN chmod 744 /ha_automodules.sh && /ha_automodules.sh "$MODULES" && rm /ha_auto
# && chmod a+x /etc/s6-overlay/s6-rc.d/$SCRIPTSNAME/* ; done; fi # && chmod a+x /etc/s6-overlay/s6-rc.d/$SCRIPTSNAME/* ; done; fi
# Manual apps # Manual apps
ARG PACKAGES="wireguard-tools iptables ip6tables iptables-legacy" ARG PACKAGES="ipcalc wireguard-tools iptables ip6tables iptables-legacy"
# Automatic apps & bashio # Automatic apps & bashio
ADD "https://raw.githubusercontent.com/alexbelgium/hassio-addons/master/.templates/ha_autoapps.sh" "/ha_autoapps.sh" ADD "https://raw.githubusercontent.com/alexbelgium/hassio-addons/master/.templates/ha_autoapps.sh" "/ha_autoapps.sh"

View File

@@ -31,13 +31,14 @@ _parse_dns() {
local -a dns_conf=() local -a dns_conf=()
local -a dns_backup_ipv4=("8.8.8.8" "1.1.1.1") local -a dns_backup_ipv4=("8.8.8.8" "1.1.1.1")
local -a dns_backup_ipv6=("2001:4860:4860::8888" "2606:4700:4700::1111") local -a dns_backup_ipv6=("2001:4860:4860::8888" "2606:4700:4700::1111")
local dns_servers=$(bashio::config 'DNS_server')
mapfile -d ',' -t dns_conf < <(bashio::config 'DNS_server' | tr -d ' ') mapfile -d ',' -t dns_conf < <(echo "${dns_servers}" | tr -d ' ')
if [ ${config["IPv4Enabled"]} = "true" ]; then if [ ${config["IPv4Enabled"]} = "true" ]; then
for dns_ip in "${dns_conf[@]}"; do for dns_ip in "${dns_conf[@]}"; do
_is_ip_address "${dns_ip}" local result=0
local is_ip=$? _check_host "${dns_ip}" || result=$?
if [ "${is_ip}" -eq 1 ]; then if [ "${result}" -eq 1 ]; then
dns_servers_ipv4+=("${dns_ip}") dns_servers_ipv4+=("${dns_ip}")
fi fi
done done
@@ -48,9 +49,9 @@ _parse_dns() {
fi fi
if [ ${config["IPv6Enabled"]} = "true" ]; then if [ ${config["IPv6Enabled"]} = "true" ]; then
for dns_ip in "${dns_conf[@]}"; do for dns_ip in "${dns_conf[@]}"; do
_is_ip_address "${dns_ip}" local result=0
local is_ip=$? _check_host "${dns_ip}" || result=$?
if [ "${is_ip}" -eq 2 ]; then if [ "${result}" -eq 2 ]; then
dns_servers_ipv6+=("${dns_ip}") dns_servers_ipv6+=("${dns_ip}")
fi fi
done done
@@ -67,13 +68,13 @@ _cmd() {
eval "${cmd}" eval "${cmd}"
} }
_is_ip_address() { _check_host() {
if [ "$1" != "${1#*[0-9].[0-9]}" ]; then if ipcalc -c -4 "$1" >/dev/null 2>&1; then
return 1 # IPv4 return 1 # IPv4
elif [ "$1" != "${1#*:[0-9a-fA-F]}" ]; then elif ipcalc -c -6 "$1" >/dev/null 2>&1; then
return 2 # IPv6 return 2 # IPv6
elif getent ahosts "$1" >/dev/null 2>&1; then elif getent ahosts "$1" >/dev/null 2>&1; then
return 3 # resolvable hostname return 3 # resolvable hostnamee
else else
return 0 # neither IP nor resolvable hostname return 0 # neither IP nor resolvable hostname
fi fi
@@ -133,12 +134,6 @@ _resolve_hostname() {
fi fi
echo "${ips[@]}" echo "${ips[@]}"
if [ ${#ips[@]} -gt 0 ]; then
return 0
else
return 1
fi
} }
_routing_add() { _routing_add() {
@@ -210,12 +205,12 @@ _wireguard_up() {
local -a local_ips=() local -a local_ips=()
mapfile -d ',' -t local_ips < <(echo "${config["Address"]}" | tr -d ' ') mapfile -d ',' -t local_ips < <(echo "${config["Address"]}" | tr -d ' ')
for local_ip in ${local_ips[@]}; do for local_ip in ${local_ips[@]}; do
_is_ip_address "${local_ip}" local result=0
local is_ip=$? _check_host "${local_ip}" || result=$?
if [ "${is_ip}" -eq 1 ]; then if [ "${result}" -eq 1 ]; then
allowed_ips="${allowed_ips},0.0.0.0/0" allowed_ips="${allowed_ips},0.0.0.0/0"
_cmd "ip addr add ${local_ip} dev ${config["Interface"]}" || return 1 _cmd "ip addr add ${local_ip} dev ${config["Interface"]}" || return 1
elif [ "${is_ip}" -eq 2 ]; then elif [ "${result}" -eq 2 ]; then
allowed_ips="${allowed_ips},::/0" allowed_ips="${allowed_ips},::/0"
_cmd "ip addr add ${local_ip} dev ${config["Interface"]}" || return 1 _cmd "ip addr add ${local_ip} dev ${config["Interface"]}" || return 1
else else
@@ -290,12 +285,12 @@ wireguard() {
if [ "${mode}" = "up" ]; then if [ "${mode}" = "up" ]; then
bashio::log.info "Starting WireGuard on interface ${config["Interface"]}..." bashio::log.info "Starting WireGuard on interface ${config["Interface"]}..."
_is_ip_address ${config["EndpointHost"]} local result=0
local is_ip=$? _check_host ${config["EndpointHost"]} || result=$?
if [ "$is_ip" -eq 0 ]; then if [ "${result}" -eq 0 ]; then
bashio::log.error "WireGuard endpoint ${config["EndpointHost"]} is neither a valid IP address nor a resolvable hostname." bashio::log.error "WireGuard endpoint ${config["EndpointHost"]} is neither a valid IP address nor a resolvable hostname."
bashio::exit.nok 'WireGuard start failed.' bashio::exit.nok 'WireGuard start failed.'
elif [ "$is_ip" -eq 3 ]; then elif [ "${result}" -eq 3 ]; then
local -a endpoint_ips=() local -a endpoint_ips=()
mapfile -d ' ' -t endpoint_ips < <(_resolve_hostname ${config["EndpointHost"]}) mapfile -d ' ' -t endpoint_ips < <(_resolve_hostname ${config["EndpointHost"]})
if [ ${#endpoint_ips[@]} -eq 0 ]; then if [ ${#endpoint_ips[@]} -eq 0 ]; then