From 1fd13df6ac5421919829c8e02c425c5b1d2ef516 Mon Sep 17 00:00:00 2001 From: wujunzhuo Date: Fri, 4 Nov 2022 22:59:41 +0800 Subject: [PATCH] feat: version negotiation between server and client (#390) --- core/client.go | 2 +- core/listener_default.go | 2 +- core/server.go | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/core/client.go b/core/client.go index c7ceaeff2..1982c4a52 100644 --- a/core/client.go +++ b/core/client.go @@ -346,7 +346,7 @@ func (c *Client) initOptions() error { // quic config if c.opts.QuicConfig == nil { c.opts.QuicConfig = &quic.Config{ - Versions: []quic.VersionNumber{quic.Version1, quic.VersionDraft29}, + Versions: []quic.VersionNumber{quic.Version2}, MaxIdleTimeout: time.Second * 40, KeepAlivePeriod: time.Second * 20, MaxIncomingStreams: 1000, diff --git a/core/listener_default.go b/core/listener_default.go index 5bdb1c470..e926cf7a8 100644 --- a/core/listener_default.go +++ b/core/listener_default.go @@ -19,7 +19,7 @@ type defaultListener struct { // DefalutQuicConfig be used when `quicConfig` is nil. var DefalutQuicConfig = &quic.Config{ - Versions: []quic.VersionNumber{quic.Version1, quic.VersionDraft29}, + Versions: []quic.VersionNumber{quic.Version2, quic.Version1, quic.VersionDraft29}, MaxIdleTimeout: time.Second * 5, KeepAlivePeriod: time.Second * 2, MaxIncomingStreams: 1000, diff --git a/core/server.go b/core/server.go index 69daff318..ec3313a56 100644 --- a/core/server.go +++ b/core/server.go @@ -40,6 +40,7 @@ type Server struct { connector Connector router Router metadataBuilder MetadataBuilder + alpnHandler func(proto string) error counterOfDataFrame int64 downstreams map[string]*Client mu sync.Mutex @@ -121,6 +122,12 @@ func (s *Server) Serve(ctx context.Context, conn net.PacketConn) error { logger.Errorf("%slistener accept connections error: %v", ServerLogPrefix, err) return err } + err = s.alpnHandler(conn.ConnectionState().TLS.NegotiatedProtocol) + if err != nil { + conn.CloseWithError(quic.ApplicationErrorCode(yerr.ErrorCodeRejected), err.Error()) + continue + } + // connection close handlers on server shutdown // defer s.doConnectionCloseHandlers(conn) s.wg.Add(1) @@ -485,6 +492,14 @@ func (s *Server) ConfigMetadataBuilder(builder MetadataBuilder) { s.mu.Unlock() } +// ConfigAlpnHandler is used to set alpnHandler by zipper +func (s *Server) ConfigAlpnHandler(h func(string) error) { + s.mu.Lock() + s.alpnHandler = h + logger.Debugf("%sconfig alpnHandler is %#v", ServerLogPrefix, h) + s.mu.Unlock() +} + // AddDownstreamServer add a downstream server to this server. all the DataFrames will be // dispatch to all the downstreams. func (s *Server) AddDownstreamServer(addr string, c *Client) { @@ -508,6 +523,12 @@ func GetConnID(conn quic.Connection) string { func (s *Server) initOptions() { // defaults + if s.alpnHandler == nil { + s.alpnHandler = func(proto string) error { + logger.Infof("%sclient alpn proto is: %s", ServerLogPrefix, proto) + return nil + } + } } func (s *Server) validateRouter() error {