예제 #1
0
        public byte[] GenerateC2SRequest()
        {
            string lHost = mUri.DnsSafeHost;
            //string lPath = mUri.PathAndQuery;
            string lPath = mUri.AbsolutePath + "?" + mUri.Query;

            mOrigin = "http://" + lHost;

            if ("".Equals(lPath))
            {
                lPath = "/";
            }

            string lHandshake =
                "GET " + lPath + " HTTP/1.1 \r\n"
                + "Host: " + lHost + "\r\n"
                + "Upgrade: WebSocket\r\n"
                + "Connection: Upgrade\r\n"
                + "Sec-WebSocket-Key: " + mHybiKey + "\r\n"
                + "Origin: " + mOrigin + "\r\n"
                + "Sec-WebSocket-Protocol: " + mProtocol + "\r\n"
                + "Sec-WebSocket-Version: " + mVersion + "\r\n";

            return(WebSocketConvert.StringToBytes(lHandshake, WebSocketTypeEncoding.UTF8));
        }
예제 #2
0
        private static string CalcHybiSecKeyAccept(string aKey)
        {
            aKey = aKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
            string lAccept = null;
            //SHA1 lSHA1 = new SHA1CryptoServiceProvider();
            var sha1 = new SHA1Managed();

            byte[] lBufSource = WebSocketConvert.StringToBytes(aKey, WebSocketTypeEncoding.UTF8);
            try
            {
                //byte[] lBufTarget = lSHA1.ComputeHash(lBufSource);
                byte[] lBufTarget = sha1.ComputeHash(lBufSource);
                lAccept = Convert.ToBase64String(lBufTarget);
            }
            catch (Exception lEx)
            {
                throw new WebSocketException("Error converting to Base64" + lEx.Message);
            }
            return(lAccept);
        }
예제 #3
0
        public void ReadResponseFromBuffer(byte[] aBuff)
        {
            int lHeaderComplete = 0;
            int lLines          = 0;
            int lSizeBuff       = aBuff.Length;

            byte lRT = 0;
            byte lNL = 0;

            byte[] lBuff = new byte[0];

            while (lHeaderComplete < lSizeBuff)
            {
                lRT = lNL;
                lNL = aBuff[lHeaderComplete];
                if (lRT.Equals(0x0D) && lNL.Equals(0x0A))
                {
                    string lLine = WebSocketConvert.BytesToString(lBuff, WebSocketTypeEncoding.UTF8);
                    if (lLines > 0)
                    {
                        char[]   c       = { ':' };
                        string[] lKeyVal = lLine.Split(c);
                        if (lKeyVal.Count() == 2)
                        {
                            mResponseFields.Add(lKeyVal[0].Trim(), lKeyVal[1].Trim());
                        }
                    }
                    else
                    {
                        mFirstLineResponse = lLine;
                    }
                    lBuff = new byte[0];
                    lLines++;
                }
                else if (!lNL.Equals(0x0A) && !lNL.Equals(0x0D))
                {
                    Write(ref lBuff, lNL);
                }
                lHeaderComplete++;
            }
        }
예제 #4
0
 public void SetASCII(string aString)
 {
     mByteArray = WebSocketConvert.StringToBytes(aString, WebSocketTypeEncoding.ASCII);
 }
예제 #5
0
 public string GetASCII()
 {
     return(WebSocketConvert.BytesToString(mByteArray, WebSocketTypeEncoding.ASCII));
 }
예제 #6
0
 public string GetUTF8()
 {
     return(WebSocketConvert.BytesToString(mByteArray, WebSocketTypeEncoding.UTF8));
 }
예제 #7
0
 public string GetString(WebSocketTypeEncoding aEncoding)
 {
     return(WebSocketConvert.BytesToString(mByteArray, aEncoding));
 }
예제 #8
0
 public string GetString()
 {
     return(WebSocketConvert.BytesToString(mByteArray, WebSocketTypeEncoding.Default));
 }
예제 #9
0
 public void SetUTF8(string aString)
 {
     mByteArray = WebSocketConvert.StringToBytes(aString, WebSocketTypeEncoding.UTF8);
 }
예제 #10
0
 public void SetString(string aString, WebSocketTypeEncoding aEncoding)
 {
     mByteArray = WebSocketConvert.StringToBytes(aString, aEncoding);
 }