Exemplo n.º 1
0
        internal void handleReCreateRSA(MessageBag msgBag)
        {
            try
            {
                //上一次没解码的内容
                if (!msgBag.SessionID.IsNullOrEmpty())
                {
                    this.Session = SessionState.GetSession(msgBag.SessionID);
                }
                if (this.Session == null)
                {
                    this.Session = SessionState.GetSession(Guid.NewGuid().ToString());
                }


                this.Session["$$_RSACryptoServiceProvider"] = null;
                CreateRSAKey(this.Session);
                mSendDataFunc(Newtonsoft.Json.JsonConvert.SerializeObject(new
                {
                    rsa = new { Exponent = this.Session["$$_rsa_PublicKeyExponent"], Modulus = this.Session["$$_rsa_PublicKeyModulus"] },
                }));
            }
            catch (Exception ex)
            {
                mSendDataFunc(Newtonsoft.Json.JsonConvert.SerializeObject(new
                {
                    err = ex.ToString(),
                }));
            }
        }
Exemplo n.º 2
0
        void handleInit(MessageBag msgBag)
        {
            try
            {
                bool       outputRSAKey = false;
                string     remoteName   = msgBag.ClassFullName;
                TypeDefine pageDefine   = checkRemotingName(remoteName);

                List <MethodDefineForJs> methodDefineForOutput = new List <MethodDefineForJs>();

                foreach (MethodInfo method in pageDefine.Methods)
                {
                    var parameters = method.GetParameters();
                    RemotingMethodAttribute methodAttr = (RemotingMethodAttribute)method.GetCustomAttribute(typeof(RemotingMethodAttribute));
                    var mItem = new MethodDefineForJs()
                    {
                        Method            = method.Name,
                        ParameterLength   = parameters.Length,
                        EncryptParameters = methodAttr.UseRSA.HasFlag(RSAApplyScene.EncryptParameters),
                        EncryptResult     = methodAttr.UseRSA.HasFlag(RSAApplyScene.EncryptResult),
                    };
                    if (mItem.EncryptResult || mItem.EncryptParameters)
                    {
                        outputRSAKey = true;
                    }
                    methodDefineForOutput.Add(mItem);
                }

                //RemotingController currentPage = (RemotingController)Activator.CreateInstance(pageDefine.ControllerType);
                //currentPage.Session = this.Session;
                //currentPage.onLoad();
                if (outputRSAKey)
                {
                    CreateRSAKey(this.Session);
                }
                mSendDataFunc(Newtonsoft.Json.JsonConvert.SerializeObject(new
                {
                    methods   = methodDefineForOutput,
                    SessionID = this.Session.SessionID,
                    rsa       = outputRSAKey ? new { Exponent = this.Session["$$_rsa_PublicKeyExponent"], Modulus = this.Session["$$_rsa_PublicKeyModulus"] } : null,
                }));
            }
            catch (Exception ex)
            {
                mSendDataFunc(Newtonsoft.Json.JsonConvert.SerializeObject(new
                {
                    err = ex.ToString(),
                }));
            }
        }
Exemplo n.º 3
0
        void handleUploadFile(MessageBag msgBag)
        {
            mFileGettedSize = 0;
            try
            {
                string remoteName = msgBag.ClassFullName;
                var    pageDefine = checkRemotingName(remoteName);

                RemotingController currentPage = (RemotingController)Activator.CreateInstance(pageDefine.ControllerType);
                currentPage.Session                = this.Session;
                currentPage.RequestHeaders         = new RemotingController.RequestHeaderCollection(_GetHeaderValueHandler);
                RemotingContext.Current.Controller = currentPage;

                currentPage.onLoad();
                mFileGettedSize = msgBag.Offset;
                try
                {
                    mUploadFileHandler = currentPage.OnBeginUploadFile(msgBag.FileName, msgBag.State, msgBag.FileSize, msgBag.Offset);
                }
                catch
                {
                    throw;
                }
                finally
                {
                    currentPage.unLoad();
                }

                SendData(MessageType.UploadFileBegined, "ok", "");
            }
            catch (Exception ex)
            {
                var baseException = ex.GetBaseException();
                SendData(MessageType.InvokeError, baseException != null ? baseException.Message : ex.Message, "");
            }
        }
Exemplo n.º 4
0
        public virtual void OnReceived(string data)
        {
            try
            {
                MessageBag msgBag = Newtonsoft.Json.JsonConvert.DeserializeObject <MessageBag>(data);
                mCurrentBag = msgBag;

                if (this.Session == null)
                {
                    if (msgBag.SessionID.IsNullOrEmpty())
                    {
                        this.Session = SessionState.GetSession(Guid.NewGuid().ToString());
                    }
                    else
                    {
                        this.Session = SessionState.GetSession(msgBag.SessionID);
                        if (this.Session == null)
                        {
                            this.Session = SessionState.GetSession(Guid.NewGuid().ToString());
                        }
                    }
                }

                if (msgBag.Action == "init")
                {
                    handleInit(msgBag);
                }
                else if (msgBag.Action == "exit")
                {
                    mCloseStreamHandler();
                }
                else if (msgBag.Action == "UploadFile")
                {
                    this.StreamType = RemotingStreamType.Bytes;
                    handleUploadFile(msgBag);
                }
                else if (msgBag.Action == "w_heart")//websocket心跳包
                {
                    SendClientMessage("1", 2);
                    return;
                }
                else if (msgBag.Action == "w_msg")//websocket消息
                {
                    RemotingController.MessageReceive(this.Session, msgBag.State);
                    return;
                }
                else if (msgBag.MethodName.IsNullOrEmpty() == false)
                {
                    handleMethodInvoke(msgBag);
                }
                else if (msgBag.GroupName.IsNullOrEmpty() == false)
                {
                    this.GroupName = msgBag.GroupName;
                    if (RemotingController.MessageReceiverConnect(this.Session, this.GroupName) == false)
                    {
                        throw new Exception("服务器拒绝你接收信息");
                    }
                    KeepAliveHandlers.Add(this);
                    return;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 5
0
        void handleMethodInvoke(MessageBag msgBag)
        {
            RemotingController currentPage = null;

            try
            {
                var pageDefine = checkRemotingName(msgBag.ClassFullName);

                currentPage                = (RemotingController)Activator.CreateInstance(pageDefine.ControllerType);
                currentPage.Session        = this.Session;
                currentPage.RequestHeaders = new RemotingController.RequestHeaderCollection(_GetHeaderValueHandler);

                RemotingContext.Current.Controller = currentPage;

                currentPage.onLoad();

                try
                {
                    MethodInfo[] methodinfos = pageDefine.Methods.Where(m => m.Name == msgBag.MethodName).ToArray();
                    if (methodinfos.Length == 0)
                    {
                        throw new Exception($"没有找到方法{msgBag.MethodName},可能因为此方法没有定义[RemotingMethod]");
                    }


                    for (int k = 0; k < methodinfos.Length; k++)
                    {
                        var methodinfo = methodinfos[k];
                        RemotingMethodAttribute methodAttr = (RemotingMethodAttribute)methodinfo.GetCustomAttribute(typeof(RemotingMethodAttribute));
                        object[] jsParameters = null;
                        if (msgBag.ParameterJson.StartsWith("["))
                        {
                            jsParameters = msgBag.ParameterJson.FromJson <object[]>();
                        }
                        else
                        {
                            jsParameters = DecrptRSA(this.Session, msgBag.ParameterJson).FromJson <object[]>();
                        }

                        var pInfos = methodinfo.GetParameters();
                        if (pInfos.Length != jsParameters.Length)
                        {
                            if (k == methodinfos.Length - 1)
                            {
                                throw new Exception($"{msgBag.MethodName}参数数量与服务器不一致");
                            }
                            continue;
                        }

                        object[] parameters = new object[pInfos.Length];

                        for (int i = 0; i < parameters.Length; i++)
                        {
                            if (jsParameters[i] == null)
                            {
                                continue;
                            }

                            Type pType = pInfos[i].ParameterType;

                            if (jsParameters[i] is Newtonsoft.Json.Linq.JToken)
                            {
                                parameters[i] = (jsParameters[i] as Newtonsoft.Json.Linq.JToken).ToObject(pType);
                            }
                            else if (pType.GetTypeInfo().IsEnum || (pType.GetTypeInfo().IsGenericType&& pType.GetGenericTypeDefinition() == typeof(Nullable <>) && pType.GetGenericArguments()[0].GetTypeInfo().IsEnum))
                            {
                                Type enumType = pType;
                                if (pType.GetTypeInfo().IsGenericType)
                                {
                                    enumType = pType.GetGenericArguments()[0];
                                }
                                parameters[i] = Enum.Parse(enumType, jsParameters[i].ToSafeString());
                            }
                            else
                            {
                                parameters[i] = Convert.ChangeType(jsParameters[i], pType);
                            }
                        }
                        object result = null;

                        result = currentPage._OnInvokeMethod(methodinfo, parameters);

                        if (methodAttr.UseRSA.HasFlag(RSAApplyScene.EncryptResult) && result != null)
                        {
                            SendData(MessageType.Result, result, Session.SessionID, encryptToReturn);
                        }
                        else
                        {
                            SendData(MessageType.Result, result, Session.SessionID);
                        }
                        break;
                    }
                }
                catch
                {
                    throw;
                }
                finally
                {
                    currentPage.unLoad();
                }
            }
            catch (RSADecrptException)
            {
                CreateRSAKey(this.Session);
                SendData(MessageType.RSADecrptError, new { Exponent = this.Session["$$_rsa_PublicKeyExponent"], Modulus = this.Session["$$_rsa_PublicKeyModulus"] }, currentPage.Session.SessionID);
            }
            catch (Exception ex)
            {
                var baseException = ex.GetBaseException();
                SendData(MessageType.InvokeError, baseException != null ? baseException.Message : ex.Message, currentPage.Session.SessionID);
            }
        }