Improve error processing

This commit is contained in:
世界 2022-07-30 09:57:02 +08:00
parent 9b9b5ebb72
commit d3378a575c
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
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)
}

2
go.mod
View file

@ -16,7 +16,7 @@ require (
github.com/sagernet/sing v0.0.0-20220729120910-4376f188c512
github.com/sagernet/sing-dns v0.0.0-20220729120941-109c0a7aabb1
github.com/sagernet/sing-shadowsocks v0.0.0-20220729155919-91d2780bfc80
github.com/sagernet/sing-tun v0.0.0-20220726111504-b4bded886e01
github.com/sagernet/sing-tun v0.0.0-20220730015349-3b4e77c4c1b3
github.com/sagernet/sing-vmess v0.0.0-20220726034841-4dae776653e5
github.com/spf13/cobra v1.5.0
github.com/stretchr/testify v1.8.0

4
go.sum
View file

@ -153,8 +153,8 @@ github.com/sagernet/sing-dns v0.0.0-20220729120941-109c0a7aabb1 h1:Gv9ow1IF98Qdx
github.com/sagernet/sing-dns v0.0.0-20220729120941-109c0a7aabb1/go.mod h1:LQJDT4IpqyWI6NugkSSqxTcFfxxNBp94n+fXtHFMboQ=
github.com/sagernet/sing-shadowsocks v0.0.0-20220729155919-91d2780bfc80 h1:gpCPZyZJQVn6ZTBCJ/XaYbPi6j43TdyTty/MI5bXhbE=
github.com/sagernet/sing-shadowsocks v0.0.0-20220729155919-91d2780bfc80/go.mod h1:mH6wE4b5FZp1Q/meATe4tjiPjvQO9E7Lr0FBBwFYp4I=
github.com/sagernet/sing-tun v0.0.0-20220726111504-b4bded886e01 h1:tNJn7T87sgQyA8gpEvC6LbusV4lkhZU8oi4mRujOhM8=
github.com/sagernet/sing-tun v0.0.0-20220726111504-b4bded886e01/go.mod h1:bYHamPB16GFGt34ayYt56Pb7aN64RPY0+uuFPBSbj0U=
github.com/sagernet/sing-tun v0.0.0-20220730015349-3b4e77c4c1b3 h1:QYKRVeyRa8bGE2ggOaroNlXQ/1cyRKGwtJOUOO/ZvXk=
github.com/sagernet/sing-tun v0.0.0-20220730015349-3b4e77c4c1b3/go.mod h1:lOVup6Io7873/8lUpdrBy/TLjQ7PJHUqSP/yp1k0ld8=
github.com/sagernet/sing-vmess v0.0.0-20220726034841-4dae776653e5 h1:TNguWTPF6gxX/gR02hY3LGviUn6LGlDPofE6lpSJWeo=
github.com/sagernet/sing-vmess v0.0.0-20220726034841-4dae776653e5/go.mod h1:Q8csko2kQZHRZTz8ztqELrJB22HV60/tztPVgACV84E=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=

View file

@ -66,6 +66,10 @@ func (h *Shadowsocks) NewPacketConnection(ctx context.Context, conn N.PacketConn
return NewPacketConnection(ctx, h, conn, metadata)
}
func (h *Shadowsocks) Close() error {
return common.Close(h.multiplexDialer)
}
var _ N.Dialer = (*shadowsocksDialer)(nil)
type shadowsocksDialer Shadowsocks

View file

@ -53,7 +53,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sagernet/sing-dns v0.0.0-20220729120941-109c0a7aabb1 // indirect
github.com/sagernet/sing-tun v0.0.0-20220726111504-b4bded886e01 // indirect
github.com/sagernet/sing-tun v0.0.0-20220730015349-3b4e77c4c1b3 // indirect
github.com/sagernet/sing-vmess v0.0.0-20220726034841-4dae776653e5 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/vishvananda/netlink v1.1.0 // indirect

View file

@ -178,8 +178,8 @@ github.com/sagernet/sing-dns v0.0.0-20220729120941-109c0a7aabb1 h1:Gv9ow1IF98Qdx
github.com/sagernet/sing-dns v0.0.0-20220729120941-109c0a7aabb1/go.mod h1:LQJDT4IpqyWI6NugkSSqxTcFfxxNBp94n+fXtHFMboQ=
github.com/sagernet/sing-shadowsocks v0.0.0-20220729155919-91d2780bfc80 h1:gpCPZyZJQVn6ZTBCJ/XaYbPi6j43TdyTty/MI5bXhbE=
github.com/sagernet/sing-shadowsocks v0.0.0-20220729155919-91d2780bfc80/go.mod h1:mH6wE4b5FZp1Q/meATe4tjiPjvQO9E7Lr0FBBwFYp4I=
github.com/sagernet/sing-tun v0.0.0-20220726111504-b4bded886e01 h1:tNJn7T87sgQyA8gpEvC6LbusV4lkhZU8oi4mRujOhM8=
github.com/sagernet/sing-tun v0.0.0-20220726111504-b4bded886e01/go.mod h1:bYHamPB16GFGt34ayYt56Pb7aN64RPY0+uuFPBSbj0U=
github.com/sagernet/sing-tun v0.0.0-20220730015349-3b4e77c4c1b3 h1:QYKRVeyRa8bGE2ggOaroNlXQ/1cyRKGwtJOUOO/ZvXk=
github.com/sagernet/sing-tun v0.0.0-20220730015349-3b4e77c4c1b3/go.mod h1:lOVup6Io7873/8lUpdrBy/TLjQ7PJHUqSP/yp1k0ld8=
github.com/sagernet/sing-vmess v0.0.0-20220726034841-4dae776653e5 h1:TNguWTPF6gxX/gR02hY3LGviUn6LGlDPofE6lpSJWeo=
github.com/sagernet/sing-vmess v0.0.0-20220726034841-4dae776653e5/go.mod h1:Q8csko2kQZHRZTz8ztqELrJB22HV60/tztPVgACV84E=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=