diff --git a/common/mux/client.go b/common/mux/client.go index b39e1bbc..22b762c6 100644 --- a/common/mux/client.go +++ b/common/mux/client.go @@ -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) { diff --git a/common/mux/protocol.go b/common/mux/protocol.go index 00df1823..97f6de31 100644 --- a/common/mux/protocol.go +++ b/common/mux/protocol.go @@ -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 + } +} diff --git a/common/mux/service.go b/common/mux/service.go index 21b6d1c3..d2072714 100644 --- a/common/mux/service.go +++ b/common/mux/service.go @@ -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) } diff --git a/go.mod b/go.mod index 574190b2..a25811d0 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 23e3e5b4..356bcca8 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/outbound/shadowsocks.go b/outbound/shadowsocks.go index 31f83b38..6557e4c9 100644 --- a/outbound/shadowsocks.go +++ b/outbound/shadowsocks.go @@ -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 diff --git a/test/go.mod b/test/go.mod index 1376263f..59f1915d 100644 --- a/test/go.mod +++ b/test/go.mod @@ -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 diff --git a/test/go.sum b/test/go.sum index bf84feba..7b2e33d2 100644 --- a/test/go.sum +++ b/test/go.sum @@ -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=