From 8c854556d37b9bd4a02aa0d9d3429d4312f249f0 Mon Sep 17 00:00:00 2001 From: Eryu Guan Date: Tue, 31 May 2022 12:50:45 +0800 Subject: [PATCH] feat: use 127.0.0.1 as IPv4 if there's no external IPv4 addr Since commit 0bc0d3b7ead8 ("feat: add vsock network type support (#1303)") we have vsock support, which means we could run dfget in a constrained VM env that only has loopback net interface, and connect to dfdaemon on host via vsock connection. So it's a valid setup to have no external IPv4 addr. Don't panic in this case and use "127.0.0.1" as IPv4. Signed-off-by: Eryu Guan --- pkg/util/net/iputils/ip_utils.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/util/net/iputils/ip_utils.go b/pkg/util/net/iputils/ip_utils.go index b2c8d69b0f9..bb83ae315d6 100644 --- a/pkg/util/net/iputils/ip_utils.go +++ b/pkg/util/net/iputils/ip_utils.go @@ -22,17 +22,22 @@ import ( "sort" "github.com/pkg/errors" + logger "d7y.io/dragonfly/v2/internal/dflog" ) var IPv4 string +const internalIPv4 string = "127.0.0.1" + func init() { ip, err := externalIPv4() if err != nil { - panic(err) + logger.Warnf("Failed to get IPv4 address: %s", err.Error()) + logger.Infof("Use %s as IPv4 addr", internalIPv4) + IPv4 = internalIPv4 + } else { + IPv4 = ip } - - IPv4 = ip } // IsIPv4 returns whether the ip is a valid IPv4 Address.