fix(bootstrap): 没有 IPv4 默认路由时从 hostname -I 取第一个 IPv4 地址,跳过 IPv6
This commit is contained in:
2 files changed
+20
-5
No files matched your search
+9
-1
@@ -1116,7 +1116,15 @@ persisted_root_domain() {
|
|||||||
|
|
||||||
detect_node_ip() {
|
detect_node_ip() {
|
||||||
NODE_IP="$(ip -4 route get 1.1.1.1 2>/dev/null | awk '{for(i=1;i<=NF;i++) if($i=="src"){print $(i+1); exit}}')"
|
NODE_IP="$(ip -4 route get 1.1.1.1 2>/dev/null | awk '{for(i=1;i<=NF;i++) if($i=="src"){print $(i+1); exit}}')"
|
||||||
[ -n "${NODE_IP:-}" ] || NODE_IP="$(hostname -I 2>/dev/null | awk '{print $1}')"
|
if [ -z "${NODE_IP:-}" ]; then
|
||||||
|
# No IPv4 default route. hostname -I lists the IPv6 addresses too, in interface
|
||||||
|
# order, and an IPv6 one would end up in the nip.io name, the /32 policies and the
|
||||||
|
# certificate's IP entry: take the first IPv4 one.
|
||||||
|
local a
|
||||||
|
for a in $(hostname -I 2>/dev/null); do
|
||||||
|
if ipv4_to_int "$a" >/dev/null; then NODE_IP="$a"; break; fi
|
||||||
|
done
|
||||||
|
fi
|
||||||
[ -n "${NODE_IP:-}" ] || die "could not determine this host's primary IPv4 address"
|
[ -n "${NODE_IP:-}" ] || die "could not determine this host's primary IPv4 address"
|
||||||
|
|
||||||
# Precedence: an explicit FELIS_ROOT_DOMAIN, then whatever the last run persisted, then
|
# Precedence: an explicit FELIS_ROOT_DOMAIN, then whatever the last run persisted, then
|
||||||
|
|||||||
@@ -2038,13 +2038,14 @@ rm -rf "$ldir2"
|
|||||||
dnblock="$(awk '/^detect_node_ip\(\) \{/,/^}/' "$BS")"
|
dnblock="$(awk '/^detect_node_ip\(\) \{/,/^}/' "$BS")"
|
||||||
prdblock="$(awk '/^persisted_root_domain\(\) \{/,/^}/' "$BS")"
|
prdblock="$(awk '/^persisted_root_domain\(\) \{/,/^}/' "$BS")"
|
||||||
{ [ -n "$dnblock" ] && [ -n "$prdblock" ]; } || { echo "FAIL: detect_node_ip / persisted_root_domain not found in $BS"; exit 1; }
|
{ [ -n "$dnblock" ] && [ -n "$prdblock" ]; } || { echo "FAIL: detect_node_ip / persisted_root_domain not found in $BS"; exit 1; }
|
||||||
printf '%s\n' "$prdblock" "$dnblock" > "$fnfile"
|
printf '%s\n' "$prdblock" "$dnblock" "$(awk '/^ipv4_to_int\(\) \{/,/^}/' "$BS")" > "$fnfile"
|
||||||
run_detect() { # state-dir [FELIS_ROOT_DOMAIN]
|
run_detect() { # state-dir [FELIS_ROOT_DOMAIN]; IPOUT and HOSTOUT stand in for ip route get and hostname -I
|
||||||
STATE_DIR="$1" RD="${2:-}" FNFILE="$fnfile" bash -c '
|
STATE_DIR="$1" RD="${2:-}" FNFILE="$fnfile" IPOUT="${IPOUT-1.1.1.1 via 10.0.0.1 dev eth0 src 10.0.0.5 uid 0}" HOSTOUT="${HOSTOUT:-}" bash -c '
|
||||||
die() { printf "DIE: %s\n" "$*"; exit 1; }
|
die() { printf "DIE: %s\n" "$*"; exit 1; }
|
||||||
log() { printf "LOG: %s\n" "$*"; }
|
log() { printf "LOG: %s\n" "$*"; }
|
||||||
warn() { printf "WARN: %s\n" "$*"; }
|
warn() { printf "WARN: %s\n" "$*"; }
|
||||||
ip() { echo "1.1.1.1 via 10.0.0.1 dev eth0 src 10.0.0.5 uid 0"; }
|
ip() { [ -z "$IPOUT" ] || printf "%s\n" "$IPOUT"; }
|
||||||
|
hostname() { [ "$1" = -I ] && printf "%s \n" "$HOSTOUT"; }
|
||||||
. "$FNFILE"
|
. "$FNFILE"
|
||||||
if [ -n "$RD" ]; then FELIS_ROOT_DOMAIN="$RD"; fi
|
if [ -n "$RD" ]; then FELIS_ROOT_DOMAIN="$RD"; fi
|
||||||
detect_node_ip
|
detect_node_ip
|
||||||
@@ -2062,6 +2063,12 @@ expect "the installed domain is reused when unset" "ROOT=r.example.com" "$(run_d
|
|||||||
rm -f "$adir/felis.host.toml"
|
rm -f "$adir/felis.host.toml"
|
||||||
expect "a first install takes FELIS_ROOT_DOMAIN" "ROOT=new.example.net" "$(run_detect "$adir" new.example.net)"
|
expect "a first install takes FELIS_ROOT_DOMAIN" "ROOT=new.example.net" "$(run_detect "$adir" new.example.net)"
|
||||||
expect "a first install defaults to nip.io" "ROOT=10.0.0.5.nip.io" "$(run_detect "$adir")"
|
expect "a first install defaults to nip.io" "ROOT=10.0.0.5.nip.io" "$(run_detect "$adir")"
|
||||||
|
# Without an IPv4 default route, hostname -I is the fallback; it lists IPv6 addresses
|
||||||
|
# too, and the first IPv4 one is taken.
|
||||||
|
expect "the fallback skips IPv6 addresses" "ROOT=192.168.1.20.nip.io" \
|
||||||
|
"$(IPOUT='' HOSTOUT='fd00::5 2001:db8::20 192.168.1.20 10.42.0.1' run_detect "$adir")"
|
||||||
|
expect "a host with IPv6 addresses alone is refused" "DIE: could not determine this host's primary IPv4 address" \
|
||||||
|
"$(IPOUT='' HOSTOUT='fd00::5 2001:db8::20' run_detect "$adir")"
|
||||||
|
|
||||||
rm -rf "$adir"
|
rm -rf "$adir"
|
||||||
rm -f "$fnfile"
|
rm -f "$fnfile"
|
||||||
|
|||||||
Reference in new issue
Block a user