From 1dd2c26f3100a819dc4b9d2effbf5ccec2f8c9de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 13 Aug 2023 16:49:55 +0800 Subject: [PATCH] Fix UDP route --- route/router.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/route/router.go b/route/router.go index e2626c2b..4fa8e8e2 100644 --- a/route/router.go +++ b/route/router.go @@ -740,13 +740,11 @@ func (r *Router) RoutePacketConnection(ctx context.Context, conn N.PacketConn, m } metadata.Network = N.NetworkUDP - var originAddress M.Socksaddr if r.fakeIPStore != nil && r.fakeIPStore.Contains(metadata.Destination.Addr) { domain, loaded := r.fakeIPStore.Lookup(metadata.Destination.Addr) if !loaded { return E.New("missing fakeip context") } - originAddress = metadata.Destination metadata.OriginDestination = metadata.Destination metadata.Destination = M.Socksaddr{ Fqdn: domain, @@ -761,7 +759,7 @@ func (r *Router) RoutePacketConnection(ctx context.Context, conn N.PacketConn, m conn = deadline.NewPacketConn(bufio.NewNetPacketConn(conn)) }*/ - if metadata.InboundOptions.SniffEnabled { + if metadata.InboundOptions.SniffEnabled || metadata.Destination.Addr.IsUnspecified() { buffer := buf.NewPacket() buffer.FullReset() destination, err := conn.ReadPacket(buffer) @@ -769,6 +767,9 @@ func (r *Router) RoutePacketConnection(ctx context.Context, conn N.PacketConn, m buffer.Release() return err } + if metadata.Destination.Addr.IsUnspecified() { + metadata.Destination = destination + } sniffMetadata, _ := sniff.PeekPacket(ctx, buffer.Bytes(), sniff.DomainNameQuery, sniff.QUICClientHello, sniff.STUNMessage) if sniffMetadata != nil { metadata.Protocol = sniffMetadata.Protocol @@ -819,8 +820,8 @@ func (r *Router) RoutePacketConnection(ctx context.Context, conn N.PacketConn, m conn = statsService.RoutedPacketConnection(metadata.Inbound, detour.Tag(), metadata.User, conn) } } - if originAddress.IsValid() { - conn = fakeip.NewNATPacketConn(conn, originAddress, metadata.Destination) + if metadata.FakeIP { + conn = fakeip.NewNATPacketConn(conn, metadata.OriginDestination, metadata.Destination) } return detour.NewPacketConnection(ctx, conn, metadata) }