コード例 #1
0
        /// <summary>
        ///     Обработать параметры командной строки
        /// </summary>
        /// <param name="e">
        ///     Параметры командной строки
        /// </param>
        /// <returns>
        ///     Извлечённые значения
        /// </returns>
        public static CommandLineParamsType RecognizeCommandLineParams(StartupEventArgs e)
        {
            CommandLineParamsType result = new CommandLineParamsType();

            // proceed command line parameters
            foreach (string param in e.Args)
            {
                if (param.Contains("-?"))
                {
                    result.ShowHelp = true;
                }
                if (param.Contains("-master"))
                {
                    result.MasterInstance = true;
                }
                else
                if (param.Contains("-enable"))
                {
                    result.RequestedStatus = Status.sOff;
                }
                else
                if (param.Contains("-disable"))
                {
                    result.RequestedStatus = Status.sOn;
                }
                else
                if (param.Contains("-force"))
                {
                    result.ForceMode = true;
                }
                else
                //if (param.Contains("-stop_force"))
                //{
                //    result.ForceMode = false;
                //}
                //else
                //if (param.Contains("-silent"))
                //{
                //    result.SilentMode = true;
                //}
                //else
                if (param.Contains("-exit"))
                {
                    result.ExitRequest = true;
                }
                else
                if (param.Contains("-IgnoreMutex" /* + AppConsts.MUTEX_ID*/))
                {
                    result.IgnoreMutex = true;
                }
            }

            return(result);
        }
コード例 #2
0
        /// <summary>
        ///     Установить параметры командной строки
        /// </summary>
        /// <param name="commandLineParams"></param>
        public static bool ApplyCommandLineParameters(CommandLineParamsType commandLineParams)
        {
            // Специальный режим. Показать Help в консоле и выйти
            if (commandLineParams.ShowHelp)
            {
                ConsoleManager.Show();

                Console.WriteLine("\n\n======================================");
                Console.WriteLine(StringsFunctions.ResourceString("resVersion"));
                Console.WriteLine("======================================\n");
                Console.WriteLine("Usage: SrpManager.exe [[[-master] | [-exit]] [[-enable] | [-disable]] [[-run_force] | [-stop_force]]] | [-?]");
                Console.WriteLine("\nOptions:");
                Console.WriteLine("\t-master    \tclose all other instance of application, and run new instance");
                Console.WriteLine("\t-exit      \tclose all other instance of application");
                Console.WriteLine("\t-enable    \tswitch SRP/AppLocker to the \"Whitelisting\" mode");
                Console.WriteLine("\t-disable   \tswitch SRP/AppLocker to the \"Blacklisting\" mode");
                Console.WriteLine("\t-force \tenable keeping of the selected mode while running");
                Console.WriteLine("\t-?         \tshow this help");
                ConsoleManager.FreeConsole();

                Application.Current.Shutdown();
                return(false);
            }

            if (commandLineParams.RequestedStatus != Status.sNotAvailable)
            {
                AppData.MustSwitch     = true;
                AppData.SwitchToStatus = commandLineParams.RequestedStatus;
            }

            if (commandLineParams.ForceMode != null)
            {
                AppData.EnforceWhileRun = (bool)commandLineParams.ForceMode;
            }

//            AppData.SilentMode = commandLineParams.SilentMode;

            return(true);
        }
コード例 #3
0
ファイル: App.xaml.cs プロジェクト: KohrAhr/SRPManager
        protected override void OnStartup(StartupEventArgs e)
        {
            bool shutDown = false;
            CommandLineParamsType commandLineParams = CoreFunctions.RecognizeCommandLineParams(e);

            if (!CoreFunctions.ApplyCommandLineParameters(commandLineParams))
            {
                return;
            }

            //////////////

            string currentSessionUsername = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            int    currentSessionId       = 0;
            bool   alreadyExistInCurrent  = false;

            // Get all sessions with username
            List <SessionsFunctions.SessionInfo> sessionInfos   = new SessionsFunctions().ListUsers(Environment.MachineName).Where(z => !String.IsNullOrEmpty(z.DomainName) && !String.IsNullOrEmpty(z.UserName)).ToList();
            List <SessionsFunctions.SessionInfo> sessionWithMmf = new List <SessionsFunctions.SessionInfo>();


            currentSessionId = sessionInfos.Where(z => z.DomainName + "\\" + z.UserName == currentSessionUsername).FirstOrDefault().SessionID;

            // Try to get access to MMF in other Sessions
            foreach (SessionsFunctions.SessionInfo sessionInfo in sessionInfos)
            {
                // Session/
                string name = Functions.MMFFunctions.GetSessionMmfName(sessionInfo.SessionID);

                if (Lib.System.MMFFunctions.Exist(name))
                {
                    sessionWithMmf.Add(sessionInfo);

                    if (currentSessionId == sessionInfo.SessionID)
                    {
                        alreadyExistInCurrent = true;
                    }
                }
            }
//            alreadyExistInCurrent = sessionWithMmf.Where(z => z.SessionID == currentSessionId).Count() > 0;



            ////////////

            if (!alreadyExistInCurrent)
            {
                CreateMMF();

                if (commandLineParams.ForceMode != true)
                {
                    // Just apply
                    if (commandLineParams.RequestedStatus == Status.sOn)
                    {
                        new RegistryFunctions().SetRegKeyValue(AppConsts.KEY_SRP_NODE, AppConsts.KEY_SRP_DEFAULT_LEVEL, AppConsts.SRP_ON, RegistryValueKind.DWord);
                        shutDown = true;
                    }
                    else
                    if (commandLineParams.RequestedStatus == Status.sOff)
                    {
                        new RegistryFunctions().SetRegKeyValue(AppConsts.KEY_SRP_NODE, AppConsts.KEY_SRP_DEFAULT_LEVEL, AppConsts.SRP_OFF, RegistryValueKind.DWord);
                        shutDown = true;
                    }
                }
            }
            else
            {
                if (UserFunctions.IsCurrentUserAdmin())
                {
                    // Exit & -Enable/Disable могу отправить только если есть права Админа

                    byte subCommand = 0;
                    byte param1     = 0;
                    byte param2     = 0;

                    if (commandLineParams.ExitRequest || commandLineParams.MasterInstance)
                    {
                        subCommand = AppData.SUB_COMMAND_EXIT;
                    }

                    if (subCommand != 0)
                    {
                        foreach (SessionsFunctions.SessionInfo session in sessionWithMmf)
                        {
                            // Skip _FOR_ current one
                            if (session.SessionID == currentSessionId && !alreadyExistInCurrent)
                            {
                                continue;
                            }

                            MemoryMappedFile memoryMappedFile = Lib.System.MMFFunctions.Open(Functions.MMFFunctions.GetSessionMmfName(session.SessionID));

                            Functions.MMFFunctions.Write(
                                memoryMappedFile,
                                AppConsts.MEMORY_MANAGED_FILE_LENGTH,
                                AppData.COMMAND,
                                subCommand,
                                param1,
                                param2
                                );

                            memoryMappedFile.Dispose();
                        }
                    }

                    subCommand = 0;
                    param1     = 0;
                    param2     = 0;

                    // Command to itself for change status
                    if (commandLineParams.RequestedStatus == Status.sOn)
                    {
                        subCommand = AppData.SUB_COMMAND_CHANGE;
                        param1     = 0;
                    }
                    else
                    if (commandLineParams.RequestedStatus == Status.sOff)
                    {
                        subCommand = AppData.SUB_COMMAND_CHANGE;
                        param1     = 1;
                    }

                    if (commandLineParams.ForceMode == true)
                    {
                        subCommand = AppData.SUB_COMMAND_CHANGE;
                        param2     = 1;
                    }

                    if (subCommand != 0)
                    {
                        using (MemoryMappedFile memoryMappedFile = Lib.System.MMFFunctions.Open(Functions.MMFFunctions.GetSessionMmfName(currentSessionId)))
                        {
                            Functions.MMFFunctions.Write(
                                memoryMappedFile,
                                AppConsts.MEMORY_MANAGED_FILE_LENGTH,
                                AppData.COMMAND,
                                subCommand,
                                param1,
                                param2
                                );
                        }
                    }
                }

                //

                if (!commandLineParams.MasterInstance)
                {
                    //if (!commandLineParams.ExitRequest && commandLineParams.RequestedStatus == Status.sNotAvailable)
                    //{
                    //    CoreFunctions.OneInstanceCheck(CreateMutex(AppConsts.MUTEX_ID));
                    //}

                    Application.Current.Shutdown();
                    return;
                }
                ////else
                ////{
                ////    commandLineParams.IgnoreMutex = true;
                ////}

                // Because we need to wait some time, when other instance will proceed command in MMF
                Thread.Sleep(500);
            }

            if (shutDown)
            {
                Application.Current.Shutdown();
                return;
            }

            RunMMFMonitor();

            ////mutex = CreateMutex(AppConsts.MUTEX_ID);
            ////if (!commandLineParams.IgnoreMutex)
            ////{
            ////    CoreFunctions.OneInstanceCheck(mutex);
            ////}

            base.OnStartup(e);

            InitTrayIcon();
        }