Skip to content

Commit 85830ef

Browse files
James Fargherproglottis
authored andcommitted
sshd: Extract static proxy policy handler
Instead of interpreting the configuration for every new connection, we can rely on a closure to simplify the proxy handler path. This is more similar to how the provided MustStrictWhiteListPolicy works which will be added in a later commit.
1 parent 0a8db0d commit 85830ef

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

internal/sshd/sshd.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"sync"
1010
"time"
1111

12-
"github.com/pires/go-proxyproto"
12+
proxyproto "github.com/pires/go-proxyproto"
1313
"golang.org/x/crypto/ssh"
1414

1515
"gitlab.com/gitlab-org/gitlab-shell/v14/client"
@@ -97,7 +97,7 @@ func (s *Server) listen(ctx context.Context) error {
9797
if s.Config.Server.ProxyProtocol {
9898
sshListener = &proxyproto.Listener{
9999
Listener: sshListener,
100-
Policy: s.requirePolicy,
100+
Policy: s.requirePolicy(),
101101
ReadHeaderTimeout: time.Duration(s.Config.Server.ProxyHeaderTimeout),
102102
}
103103

@@ -200,17 +200,23 @@ func (s *Server) handleConn(ctx context.Context, nconn net.Conn) {
200200
})
201201
}
202202

203-
func (s *Server) requirePolicy(_ net.Addr) (proxyproto.Policy, error) {
203+
func (s *Server) requirePolicy() proxyproto.PolicyFunc {
204204
// Set the Policy value based on config
205205
// Values are taken from https://github.com/pires/go-proxyproto/blob/195fedcfbfc1be163f3a0d507fac1709e9d81fed/policy.go#L20
206206
switch strings.ToLower(s.Config.Server.ProxyPolicy) {
207207
case "require":
208-
return proxyproto.REQUIRE, nil
208+
return staticProxyPolicy(proxyproto.REQUIRE)
209209
case "ignore":
210-
return proxyproto.IGNORE, nil
210+
return staticProxyPolicy(proxyproto.IGNORE)
211211
case "reject":
212-
return proxyproto.REJECT, nil
212+
return staticProxyPolicy(proxyproto.REJECT)
213213
default:
214-
return proxyproto.USE, nil
214+
return staticProxyPolicy(proxyproto.USE)
215+
}
216+
}
217+
218+
func staticProxyPolicy(policy proxyproto.Policy) proxyproto.PolicyFunc {
219+
return func(_ net.Addr) (proxyproto.Policy, error) {
220+
return policy, nil
215221
}
216222
}

0 commit comments

Comments
 (0)