Fix direct UDP override

This commit is contained in:
世界 2023-12-18 21:32:42 +08:00
parent 2f31202c6b
commit d0aaf71770
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -12,7 +12,6 @@ import (
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-dns"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
@ -123,6 +122,7 @@ func (h *Direct) ListenPacket(ctx context.Context, destination M.Socksaddr) (net
ctx, metadata := adapter.ExtendContext(ctx)
metadata.Outbound = h.tag
metadata.Destination = destination
originDestination := destination
switch h.overrideOption {
case 1:
destination = h.overrideDestination
@ -142,11 +142,10 @@ func (h *Direct) ListenPacket(ctx context.Context, destination M.Socksaddr) (net
if err != nil {
return nil, err
}
if h.overrideOption == 0 {
return conn, nil
} else {
return &overridePacketConn{bufio.NewPacketConn(conn), destination}, nil
if originDestination != destination {
conn = bufio.NewNATPacketConn(bufio.NewPacketConn(conn), destination, originDestination)
}
return conn, nil
}
func (h *Direct) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
@ -156,20 +155,3 @@ func (h *Direct) NewConnection(ctx context.Context, conn net.Conn, metadata adap
func (h *Direct) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
return NewPacketConnection(ctx, h, conn, metadata)
}
type overridePacketConn struct {
N.NetPacketConn
overrideDestination M.Socksaddr
}
func (c *overridePacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
return c.NetPacketConn.WritePacket(buffer, c.overrideDestination)
}
func (c *overridePacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
return c.NetPacketConn.WriteTo(p, c.overrideDestination.UDPAddr())
}
func (c *overridePacketConn) Upstream() any {
return c.NetPacketConn
}