private ValueDictionary parseBEncodeDict(MemoryStream responseStream)
        {
            ValueDictionary dictionary = null;

            if ((this._contentEncoding == "gzip") || (this._contentEncoding == "x-gzip"))
            {
                GZipStream d = new GZipStream(responseStream, CompressionMode.Decompress);
                try
                {
                    dictionary = (ValueDictionary)BEncode.Parse(d);
                }
                catch (Exception)
                {
                }
                return(dictionary);
            }
            try
            {
                dictionary = (ValueDictionary)BEncode.Parse(responseStream);
            }
            catch (Exception exception)
            {
                Console.Write(exception.StackTrace);
            }
            return(dictionary);
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        private byte[] BuildAckMessage(byte[] key, byte[] iv)
        {
            DictNode node = new DictNode();

            node.Add("key", key);
            node.Add("iv", iv);
            return(BEncode.ByteArrayEncode(node));
        }
Example #3
0
        /// <summary>
        /// 建立连接包
        /// </summary>
        /// <returns></returns>
        private byte[] BuildHandshakeMessage()
        {
            DictNode node = new DictNode();

            node.Add("port", clientPort);
            node.Add("key", key);
            node.Add("iv", iv);
            return(BEncode.ByteArrayEncode(node));
        }
Example #4
0
        /// <summary>
        /// 交互函数
        /// </summary>
        /// <param name="stateInfo"></param>
        internal void Exchange(object stateInfo)
        {
            try
            {
                isClose = false;
                do
                {
                    byte[]   rcvMsg       = handler.ReceiveSealedPacket();
                    DictNode responseNode = HandlePacket(rcvMsg);
                    byte[]   sndMsg       = BEncode.ByteArrayEncode(responseNode);
                    handler.SendSealedPacket(sndMsg);
                    if (isClose)
                    {
                        return;
                    }
                } while (true);
            }

            catch (TdsException te)
            {
                Console.WriteLine("Tds Exception:{0}", te.Message);
            }

            catch (PacketException pe)
            {
                Console.WriteLine("Packet Exception:{0}", pe.Message);
            }

            catch (IOException ioe)
            {
                Console.WriteLine("IO Exception:{0}", ioe.Message);
            }

            catch (SocketException se)
            {
                Console.WriteLine("Socket Exception:{0}", se.Message);
            }

            catch (BEncodingException bee)
            {
                Console.WriteLine("BEncoding Exception:{0}", bee.Message);
            }

            finally
            {
                monitor.Unregister(handler);
                handler.Close();
            }
        }
Example #5
0
 /// <summary>
 /// 检验打开数据包
 /// </summary>
 /// <param name="packet">打开数据包</param>
 /// <param name="key">密钥</param>
 /// <param name="iv">初始化向量</param>
 /// <param name="error">错误信息</param>
 /// <returns>如果打开成功,返回ture,否则返回false</returns>
 private bool CheckOpenPacket(byte[] packet, out byte[] key, out byte[] iv, out string error)
 {
     key   = null;
     iv    = null;
     port  = -1;
     error = string.Empty;
     try
     {
         DictNode node = (DictNode)BEncode.Decode(packet);
         if (node.ContainsKey("key") && node.ContainsKey("iv"))
         {
             key = ((BytesNode)node["key"]).ByteArray;
             iv  = ((BytesNode)node["iv"]).ByteArray;
             return(true);
         }
         error = "数据包不包含密钥或者初始化向量或者端口信息!";
     }
     catch (BEncodingException bee)
     {
         error = bee.Message;
     }
     return(false);
 }