From ec46a570bfb234ee3b5303d53418253767c39dbe Mon Sep 17 00:00:00 2001 From: RPRX <63339210+RPRX@users.noreply.github.com> Date: Sat, 28 Dec 2024 01:37:55 +0000 Subject: [PATCH] Dokodemo TRPOXY: Interrupt download if upload timeouts/ends for UDP Fixes https://github.com/XTLS/Xray-core/issues/4194 --- proxy/dokodemo/dokodemo.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/proxy/dokodemo/dokodemo.go b/proxy/dokodemo/dokodemo.go index bbeab18ff621..ecdbebfa8b99 100644 --- a/proxy/dokodemo/dokodemo.go +++ b/proxy/dokodemo/dokodemo.go @@ -2,6 +2,7 @@ package dokodemo import ( "context" + "runtime" "sync/atomic" "github.com/xtls/xray-core/common" @@ -147,10 +148,6 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn st return nil } - tproxyRequest := func() error { - return nil - } - var writer buf.Writer if network == net.Network_TCP { writer = buf.NewWriter(conn) @@ -180,7 +177,11 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn st return err } writer = NewPacketWriter(pConn, &dest, mark, back) - defer writer.(*PacketWriter).Close() + defer func() { + common.Interrupt(link.Reader) + runtime.Gosched() + writer.(*PacketWriter).Close() + }() /* sockopt := &internet.SocketConfig{ Tproxy: internet.SocketConfig_TProxy, @@ -219,15 +220,21 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn st responseDone := func() error { defer timer.SetTimeout(plcy.Timeouts.UplinkOnly) - if err := buf.Copy(link.Reader, writer, buf.UpdateActivity(timer)); err != nil { + var err error + if network == net.Network_TCP || !destinationOverridden { + err = buf.Copy(link.Reader, writer, buf.UpdateActivity(timer)) + } else { + err = buf.Copy(link.Reader, writer) + } + if err != nil { return errors.New("failed to transport response").Base(err) } return nil } - if err := task.Run(ctx, task.OnSuccess(func() error { - return task.Run(ctx, requestDone, tproxyRequest) - }, task.Close(link.Writer)), responseDone); err != nil { + if err := task.Run(ctx, + task.OnSuccess(func() error { return task.Run(ctx, requestDone) }, task.Close(link.Writer)), + responseDone); err != nil { common.Interrupt(link.Reader) common.Interrupt(link.Writer) return errors.New("connection ends").Base(err)