public async Task <IActionResult> ShareConfirmed(string id, Method @method)
        {
            if (id != @method.Id)
            {
                return(NotFound());
            }


            List <SharedMethod> sharedMethodUsers = _context.SharedMethods.Where(x => (x.MethodId == id)).ToList();

            _context.RemoveRange(sharedMethodUsers);

            List <ApplicationUser> sharedUsers = @method.AvailableUsers.Where(x => x.IsSelected).ToList();

            if (sharedUsers.Count > 0)
            {
                foreach (var usr in sharedUsers)
                {
                    SharedMethod shared = new SharedMethod {
                        MethodId = id, UserId = usr.Id, CanEdit = usr.CanEditMethod
                    };
                    await _context.AddAsync(shared);
                }
            }

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 2
0
        private void RegisterFunctions()
        {
            foreach (MethodInfo SharedMethod in typeof(T).GetMethods())
            {
                if (SharedMethod.GetCustomAttribute(typeof(NLCCall)) is NLCCall NLCAttribute)
                {
                    if (RemotingMethods.ContainsKey(NLCAttribute.Identifier))
                    {
                        OnServerExceptionOccurred?.Start(this, new OnServerExceptionOccurredEventArgs(new Exception($"Method with identifier {NLCAttribute.Identifier} already exists!")));
                        continue;
                    }

                    var IsAsync        = SharedMethod.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) is AsyncStateMachineAttribute;
                    var ReturnType     = SharedMethod.ReturnType;
                    var HasAsyncResult = ReturnType.IsGenericType && ReturnType.GetGenericTypeDefinition() == typeof(Task <>);

                    if (!HasAsyncResult && IsAsync && ReturnType != typeof(Task))
                    {
                        throw new Exception($"Shared function {NLCAttribute.Identifier} is asynchronous but does not return a Task!");
                    }

                    RemotingMethods.Add(NLCAttribute.Identifier, new RemotingMethod()
                    {
                        Method         = Helpers.CreateMethodWrapper(typeof(T), SharedMethod),
                        WithContext    = NLCAttribute.WithContext,
                        HasAsyncResult = HasAsyncResult
                    });
                }
            }
        }
Exemplo n.º 3
0
        public static bool LoadPayload(string PayloadID, byte[] Payload)
        {
            if (Payloads.ContainsKey(PayloadID))
            {
                return(false);
            }

            var TargetAssembly = Assembly.Load(Payload);

            var SharedClass    = TargetAssembly.GetTypes().First(x => x.GetMethods().Any(y => y.GetCustomAttributes(typeof(NLCCall), false).Length > 0));
            var SharedInstance = Activator.CreateInstance(SharedClass);
            var SharedMethods  = new Dictionary <string, MethodInfo>();

            foreach (MethodInfo SharedMethod in SharedClass.GetMethods())
            {
                var SharedMethodAttribute = SharedMethod.GetCustomAttributes(typeof(NLCCall), false);

                if (SharedMethodAttribute.Length > 0)
                {
                    var thisAttr = SharedMethodAttribute[0] as NLCCall;

                    if (SharedMethods.ContainsKey(thisAttr.Identifier))
                    {
                        continue;
                    }

                    SharedMethods.Add(thisAttr.Identifier, SharedMethod);
                }
            }

            Payloads.Add(PayloadID, new Payload()
            {
                Instance = SharedInstance, Methods = SharedMethods
            });

            return(true);
        }
Exemplo n.º 4
0
        public object onRequest(AClient connection, PayloadReader pr)
        {
            ReturnResult result         = new ReturnResult(null, false);
            bool         isDelegate     = false;
            bool         ReadFullHeader = false;

            try
            {
                int SharedClassId = pr.ReadInteger();
                int MethodId      = pr.ReadInteger();
                isDelegate = pr.ReadByte() == 1;
                int DelegateId      = isDelegate ? pr.ReadInteger() : 0;
                int DelegateClassId = isDelegate ? pr.ReadInteger() : 0;
                ReadFullHeader = true;

                if (connection.RemoteSharedClasses.ContainsKey(SharedClassId) ||
                    (isDelegate && connection.LocalSharedClasses.ContainsKey(DelegateClassId)))
                {
                    SharedClass  sClass       = isDelegate ? connection.LocalSharedClasses[SharedClassId] : connection.RemoteSharedClasses[SharedClassId];
                    SharedMethod sharedMethod = sClass.GetMethod(MethodId);

                    if (sharedMethod != null)
                    {
                        List <object> args  = new List <object>();
                        List <Type>   types = new List <Type>();
                        SortedList <int, SharedDelegate> SharedDelegates = new SortedList <int, SharedDelegate>();
                        SmartSerializer serializer = new SmartSerializer();

                        lock (sharedMethod.Delegates)
                        {
                            if (sharedMethod.Delegates.ContainsKey(DelegateId))
                            {
                                for (int i = 0; i < sharedMethod.Delegates[DelegateId].sharedMethod.ArgumentTypes.Length; i++)
                                {
                                    args.Add(serializer.Deserialize(pr.ReadBytes(pr.ReadInteger())));
                                }
                            }
                            else
                            {
                                for (int i = 0; i < sharedMethod.ArgumentTypes.Length; i++)
                                {
                                    args.Add(serializer.Deserialize(pr.ReadBytes(pr.ReadInteger())));
                                }
                            }
                        }

                        if (!isDelegate) //atm no support yet for delegate inside another delegate
                        {
                            for (int i = 0; i < sharedMethod.DelegateIndex.Count; i++)
                            {
                                if (pr.ReadByte() == 1)
                                {
                                    SharedDelegate del = pr.ReadObject <SharedDelegate>();
                                    del.sharedMethod.sharedClass             = sClass;
                                    args[sharedMethod.DelegateIndex.Keys[i]] = DynamicDelegateCreator.CreateDelegate(del);
                                    SharedDelegates.Add(del.sharedMethod.DelegateId, del);
                                }
                            }
                        }

                        if (isDelegate)
                        {
                            //only show the result and not the actual ReturnResult type
                            return(sharedMethod.Delegates[DelegateId].Delegate.DynamicInvoke(args.ToArray()));
                        }
                        else
                        {
                            MethodInfo m = sClass.InitializedClass.GetType().GetMethod(sharedMethod.Name, sharedMethod.ArgumentTypes);

                            if (m.GetCustomAttributes(typeof(RemoteExecutionAttribute), false).Length == 0 &&
                                m.GetCustomAttributes(typeof(UncheckedRemoteExecutionAttribute), false).Length == 0)
                            {
                                return(null);
                            }
                            result.ReturnValue = m.Invoke(sClass.InitializedClass, args.ToArray());
                        }
                    }
                }
            } catch (Exception ex)
            {
                if (isDelegate && ReadFullHeader)
                {
                    throw ex;
                }

                result.exceptionMessage = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                result.ExceptionOccured = true;
            }
            return(result);
        }
Exemplo n.º 5
0
        public override void ProcessPayload(SSPClient client, OperationalSocket OpSocket)
        {
            ReturnResult   result = new ReturnResult(null, false);
            LiteCodeClient Client = OpSocket as LiteCodeClient;

            try
            {
                PayloadReader pr     = new PayloadReader(Data);
                SharedClass   sClass = null;

                if (Client.InitializedClasses.TryGetValue(SharedClassId, out sClass))
                {
                    SharedMethod sharedMethod = sClass.GetMethod(MethodId);

                    if (sharedMethod != null)
                    {
                        List <object> args  = new List <object>();
                        List <Type>   types = new List <Type>();
                        SortedList <int, SharedDelegate> SharedDelegates = new SortedList <int, SharedDelegate>();
                        SmartSerializer serializer = new SmartSerializer();

                        lock (sharedMethod.Delegates)
                        {
                            SharedDelegate sharedDel = null;
                            if (sharedMethod.Delegates.TryGetValue(DelegateId, out sharedDel))
                            {
                                for (int i = 0; i < sharedDel.sharedMethod.ArgumentTypes.Length; i++)
                                {
                                    args.Add(serializer.Deserialize(pr.ReadBytes(pr.ReadInteger())));
                                }
                            }
                            else
                            {
                                for (int i = 0; i < sharedMethod.ArgumentTypes.Length; i++)
                                {
                                    args.Add(serializer.Deserialize(pr.ReadBytes(pr.ReadInteger())));
                                }
                            }
                        }

                        if (!isDelegate) //atm no support yet for delegate inside another delegate
                        {
                            for (int i = 0; i < sharedMethod.DelegateIndex.Count; i++)
                            {
                                if (pr.ReadByte() == 1)
                                {
                                    SharedDelegate del = pr.ReadObject <SharedDelegate>();
                                    del.sharedMethod.sharedClass             = sClass;
                                    args[sharedMethod.DelegateIndex.Keys[i]] = DynamicDelegateCreator.CreateDelegate(del);
                                    SharedDelegates.Add(del.sharedMethod.DelegateId, del);
                                }
                            }
                        }

                        if (isDelegate)
                        {
                            result.ReturnValue = sharedMethod.Delegates[DelegateId].Delegate.DynamicInvoke(args.ToArray());
                        }
                        else
                        {
                            if (sharedMethod.CallCache == null)
                            {
                                MethodInfo methodInf = sClass.InitializedClass.GetType().GetMethod(sharedMethod.Name, sharedMethod.ArgumentTypes);
                                sharedMethod.CallCache = methodInf.Bind();
                            }
                            result.ReturnValue = sharedMethod.CallCache(sClass.InitializedClass, args.ToArray());

                            /*MethodInfo methodInf = sClass.InitializedClass.GetType().GetMethod(sharedMethod.Name, sharedMethod.ArgumentTypes);
                             * result.ReturnValue = methodInf.Invoke(sClass.InitializedClass, args.ToArray());*/
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.exceptionMessage = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                result.ExceptionOccured = true;
                client.onException(ex.InnerException != null ? ex.InnerException : ex, ErrorType.UserLand);
            }

            if (RequireResultBack)
            {
                Client.Send(new MsgExecuteMethodResponse(RequestId, result));
            }
        }
Exemplo n.º 6
0
        public override void ProcessPayload(IClient client, IPlugin plugin = null)
        {
            ReturnResult result         = new ReturnResult(null, false);
            bool         isDelegate     = false;
            bool         ReadFullHeader = false;
            SSPClient    Client         = client as SSPClient;

            try
            {
                PayloadReader pr            = new PayloadReader(Data);
                int           SharedClassId = pr.ReadInteger();
                int           MethodId      = pr.ReadInteger();
                isDelegate = pr.ReadByte() == 1;
                int DelegateId      = isDelegate ? pr.ReadInteger() : 0;
                int DelegateClassId = isDelegate ? pr.ReadInteger() : 0;
                ReadFullHeader = true;

                if (Client.Connection.InitializedClasses.ContainsKey(SharedClassId))
                {
                    SharedClass  sClass       = Client.Connection.InitializedClasses[SharedClassId];
                    SharedMethod sharedMethod = sClass.GetMethod(MethodId);

                    if (sharedMethod != null)
                    {
                        List <object> args  = new List <object>();
                        List <Type>   types = new List <Type>();
                        SortedList <int, SharedDelegate> SharedDelegates = new SortedList <int, SharedDelegate>();
                        SmartSerializer serializer = new SmartSerializer();

                        lock (sharedMethod.Delegates)
                        {
                            if (sharedMethod.Delegates.ContainsKey(DelegateId))
                            {
                                for (int i = 0; i < sharedMethod.Delegates[DelegateId].sharedMethod.ArgumentTypes.Length; i++)
                                {
                                    args.Add(serializer.Deserialize(pr.ReadBytes(pr.ReadInteger())));
                                }
                            }
                            else
                            {
                                for (int i = 0; i < sharedMethod.ArgumentTypes.Length; i++)
                                {
                                    args.Add(serializer.Deserialize(pr.ReadBytes(pr.ReadInteger())));
                                }
                            }
                        }

                        if (!isDelegate) //atm no support yet for delegate inside another delegate
                        {
                            for (int i = 0; i < sharedMethod.DelegateIndex.Count; i++)
                            {
                                if (pr.ReadByte() == 1)
                                {
                                    SharedDelegate del = pr.ReadObject <SharedDelegate>();
                                    del.sharedMethod.sharedClass             = sClass;
                                    args[sharedMethod.DelegateIndex.Keys[i]] = DynamicDelegateCreator.CreateDelegate(del);
                                    SharedDelegates.Add(del.sharedMethod.DelegateId, del);
                                }
                            }
                        }

                        if (isDelegate)
                        {
                            result.ReturnValue = sharedMethod.Delegates[DelegateId].Delegate.DynamicInvoke(args.ToArray());
                        }
                        else
                        {
                            MethodInfo methodInf = sClass.InitializedClass.GetType().GetMethod(sharedMethod.Name, sharedMethod.ArgumentTypes);

                            if (methodInf.GetCustomAttributes(typeof(RemoteExecutionAttribute), false).Length == 0 &&
                                methodInf.GetCustomAttributes(typeof(UncheckedRemoteExecutionAttribute), false).Length == 0)
                            {
                                //return null;
                            }
                            result.ReturnValue = methodInf.Invoke(sClass.InitializedClass, args.ToArray());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //if (isDelegate && ReadFullHeader)
                //    throw ex;

                result.exceptionMessage = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                result.ExceptionOccured = true;
                client.onException(ex.InnerException != null ? ex.InnerException : ex, ErrorType.UserLand);
            }

            if (RequireResultBack)
            {
                Client.Connection.SendMessage(new MsgExecuteMethodResponse(RequestId, result), PacketId.LiteCodeResponse);
            }

            base.ProcessPayload(client, plugin);
        }