Fix UDP route

This commit is contained in:
世界 2023-08-13 16:49:55 +08:00
parent d14170348d
commit 1dd2c26f31
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -740,13 +740,11 @@ func (r *Router) RoutePacketConnection(ctx context.Context, conn N.PacketConn, m
} }
metadata.Network = N.NetworkUDP metadata.Network = N.NetworkUDP
var originAddress M.Socksaddr
if r.fakeIPStore != nil && r.fakeIPStore.Contains(metadata.Destination.Addr) { if r.fakeIPStore != nil && r.fakeIPStore.Contains(metadata.Destination.Addr) {
domain, loaded := r.fakeIPStore.Lookup(metadata.Destination.Addr) domain, loaded := r.fakeIPStore.Lookup(metadata.Destination.Addr)
if !loaded { if !loaded {
return E.New("missing fakeip context") return E.New("missing fakeip context")
} }
originAddress = metadata.Destination
metadata.OriginDestination = metadata.Destination metadata.OriginDestination = metadata.Destination
metadata.Destination = M.Socksaddr{ metadata.Destination = M.Socksaddr{
Fqdn: domain, Fqdn: domain,
@ -761,7 +759,7 @@ func (r *Router) RoutePacketConnection(ctx context.Context, conn N.PacketConn, m
conn = deadline.NewPacketConn(bufio.NewNetPacketConn(conn)) conn = deadline.NewPacketConn(bufio.NewNetPacketConn(conn))
}*/ }*/
if metadata.InboundOptions.SniffEnabled { if metadata.InboundOptions.SniffEnabled || metadata.Destination.Addr.IsUnspecified() {
buffer := buf.NewPacket() buffer := buf.NewPacket()
buffer.FullReset() buffer.FullReset()
destination, err := conn.ReadPacket(buffer) destination, err := conn.ReadPacket(buffer)
@ -769,6 +767,9 @@ func (r *Router) RoutePacketConnection(ctx context.Context, conn N.PacketConn, m
buffer.Release() buffer.Release()
return err return err
} }
if metadata.Destination.Addr.IsUnspecified() {
metadata.Destination = destination
}
sniffMetadata, _ := sniff.PeekPacket(ctx, buffer.Bytes(), sniff.DomainNameQuery, sniff.QUICClientHello, sniff.STUNMessage) sniffMetadata, _ := sniff.PeekPacket(ctx, buffer.Bytes(), sniff.DomainNameQuery, sniff.QUICClientHello, sniff.STUNMessage)
if sniffMetadata != nil { if sniffMetadata != nil {
metadata.Protocol = sniffMetadata.Protocol 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) conn = statsService.RoutedPacketConnection(metadata.Inbound, detour.Tag(), metadata.User, conn)
} }
} }
if originAddress.IsValid() { if metadata.FakeIP {
conn = fakeip.NewNATPacketConn(conn, originAddress, metadata.Destination) conn = fakeip.NewNATPacketConn(conn, metadata.OriginDestination, metadata.Destination)
} }
return detour.NewPacketConnection(ctx, conn, metadata) return detour.NewPacketConnection(ctx, conn, metadata)
} }