mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-13 20:33:16 +00:00
Fix direct UDP override
This commit is contained in:
parent
2f31202c6b
commit
d0aaf71770
|
@ -12,7 +12,6 @@ import (
|
||||||
"github.com/sagernet/sing-box/log"
|
"github.com/sagernet/sing-box/log"
|
||||||
"github.com/sagernet/sing-box/option"
|
"github.com/sagernet/sing-box/option"
|
||||||
"github.com/sagernet/sing-dns"
|
"github.com/sagernet/sing-dns"
|
||||||
"github.com/sagernet/sing/common/buf"
|
|
||||||
"github.com/sagernet/sing/common/bufio"
|
"github.com/sagernet/sing/common/bufio"
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
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)
|
ctx, metadata := adapter.ExtendContext(ctx)
|
||||||
metadata.Outbound = h.tag
|
metadata.Outbound = h.tag
|
||||||
metadata.Destination = destination
|
metadata.Destination = destination
|
||||||
|
originDestination := destination
|
||||||
switch h.overrideOption {
|
switch h.overrideOption {
|
||||||
case 1:
|
case 1:
|
||||||
destination = h.overrideDestination
|
destination = h.overrideDestination
|
||||||
|
@ -142,11 +142,10 @@ func (h *Direct) ListenPacket(ctx context.Context, destination M.Socksaddr) (net
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if h.overrideOption == 0 {
|
if originDestination != destination {
|
||||||
return conn, nil
|
conn = bufio.NewNATPacketConn(bufio.NewPacketConn(conn), destination, originDestination)
|
||||||
} else {
|
|
||||||
return &overridePacketConn{bufio.NewPacketConn(conn), destination}, nil
|
|
||||||
}
|
}
|
||||||
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Direct) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
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 {
|
func (h *Direct) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
|
||||||
return NewPacketConnection(ctx, h, conn, metadata)
|
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
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue