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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: version negotiation between server and client #390

Merged
merged 1 commit into from Nov 4, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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},
fanweixiao marked this conversation as resolved.
Show resolved Hide resolved
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