Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite to support IPv6 Link Local addresses #470

Merged
merged 4 commits into from
May 24, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
frenck marked this conversation as resolved.
Show resolved Hide resolved
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