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

View File

@@ -2,15 +2,28 @@ package core
import (
"time"
"github.com/lucas-clemente/quic-go"
)
const (
protocolVersion = uint8(3)
protocolTimeout = 10 * time.Second
)
closeErrorCodeGeneric = 0
closeErrorCodeProtocol = 1
closeErrorCodeAuth = 2
type closeError struct {
Code quic.ApplicationErrorCode
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 {

View File

@@ -117,17 +117,17 @@ func (s *Server) handleClient(cc quic.Connection) {
stream, err := cc.AcceptStream(ctx)
ctxCancel()
if err != nil {
_ = cc.CloseWithError(closeErrorCodeProtocol, "protocol error")
_ = closeErrorProtocol.Send(cc)
return
}
// Handle the control stream
auth, ok, err := s.handleControlStream(cc, stream)
if err != nil {
_ = cc.CloseWithError(closeErrorCodeProtocol, "protocol error")
_ = closeErrorProtocol.Send(cc)
return
}
if !ok {
_ = cc.CloseWithError(closeErrorCodeAuth, "auth error")
_ = closeErrorAuth.Send(cc)
return
}
// 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.upCounterVec, s.downCounterVec, s.connGaugeVec)
err = sc.Run()
_ = cc.CloseWithError(closeErrorCodeGeneric, "")
_ = closeErrorGeneric.Send(cc)
s.disconnectFunc(cc.RemoteAddr(), auth, err)
}