@@ -6,14 +6,28 @@ import (
6
6
"strings"
7
7
)
8
8
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.
12
11
//
13
12
// See below for the different policies.
14
13
//
15
14
// 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
+ }
17
31
18
32
// Policy defines how a connection with a PROXY header address is treated.
19
33
type Policy int
@@ -44,7 +58,7 @@ const (
44
58
// Kubernetes pods local traffic. The def is a policy to use when an upstream
45
59
// address doesn't match the skipHeaderCIDR.
46
60
func 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 ) {
48
62
ip , err := ipFromAddr (upstream )
49
63
if err != nil {
50
64
return def , err
@@ -58,25 +72,6 @@ func SkipProxyHeaderForCIDR(skipHeaderCIDR *net.IPNet, def Policy) PolicyFunc {
58
72
}
59
73
}
60
74
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
-
80
75
// WithPolicy adds given policy to a connection when passed as option to NewConn()
81
76
func WithPolicy (p Policy ) func (* Conn ) {
82
77
return func (c * Conn ) {
@@ -137,7 +132,7 @@ func MustStrictWhiteListPolicy(allowed []string) PolicyFunc {
137
132
}
138
133
139
134
func 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 ) {
141
136
upstreamIP , err := ipFromAddr (upstream )
142
137
if err != nil {
143
138
// something is wrong with the source IP, better reject the connection
@@ -190,3 +185,22 @@ func ipFromAddr(upstream net.Addr) (net.IP, error) {
190
185
191
186
return upstreamIP , nil
192
187
}
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