Пример #1
0
        protected void XMPPReader(XmlStream xml, CancellationToken canceltoken)
        {
            try
            {
                while (!canceltoken.IsCancellationRequested)
                {
                    if (xml.ReadStartElement(true))
                    {
                        XElement el = xml.ReadElement(true);

                        if (!canceltoken.IsCancellationRequested)
                        {
                            if (el.Name == (NamespaceClient + "iq"))
                            {
                                EnqueueInfoQueryResponse(el);
                            }
                            else if (el.Name == (NamespaceClient + "message"))
                            {
                                EnqueueMessage(el);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevel.Warning, "XMPP Reader task caught exception {0}\n{1}", ex.Message, ex.ToString());
                ReaderException = ex;
                ReaderFaulted.Set();
            }
        }
Пример #2
0
 protected void StartPlaintextStream(CancellationToken canceltoken)
 {
     using (var stream = Connect(EndpointHost, EndpointPort, ProxyHost, ProxyPort))
     {
         using (var xml = new XmlStream(stream))
         {
             using (var xmppstream = xml.WriteStart(new XElement(NamespaceStream + "stream", new XAttribute("to", Domain), new XAttribute(XNamespace.Xml + "lang", "en"), new XAttribute("version", "1.0"))))
             {
                 xml.ReadStartElement(NamespaceStream + "stream");
                 xml.Expect((rdr) => rdr.GetAttribute("from") == Domain, (rdr) => String.Format("Expected domain {0}; got domain {1}", Domain, rdr.GetAttribute("from")));
                 var features = xml.ReadElement(NamespaceStream + "features");
                 xml.Write(new XElement(NamespaceStartTLS + "starttls"));
                 xml.ReadStartElement(NamespaceStartTLS + "proceed");
                 xml.Stream.Logged = false;
                 using (var ssl = new SslStream(xml.Stream, true, (sender, cert, chain, errs) => true))
                 {
                     ssl.AuthenticateAsClient(EndpointHost);
                     using (var sslxml = new XmlStream(ssl))
                     {
                         StartTLSStream(sslxml, canceltoken);
                     }
                 }
             }
         }
     }
 }
Пример #3
0
        protected void StartXMPPStream(XmlStream xml, CancellationToken canceltoken)
        {
            using (var xmppstream = xml.WriteStart(new XElement(NamespaceStream + "stream", new XAttribute("to", Domain), new XAttribute(XNamespace.Xml + "lang", "en"), new XAttribute("version", "1.0"))))
            {
                xml.ReadStartElement(NamespaceStream + "stream");
                xml.Expect((rdr) => rdr.GetAttribute("from") == Domain, (rdr) => String.Format("Expected domain {0}; got domain {1}", Domain, rdr.GetAttribute("from")));
                var features = xml.ReadElement(NamespaceStream + "features");
                ReaderThread = new Thread(new ThreadStart(() => XMPPReader(xml, canceltoken)));
                ReaderThread.Start();
                BeginBind();

                while (!canceltoken.IsCancellationRequested)
                {
                    switch (WaitHandle.WaitAny(new WaitHandle[] { canceltoken.WaitHandle, ReaderFaulted, QueriesQueued, IsReadyForSubscriptions ? SubscriptionsQueued : QueriesQueued, ResponsesQueued, MessagesQueued }, 15000))
                    {
                    case 0:
                        break;

                    case 1:
                        throw new AggregateException(ReaderException);

                    case 2:
                        ProcessQueuedQueries(xml, canceltoken);
                        break;

                    case 3:
                        goto case 2;

                    case 4:
                        ProcessQueuedResponses(xml, canceltoken);
                        break;

                    case 5:
                        ProcessQueuedMessages(xml, canceltoken);
                        break;

                    case WaitHandle.WaitTimeout:
                        xml.Write(new XText(" "));
                        break;
                    }
                }

                throw new OperationCanceledException();
            }
        }
Пример #4
0
 protected void StartTLSStream(XmlStream xml, CancellationToken canceltoken)
 {
     using (var xmppstream = xml.WriteStart(new XElement(NamespaceStream + "stream", new XAttribute("to", Domain), new XAttribute(XNamespace.Xml + "lang", "en"), new XAttribute("version", "1.0"))))
     {
         xml.ReadStartElement(NamespaceStream + "stream");
         xml.Expect((rdr) => rdr.GetAttribute("from") == Domain, (rdr) => String.Format("Expected domain {0}; got domain {1}", Domain, rdr.GetAttribute("from")));
         var features = xml.ReadElement(NamespaceStream + "features");
         xml.Write(
             new XElement(NamespaceSASL + "auth",
                          new XAttribute("mechanism", AuthMechanism),
                          new XAttribute(NamespaceAuth + "service", "chromiumsync"),
                          new XAttribute(NamespaceAuth + "allow-generated-jid", "true"),
                          new XAttribute(NamespaceAuth + "client-uses-full-bind-result", "true"),
                          new XText(Convert.ToBase64String(Encoding.ASCII.GetBytes("\0" + LoginJID + "\0" + AuthCookie)))
                          )
             );
         xml.ReadStartElement(NamespaceSASL + "success");
         xml.Stream.Logged = false;
         StartXMPPStream(new XmlStream(xml.Stream), canceltoken);
     }
 }
Пример #5
0
        protected void XMPPReader(XmlStream xml, CancellationToken canceltoken)
        {
            try
            {
                while (!canceltoken.IsCancellationRequested)
                {
                    if (xml.ReadStartElement(true))
                    {
                        XElement el = xml.ReadElement(true);

                        if (!canceltoken.IsCancellationRequested)
                        {
                            if (el.Name == (NamespaceClient + "iq"))
                            {
                                EnqueueInfoQueryResponse(el);
                            }
                            else if (el.Name == (NamespaceClient + "message"))
                            {
                                EnqueueMessage(el);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevel.Warning, "XMPP Reader task caught exception {0}\n{1}", ex.Message, ex.ToString());
                ReaderException = ex;
                ReaderFaulted.Set();
            }
        }
Пример #6
0
        protected void StartXMPPStream(XmlStream xml, CancellationToken canceltoken)
        {
            using (var xmppstream = xml.WriteStart(new XElement(NamespaceStream + "stream", new XAttribute("to", Domain), new XAttribute(XNamespace.Xml + "lang", "en"), new XAttribute("version", "1.0"))))
            {
                xml.ReadStartElement(NamespaceStream + "stream");
                xml.Expect((rdr) => rdr.GetAttribute("from") == Domain, (rdr) => String.Format("Expected domain {0}; got domain {1}", Domain, rdr.GetAttribute("from")));
                var features = xml.ReadElement(NamespaceStream + "features");
                ReaderThread = new Thread(new ThreadStart(() => XMPPReader(xml, canceltoken)));
                ReaderThread.Start();
                BeginBind();

                while (!canceltoken.IsCancellationRequested)
                {
                    switch (WaitHandle.WaitAny(new WaitHandle[] { canceltoken.WaitHandle, ReaderFaulted, QueriesQueued, IsReadyForSubscriptions ? SubscriptionsQueued : QueriesQueued, ResponsesQueued, MessagesQueued }, 15000))
                    {
                        case 0:
                            break;
                        case 1:
                            throw new AggregateException(ReaderException);
                        case 2:
                            ProcessQueuedQueries(xml, canceltoken);
                            break;
                        case 3:
                            goto case 2;
                        case 4:
                            ProcessQueuedResponses(xml, canceltoken);
                            break;
                        case 5:
                            ProcessQueuedMessages(xml, canceltoken);
                            break;
                        case WaitHandle.WaitTimeout:
                            xml.Write(new XText(" "));
                            break;
                    }
                }

                throw new OperationCanceledException();
            }
        }
Пример #7
0
 protected void StartTLSStream(XmlStream xml, CancellationToken canceltoken)
 {
     using (var xmppstream = xml.WriteStart(new XElement(NamespaceStream + "stream", new XAttribute("to", Domain), new XAttribute(XNamespace.Xml + "lang", "en"), new XAttribute("version", "1.0"))))
     {
         xml.ReadStartElement(NamespaceStream + "stream");
         xml.Expect((rdr) => rdr.GetAttribute("from") == Domain, (rdr) => String.Format("Expected domain {0}; got domain {1}", Domain, rdr.GetAttribute("from")));
         var features = xml.ReadElement(NamespaceStream + "features");
         xml.Write(
             new XElement(NamespaceSASL + "auth",
                 new XAttribute("mechanism", AuthMechanism),
                 new XAttribute(NamespaceAuth + "service", "chromiumsync"),
                 new XAttribute(NamespaceAuth + "allow-generated-jid", "true"),
                 new XAttribute(NamespaceAuth + "client-uses-full-bind-result", "true"),
                 new XText(Convert.ToBase64String(Encoding.ASCII.GetBytes("\0" + LoginJID + "\0" + AuthCookie)))
             )
         );
         xml.ReadStartElement(NamespaceSASL + "success");
         xml.Stream.Logged = false;
         StartXMPPStream(new XmlStream(xml.Stream), canceltoken);
     }
 }
Пример #8
0
 protected void StartPlaintextStream(CancellationToken canceltoken)
 {
     using (var stream = Connect(EndpointHost, EndpointPort, ProxyHost, ProxyPort))
     {
         using (var xml = new XmlStream(stream))
         {
             using (var xmppstream = xml.WriteStart(new XElement(NamespaceStream + "stream", new XAttribute("to", Domain), new XAttribute(XNamespace.Xml + "lang", "en"), new XAttribute("version", "1.0"))))
             {
                 xml.ReadStartElement(NamespaceStream + "stream");
                 xml.Expect((rdr) => rdr.GetAttribute("from") == Domain, (rdr) => String.Format("Expected domain {0}; got domain {1}", Domain, rdr.GetAttribute("from")));
                 var features = xml.ReadElement(NamespaceStream + "features");
                 xml.Write(new XElement(NamespaceStartTLS + "starttls"));
                 xml.ReadStartElement(NamespaceStartTLS + "proceed");
                 xml.Stream.Logged = false;
                 using (var ssl = new SslStream(xml.Stream, true, (sender, cert, chain, errs) => true))
                 {
                     ssl.AuthenticateAsClient(EndpointHost);
                     using (var sslxml = new XmlStream(ssl))
                     {
                         StartTLSStream(sslxml, canceltoken);
                     }
                 }
             }
         }
     }
 }