Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow client to connect without ALPN if listener has a single handler #59

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions tls/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,32 +258,46 @@ func (self *sharedListener) getConfig(info *tls.ClientHelloInfo) (*tls.Config, e
protos := info.SupportedProtos
log.Debug("client requesting protocols = ", protos)

if protos == nil {
protos = append(protos, noProtocol)
}

ctx := info.Context()
handler := ctx.Value(handlerKey).(**protocolHandler)
handlerOut := ctx.Value(handlerKey).(**protocolHandler)

self.mtx.RLock()
defer self.mtx.RUnlock()

for _, proto := range protos {
acc, found := self.handlers[proto]
if found {
log.Debugf("found handler for proto[%s]", proto)
*handler = acc
cfg := acc.tls
if cfg.GetConfigForClient != nil {
c, _ := cfg.GetConfigForClient(info)
if c != nil {
cfg = c
}
var handler *protocolHandler
var proto string
if protos == nil && len(self.handlers) == 1 {
log.Debugf("using single protocol as default")
for p, h := range self.handlers {
proto, handler = p, h
}
} else {
if protos == nil {
protos = append(protos, noProtocol)
}

for _, p := range protos {
h, found := self.handlers[p]
if found {
log.Debugf("found handler for proto[%s]", proto)
handler = h
proto = p
}
}
}

if handler != nil {
*handlerOut = handler
cfg := handler.tls
if cfg.GetConfigForClient != nil {
c, _ := cfg.GetConfigForClient(info)
if c != nil {
cfg = c
}
cfg = cfg.Clone()
cfg.NextProtos = []string{proto}
return cfg, nil
}
cfg = cfg.Clone()
cfg.NextProtos = []string{proto}
return cfg, nil
}

return nil, fmt.Errorf("not handler for requested protocols %+v", protos)
Expand Down
25 changes: 25 additions & 0 deletions tls/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,28 @@ func TestListenTLS(t *testing.T) {
req.NoError(httpListener.Close())
req.NoError(fooListener.Close())
}

func TestListenSingleProto(t *testing.T) {
req := require.New(t)

ident := &identity.TokenId{
Identity: serverId,
Token: "test",
Data: nil,
}

testAddress := "localhost:14444"

if _, ok := sharedListeners.Load(testAddress); ok {
t.Error("should be empty")
}

fooListener, err := Listen(testAddress, "fooListener", ident, makeGreeter("foo"), "foo")
req.NoError(err)

req.NoError(checkClient(testAddress, "foo", "foo", t), "should find handler")
req.NoError(checkClient(testAddress, "", "foo", t), "should find handler")
req.Error(checkClient(testAddress, "bar", "bar", t), "should have no handler")

req.NoError(fooListener.Close())
}
Loading