Skip to content

Commit

Permalink
fix(tunnel): udp can't be closed correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
igoogolx committed Nov 27, 2023
1 parent e8b4f1e commit 4788181
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions internal/proxy_handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ import (
"github.com/sagernet/sing/common/buf"
M "github.com/sagernet/sing/common/metadata"
"github.com/sagernet/sing/common/network"
"io"
"net"
"sync"
)

type udpConn struct {
network.PacketConn
read bool
}

func (uc udpConn) ReadFrom(data []byte) (int, net.Addr, error) {
func (uc *udpConn) ReadFrom(data []byte) (int, net.Addr, error) {
if uc.read {
return 0, nil, io.EOF
}
newBuf := buf.NewPacket()
defer newBuf.Release()
des, err := uc.ReadPacket(newBuf)
Expand All @@ -27,10 +32,11 @@ func (uc udpConn) ReadFrom(data []byte) (int, net.Addr, error) {
if err != nil {
return 0, nil, err
}
uc.read = true
return n, des.UDPAddr(), nil
}

func (uc udpConn) WriteTo(data []byte, addr net.Addr) (int, error) {
func (uc *udpConn) WriteTo(data []byte, addr net.Addr) (int, error) {
newBuf := buf.NewPacket()
defer newBuf.Release()
_, err := newBuf.Write(data)
Expand Down Expand Up @@ -80,7 +86,7 @@ func (c ConnHandler) NewPacketConnection(ctx context.Context, packetConn network
var wg sync.WaitGroup
wg.Add(1)
defer wg.Wait()
ct, err := conn.NewUdpConnContext(ctx, udpConn{packetConn}, &m, &wg)
ct, err := conn.NewUdpConnContext(ctx, &udpConn{PacketConn: packetConn}, &m, &wg)
if err != nil {
return err
}
Expand Down

0 comments on commit 4788181

Please sign in to comment.