-
Notifications
You must be signed in to change notification settings - Fork 701
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend realip interceptors with ip selection based on proxy count and…
… list (#695) * Extend realip interceptors with ip selection based on proxy count and list The rightmost IP is not always the client IP. One example is Google: https://cloud.google.com/load-balancing/docs/https#x-forwarded-for_header The PR extends the IP selection for `X-Forwarded-For` based on [MDN Selecting an IP address](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For#selecting_an_ip_address). Just so you know, it is possible to configure both at the same time. The user needs to be cautious when configuring these for IP selection and preferably pick `TrustedProxies` or `TrustedProxiesCount`. * Use functional options to configure the interceptor * Fix linter
- Loading branch information
Showing
5 changed files
with
252 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) The go-grpc-middleware Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package realip | ||
|
||
import "net/netip" | ||
|
||
// options represents the configuration options for the realip middleware. | ||
type options struct { | ||
// trustedPeers is a list of trusted peers network prefixes. | ||
trustedPeers []netip.Prefix | ||
// trustedProxies is a list of trusted proxies network prefixes. | ||
// The first rightmost non-matching IP when going through X-Forwarded-For is considered the client IP. | ||
trustedProxies []netip.Prefix | ||
// trustedProxiesCount specifies the number of proxies in front that may append X-Forwarded-For. | ||
// It defaults to 0. | ||
trustedProxiesCount uint | ||
// headers specifies the headers to use in real IP extraction when the request is from a trusted peer. | ||
headers []string | ||
} | ||
|
||
// An Option lets you add options to realip interceptors using With* functions. | ||
type Option func(*options) | ||
|
||
func evaluateOpts(opts []Option) *options { | ||
optCopy := &options{} | ||
for _, o := range opts { | ||
o(optCopy) | ||
} | ||
return optCopy | ||
} | ||
|
||
// WithTrustedPeers sets the trusted peers network prefixes. | ||
func WithTrustedPeers(peers []netip.Prefix) Option { | ||
return func(o *options) { | ||
o.trustedPeers = peers | ||
} | ||
} | ||
|
||
// WithTrustedProxies sets the trusted proxies network prefixes. | ||
func WithTrustedProxies(proxies []netip.Prefix) Option { | ||
return func(o *options) { | ||
o.trustedProxies = proxies | ||
} | ||
} | ||
|
||
// WithTrustedProxiesCount sets the number of trusted proxies that may append X-Forwarded-For. | ||
func WithTrustedProxiesCount(count uint) Option { | ||
return func(o *options) { | ||
o.trustedProxiesCount = count | ||
} | ||
} | ||
|
||
// WithHeaders sets the headers to use in real IP extraction for requests from trusted peers. | ||
func WithHeaders(headers []string) Option { | ||
return func(o *options) { | ||
o.headers = headers | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters