@@ -6,14 +6,28 @@ import (
66 "strings"
77)
88
9- // PolicyFunc can be used to decide whether to trust the PROXY info based on
10- // upstream/downstream IP. If set, the connecting addresses(remote and local)
11- // are passed in as arguments.
9+ // PolicyFunc can be used to decide whether to trust the PROXY info from
10+ // upstream. If set, the connecting address is passed in as an argument.
1211//
1312// See below for the different policies.
1413//
1514// In case an error is returned the connection is denied.
16- type PolicyFunc func (upstream net.Addr , downstream net.Addr ) (Policy , error )
15+ type PolicyFunc func (upstream net.Addr ) (Policy , error )
16+
17+ // ConnPolicyFunc can be used to decide whether to trust the PROXY info
18+ // based on connection policy options. If set, the connecting addresses
19+ // (remote and local) are passed in as argument.
20+ //
21+ // See below for the different policies.
22+ //
23+ // In case an error is returned the connection is denied.
24+ type ConnPolicyFunc func (connPolicyOptions ConnPolicyOptions ) (Policy , error )
25+
26+ // ConnPolicyOptions contains the remote and local addresses of a connection.
27+ type ConnPolicyOptions struct {
28+ Upstream net.Addr
29+ Downstream net.Addr
30+ }
1731
1832// Policy defines how a connection with a PROXY header address is treated.
1933type Policy int
@@ -44,7 +58,7 @@ const (
4458// Kubernetes pods local traffic. The def is a policy to use when an upstream
4559// address doesn't match the skipHeaderCIDR.
4660func SkipProxyHeaderForCIDR (skipHeaderCIDR * net.IPNet , def Policy ) PolicyFunc {
47- return func (upstream net.Addr , downstream net. Addr ) (Policy , error ) {
61+ return func (upstream net.Addr ) (Policy , error ) {
4862 ip , err := ipFromAddr (upstream )
4963 if err != nil {
5064 return def , err
@@ -58,25 +72,6 @@ func SkipProxyHeaderForCIDR(skipHeaderCIDR *net.IPNet, def Policy) PolicyFunc {
5872 }
5973}
6074
61- // IgnoreProxyHeaderNotOnInterface retuns a PolicyFunc which can be used to
62- // decide whether to use or ignore PROXY headers depending on the connection
63- // being made on a specific interface. This policy can be used when the server
64- // is bound to multiple interfaces but wants to allow on only one interface.
65- func IgnoreProxyHeaderNotOnInterface (allowedIP net.IP ) PolicyFunc {
66- return func (upstream net.Addr , downstream net.Addr ) (Policy , error ) {
67- ip , err := ipFromAddr (downstream )
68- if err != nil {
69- return REJECT , err
70- }
71-
72- if allowedIP .Equal (ip ) {
73- return USE , nil
74- }
75-
76- return IGNORE , nil
77- }
78- }
79-
8075// WithPolicy adds given policy to a connection when passed as option to NewConn()
8176func WithPolicy (p Policy ) func (* Conn ) {
8277 return func (c * Conn ) {
@@ -137,7 +132,7 @@ func MustStrictWhiteListPolicy(allowed []string) PolicyFunc {
137132}
138133
139134func whitelistPolicy (allowed []func (net.IP ) bool , def Policy ) PolicyFunc {
140- return func (upstream net.Addr , downstream net. Addr ) (Policy , error ) {
135+ return func (upstream net.Addr ) (Policy , error ) {
141136 upstreamIP , err := ipFromAddr (upstream )
142137 if err != nil {
143138 // something is wrong with the source IP, better reject the connection
@@ -190,3 +185,22 @@ func ipFromAddr(upstream net.Addr) (net.IP, error) {
190185
191186 return upstreamIP , nil
192187}
188+
189+ // IgnoreProxyHeaderNotOnInterface retuns a ConnPolicyFunc which can be used to
190+ // decide whether to use or ignore PROXY headers depending on the connection
191+ // being made on a specific interface. This policy can be used when the server
192+ // is bound to multiple interfaces but wants to allow on only one interface.
193+ func IgnoreProxyHeaderNotOnInterface (allowedIP net.IP ) ConnPolicyFunc {
194+ return func (connOpts ConnPolicyOptions ) (Policy , error ) {
195+ ip , err := ipFromAddr (connOpts .Downstream )
196+ if err != nil {
197+ return REJECT , err
198+ }
199+
200+ if allowedIP .Equal (ip ) {
201+ return USE , nil
202+ }
203+
204+ return IGNORE , nil
205+ }
206+ }
0 commit comments