Skip to content

Commit

Permalink
localhost handling
Browse files Browse the repository at this point in the history
  • Loading branch information
solidiquis committed Nov 15, 2024
1 parent 52b2b5b commit 9663975
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions go/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
}

Expand All @@ -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"
}

0 comments on commit 9663975

Please sign in to comment.