private async Task InitiationResponse(object Sender, IqResultEventArgs e) { InitiationRec Rec = (InitiationRec)e.State; if (e.Ok) { XmlElement E = e.FirstElement; if (E != null && E.LocalName == "query" && E.NamespaceURI == Namespace && XML.Attribute(E, "sid") == Rec.streamId) { XmlElement E2; string StreamHostUsed = null; foreach (XmlNode N in E.ChildNodes) { E2 = N as XmlElement; if (E2.LocalName == "streamhost-used" && E2.NamespaceURI == Namespace) { StreamHostUsed = XML.Attribute(E2, "jid"); break; } } if (!string.IsNullOrEmpty(StreamHostUsed) && StreamHostUsed == this.host) { Rec.stream = new Socks5Client(this.host, this.port, this.jid); Rec.stream.OnStateChange += Rec.StateChanged; lock (this.streams) { this.streams[Rec.streamId] = Rec.stream; } } else { await this.Callback(Rec.callback, Rec.state, false, null, Rec.streamId); } } else { await this.Callback(Rec.callback, Rec.state, false, null, Rec.streamId); } } else { await this.Callback(Rec.callback, Rec.state, false, null, Rec.streamId); } }
private async Task ActivationResponse(object Sender, IqResultEventArgs e) { InitiationRec Rec = (InitiationRec)e.State; if (e.Ok) { await this.Callback(Rec.callback, Rec.state, true, Rec.stream, Rec.streamId); } else { Rec.stream.Dispose(); await this.Callback(Rec.callback, Rec.state, false, null, Rec.streamId); } Rec.callback = null; }
/// <summary> /// Initiates a mediated SOCKS5 session with another. /// </summary> /// <param name="DestinationJid">JID of destination.</param> /// <param name="StreamId">Stream ID to use.</param> /// <param name="Callback">Method to call when initiation attempt completes.</param> /// <param name="State">State object to pass on to callback method.</param> public async Task InitiateSession(string DestinationJid, string StreamId, StreamEventHandler Callback, object State) { if (!this.hasProxy) { await this.Callback(Callback, State, false, null, null); return; } lock (this.streams) { if (string.IsNullOrEmpty(StreamId)) { do { StreamId = Guid.NewGuid().ToString().Replace("-", string.Empty); }while (this.streams.ContainsKey(StreamId)); } else if (this.streams.ContainsKey(StreamId)) { StreamId = null; } if (StreamId != null) { this.streams[StreamId] = null; } } if (StreamId is null) { await this.Callback(Callback, State, false, null, null); return; } StringBuilder Xml = new StringBuilder(); Xml.Append("<query xmlns=\""); Xml.Append(Namespace); Xml.Append("\" sid=\""); Xml.Append(StreamId); Xml.Append("\"><streamhost host=\""); Xml.Append(this.host); Xml.Append("\" jid=\""); Xml.Append(this.jid); Xml.Append("\" port=\""); Xml.Append(this.port.ToString()); Xml.Append("\"/></query>"); InitiationRec Rec = new InitiationRec() { destinationJid = DestinationJid, streamId = StreamId, callback = Callback, state = State, proxy = this }; if (this.e2e != null) { this.e2e.SendIqSet(this.client, E2ETransmission.NormalIfNotE2E, DestinationJid, Xml.ToString(), this.InitiationResponse, Rec); } else { this.client.SendIqSet(DestinationJid, Xml.ToString(), this.InitiationResponse, Rec); } }