使用NCindy连接需要TLS的服务器

本文演示了如何使用 NCindy 的 SSLFilter 实现 GTalk XMPP 协议的 TLS 加密传输。通过创建 Session 并配置 SSLFilter 来完成加密过程,并展示了完整的 C# 代码实现。
/**
Gtalk的XMPP协议需要使用TLS来传输,下面的代码演示了如何使用NCindy的SSLFilter来给连接加上SSL处理
**/

  1
None.gifusing System;
  2None.gifusing System.Text;
  3None.gifusing NCindy.Session.AIO;
  4None.gifusing System.Net;
  5None.gifusing NCindy.Packet;
  6None.gifusing NCindy.Filter;
  7None.gifusing System.Threading;
  8None.gif
  9None.gifnamespace NCindy.Test.SSL
 10ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 11InBlock.gif    class GtalkTest
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 13InBlock.gif        static ManualResetEvent startSSLEvent = new ManualResetEvent(false);
 14InBlock.gif
 15InBlock.gif        static int recvPacketCount = 0;
 16InBlock.gif        static void Main(string[] args)
 17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 18InBlock.gif            try
 19ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 20InBlock.gif                //获取gtalk服务器的IP地址
 21InBlock.gif                IPAddress addr = Dns.GetHostAddresses("talk.google.com")[0];
 22InBlock.gif                
 23InBlock.gif                IPEndPoint gtalkEndpoint = new IPEndPoint(addr, 5222);
 24InBlock.gif                //创建一个Session
 25InBlock.gif                ISession session = new AsyncTcpSession();
 26InBlock.gif
 27InBlock.gif                //设置远端地址
 28InBlock.gif                session.RemoteEndPoint = gtalkEndpoint;
 29InBlock.gif
 30InBlock.gif                //设置数据包的编码器和解码器
 31InBlock.gif                session.PacketEncoder = new StringEncoder();
 32InBlock.gif                session.PacketDecoder = new StringDecoder();
 33InBlock.gif
 34InBlock.gif                
 35InBlock.gif                //设置事件处理函数
 36InBlock.gif                session.Started += new EventHandler<NCindy.SessionEventArgs>(Session_Started);
 37InBlock.gif                session.ObjectReceived += new EventHandler<NCindy.SessionEventArgs>(Session_ObjectReceived);
 38InBlock.gif                session.ObjectSent += new EventHandler<NCindy.SessionEventArgs>(Session_ObjectSent);
 39InBlock.gif                session.ExceptionCaught += new EventHandler<NCindy.ExceptionEventArgs>(Session_ExceptionCaught);
 40InBlock.gif
 41InBlock.gif
 42InBlock.gif                //创建并配置SSLFilter
 43InBlock.gif                SSLFilter sslFilter = CreateGtalkSSLFilter(session);
 44InBlock.gif
 45InBlock.gif                //为Session增加SSLFilter
 46InBlock.gif                session.AddSessionFilter(sslFilter);
 47InBlock.gif
 48InBlock.gif                session.Start();
 49InBlock.gif
 50InBlock.gif                //等待接收到3个数据包之后,启动SSLFilter
 51InBlock.gif                startSSLEvent.WaitOne();
 52InBlock.gif
 53InBlock.gif                Console.WriteLine("Start SSLFilter.");
 54InBlock.gif
 55InBlock.gif                sslFilter.Start();
 56InBlock.gif
 57InBlock.gif                //SSLFilter认证通过,再次发送数据。
 58InBlock.gif                DoSendData(session);
 59InBlock.gif
 60InBlock.gif
 61InBlock.gif                Console.ReadLine();
 62InBlock.gif
 63ExpandedSubBlockEnd.gif            }

 64InBlock.gif            catch (Exception e)
 65ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 66InBlock.gif                Console.WriteLine(e);
 67ExpandedSubBlockEnd.gif            }

 68ExpandedSubBlockEnd.gif        }

 69InBlock.gif
 70InBlock.gif        private static SSLFilter CreateGtalkSSLFilter(ISession session)
 71ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 72InBlock.gif            //设置Filter所属Session和Filter的加密方式:TLS
 73InBlock.gif            SSLFilter sslFilter = new SSLFilter(session, System.Security.Authentication.SslProtocols.Tls);
 74InBlock.gif
 75InBlock.gif            sslFilter.TargetHost = "gmail.com";
 76InBlock.gif
 77InBlock.gif            //作为客户端进行SSL认证
 78InBlock.gif            sslFilter.IsClientMode = true;
 79InBlock.gif            sslFilter.IsNeedClientAuth = true;
 80InBlock.gif            return sslFilter;
 81ExpandedSubBlockEnd.gif        }

 82InBlock.gif
 83InBlock.gif        static void Session_ExceptionCaught(object sender, NCindy.ExceptionEventArgs e)
 84ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 85InBlock.gif            Console.WriteLine(e.Cause);
 86ExpandedSubBlockEnd.gif        }

 87InBlock.gif
 88InBlock.gif        static void Session_ObjectSent(object sender, NCindy.SessionEventArgs e)
 89ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 90InBlock.gif            Console.WriteLine("Object sent: " + e.Obj);
 91ExpandedSubBlockEnd.gif        }

 92InBlock.gif
 93InBlock.gif        static void Session_ObjectReceived(object sender, NCindy.SessionEventArgs e)
 94ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 95InBlock.gif            if (Interlocked.Increment(ref recvPacketCount) > 2 && startSSLEvent != null)
 96ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 97InBlock.gif                startSSLEvent.Set();
 98InBlock.gif                startSSLEvent = null;
 99ExpandedSubBlockEnd.gif            }

100InBlock.gif
101InBlock.gif
102InBlock.gif            Console.WriteLine("Object recv: " + e.Obj);
103ExpandedSubBlockEnd.gif        }

104InBlock.gif
105InBlock.gif        static void Session_Started(object sender, NCindy.SessionEventArgs e)
106ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
107InBlock.gif            Console.WriteLine("Session started.");
108InBlock.gif            //当Session启动,开始发送
109InBlock.gif            DoSendData(e.Session);
110ExpandedSubBlockEnd.gif        }

111InBlock.gif
112InBlock.gif        private static void DoSendData(NCindy.ISession session)
113ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
114InBlock.gif            Console.WriteLine("Send some data.");
115InBlock.gif            string s = "<stream:stream to=\"gmail.com\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\">";
116InBlock.gif            string s1 = "<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>";
117InBlock.gif
118InBlock.gif            session.Send(s);
119InBlock.gif            session.Send(s1);
120ExpandedSubBlockEnd.gif        }

121ExpandedSubBlockEnd.gif    }

122InBlock.gif
123InBlock.gif
124InBlock.gif    public class StringEncoder : NCindy.IPacketEncoder
125ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
126InBlock.gif
127ContractedSubBlock.gifExpandedSubBlockStart.gif        IPacketEncoder Members#region IPacketEncoder Members
128InBlock.gif
129InBlock.gif        public NCindy.IPacket Encode(NCindy.ISession session, object obj)
130ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
131InBlock.gif            DefaultPacket dp = new DefaultPacket(System.Text.Encoding.Default.GetBytes(obj as string));
132InBlock.gif            dp.EndPoint = session.RemoteEndPoint;
133InBlock.gif
134InBlock.gif            return dp;
135ExpandedSubBlockEnd.gif        }

136InBlock.gif
137ExpandedSubBlockEnd.gif        #endregion

138ExpandedSubBlockEnd.gif    }

139InBlock.gif
140InBlock.gif    public class StringDecoder : NCindy.IPacketDecoder
141ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
142ContractedSubBlock.gifExpandedSubBlockStart.gif        IPacketDecoder Members#region IPacketDecoder Members
143InBlock.gif
144InBlock.gif        public object Decode(NCindy.ISession session, NCindy.IPacket packet)
145ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
146InBlock.gif            object obj = Encoding.Default.GetString(packet.Content.AsByteArray());
147InBlock.gif
148InBlock.gif            packet.Content.Position += packet.Content.Remaining;
149InBlock.gif
150InBlock.gif            return obj;
151ExpandedSubBlockEnd.gif        }

152InBlock.gif
153ExpandedSubBlockEnd.gif        #endregion

154ExpandedSubBlockEnd.gif    }

155ExpandedBlockEnd.gif}

156None.gif

转载于:https://www.cnblogs.com/ncindy/archive/2006/10/27/541658.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值