From 966397597d50a5984f1299b924625e8251d1a722 Mon Sep 17 00:00:00 2001 From: Benjamin Nguyen Date: Thu, 14 Nov 2024 19:32:04 -0800 Subject: [PATCH] localhost handling --- go/grpc/grpc.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/go/grpc/grpc.go b/go/grpc/grpc.go index 2c46a51e..e949f53c 100644 --- a/go/grpc/grpc.go +++ b/go/grpc/grpc.go @@ -2,15 +2,14 @@ package grpc import ( "context" - "crypto/tls" - "net/url" - "strings" + "net" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials/insecure" ) +// Configuration for [SiftChannel]. type SiftChannelConfig struct { Uri string Apikey string @@ -21,13 +20,8 @@ type SiftChannel = *grpc.ClientConn // Initializes a gRPC connection to Sift. func UseSiftChannel(ctx context.Context, config SiftChannelConfig) (SiftChannel, error) { - url, err := url.Parse(config.Uri) - if err != nil { - return nil, err - } - - transportCred := grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})) - if !strings.Contains(url.Scheme, "https") { + transportCred := grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")) + if useInsecure(config.Uri) { transportCred = grpc.WithTransportCredentials(insecure.NewCredentials()) } @@ -51,3 +45,11 @@ func UseSiftChannel(ctx context.Context, config SiftChannelConfig) (SiftChannel, streamInterceptors, ) } + +func useInsecure(uri string) bool { + host, _, err := net.SplitHostPort(uri) + if err != nil { + host = uri + } + return host == "localhost" || host == "127.0.0.1" || host == "::1" +}