Skip to content

Commit

Permalink
Rewrite to support IPv6 Link Local addresses (#470)
Browse files Browse the repository at this point in the history
Co-authored-by: erwin-willems <[email protected]>
Co-authored-by: Franck Nijhof <[email protected]>
  • Loading branch information
3 people authored May 24, 2024
1 parent a47109d commit 591442f
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions adguard/rootfs/etc/s6-overlay/s6-rc.d/init-adguard/run
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,33 @@ else
yq --inplace e '.dns.bind_host = "127.0.0.1"' "${CONFIG}"
fi

# Collect IP addresses
# Collect IP addresses from interfaces
interfaces+=($(bashio::network.interfaces))
for interface in "${interfaces[@]}"; do
hosts+=($(bashio::network.ipv4_address "${interface}"))
hosts+=($(bashio::network.ipv6_address "${interface}"))

# IPv4 addresses on the interface
for host in $(bashio::network.ipv4_address "${interface}"); do
# Remove the netmask (for example /24) from the address
hosts+=("${host%/*}")
done

# IPv6 addresses on the interface
for host in $(bashio::network.ipv6_address "${interface}"); do
part="${host%%:*}"
# The decimal values for 0xfd & 0xa2
fd=$(( (0x$part) / 256 ))
a2=$(( (0x$part) % 256 ))
# fe80::/10 according to RFC 4193 -> Local link. Add interface to bind to
if (( (fd == 254) && ( (a2 & 192) == 128) )); then
hosts+=("${host%/*}%${interface}")
else
# Remove the netmask (for example /64) from the address
hosts+=("${host%/*}")
fi
done

done
# Bind to addon IP address
hosts+=($(bashio::addon.ip_address))
# Bind to localhost ip addresses as well.
hosts+=("127.0.0.1")
Expand All @@ -68,19 +89,8 @@ for host in "${hosts[@]}"; do
continue
fi

if [[ "${host}" =~ .*:.* ]]; then
# IPv6
part="${host%%:*}"
if [[ "${host}" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then

# The decimal values for 0xfd & 0xa2
fd=$(( (0x$part) / 256 ))
a2=$(( (0x$part) % 256 ))

# fe80::/10 according to RFC 4193 -> Local link. Skip it
if (( (fd == 254) && ( (a2 & 192) == 128) )); then
continue
fi
else
# IPv4
part="${host%%.*}"

Expand All @@ -90,7 +100,7 @@ for host in "${hosts[@]}"; do
fi
fi

host="${host%/*}" yq --inplace e \
host="${host}" yq --inplace e \
'.dns.bind_hosts += [env(host)]' "${CONFIG}" \
|| bashio::exit.nok 'Failed updating AdGuardHome host'
done

0 comments on commit 591442f

Please sign in to comment.