From 3f2d2eb6591d512f08305990cd77275e1e6c9957 Mon Sep 17 00:00:00 2001 From: Phi Date: Sat, 23 Sep 2023 09:38:53 +0200 Subject: [PATCH 1/2] Connect to newAddress Connect to newAddress --- cmd/lotus-worker/main.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cmd/lotus-worker/main.go b/cmd/lotus-worker/main.go index 6ad3a448e..83f88d4e8 100644 --- a/cmd/lotus-worker/main.go +++ b/cmd/lotus-worker/main.go @@ -627,7 +627,7 @@ var runCmd = &cli.Command{ Storage: lr, } - log.Info("Setting up control endpoint at " + address) + log.Info("Setting up control endpoint at " + newAddress) timeout, err := time.ParseDuration(cctx.String("http-server-timeout")) if err != nil { @@ -652,13 +652,13 @@ var runCmd = &cli.Command{ log.Warn("Graceful shutdown successful") }() - nl, err := net.Listen("tcp", address) + nl, err := net.Listen("tcp", newAddress) if err != nil { return err } { - a, err := net.ResolveTCPAddr("tcp", address) + a, err := net.ResolveTCPAddr("tcp", newAddress) if err != nil { return xerrors.Errorf("parsing address: %w", err) } @@ -739,7 +739,7 @@ var runCmd = &cli.Command{ select { case <-readyCh: - if err := nodeApi.WorkerConnect(ctx, "http://"+address+"/rpc/v0"); err != nil { + if err := nodeApi.WorkerConnect(ctx, "http://"+newAddress+"/rpc/v0"); err != nil { log.Errorf("Registering worker failed: %+v", err) cancel() return @@ -801,6 +801,9 @@ func extractRoutableIP(timeout time.Duration) (string, error) { } minerIP, _ := maddr.ValueForProtocol(multiaddr.P_IP6) + if minerIP == "" { + minerIP, _ = maddr.ValueForProtocol(multiaddr.P_IP4) + } minerPort, _ := maddr.ValueForProtocol(multiaddr.P_TCP) // Check if the IP is IPv6 and format the address appropriately From f1c70720df31c5c57d74f02012637cef3fada2f1 Mon Sep 17 00:00:00 2001 From: Phi Date: Mon, 25 Sep 2023 13:56:35 +0200 Subject: [PATCH 2/2] Handle both IPv4 and IPv6 use net.JoinHostPort(minerIP, minerPort) to handle both IPv4 and IPv6 addresses. --- cmd/lotus-worker/main.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/cmd/lotus-worker/main.go b/cmd/lotus-worker/main.go index 83f88d4e8..873dada47 100644 --- a/cmd/lotus-worker/main.go +++ b/cmd/lotus-worker/main.go @@ -806,13 +806,8 @@ func extractRoutableIP(timeout time.Duration) (string, error) { } minerPort, _ := maddr.ValueForProtocol(multiaddr.P_TCP) - // Check if the IP is IPv6 and format the address appropriately - var addressToDial string - if ip := net.ParseIP(minerIP); ip.To4() == nil && ip.To16() != nil { - addressToDial = "[" + minerIP + "]:" + minerPort - } else { - addressToDial = minerIP + ":" + minerPort - } + // Format the address appropriately + addressToDial := net.JoinHostPort(minerIP, minerPort) conn, err := net.DialTimeout("tcp", addressToDial, timeout) if err != nil {