diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index 215683ad6..47fc32b50 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -76,6 +76,7 @@ func startCommunicate(request *protocol.VMessRequest, dest v2net.Destination, ra input := ray.OutboundInput() output := ray.OutboundOutput() + var requestFinish, responseFinish sync.Mutex requestFinish.Lock() responseFinish.Lock() @@ -110,20 +111,23 @@ func handleRequest(conn net.Conn, request *protocol.VMessRequest, firstPacket v2 firstChunk := firstPacket.Chunk() moreChunks := firstPacket.MoreChunks() - if firstChunk == nil && moreChunks { + for firstChunk == nil && moreChunks { firstChunk, moreChunks = <-input } - if firstChunk != nil { - aesStream.XORKeyStream(firstChunk.Value, firstChunk.Value) - buffer.Append(firstChunk.Value) - firstChunk.Release() + if firstChunk == nil && !moreChunks { + log.Warning("VMessOut: Nothing to send. Existing...") + return + } - _, err = conn.Write(buffer.Value) - if err != nil { - log.Error("VMessOut: Failed to write VMess request: %v", err) - return - } + aesStream.XORKeyStream(firstChunk.Value, firstChunk.Value) + buffer.Append(firstChunk.Value) + firstChunk.Release() + + _, err = conn.Write(buffer.Value) + if err != nil { + log.Error("VMessOut: Failed to write VMess request: %v", err) + return } if moreChunks { diff --git a/shell/point/point.go b/shell/point/point.go index f42134089..dbc3dae37 100644 --- a/shell/point/point.go +++ b/shell/point/point.go @@ -171,3 +171,24 @@ func (this *Point) DispatchToOutbound(context app.Context, packet v2net.Packet) go this.och.Dispatch(packet, direct) return direct } + +func (this *Point) FilterPacketAndDispatch(packet v2net.Packet, link ray.OutboundRay, dispatcher proxy.OutboundConnectionHandler) { + // Filter empty packets + chunk := packet.Chunk() + moreChunks := packet.MoreChunks() + changed := false + for chunk == nil && moreChunks { + changed = true + chunk, moreChunks = <-link.OutboundInput() + } + if chunk == nil && !moreChunks { + close(link.OutboundOutput()) + return + } + + if changed { + packet = v2net.NewPacket(packet.Destination(), chunk, moreChunks) + } + + dispatcher.Dispatch(packet, link) +}