From 582084be15637a594902d2f8f3790898d731e770 Mon Sep 17 00:00:00 2001 From: lzgabel Date: Mon, 26 Feb 2024 21:15:37 +0800 Subject: [PATCH] feat: support ipv6 from address --- .../go/cmd/zbctl/internal/commands/root.go | 7 +++++ clients/go/pkg/zbc/client_test.go | 29 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/clients/go/cmd/zbctl/internal/commands/root.go b/clients/go/cmd/zbctl/internal/commands/root.go index e3a91caa15f8..2d58c7893728 100644 --- a/clients/go/cmd/zbctl/internal/commands/root.go +++ b/clients/go/cmd/zbctl/internal/commands/root.go @@ -98,6 +98,13 @@ var initClient = func(cmd *cobra.Command, args []string) error { var credsProvider zbc.CredentialsProvider host, port, err := parseAddress() + ip := net.ParseIP(host) + if ip == nil { + err = fmt.Errorf("Invalid IP address: %s\n", host) + } else if ip.To16() != nil { + host = fmt.Sprintf("[%s]", host) + } + if err != nil { return err } diff --git a/clients/go/pkg/zbc/client_test.go b/clients/go/pkg/zbc/client_test.go index edfe3d8df3ec..443798b43774 100644 --- a/clients/go/pkg/zbc/client_test.go +++ b/clients/go/pkg/zbc/client_test.go @@ -544,6 +544,35 @@ func (s *clientTestSuite) TestOverrideHostAndPortEnvVar() { s.EqualValues(fmt.Sprintf("%s:%s", address, port), config.GatewayAddress) } +func (s *clientTestSuite) TestHostWithIPV6() { + // given + host := "[::1]" + port := "9090" + lis, grpcServer := createServer(host, port) + + go grpcServer.Serve(lis) + defer func() { + grpcServer.Stop() + _ = lis.Close() + }() + + client, err := NewClient(&ClientConfig{ + GatewayAddress: fmt.Sprintf("%s:%s", host, port), + UsePlaintextConnection: true, + }) + + s.NoError(err) + + // when + _, err = client.NewTopologyCommand().Send(context.Background()) + + // then + s.Error(err) + if grpcStatus, ok := status.FromError(err); ok { + s.EqualValues(codes.Unimplemented, grpcStatus.Code()) + } +} + func createSecureServer(withSan bool) (net.Listener, *grpc.Server) { certFile := "testdata/chain.cert.pem" keyFile := "testdata/private.key.pem"