chore: better quic close error codes

This commit is contained in:
Toby
2022-10-22 12:17:10 -07:00
parent b1d9ab6c6a
commit c98c7eca4e
3 changed files with 24 additions and 11 deletions

View File

@@ -94,18 +94,18 @@ func (c *Client) connect() error {
stream, err := quicConn.OpenStreamSync(ctx) stream, err := quicConn.OpenStreamSync(ctx)
ctxCancel() ctxCancel()
if err != nil { if err != nil {
_ = quicConn.CloseWithError(closeErrorCodeProtocol, "protocol error") _ = closeErrorProtocol.Send(quicConn)
_ = pktConn.Close() _ = pktConn.Close()
return err return err
} }
ok, msg, err := c.handleControlStream(quicConn, stream) ok, msg, err := c.handleControlStream(quicConn, stream)
if err != nil { if err != nil {
_ = quicConn.CloseWithError(closeErrorCodeProtocol, "protocol error") _ = closeErrorProtocol.Send(quicConn)
_ = pktConn.Close() _ = pktConn.Close()
return err return err
} }
if !ok { if !ok {
_ = quicConn.CloseWithError(closeErrorCodeAuth, "auth error") _ = closeErrorAuth.Send(quicConn)
_ = pktConn.Close() _ = pktConn.Close()
return fmt.Errorf("auth error: %s", msg) return fmt.Errorf("auth error: %s", msg)
} }
@@ -296,7 +296,7 @@ func (c *Client) DialUDP() (UDPConn, error) {
func (c *Client) Close() error { func (c *Client) Close() error {
c.reconnectMutex.Lock() c.reconnectMutex.Lock()
defer c.reconnectMutex.Unlock() defer c.reconnectMutex.Unlock()
err := c.quicConn.CloseWithError(closeErrorCodeGeneric, "") err := closeErrorGeneric.Send(c.quicConn)
_ = c.pktConn.Close() _ = c.pktConn.Close()
c.closed = true c.closed = true
return err return err

View File

@@ -2,15 +2,28 @@ package core
import ( import (
"time" "time"
"github.com/lucas-clemente/quic-go"
) )
const ( const (
protocolVersion = uint8(3) protocolVersion = uint8(3)
protocolTimeout = 10 * time.Second protocolTimeout = 10 * time.Second
)
closeErrorCodeGeneric = 0 type closeError struct {
closeErrorCodeProtocol = 1 Code quic.ApplicationErrorCode
closeErrorCodeAuth = 2 Msg string
}
func (e closeError) Send(c quic.Connection) error {
return c.CloseWithError(e.Code, e.Msg)
}
var (
closeErrorGeneric = closeError{0, ""}
closeErrorProtocol = closeError{1, "protocol error"}
closeErrorAuth = closeError{2, "auth error"}
) )
type transmissionRate struct { type transmissionRate struct {

View File

@@ -117,17 +117,17 @@ func (s *Server) handleClient(cc quic.Connection) {
stream, err := cc.AcceptStream(ctx) stream, err := cc.AcceptStream(ctx)
ctxCancel() ctxCancel()
if err != nil { if err != nil {
_ = cc.CloseWithError(closeErrorCodeProtocol, "protocol error") _ = closeErrorProtocol.Send(cc)
return return
} }
// Handle the control stream // Handle the control stream
auth, ok, err := s.handleControlStream(cc, stream) auth, ok, err := s.handleControlStream(cc, stream)
if err != nil { if err != nil {
_ = cc.CloseWithError(closeErrorCodeProtocol, "protocol error") _ = closeErrorProtocol.Send(cc)
return return
} }
if !ok { if !ok {
_ = cc.CloseWithError(closeErrorCodeAuth, "auth error") _ = closeErrorAuth.Send(cc)
return return
} }
// Start accepting streams and messages // Start accepting streams and messages
@@ -135,7 +135,7 @@ func (s *Server) handleClient(cc quic.Connection) {
s.tcpRequestFunc, s.tcpErrorFunc, s.udpRequestFunc, s.udpErrorFunc, s.tcpRequestFunc, s.tcpErrorFunc, s.udpRequestFunc, s.udpErrorFunc,
s.upCounterVec, s.downCounterVec, s.connGaugeVec) s.upCounterVec, s.downCounterVec, s.connGaugeVec)
err = sc.Run() err = sc.Run()
_ = cc.CloseWithError(closeErrorCodeGeneric, "") _ = closeErrorGeneric.Send(cc)
s.disconnectFunc(cc.RemoteAddr(), auth, err) s.disconnectFunc(cc.RemoteAddr(), auth, err)
} }