Handle connected UDP EOF on BSD

This commit is contained in:
世界
2026-10-09 12:36:03 +08:00
parent 52f9bad034
commit bff5acf292
4 changed files with 71 additions and 9 deletions
+9
View File
@@ -423,6 +423,15 @@ func (d *DefaultDialer) trackConn(ctx context.Context, destination M.Socksaddr,
if err != nil {
return conn, err
}
if nativeConn, isUDPConn := conn.(*net.UDPConn); isUDPConn {
var rawConn syscall.RawConn
rawConn, err = nativeConn.SyscallConn()
if err != nil {
conn.Close()
return nil, err
}
conn = &udpConn{Conn: conn, rawConn: rawConn}
}
if d.connectionManager != nil {
conn = d.connectionManager.TrackConn(conn)
}
+53
View File
@@ -0,0 +1,53 @@
package dialer
import (
"io"
"net"
"syscall"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/control"
N "github.com/sagernet/sing/common/network"
)
type udpConn struct {
net.Conn
rawConn syscall.RawConn
}
func (c *udpConn) Read(b []byte) (n int, err error) {
n, err = c.Conn.Read(b)
if n == 0 && err == nil {
var isEOF bool
c.rawConn.Control(func(fd uintptr) {
isEOF = control.IsUDPEOF(fd)
})
if isEOF {
err = io.EOF
}
}
return
}
func (c *udpConn) SyscallConn() (syscall.RawConn, error) {
return c.rawConn, nil
}
func (c *udpConn) Upstream() any {
return c.Conn
}
func (c *udpConn) ReaderReplaceable() bool {
return true
}
func (c *udpConn) WriterReplaceable() bool {
return true
}
var (
_ syscall.Conn = (*udpConn)(nil)
_ common.WithUpstream = (*udpConn)(nil)
_ N.ReaderWithUpstream = (*udpConn)(nil)
_ N.WriterWithUpstream = (*udpConn)(nil)
)
+3 -3
View File
@@ -44,8 +44,8 @@ require (
github.com/sagernet/gvisor v0.0.0-20260727.0-sing-box-mod.1
github.com/sagernet/netlink v0.0.0-20260814022025-64455d367bbf
github.com/sagernet/nftables v0.3.0-mod.4
github.com/sagernet/quic-go v0.61.0-sing-box-mod.7
github.com/sagernet/sing v0.9.7-0.20260929145605-8d939df03478
github.com/sagernet/quic-go v0.61.0-sing-box-mod.9
github.com/sagernet/sing v0.9.7-0.20260929150544-0ad23b637bd4
github.com/sagernet/sing-cloudflared v0.1.4-0.20260929150702-b3a1e8f3018c
github.com/sagernet/sing-mux v0.3.10-0.20260929204512-caf09fe32475
github.com/sagernet/sing-openconnect v0.1.1-0.20260929151225-cbd68c45de58
@@ -60,7 +60,7 @@ require (
github.com/sagernet/sing-vmess v0.2.9-0.20260929152519-9b95ab8c9478
github.com/sagernet/smux v1.5.50-sing-box-mod.1
github.com/sagernet/tailscale v1.102.1-sing-box-1.14-mod.5
github.com/sagernet/wireguard-go v0.0.7
github.com/sagernet/wireguard-go v0.0.8-0.20260929150556-6731c7387c81
github.com/sagernet/ws v0.0.0-20231204124109-acfe8907c854
github.com/spf13/cobra v1.10.2
github.com/stretchr/testify v1.12.0
+6 -6
View File
@@ -316,10 +316,10 @@ github.com/sagernet/netlink v0.0.0-20260814022025-64455d367bbf h1:b/3rm+KvxzuYRc
github.com/sagernet/netlink v0.0.0-20260814022025-64455d367bbf/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM=
github.com/sagernet/nftables v0.3.0-mod.4 h1:vnOtcDYeSXv2e5RoRuGH0lrpttQFJ8iC4ICS2nhlDSo=
github.com/sagernet/nftables v0.3.0-mod.4/go.mod h1:8kslHG4VvYNihcco+i6uxIX7qbT8A56T0y5q7U44ZaQ=
github.com/sagernet/quic-go v0.61.0-sing-box-mod.7 h1:sW2O+DoNF+my1PGlSZ3zG2N+fdxLR3DC0l4sXCM0lBE=
github.com/sagernet/quic-go v0.61.0-sing-box-mod.7/go.mod h1:hmLC8GJPp+BrpBgrgJnacvI4fZGIhasuy1ROJJ8GR4E=
github.com/sagernet/sing v0.9.7-0.20260929145605-8d939df03478 h1:c3Jpk13MOAc516zCuqAGBr7ok9UcpGjCjgWWOQslL0w=
github.com/sagernet/sing v0.9.7-0.20260929145605-8d939df03478/go.mod h1:K3Owt3xPhHugvlnlPPxZJ/exXdaJfEPOTNorGk4AXjo=
github.com/sagernet/quic-go v0.61.0-sing-box-mod.9 h1:Lojx77uC63TxZO38Z1OSo6aOoBSXY+ueH0/IKKeTZuI=
github.com/sagernet/quic-go v0.61.0-sing-box-mod.9/go.mod h1:hmLC8GJPp+BrpBgrgJnacvI4fZGIhasuy1ROJJ8GR4E=
github.com/sagernet/sing v0.9.7-0.20260929150544-0ad23b637bd4 h1:d4WT3KO4zhp9qzPHUZCKqCP+ZmP1GXBsAmReqlnE4zA=
github.com/sagernet/sing v0.9.7-0.20260929150544-0ad23b637bd4/go.mod h1:K3Owt3xPhHugvlnlPPxZJ/exXdaJfEPOTNorGk4AXjo=
github.com/sagernet/sing-cloudflared v0.1.4-0.20260929150702-b3a1e8f3018c h1:36zXD/qnDk7sh/J4zSEJsnHf4W3TYNCbmjo9MNIlrE4=
github.com/sagernet/sing-cloudflared v0.1.4-0.20260929150702-b3a1e8f3018c/go.mod h1:XEqEDYRCAYLaoPjZ1ifVWJg5iWAJHL2gOAXe/PM28Cg=
github.com/sagernet/sing-mux v0.3.10-0.20260929204512-caf09fe32475 h1:aG0DqHEOjax7DyfE1GTAXZotOg3QGVCz3/8rCSua2kg=
@@ -348,8 +348,8 @@ github.com/sagernet/smux v1.5.50-sing-box-mod.1 h1:XkJcivBC9V4wBjiGXIXZ229aZCU1h
github.com/sagernet/smux v1.5.50-sing-box-mod.1/go.mod h1:NjhsCEWedJm7eFLyhuBgIEzwfhRmytrUoiLluxs5Sk8=
github.com/sagernet/tailscale v1.102.1-sing-box-1.14-mod.5 h1:mGLWVgj2Pl2NlgE1+sEUIDIMB42JdWEPwYmxTnVWg44=
github.com/sagernet/tailscale v1.102.1-sing-box-1.14-mod.5/go.mod h1:WLUSOPmTcf7VN9gLCe01qUSIvD+/cKC177neENyZPkI=
github.com/sagernet/wireguard-go v0.0.7 h1:FmP7QY5OcFlhKtDVB81Xd+uoBv4BjW5GORBTUYwpuu0=
github.com/sagernet/wireguard-go v0.0.7/go.mod h1:er10sELpmzLXq7S7Pbc1Zsbyapcr+/gxNAHKTo6fzVA=
github.com/sagernet/wireguard-go v0.0.8-0.20260929150556-6731c7387c81 h1:VsrjoaljEz7jQ1LWbrNyf/scQSQIkVVp7v4nSDrPJ+I=
github.com/sagernet/wireguard-go v0.0.8-0.20260929150556-6731c7387c81/go.mod h1:er10sELpmzLXq7S7Pbc1Zsbyapcr+/gxNAHKTo6fzVA=
github.com/sagernet/ws v0.0.0-20231204124109-acfe8907c854 h1:6uUiZcDRnZSAegryaUGwPC/Fj13JSHwiTftrXhMmYOc=
github.com/sagernet/ws v0.0.0-20231204124109-acfe8907c854/go.mod h1:LtfoSK3+NG57tvnVEHgcuBW9ujgE8enPSgzgwStwCAA=
github.com/smallstep/pkcs7 v0.1.1 h1:x+rPdt2W088V9Vkjho4KtoggyktZJlMduZAtRHm68LU=