private void IbbStreamClosed(object Sender, InBandBytestreams.StreamClosedEventArgs e) { object[] P = (object[])e.State; string Key = (string)P[0]; int Nr = (int)P[1]; byte[] PrevData = (byte[])P[2]; if (HttpxChunks.chunkedStreams.TryGetValue(Key, out ChunkRecord Rec)) { if (e.Reason == InBandBytestreams.CloseReason.Done) { if (PrevData != null) { Rec.ChunkReceived(Nr, true, PrevData); } else { Rec.ChunkReceived(Nr, true, new byte[0]); } P[2] = null; } else { HttpxChunks.chunkedStreams.Remove(Key); } } }
internal static void ChunkReceived(object Sender, MessageEventArgs e) { string StreamId = XML.Attribute(e.Content, "streamId"); string Key = e.From + " " + StreamId; if (!chunkedStreams.TryGetValue(Key, out ChunkRecord Rec)) { return; } int Nr = XML.Attribute(e.Content, "nr", 0); if (Nr < 0) { return; } bool Last = XML.Attribute(e.Content, "last", false); byte[] Data = Convert.FromBase64String(e.Content.InnerText); if (!Rec.ChunkReceived(Nr, Last, Data)) { Rec.Dispose(); chunkedStreams.Remove(Key); } }
private async Task Socks5StreamClosed(object Sender, P2P.SOCKS5.StreamEventArgs e) { #if LOG_SOCKS5_EVENTS this.client.Information("SOCKS5 stream closed."); #endif Socks5Receiver Rx = (Socks5Receiver)e.State; if (HttpxChunks.chunkedStreams.TryGetValue(Rx.Key, out ChunkRecord Rec)) { HttpxChunks.chunkedStreams.Remove(Rx.Key); await Rec.ChunkReceived(Rx.Nr++, true, new byte[0]); } }
private void IbbDataReceived(object Sender, InBandBytestreams.DataReceivedEventArgs e) { object[] P = (object[])e.State; string Key = (string)P[0]; int Nr = (int)P[1]; byte[] PrevData = (byte[])P[2]; if (HttpxChunks.chunkedStreams.TryGetValue(Key, out ChunkRecord Rec)) { if (PrevData != null) { Rec.ChunkReceived(Nr, false, PrevData); } Nr++; P[1] = Nr; P[2] = e.Data; } }
private void Socks5DataReceived(object Sender, P2P.SOCKS5.DataReceivedEventArgs e) { Socks5Receiver Rx = (Socks5Receiver)e.State; if (HttpxChunks.chunkedStreams.TryGetValue(Rx.Key, out ChunkRecord Rec)) { //this.client.Information(e.Data.Length.ToString() + " bytes received over SOCKS5 stream " + Rx.Key + "."); byte[] Data = e.Data; int i = 0; int c = e.Data.Length; int d; while (i < c) { switch (Rx.State) { case 0: Rx.BlockSize = Data[i++]; Rx.State++; break; case 1: Rx.BlockSize <<= 8; Rx.BlockSize |= Data[i++]; if (Rx.BlockSize == 0) { Rec.ChunkReceived(Rx.Nr++, true, new byte[0]); e.Stream.Dispose(); return; } Rx.BlockPos = 0; if (Rx.Block == null || Rx.Block.Length != Rx.BlockSize) { Rx.Block = new byte[Rx.BlockSize]; } Rx.State++; break; case 2: d = c - i; if (d > Rx.BlockSize - Rx.BlockPos) { d = Rx.BlockSize - Rx.BlockPos; } Array.Copy(Data, i, Rx.Block, Rx.BlockPos, d); i += d; Rx.BlockPos += d; if (Rx.BlockPos >= Rx.BlockSize) { if (Rx.E2e) { Rx.Block = this.e2e.Decrypt(Rx.Jid, Rx.Block); if (Rx.Block == null) { e.Stream.Dispose(); return; } } //this.client.Information("Chunk " + Rx.Nr.ToString() + " received and forwarded."); Rec.ChunkReceived(Rx.Nr++, false, Rx.Block); Rx.State = 0; } break; } } } else { //this.client.Warning(e.Data.Length.ToString() + " bytes received over SOCKS5 stream " + Rx.Key + " and discarded."); e.Stream.Dispose(); } }
private async Task Socks5DataReceived(object Sender, P2P.SOCKS5.DataReceivedEventArgs e) { Socks5Receiver Rx = (Socks5Receiver)e.State; if (HttpxChunks.chunkedStreams.TryGetValue(Rx.Key, out ChunkRecord Rec)) { #if LOG_SOCKS5_EVENTS this.client.Information(e.Count.ToString() + " bytes received over SOCKS5 stream " + Rx.Key + "."); #endif byte[] Buffer = e.Buffer; int Offset = e.Offset; int Count = e.Count; int d; while (Count > 0) { switch (Rx.State) { case 0: Rx.BlockSize = Buffer[Offset++]; Count--; Rx.State++; break; case 1: Rx.BlockSize <<= 8; Rx.BlockSize |= Buffer[Offset++]; Count--; if (Rx.BlockSize == 0) { HttpxChunks.chunkedStreams.Remove(Rx.Key); await Rec.ChunkReceived(Rx.Nr++, true, new byte[0]); e.Stream.Dispose(); return; } Rx.BlockPos = 0; if (Rx.Block is null || Rx.Block.Length != Rx.BlockSize) { Rx.Block = new byte[Rx.BlockSize]; } Rx.State++; break; case 2: d = Math.Min(Count, Rx.BlockSize - Rx.BlockPos); Array.Copy(Buffer, Offset, Rx.Block, Rx.BlockPos, d); Offset += d; Rx.BlockPos += d; Count -= d; if (Rx.BlockPos >= Rx.BlockSize) { if (Rx.E2e) { string Id = Rec.NextId().ToString(); Rx.Block = this.e2e.Decrypt(Rx.EndpointReference, Id, Rx.StreamId, Rx.From, Rx.To, Rx.Block, Rx.SymmetricCipher); if (Rx.Block is null) { string Message = "Decryption of chunk " + Rx.Nr.ToString() + " failed."; #if LOG_SOCKS5_EVENTS this.client.Error(Message); #endif await Rec.Fail(Message); e.Stream.Dispose(); return; } } #if LOG_SOCKS5_EVENTS this.client.Information("Chunk " + Rx.Nr.ToString() + " received and forwarded."); #endif await Rec.ChunkReceived(Rx.Nr++, false, Rx.Block); Rx.State = 0; } break; } } } else { #if LOG_SOCKS5_EVENTS this.client.Warning(e.Count.ToString() + " bytes received over SOCKS5 stream " + Rx.Key + " and discarded."); #endif e.Stream.Dispose(); } }