From 3b31189f13e895fa90ecaf8ba3c7b7678dea59b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Mon, 27 Sep 2021 13:30:58 +0800 Subject: [PATCH] Send shadowsocks handshake with payload if available (#736) --- proxy/shadowsocks/client.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/proxy/shadowsocks/client.go b/proxy/shadowsocks/client.go index 67b2216d..6b362d33 100644 --- a/proxy/shadowsocks/client.go +++ b/proxy/shadowsocks/client.go @@ -2,6 +2,7 @@ package shadowsocks import ( "context" + "time" "github.com/xtls/xray-core/transport/internet/stat" @@ -101,18 +102,22 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle) if request.Command == protocol.RequestCommandTCP { - bufferedWriter := buf.NewBufferedWriter(buf.NewWriter(conn)) - bodyWriter, err := WriteTCPRequest(request, bufferedWriter) - if err != nil { - return newError("failed to write request").Base(err) - } - - if err := bufferedWriter.SetBuffered(false); err != nil { - return err - } - requestDone := func() error { defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly) + bufferedWriter := buf.NewBufferedWriter(buf.NewWriter(conn)) + bodyWriter, err := WriteTCPRequest(request, bufferedWriter) + if err != nil { + return newError("failed to write request").Base(err) + } + + if err = buf.CopyOnceTimeout(link.Reader, bodyWriter, time.Millisecond*100); err != nil && err != buf.ErrNotTimeoutReader && err != buf.ErrReadTimeout { + return newError("failed to write A request payload").Base(err).AtWarning() + } + + if err := bufferedWriter.SetBuffered(false); err != nil { + return err + } + return buf.Copy(link.Reader, bodyWriter, buf.UpdateActivity(timer)) }