istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/protocol/instance.go (about)

     1  // Copyright Istio Authors
     2  //
     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 protocol
    16  
    17  import "strings"
    18  
    19  // Instance defines network protocols for ports
    20  type Instance string
    21  
    22  func (i Instance) String() string {
    23  	return string(i)
    24  }
    25  
    26  const (
    27  	// GRPC declares that the port carries gRPC traffic.
    28  	GRPC Instance = "GRPC"
    29  	// GRPCWeb declares that the port carries gRPC traffic.
    30  	GRPCWeb Instance = "GRPC-Web"
    31  	// HTTP declares that the port carries HTTP/1.1 traffic.
    32  	// Note that HTTP/1.0 or earlier may not be supported by the proxy.
    33  	HTTP Instance = "HTTP"
    34  	// HTTP_PROXY declares that the port is a generic outbound proxy port.
    35  	// Note that this is currently applicable only for defining sidecar egress listeners.
    36  	// nolint
    37  	HTTP_PROXY Instance = "HTTP_PROXY"
    38  	// HTTP2 declares that the port carries HTTP/2 traffic.
    39  	HTTP2 Instance = "HTTP2"
    40  	// HTTPS declares that the port carries HTTPS traffic.
    41  	HTTPS Instance = "HTTPS"
    42  	// TCP declares the port uses TCP.
    43  	// This is the default protocol for a service port.
    44  	TCP Instance = "TCP"
    45  	// TLS declares that the port carries TLS traffic.
    46  	// TLS traffic is assumed to contain SNI as part of the handshake.
    47  	TLS Instance = "TLS"
    48  	// UDP declares that the port uses UDP.
    49  	// Note that UDP protocol is not currently supported by the proxy.
    50  	UDP Instance = "UDP"
    51  	// Mongo declares that the port carries MongoDB traffic.
    52  	Mongo Instance = "Mongo"
    53  	// Redis declares that the port carries Redis traffic.
    54  	Redis Instance = "Redis"
    55  	// MySQL declares that the port carries MySQL traffic.
    56  	MySQL Instance = "MySQL"
    57  	// HBONE declares that the port carries HBONE traffic.
    58  	// This cannot be declared by Services, but is used for some internal code that uses Protocol
    59  	HBONE Instance = "HBONE"
    60  	// Unsupported - value to signify that the protocol is unsupported.
    61  	Unsupported Instance = "UnsupportedProtocol"
    62  )
    63  
    64  // Parse from string ignoring case
    65  func Parse(s string) Instance {
    66  	switch strings.ToLower(s) {
    67  	case "tcp":
    68  		return TCP
    69  	case "udp":
    70  		return UDP
    71  	case "grpc":
    72  		return GRPC
    73  	case "grpc-web":
    74  		return GRPCWeb
    75  	case "http":
    76  		return HTTP
    77  	case "http_proxy":
    78  		return HTTP_PROXY
    79  	case "http2":
    80  		return HTTP2
    81  	case "https":
    82  		return HTTPS
    83  	case "tls":
    84  		return TLS
    85  	case "mongo":
    86  		return Mongo
    87  	case "redis":
    88  		return Redis
    89  	case "mysql":
    90  		return MySQL
    91  	}
    92  
    93  	return Unsupported
    94  }
    95  
    96  // IsHTTP2 is true for protocols that use HTTP/2 as transport protocol
    97  func (i Instance) IsHTTP2() bool {
    98  	switch i {
    99  	case HTTP2, GRPC, GRPCWeb:
   100  		return true
   101  	default:
   102  		return false
   103  	}
   104  }
   105  
   106  // IsHTTPOrSniffed is true for protocols that use HTTP as transport protocol, or *can* use it if sniffed to be HTTP
   107  func (i Instance) IsHTTPOrSniffed() bool {
   108  	return i.IsHTTP() || i.IsUnsupported()
   109  }
   110  
   111  // IsHTTP is true for protocols that use HTTP as transport protocol
   112  func (i Instance) IsHTTP() bool {
   113  	switch i {
   114  	case HTTP, HTTP2, HTTP_PROXY, GRPC, GRPCWeb:
   115  		return true
   116  	default:
   117  		return false
   118  	}
   119  }
   120  
   121  // IsTCP is true for protocols that use TCP as transport protocol
   122  func (i Instance) IsTCP() bool {
   123  	switch i {
   124  	case TCP, HTTPS, TLS, Mongo, Redis, MySQL:
   125  		return true
   126  	default:
   127  		return false
   128  	}
   129  }
   130  
   131  // IsTLS is true for protocols on top of TLS (e.g. HTTPS)
   132  func (i Instance) IsTLS() bool {
   133  	switch i {
   134  	case HTTPS, TLS:
   135  		return true
   136  	default:
   137  		return false
   138  	}
   139  }
   140  
   141  // IsHTTPS is true if protocol is HTTPS
   142  func (i Instance) IsHTTPS() bool {
   143  	switch i {
   144  	case HTTPS:
   145  		return true
   146  	default:
   147  		return false
   148  	}
   149  }
   150  
   151  // IsGRPC is true for GRPC protocols.
   152  func (i Instance) IsGRPC() bool {
   153  	switch i {
   154  	case GRPC, GRPCWeb:
   155  		return true
   156  	default:
   157  		return false
   158  	}
   159  }
   160  
   161  func (i Instance) IsUnsupported() bool {
   162  	return i == Unsupported
   163  }
   164  
   165  // AfterTLSTermination returns the protocol that will be used if TLS is terminated on the current protocol.
   166  func (i Instance) AfterTLSTermination() Instance {
   167  	switch i {
   168  	case HTTPS:
   169  		return HTTP
   170  	case TLS:
   171  		return TCP
   172  	default:
   173  		return i
   174  	}
   175  }