Esempio n. 1
0
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="msg"></param>
        public void SendMessage(string msg, bool bIsEncryted, SslStream sslStream)
        {
            NetPacketHeader header = new NetPacketHeader();

            header.Flag   = Encoding.ASCII.GetBytes("LM");
            header.Format = 1;
            byte[] data;
            if (bIsEncryted)
            {
                header.State = 4;
                //data = AESEncryption.EncryptNN256Data(msg);

                byte[] temp = Encoding.ASCII.GetBytes(msg);
                data = ServerAESEncryption.EncryptBytes(temp, EncryptionMode.AES256V13Hex);
            }
            else
            {
                header.State = 0;
                data         = Encoding.ASCII.GetBytes(msg);
            }
            header.Size = (uint)data.Length;
            byte[] headData = Common.StructToBytes(header);
            sslStream.Write(headData);
            sslStream.Write(data);
            sslStream.Flush();
        }
Esempio n. 2
0
        /// <summary>
        /// 处理消息
        /// </summary>
        /// <param name="header"></param>
        /// <param name="byteData"></param>
        /// <returns></returns>
        public string Processmessage(NetPacketHeader header, byte[] byteData)
        {
            bool   mIsEncryt = (header.State & 4) != 0;
            string strData   = string.Empty;

            if (mIsEncryt)
            {
                //strData = AESEncryption.DecryptNN256(byteData);

                byte[] temp = ServerAESEncryption.DecryptBytes(byteData, EncryptionMode.AES256V13Hex);
                strData = Encoding.ASCII.GetString(temp).TrimEnd('\0', '\r', '\n');
            }
            else
            {
                strData = Encoding.ASCII.GetString(byteData);
            }
            strData = strData.Trim('\n');
            return(strData);
        }
Esempio n. 3
0
        private JsonObject ThreadWorker(SslStream sslStream, TcpClient client)
        {
            NetPacketHeader header = new NetPacketHeader();

            byte[]     byteFollowMsg  = new byte[1];
            bool       bIsGetResponse = false;  //是否已经获得了请求回应信息
            JsonObject objAllLicenses = null;

            while (!bIsGetResponse)
            {
                ReceiveMessage(ref header, ref byteFollowMsg, sslStream, client);
                string     strMsg = Processmessage(header, byteFollowMsg);
                JsonObject obj    = new JsonObject(strMsg);
                string     st     = obj[LicenseDefine.KEYWORD_MSG_CLASSDESC].GetValue().ToString();
                if (int.Parse(obj[LicenseDefine.KEYWORD_MSG_CLASSID].GetValue().ToString()) == 5 &&
                    int.Parse(obj[LicenseDefine.KEYWORD_MSG_MESSAGEID].GetValue().ToString()) == 3 &&
                    int.Parse(obj[LicenseDefine.KEYWORD_MSG_REQUESTID].GetValue().ToString()) == 1)
                {
                    objAllLicenses = obj;
                    bIsGetResponse = true;
                }
            }
            return(objAllLicenses);
        }
Esempio n. 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="strHost"></param>
        /// <param name="iPort"></param>
        /// <param name="moduleTypeID">用来区分是管理员方式连接 和 取license的客户端连接</param>
        /// <param name="sslStream"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public int ConnectToLicenseServer(string strHost, int iPort, int moduleTypeID, ref SslStream sslStream, ref TcpClient client)
        {
            int iResult = 0;

            try
            {
                client = new TcpClient();
                for (int i = 0; i < 3; i++)
                {
                    try
                    {
                        client.Connect(strHost, iPort);
                        if (client.Connected)
                        {
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        UMPService00.WriteLog("ConnLic", string.Format("Fail.\t{0}", ex.Message));
                        continue;
                    }
                }
                if (!client.Connected)
                {
                    return(-1);
                }
                sslStream = new SslStream(client.GetStream(), false, Common.ServerValidation, null);
                sslStream.AuthenticateAsClient(client.Client.RemoteEndPoint.ToString());
                string          strResult     = string.Empty;
                NetPacketHeader header        = new NetPacketHeader();
                byte[]          byteFollowMsg = new byte[1];
                ReceiveMessage(ref header, ref byteFollowMsg, sslStream, client);
                bIsEncryted = (header.State & 4) != 0;
                string     strMsg = Processmessage(header, byteFollowMsg);
                JsonObject obj    = new JsonObject(strMsg);
                strSession = obj[LicenseDefine.KEYWORD_MSG_DATA][LicenseDefine.KEYWORD_MSG_SESSION].GetValue().ToString();
                int iMessageID = int.Parse(obj[LicenseDefine.KEYWORD_MSG_MESSAGEID].ToString());
                if (iMessageID != 1)
                {
                    UMPService00.WriteLog("The welcome package error");
                    return(-1);
                }

                if (iMessageID == 1)       //收到的是welcome 则返回logon数据包
                {
                    strMsg = string.Empty;
                    JsonObject json = new JsonObject();
                    json[LicenseDefine.KEYWORD_MSG_CLASSDESC]   = new JsonProperty("\"authenticate\"");
                    json[LicenseDefine.KEYWORD_MSG_CLASSID]     = new JsonProperty(LicenseDefine.LICENSE_MSG_CLASS_AUTHENTICATE);
                    json[LicenseDefine.KEYWORD_MSG_CURRENTTIME] = new JsonProperty(DateTime.Now.ToString("\""
                                                                                                         + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\""));
                    json[LicenseDefine.KEYWORD_MSG_DATA] = new JsonProperty(new JsonObject());
                    json[LicenseDefine.KEYWORD_MSG_DATA][LicenseDefine.KEYWORD_MSG_HEARTBEAT]    = new JsonProperty(LicenseDefine.NET_HEARTBEAT_INTEVAL);
                    json[LicenseDefine.KEYWORD_MSG_DATA][LicenseDefine.KEYWORD_MSG_MODULENAME]   = new JsonProperty("\"License Test\"");
                    json[LicenseDefine.KEYWORD_MSG_DATA][LicenseDefine.KEYWORD_MSG_MODULENUMBER] = new JsonProperty(-1);
                    json[LicenseDefine.KEYWORD_MSG_DATA][LicenseDefine.KEYWORD_MSG_MODULETYPEID] = new JsonProperty(moduleTypeID);
                    json[LicenseDefine.KEYWORD_MSG_DATA][LicenseDefine.KEYWORD_MSG_PROTOCOL]     = new JsonProperty("\"" + LicenseDefine.NET_PROTOCOL_VERSION + "\"");
                    json[LicenseDefine.KEYWORD_MSG_DATA][LicenseDefine.KEYWORD_MSG_SESSION]      = new JsonProperty(strSession);
                    json[LicenseDefine.KEYWORD_MSG_DATA][LicenseDefine.KEYWORD_MSG_VERIFICATION] = new JsonProperty("\"" + Common.GetValidication(strSession) + "\"");
                    json[LicenseDefine.KEYWORD_MSG_MESSAGEDESC] = new JsonProperty("\"longon\"");
                    json[LicenseDefine.KEYWORD_MSG_MESSAGEID]   = new JsonProperty(LicenseDefine.LICENSE_MSG_CLASS_CONNECTION);
                    strMsg = json.ToString();
                    SendMessage(json.ToString(), bIsEncryted, sslStream);
                    ReceiveMessage(ref header, ref byteFollowMsg, sslStream, client);
                    strMsg     = Processmessage(header, byteFollowMsg);
                    obj        = new JsonObject(strMsg);
                    iMessageID = int.Parse(obj[LicenseDefine.KEYWORD_MSG_MESSAGEID].GetValue().ToString());
                    if (iMessageID != 3)
                    {
                        UMPService00.WriteLog("License server verification error : ");
                        return(-1);
                    }
                }
            }
            catch (Exception ex)
            {
                UMPService00.WriteLog("ConnLic", string.Format("Fail.\t{0}", ex.Message));
                iResult = -1;
            }
            return(iResult);
        }
Esempio n. 5
0
        /// <summary>
        /// 接受数据
        /// </summary>
        private void ReceiveMessage(ref NetPacketHeader packHeader, ref byte[] byteFollowMsg, SslStream sslStream, TcpClient client)
        {
            string str = string.Empty;

            byte[] byteHeadMsg    = new byte[LicenseDefine.NET_HEAD_SIZE];
            byte[] byteTemp       = new byte[1024]; //临时存放数据
            byte[] byteReceive    = new byte[16];   //用于接收消息
            int    iReceiveResult = 0;
            int    iTotalRecive   = 0;              //计算总共收下多少数据
            bool   isReceivedHead = false;          //包头有没有收完 true为收完

            byte[]          byteFollowData     = new byte[1];
            int             iReceiveByteLength = LicenseDefine.NET_HEAD_SIZE;
            NetPacketHeader disHead;

            while (true)
            {
                if (!sslStream.CanRead || !client.Connected)
                {
                    break;
                }
                byteReceive    = new byte[iReceiveByteLength];
                iReceiveResult = sslStream.Read(byteReceive, 0, iReceiveByteLength);
                if (iReceiveResult <= 0)
                {
                    break;
                }
                if (!isReceivedHead)
                {
                    byteReceive.CopyTo(byteTemp, iTotalRecive);
                    if (iReceiveResult + iTotalRecive >= byteReceive.Length)
                    {
                        isReceivedHead = true;
                        disHead        = (NetPacketHeader)Common.BytesToStruct(byteTemp, typeof(NetPacketHeader));
                        if (disHead.Flag[0] == 'L' && disHead.Flag[1] == 'M')
                        {
                            byteFollowData     = new byte[disHead.Size];
                            iReceiveByteLength = (int)disHead.Size;
                            packHeader         = disHead;
                            iTotalRecive       = 0;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        iTotalRecive += iReceiveResult;
                        continue;
                    }
                }
                else
                {
                    Array.Copy(byteReceive, 0, byteFollowData, iTotalRecive, iReceiveResult);
                    iTotalRecive += iReceiveResult;
                    //判断本次接收完后 如果下次接收的数据加上已经收完的数据 大于总数据数
                    //则设置下次接收的数据大小为总数据-已经接收完的数据 避免接收到后续包的数据 导致数据不完整
                    if (iTotalRecive + iReceiveByteLength > byteFollowData.Length)
                    {
                        iReceiveByteLength = byteFollowData.Length - iTotalRecive;
                    }
                    if (iTotalRecive == packHeader.Size)
                    {
                        break;
                    }
                }
            }
            byteFollowMsg = new byte[byteFollowData.Count()];
            byteFollowData.CopyTo(byteFollowMsg, 0);
        }