Skip to content

Commit

Permalink
feat: version negotiation between server and client (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
wujunzhuo committed Nov 4, 2022
1 parent d2e4851 commit 1fd13df
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/client.go
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion core/listener_default.go
Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions core/server.go
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down

0 comments on commit 1fd13df

Please sign in to comment.