Documentation
¶
Index ¶
- Constants
- func GetIPAddr(ctx context.Context) (string, error)
- func InjectLoggingMetadata(ctx context.Context, log *zap.Logger, userAgentName string) *zap.Logger
- func MinVersionStreamServerInterceptor(userAgentNames ...string) grpc.StreamServerInterceptor
- func MinVersionUnaryServerInterceptor(userAgentNames ...string) grpc.UnaryServerInterceptor
- func ResolveIPAddr(ctx context.Context) (netip.Addr, error)
- func SetTrustedProxies(n int) error
- type DeviceType
- type IPResolver
- type UserAgent
- type Version
Constants ¶
const ( UserAgentHeaderName = "user-agent" DefaultUserAgentName = "OpenCodeProtocol" )
const ( // DefaultTrustedProxies is the number of trusted proxies assumed until // SetTrustedProxies says otherwise: one, a server behind a single load // balancer. DefaultTrustedProxies = 1 )
Variables ¶
This section is empty.
Functions ¶
func GetIPAddr ¶
GetIPAddr gets the client's IP address: the one ResolveIPAddr returns, as a string.
func InjectLoggingMetadata ¶
InjectLoggingMetadata injects client metadata into a zap logger
func MinVersionStreamServerInterceptor ¶
func MinVersionStreamServerInterceptor(userAgentNames ...string) grpc.StreamServerInterceptor
MinVersionStreamServerInterceptor prevents versions below the minimum version from accessing lower version APIs. The user agent is matched against each of the provided names in order.
func MinVersionUnaryServerInterceptor ¶
func MinVersionUnaryServerInterceptor(userAgentNames ...string) grpc.UnaryServerInterceptor
MinVersionUnaryServerInterceptor prevents versions below the minimum version from accessing outdated APIs. The user agent is matched against each of the provided names in order.
func ResolveIPAddr ¶ added in v1.30.0
ResolveIPAddr returns the client's IP address, read from the request's x-forwarded-for metadata past the number of trusted proxies the process is configured with (SetTrustedProxies, DefaultTrustedProxies until it is called), exactly as ForwardedForResolver reads it. It is an IPResolver.
func SetTrustedProxies ¶ added in v1.30.0
SetTrustedProxies sets the number of proxies in front of the server that each append the address of whoever connected to them to x-forwarded-for: one for a single load balancer, two when something that appends sits in front of it. GetIPAddr and ResolveIPAddr read the client's address past that many proxies.
It is meant to be called once, at startup, before the server takes requests; the gRPC app does so from its trusted_proxies setting. A number below 1 is an error and changes nothing.
Set too low, the address read is a proxy's; set too high, it is one the client chose.
Types ¶
type DeviceType ¶
type DeviceType uint8
const ( DeviceTypeUnknown DeviceType = iota DeviceTypeIOS DeviceTypeAndroid )
func (DeviceType) IsMobile ¶
func (t DeviceType) IsMobile() bool
func (DeviceType) String ¶
func (t DeviceType) String() string
type IPResolver ¶ added in v1.30.0
IPResolver returns the IP address of the client behind an RPC.
The address is returned as it was parsed. Whether it is one the caller accepts (a public address, say, and not a proxy's own) is the caller's to judge.
An error must not name the address or the header it was read from, so that a caller may log the error where addresses must not be logged.
func ForwardedForResolver ¶ added in v1.30.0
func ForwardedForResolver(trustedProxies int) (IPResolver, error)
ForwardedForResolver returns a resolver that reads the request's x-forwarded-for metadata, for a server behind trustedProxies proxies that each append the address of whoever connected to them. ResolveIPAddr is the same reading with the process's configured number; this one is for a caller that must state its own.
Every x-forwarded-for value, in order, split on commas and trimmed, forms one list. The last proxy appends the address that connected to it, the one before it the address that connected to that one, and so on, so with N trusted proxies the client's address is the Nth entry from the right. Only that entry is read: everything left of it is whatever the client chose to send, and is neither trusted nor judged.
The leftmost entry, which many readers take as the client's, is not used: a client can put any address there by sending its own x-forwarded-for, so a caller that acts on the address (a rate limit, a compliance signal) needs one a proxy wrote.
The entry may be an address or an address with a port (1.2.3.4:5678, [2001:db8::1]:5678). The resolver fails when the list has fewer than trustedProxies entries, which means the request did not come through the proxies the server was configured for, or when the entry parses as neither; its errors never name the header's contents. trustedProxies below 1 is an error at construction.
type UserAgent ¶
type UserAgent struct {
Name string
DeviceType DeviceType
Version Version
}
func GetUserAgent ¶
GetUserAgent gets the client user agent value from headers in the provided context
func GetUserAgentByNames ¶ added in v1.27.0
GetUserAgentByNames gets the client user agent value from headers in the provided context, trying each name in order and returning the first match.