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) }