Skip to content
Open
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
5 changes: 4 additions & 1 deletion common/udpnat2/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ func (c *natConn) SetTimeout(timeout time.Duration) bool {
func (c *natConn) Close() error {
c.closeOnce.Do(func() {
close(c.doneChan)
common.Close(c.handler)
c.handlerAccess.RLock()
handler := c.handler
c.handlerAccess.RUnlock()
common.Close(handler)
})
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion common/udpnat2/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ func (s *Service) NewPacket(bufferSlices [][]byte, source M.Socksaddr, destinati
conn.handlerAccess.RLock()
readWaitOptions := conn.readWaitOptions
handler := conn.handler
conn.handlerAccess.RUnlock()
var dataLen int
for _, bufferSlice := range bufferSlices {
dataLen += len(bufferSlice)
Expand All @@ -80,6 +79,7 @@ func (s *Service) NewPacket(bufferSlices [][]byte, source M.Socksaddr, destinati
}
readWaitOptions.PostReturn(buffer)
if handler != nil {
conn.handlerAccess.RUnlock()
handler.NewPacketEx(buffer, destination)
return
}
Expand All @@ -94,6 +94,7 @@ func (s *Service) NewPacket(bufferSlices [][]byte, source M.Socksaddr, destinati
packet.Buffer.Release()
N.PutPacketBuffer(packet)
}
conn.handlerAccess.RUnlock()
}

func (s *Service) NewPacketBatch(buffers []*buf.Buffer, sources []M.Socksaddr, destination M.Socksaddr, userData any) {
Expand Down
56 changes: 56 additions & 0 deletions common/udpnat2/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package udpnat

import (
"context"
"net/netip"
"sync/atomic"
"testing"
"time"

"github.com/sagernet/sing/common/buf"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)

type testCountingHandler struct {
delivered *atomic.Int64
}

func (h testCountingHandler) NewPacketEx(buffer *buf.Buffer, _ M.Socksaddr) {
h.delivered.Add(1)
buffer.Release()
}

type testPushConnectionHandler struct {
delivered *atomic.Int64
}

func (h testPushConnectionHandler) NewPacketConnectionEx(_ context.Context, conn N.PacketConn, _ M.Socksaddr, _ M.Socksaddr, _ N.CloseHandlerFunc) {
conn.(*natConn).SetHandler(testCountingHandler{h.delivered})
}

func TestServiceDeliversFirstPacket(t *testing.T) {
t.Parallel()

var delivered atomic.Int64
prepare := func(M.Socksaddr, M.Socksaddr, any) (bool, context.Context, N.PacketWriter, N.CloseHandlerFunc) {
return true, context.Background(), testPacketWriter{}, nil
}
service := New(testPushConnectionHandler{&delivered}, prepare, time.Minute, false)
const flows = 100000
destination := M.ParseSocksaddr("1.1.1.1:53")
for i := 0; i < flows; i++ {
source := M.SocksaddrFromNetIP(netip.AddrPortFrom(netip.AddrFrom4([4]byte{10, byte(i >> 16), byte(i >> 8), byte(i)}), 1024))
service.NewPacket([][]byte{[]byte("a")}, source, destination, nil)
if i%1000 == 999 {
service.Purge()
}
}
deadline := time.Now().Add(5 * time.Second)
for delivered.Load() < flows && time.Now().Before(deadline) {
time.Sleep(10 * time.Millisecond)
}
if lost := flows - delivered.Load(); lost != 0 {
t.Fatalf("%d of %d first packets lost", lost, flows)
}
}