Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复一个导致 TPROXY 下 v2ray 断流的问题 #2348

Merged
merged 3 commits into from
Apr 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/proxyman/inbound/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ func (c *udpConn) Write(buf []byte) (int, error) {
return n, err
}

// Implements buf.ActivityNotifiable
func (c *udpConn) NotifyActivity() error {
if c.done.Done() {
return newError("connection is already closed")
}
c.updateActivity()
return nil
}

func (c *udpConn) Close() error {
common.Must(c.done.Close())
common.Must(common.Close(c.writer))
Expand Down
14 changes: 14 additions & 0 deletions common/buf/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
"v2ray.com/core/common/signal"
)

// ActivityNotifiable is a object that accepts activity notification outside the object
type ActivityNotifiable interface {
NotifyActivity() error
}

type dataHandler func(MultiBuffer)

type copyHandler struct {
Expand All @@ -31,6 +36,15 @@ func UpdateActivity(timer signal.ActivityUpdater) CopyOption {
}
}

// NotifyActivity is a CopyOption to notify activity on each data copy operation.
func NotifyActivity(notifier ActivityNotifiable) CopyOption {
return func(handler *copyHandler) {
handler.onData = append(handler.onData, func(MultiBuffer) {
notifier.NotifyActivity()
})
}
}

// CountSize is a CopyOption that sums the total size of data copied into the given SizeCounter.
func CountSize(sc *SizeCounter) CopyOption {
return func(handler *copyHandler) {
Expand Down
6 changes: 5 additions & 1 deletion proxy/dokodemo/dokodemo.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,18 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in

writer = &buf.SequentialWriter{Writer: tConn}
tReader := buf.NewPacketReader(tConn)
notify, ok := conn.(buf.ActivityNotifiable)
if !ok {
panic("conn should implement ActivityNotifiable")
}
requestCount++
tproxyRequest = func() error {
defer func() {
if atomic.AddInt32(&requestCount, -1) == 0 {
timer.SetTimeout(plcy.Timeouts.DownlinkOnly)
}
}()
if err := buf.Copy(tReader, link.Writer, buf.UpdateActivity(timer)); err != nil {
if err := buf.Copy(tReader, link.Writer, buf.UpdateActivity(timer), buf.NotifyActivity(notify)); err != nil {
return newError("failed to transport request (TPROXY conn)").Base(err)
}
return nil
Expand Down