google.golang.org/grpc@v1.72.2/internal/proxyattributes/proxyattributes.go (about)

     1  /*
     2   *
     3   * Copyright 2024 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  // Package proxyattributes contains functions for getting and setting proxy
    20  // attributes like the CONNECT address and user info.
    21  package proxyattributes
    22  
    23  import (
    24  	"net/url"
    25  
    26  	"google.golang.org/grpc/resolver"
    27  )
    28  
    29  type keyType string
    30  
    31  const proxyOptionsKey = keyType("grpc.resolver.delegatingresolver.proxyOptions")
    32  
    33  // Options holds the proxy connection details needed during the CONNECT
    34  // handshake.
    35  type Options struct {
    36  	User        *url.Userinfo
    37  	ConnectAddr string
    38  }
    39  
    40  // Set returns a copy of addr with opts set in its attributes.
    41  func Set(addr resolver.Address, opts Options) resolver.Address {
    42  	addr.Attributes = addr.Attributes.WithValue(proxyOptionsKey, opts)
    43  	return addr
    44  }
    45  
    46  // Get returns the Options for the proxy [resolver.Address] and a boolean
    47  // value representing if the attribute is present or not. The returned data
    48  // should not be mutated.
    49  func Get(addr resolver.Address) (Options, bool) {
    50  	if a := addr.Attributes.Value(proxyOptionsKey); a != nil {
    51  		return a.(Options), true
    52  	}
    53  	return Options{}, false
    54  }