Exemplo n.º 1
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.º 2
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);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 处理 http://host/controller/method类型的函数
        /// </summary>
        /// <param name="fullname"></param>
        /// <param name="methodName"></param>
        internal void handleUrlMethod(string fullname, string methodName)
        {
            var        pageDefine = checkRemotingName(fullname);
            MethodInfo methodinfo = pageDefine.Methods.SingleOrDefault(m => string.Equals(m.Name, methodName, StringComparison.CurrentCultureIgnoreCase));

            if (methodinfo == null)
            {
                throw new Exception($"没有找到方法{methodName},可能因为此方法没有定义[RemotingMethod]");
            }

            if (RemotingContext.Current.Request.Headers.ContainsKey("Cookie"))
            {
                var match = System.Text.RegularExpressions.Regex.Match(RemotingContext.Current.Request.Headers["Cookie"], @"WayScriptRemoting\=([\w|\-]+)");
                if (match.Length > 0)
                {
                    this.Session = SessionState.GetSession(match.Groups[1].Value);
                }
            }
            if (this.Session == null)
            {
                this.Session = SessionState.GetSession(Guid.NewGuid().ToString());
            }

            var currentPage = (RemotingController)Activator.CreateInstance(pageDefine.ControllerType);

            currentPage.Session        = this.Session;
            currentPage.RequestHeaders = new RemotingController.RequestHeaderCollection(_GetHeaderValueHandler);

            RemotingContext.Current.Controller = currentPage;

            currentPage.onLoad();

            try
            {
                RemotingMethodAttribute methodAttr = (RemotingMethodAttribute)methodinfo.GetCustomAttribute(typeof(RemotingMethodAttribute));
                var pInfos = methodinfo.GetParameters();

                object[] parameters = new object[pInfos.Length];
                for (int i = 0; i < parameters.Length; i++)
                {
                    Type   pType = pInfos[i].ParameterType;
                    string name  = pInfos[i].Name;
                    string value = null;
                    if (RemotingContext.Current.Request.Query.ContainsKey(name))
                    {
                        value = RemotingContext.Current.Request.Query[name];
                    }
                    else if (RemotingContext.Current.Request.Form.ContainsKey(name))
                    {
                        value = RemotingContext.Current.Request.Form[name];
                    }
                    if (value == null)
                    {
                        continue;
                    }
                    try
                    {
                        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, value);
                        }
                        else if (pType == typeof(string))
                        {
                            parameters[i] = value;
                        }
                        else
                        {
                            parameters[i] = Convert.ChangeType(value, pType);
                        }
                    }
                    catch { }
                }
                object result = null;

                result = currentPage._OnInvokeMethod(methodinfo, parameters);

                RemotingContext.Current.Response.Headers["Set-Cookie"] = $"WayScriptRemoting={this.Session.SessionID};path=/";

                if (result is FileContent)
                {
                    ((FileContent)result).Output();
                }
                else if (result is string)
                {
                    RemotingContext.Current.Response.Write(result.ToSafeString());
                }
                else if (result != null)
                {
                    RemotingContext.Current.Response.Write(result.ToJsonString());
                }
                else
                {
                    RemotingContext.Current.Response.Write(new byte[0]);
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                currentPage.unLoad();
            }
        }