Improve error processing

This commit is contained in:
世界
2022-07-30 09:57:02 +08:00
parent 9b9b5ebb72
commit d3378a575c
8 changed files with 38 additions and 8 deletions

View File

@@ -88,7 +88,7 @@ func (c *Client) openStream() (net.Conn, error) {
if err != nil {
return nil, err
}
return conn, nil
return &wrapStream{conn}, nil
}
func (c *Client) offer() (*yamux.Session, error) {

View File

@@ -3,6 +3,7 @@ package mux
import (
"encoding/binary"
"io"
"net"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing/common"
@@ -117,3 +118,28 @@ func ReadResponse(reader io.Reader) (*Response, error) {
}
return &response, nil
}
type wrapStream struct {
net.Conn
}
func (w *wrapStream) Read(p []byte) (n int, err error) {
n, err = w.Conn.Read(p)
err = wrapError(err)
return
}
func (w *wrapStream) Write(p []byte) (n int, err error) {
n, err = w.Conn.Write(p)
err = wrapError(err)
return
}
func wrapError(err error) error {
switch err {
case yamux.ErrStreamClosed:
return io.EOF
default:
return err
}
}

View File

@@ -28,6 +28,7 @@ func NewConnection(ctx context.Context, router adapter.Router, errorHandler E.Ha
if err != nil {
return err
}
stream = &wrapStream{stream}
request, err := ReadRequest(stream)
if err != nil {
return err
@@ -37,7 +38,6 @@ func NewConnection(ctx context.Context, router adapter.Router, errorHandler E.Ha
go func() {
logger.InfoContext(ctx, "inbound multiplex connection to ", metadata.Destination)
hErr := router.RouteConnection(ctx, &ServerConn{ExtendedConn: bufio.NewExtendedConn(stream)}, metadata)
// hErr := router.RouteConnection(ctx, &ServerConn{ExtendedConn: bufio.NewExtendedConn(stream)}, metadata)
if hErr != nil {
errorHandler.NewError(ctx, hErr)
}