From e014220508addc2f3e619305c02af1f9b8487ea9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 31 Jul 2022 18:08:24 +0800 Subject: [PATCH] Add retry for mux stream open --- common/mux/client.go | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/common/mux/client.go b/common/mux/client.go index cbba11f5..7c7b9a2b 100644 --- a/common/mux/client.go +++ b/common/mux/client.go @@ -75,20 +75,30 @@ func (c *Client) ListenPacket(ctx context.Context, destination M.Socksaddr) (net if err != nil { return nil, err } - // return bufio.NewUnbindPacketConn(&ClientPacketConn{ExtendedConn: bufio.NewExtendedConn(stream), destination: destination}), nil return &ClientPacketAddrConn{ExtendedConn: bufio.NewExtendedConn(stream), destination: destination}, nil } func (c *Client) openStream() (net.Conn, error) { - session, err := c.offer() + var ( + session *yamux.Session + stream *yamux.Stream + err error + ) + for attempts := 0; attempts < 2; attempts++ { + session, err = c.offer() + if err != nil { + continue + } + stream, err = session.OpenStream() + if err != nil { + continue + } + break + } if err != nil { return nil, err } - conn, err := session.Open() - if err != nil { - return nil, err - } - return &wrapStream{conn}, nil + return &wrapStream{stream}, nil } func (c *Client) offer() (*yamux.Session, error) {