コード例 #1
0
        /// <summary>
        /// The internal search method - remember that the method is reqursive.
        /// </summary>
        /// <param name="directoryDefinition">
        /// The directoryDefinition is the starting point for each call to the internal search method.
        /// </param>
        /// <exception cref="SearchException">
        /// </exception>
        private void SearchInternal(IDirectoryDefinition directoryDefinition)
        {
            string[] dirs = new string[0];
            string[] files;

            #region Get directories
            try
            {
                if (!Directory.Exists(directoryDefinition.Path))
                {
                    return;
                }

                if (!directoryDefinition.Enabled)
                {
                    return;
                }

                if (IncludeSubDirsInSearch)
                {
                    dirs      = Directory.GetDirectories(directoryDefinition.Path);
                    DirCount += dirs.Length;
                }
            }
            catch (UnauthorizedAccessException uae)
            {
                Log.Info("Unable to access directory {0} due to UnauthorizedAccessException. \n\n{1}", directoryDefinition, BaseException.Format(this, -1, "Unable to access directory " + directoryDefinition, uae));
                return;
            }
            catch (Exception exception)
            {
                string s = string.Format("Unable to retrieve directories from '{0}'.", directoryDefinition.Path);
                throw new SearchException(this, -1, s, exception);
            }
            #endregion


            #region Get files
            try
            {
                files      = Directory.GetFiles(directoryDefinition.Path, Extension);
                FileCount += files.Length;
            }
            catch (Exception exception)
            {
                string s = string.Format("Unable to retrieve files from '{0}'.", directoryDefinition);
                throw new SearchException(this, -1, s, exception);
            }
            #endregion


            #region Handle the found directories
            foreach (string dir in dirs)
            {
                bool exclude = false;
                foreach (IDirectoryDefinition excludedDir in ExcludeDirs)
                {
                    if (!excludedDir.Enabled)
                    {
                        continue;
                    }

                    if (string.Compare(excludedDir.Path, dir, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        exclude = true;
                    }
                }

                if (!exclude)
                {
                    // Not excluded - let's continue...
                    IDirectoryDefinition dirDef = ProxyHome.Instance.RetrieveConfigurationFactory(EngineKeyKeeper.Instance.AccessKey).ConfigurationFactory <IDirectoryDefinition>(typeof(IDirectoryDefinition));
                    dirDef.Enabled = true;
                    dirDef.Path    = dir;

                    SearchInternal(dirDef);
                }
            }
            #endregion


            #region Handle the found files
            foreach (string file in files)
            {
                FileFoundDuringSearch.Add(file);
            }
            #endregion
        }
コード例 #2
0
        static void Main(string[] args)
        {
            #region Testing encryption/decryption

            bool shouldTest = false;
            if (shouldTest)
            {
                AesCryptography aes = new AesCryptography();

                var password = "******";
                var salt     = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
                var ct1      = aes.Encrypt(password, salt, Encoding.UTF8.GetBytes("Alice; Bob; Eve;: PerformAct1"));
                Console.WriteLine(Convert.ToBase64String(ct1));

                var ct2 = aes.Encrypt(password, salt, Encoding.UTF8.GetBytes("Alice; Bob; Eve;: PerformAct2"));
                Console.WriteLine(Convert.ToBase64String(ct2));

                var pt1 = aes.Decrypt(password, salt, ct1);
                Console.WriteLine(Encoding.UTF8.GetString(pt1));

                var pt2 = aes.Decrypt(password, salt, ct2);
                Console.WriteLine(Encoding.UTF8.GetString(pt2));

                // Now check tampering
                try
                {
                    ct1[30]++;
                    aes.Decrypt(password, salt, ct1);
                    Console.WriteLine("Error: tamper detection failed.");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Success: tampering detected.");
                    Console.WriteLine(ex.ToString());
                }
                Console.ReadLine();
            }
            #endregion


            string COMPARE_SILENT = "SILENT";
            string COMPARE_HELP   = "HELP";

            string[] cmdArgs = Environment.GetCommandLineArgs();
            foreach (string arg in cmdArgs)
            {
                if (arg.ToUpper() == COMPARE_SILENT)
                {
                    ConsoleEnabled = false;
                }

                if (arg.ToUpper() == COMPARE_HELP)
                {
                    ConsoleEnabled = true;

                    #region Help output to console
                    //      12345678901234567890123456789012345678901234567890123456789012345678901234567890
                    Output("╔══════════════════════════════════════════════════════════════════════════════╗");
                    Output("║                                  Code Analyser                               ║");
                    Output("╠══════════════════════════════════════════════════════════════════════════════╣");
                    Output("║                                                                              ║");
                    Output("║ Purpose   The application is designed to search files for suspect code       ║");
                    Output("║           constructions, i.e. try-catch statements suppressing exceptions    ║");
                    Output("║           from being handled.                                                ║");
                    Output("║                                                                              ║");
                    Output("║           The type of code constructions that are matched during the search  ║");
                    Output("║           is specified through regular expressions in the applications       ║");
                    Output("║           configuration file. Multiple regular expressions can be added to   ║");
                    Output("║           the configuration file as well as what directories the search      ║");
                    Output("║           should include, what directories should be excluded, the type of   ║");
                    Output("║           files to include in the search.                                    ║");
                    Output("║                                                                              ║");
                    Output("║                                                                              ║");
                    Output("║  Result:  A resulting xml file containing the result of the search will be   ║");
                    Output("║           created in the execution directory, 'Analyser.xml'. Just open it   ║");
                    Output("║           in a browser - it will be transformed into html by the associated  ║");
                    Output("║           xslt file.                                                         ║");
                    Output("║                                                                              ║");
                    Output("║                                                                              ║");
                    Output("║  How to:  The application can be run with no arguments. The following        ║");
                    Output("║           arguments are allowed:                                             ║");
                    Output("║                                                                              ║");
                    Output("║           <help>   Will show this dialog.                                    ║");
                    Output("║                                                                              ║");
                    Output("║           <silent> Indicates whether output from the client should be        ║");
                    Output("║                    enabled. Adding the argument 'silent' will disable        ║");
                    Output("║                    output to the command line.                               ║");
                    Output("║                                                                              ║");
                    Output("║           NOTE:    Using the 'silent' argument will not disable output from  ║");
                    Output("║                    the log system to the 'Console' target! If all messages   ║");
                    Output("║                    to the command line should be completely disabled then    ║");
                    Output("║                    disable the 'Console' target in the log system            ║");
                    Output("║                    configuration file as well.                               ║");
                    Output("║                                                                              ║");
                    Output("║                                                                              ║");
                    Output("║   Setup:  Two configuration files (.config) are needed in order to execute   ║");
                    Output("║           the application. Both files is expected to be located in the       ║");
                    Output("║           applications execution directory. If not placed here the           ║");
                    Output("║           application will fail.                                             ║");
                    Output("║                                                                              ║");
                    Output("║           <hunter> Configuration file for setting up the include             ║");
                    Output("║                    directories, regular expressions etc.                     ║");
                    Output("║                                                                              ║");
                    Output("║           <log>    Configuration file for setting up the log system that the ║");
                    Output("║                    application uses.                                         ║");
                    Output("║                                                                              ║");
                    Output("╚══════════════════════════════════════════════════════════════════════════════╝");
                    #endregion

                    ConsoleEnabled = false;
                    return;
                }
            }

            try
            {
                DirHandler.Instance.CurrentDirectory = Environment.CurrentDirectory;
            }
            catch (Exception e)
            {
                Console.WriteLine(BaseException.Format(null, -1, @"Failed to initialize 'Directory Handler' with current DIR? Unable to continue.", e));
                Console.ReadLine();
                return;
            }


            ApplicationManager am = null;
            try
            {
                am = new ApplicationManager();
            }
            catch (Exception e)
            {
                Console.WriteLine(BaseException.Format(null, -1, @"Failed to construct the 'Application Manager'? Unable to continue.", e));
                Console.ReadLine();
                return;
            }


            try
            {
                am.Start();
            }
            catch (CoordinationException ce)
            {
                Console.WriteLine(ce.ExceptionSummary());
                Console.ReadLine();
                return;
            }

            Output(ProxyHome.Instance.StatisticsProxy.ExtractTimerMeasurings());


            // Shutdown the log system - should also empty all the queues before stopping.
            Out.Stop();
            Console.ReadLine();
        }