Fix buffer usage

This commit is contained in:
世界 2022-08-02 13:43:25 +08:00
parent 37b11e614c
commit b1c2440371
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
8 changed files with 46 additions and 61 deletions

View file

@ -492,10 +492,8 @@ func (r *Router) RouteConnection(ctx context.Context, conn net.Conn, metadata ad
return mux.NewConnection(ctx, r, r, r.logger, conn, metadata)
}
if metadata.SniffEnabled {
_buffer := buf.StackNew()
defer common.KeepAlive(_buffer)
buffer := common.Dup(_buffer)
defer buffer.Release()
buffer := buf.NewPacket()
buffer.FullReset()
sniffMetadata, err := sniff.PeekStream(ctx, conn, buffer, sniff.StreamDomainNameQuery, sniff.TLSClientHello, sniff.HTTPHost)
if err == nil {
metadata.Protocol = sniffMetadata.Protocol
@ -511,6 +509,8 @@ func (r *Router) RouteConnection(ctx context.Context, conn net.Conn, metadata ad
}
if !buffer.IsEmpty() {
conn = bufio.NewCachedConn(conn, buffer)
} else {
buffer.Release()
}
}
if metadata.Destination.IsFqdn() && metadata.DomainStrategy != dns.DomainStrategyAsIS {
@ -536,12 +536,11 @@ func (r *Router) RouteConnection(ctx context.Context, conn net.Conn, metadata ad
func (r *Router) RoutePacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
if metadata.SniffEnabled {
_buffer := buf.StackNewPacket()
defer common.KeepAlive(_buffer)
buffer := common.Dup(_buffer)
defer buffer.Release()
buffer := buf.NewPacket()
buffer.FullReset()
_, err := conn.ReadPacket(buffer)
if err != nil {
buffer.Release()
return err
}
sniffMetadata, err := sniff.PeekPacket(ctx, buffer.Bytes(), sniff.DomainNameQuery, sniff.QUICClientHello, sniff.STUNMessage)