The Go language implementation of gRPC TransportCredentials that supports optional TLS connections.
optionaltls.New wraps provided TransportCredentials. It uses provided credentials if a client connected with TLS and bypasses if connection is plain-text
import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"github.com/noxiouz/go-grpc-optionaltls-creds/optionaltls"
)
// Control in runtime. See docs
var dynamicOptionF = optionaltls.DynamicOption(func() bool {
return true
})
func createServer(credentials credentials.TransportCredentials) *grpc.Server {
serverCredentials = optionaltls.New(credentials) // optionaltls.NewWithDynamicOption(credentials, dynamicOptionF))
s := grpc.NewServer(grpc.Creds(serverCredentials))
return s
}
Detection mechanism is inspired by fbthrift and The Illustrated TLS Connection