vitess.io/vitess@v0.16.2/go/vt/grpcoptionaltls/optionaltls.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  	http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  package grpcoptionaltls
    16  
    17  import (
    18  	"net"
    19  
    20  	"google.golang.org/grpc/credentials"
    21  )
    22  
    23  type optionalTLSCreds struct {
    24  	credentials.TransportCredentials
    25  }
    26  
    27  func (c *optionalTLSCreds) Clone() credentials.TransportCredentials {
    28  	return New(c.TransportCredentials.Clone())
    29  }
    30  
    31  func (c *optionalTLSCreds) ServerHandshake(conn net.Conn) (net.Conn, credentials.AuthInfo, error) {
    32  	isTLS, bytes, err := DetectTLS(conn)
    33  	if err != nil {
    34  		conn.Close()
    35  		return nil, nil, err
    36  	}
    37  
    38  	wc := NewWrappedConn(conn, bytes)
    39  	if isTLS {
    40  		return c.TransportCredentials.ServerHandshake(wc)
    41  	}
    42  
    43  	var authInfo = info{
    44  		CommonAuthInfo: credentials.CommonAuthInfo{SecurityLevel: credentials.NoSecurity},
    45  	}
    46  
    47  	return wc, authInfo, nil
    48  }
    49  
    50  func New(tc credentials.TransportCredentials) credentials.TransportCredentials {
    51  	return &optionalTLSCreds{TransportCredentials: tc}
    52  }
    53  
    54  type info struct {
    55  	credentials.CommonAuthInfo
    56  }
    57  
    58  func (info) AuthType() string {
    59  	return "insecure"
    60  }