Skip to content

Commit

Permalink
Dokodemo TPROXY: Interrupt UDP download if upload timeouts
Browse files Browse the repository at this point in the history
Fixes #4194
  • Loading branch information
RPRX authored Dec 28, 2024
1 parent 42aea01 commit 8377975
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions proxy/dokodemo/dokodemo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dokodemo

import (
"context"
"runtime"
"sync/atomic"

"github.com/xtls/xray-core/common"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -180,7 +177,12 @@ 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() {
runtime.Gosched()
common.Interrupt(link.Reader) // maybe duplicated
runtime.Gosched()
writer.(*PacketWriter).Close()
}()
/*
sockopt := &internet.SocketConfig{
Tproxy: internet.SocketConfig_TProxy,
Expand Down Expand Up @@ -219,17 +221,24 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn st
responseDone := func() error {
defer timer.SetTimeout(plcy.Timeouts.UplinkOnly)

if network == net.Network_UDP && destinationOverridden {
buf.Copy(link.Reader, writer)
return nil
}

if err := buf.Copy(link.Reader, writer, buf.UpdateActivity(timer)); 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 {
common.Interrupt(link.Reader)
if err := task.Run(ctx,
task.OnSuccess(func() error { return task.Run(ctx, requestDone) }, task.Close(link.Writer)),
responseDone); err != nil {
runtime.Gosched()
common.Interrupt(link.Writer)
runtime.Gosched()
common.Interrupt(link.Reader)
return errors.New("connection ends").Base(err)
}

Expand Down

0 comments on commit 8377975

Please sign in to comment.