mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-01 14:18:36 +00:00
Fix flow close racing with tracker attach
This commit is contained in:
+20
-12
@@ -2,6 +2,7 @@ package route
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
@@ -16,7 +17,7 @@ import (
|
||||
var (
|
||||
_ tun.FlowTracker = (*flowLogger)(nil)
|
||||
_ tun.FlowTracker = (*flowInterrupter)(nil)
|
||||
_ tun.FlowTracker = multiFlowTracker(nil)
|
||||
_ tun.FlowTracker = (*multiFlowTracker)(nil)
|
||||
)
|
||||
|
||||
type flowLogger struct {
|
||||
@@ -70,34 +71,41 @@ func (l *flowLogger) CloseFlow(reason tun.FlowCloseReason) {
|
||||
", upload ", byteformats.FormatBytes(uint64(l.upload.Load())), ", download ", byteformats.FormatBytes(uint64(l.download.Load())))
|
||||
}
|
||||
|
||||
type multiFlowTracker []tun.FlowTracker
|
||||
type multiFlowTracker struct {
|
||||
access sync.Mutex
|
||||
trackers []tun.FlowTracker
|
||||
}
|
||||
|
||||
func (t multiFlowTracker) AttachFlow(handle tun.FlowHandle) {
|
||||
for _, tracker := range t {
|
||||
func (t *multiFlowTracker) AttachFlow(handle tun.FlowHandle) {
|
||||
t.access.Lock()
|
||||
defer t.access.Unlock()
|
||||
for _, tracker := range t.trackers {
|
||||
tracker.AttachFlow(handle)
|
||||
}
|
||||
}
|
||||
|
||||
func (t multiFlowTracker) CountForward(n int) {
|
||||
for _, tracker := range t {
|
||||
func (t *multiFlowTracker) CountForward(n int) {
|
||||
for _, tracker := range t.trackers {
|
||||
tracker.CountForward(n)
|
||||
}
|
||||
}
|
||||
|
||||
func (t multiFlowTracker) CountReverse(n int) {
|
||||
for _, tracker := range t {
|
||||
func (t *multiFlowTracker) CountReverse(n int) {
|
||||
for _, tracker := range t.trackers {
|
||||
tracker.CountReverse(n)
|
||||
}
|
||||
}
|
||||
|
||||
func (t multiFlowTracker) FlowEstablished() {
|
||||
for _, tracker := range t {
|
||||
func (t *multiFlowTracker) FlowEstablished() {
|
||||
for _, tracker := range t.trackers {
|
||||
tracker.FlowEstablished()
|
||||
}
|
||||
}
|
||||
|
||||
func (t multiFlowTracker) CloseFlow(reason tun.FlowCloseReason) {
|
||||
for _, tracker := range t {
|
||||
func (t *multiFlowTracker) CloseFlow(reason tun.FlowCloseReason) {
|
||||
t.access.Lock()
|
||||
defer t.access.Unlock()
|
||||
for _, tracker := range t.trackers {
|
||||
tracker.CloseFlow(reason)
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -564,7 +564,7 @@ func (r *Router) preMatchFlow(ctx context.Context, metadata *adapter.InboundCont
|
||||
if len(flowTrackers) == 1 {
|
||||
return flowTrackers[0]
|
||||
}
|
||||
return multiFlowTracker(flowTrackers)
|
||||
return &multiFlowTracker{trackers: flowTrackers}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user