private void OpenHandler(object Sender, IqEventArgs e) { int BlockSize = XML.Attribute(e.Query, "block-size", 0); string StreamId = XML.Attribute(e.Query, "sid"); string Stanza = XML.Attribute(e.Query, "stanza", "iq"); if (Stanza != "message" && Stanza != "iq") { throw new BadRequestException("Invalid stanza type.", e.IQ); } if (BlockSize <= 0) { throw new BadRequestException("Invalid block size.", e.IQ); } if (BlockSize > this.maxBlockSize) { throw new ResourceConstraintException("Requested block size too large. Maximum acceptable is " + this.maxBlockSize.ToString(), e.IQ); } ValidateStreamEventHandler h = this.OnOpen; ValidateStreamEventArgs e2 = new ValidateStreamEventArgs(this.client, e, StreamId, BlockSize); if (h != null) { try { h(this, e2); } catch (Exception ex) { Log.Critical(ex); } } if (e2.DataCallback is null || e2.CloseCallback is null) { throw new NotAcceptableException("Stream not expected.", e.IQ); } this.AssertCacheCreated(); string Key = e.From + " " + StreamId; if (this.cache.ContainsKey(Key)) { throw new NotAcceptableException("Stream already open.", e.IQ); } IncomingStream Input = new IncomingStream(e2.DataCallback, e2.CloseCallback, e2.State, BlockSize); this.cache[Key] = Input; e.IqResult(string.Empty); }
private async Task QueryHandler(object Sender, IqEventArgs e) { string StreamId = XML.Attribute(e.Query, "sid"); XmlElement E; if (string.IsNullOrEmpty(StreamId) || StreamId != Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(StreamId))) { throw new NotAcceptableException("Invalid Stream ID.", e.IQ); } string Host = null; string JID = null; int Port = 0; foreach (XmlNode N in e.Query.ChildNodes) { E = N as XmlElement; if (E is null) { continue; } if (E.LocalName == "streamhost" && E.NamespaceURI == Namespace) { Host = XML.Attribute(E, "host"); JID = XML.Attribute(E, "jid"); Port = XML.Attribute(E, "port", 0); break; } } if (string.IsNullOrEmpty(JID) || string.IsNullOrEmpty(Host) || Port <= 0 || Port >= 0x10000) { throw new BadRequestException("Invalid parameters.", e.IQ); } ValidateStreamEventHandler h = this.OnOpen; ValidateStreamEventArgs e2 = new ValidateStreamEventArgs(this.client, e, StreamId); if (h != null) { try { await h(this, e2); } catch (Exception ex) { Log.Critical(ex); } } if (e2.DataCallback is null || e2.CloseCallback is null) { throw new NotAcceptableException("Stream not expected.", e.IQ); } Socks5Client Client; lock (this.streams) { if (this.streams.ContainsKey(StreamId)) { throw new ConflictException("Stream already exists.", e.IQ); } Client = new Socks5Client(Host, Port, JID) { CallbackState = e2.State }; this.streams[StreamId] = Client; } Client.Tag = new Socks5QueryState() { streamId = StreamId, eventargs = e, eventargs2 = e2 }; Client.OnDataReceived += e2.DataCallback; Client.OnStateChange += this.ClientStateChanged; }