Ejemplo n.º 1
0
        public override Dictionary <AuditFileInfo, string> ReadFilesAsText(List <AuditFileInfo> files)
        {
            CallerInformation here = this.Here();
            Stopwatch         sw   = new Stopwatch();

            sw.Start();
            Dictionary <AuditFileInfo, string> results = new Dictionary <AuditFileInfo, string>(files.Count);
            object results_lock = new object();

            Parallel.ForEach(files, new ParallelOptions()
            {
                MaxDegreeOfParallelism = 20
            }, (_f, state) =>
            {
                LocalAuditFileInfo _lf = _f as LocalAuditFileInfo;
                string text            = _lf.ReadAsText();
                if (text != string.Empty)
                {
                    lock (results_lock)
                    {
                        results.Add(_f, text);
                    }
                }
            });
            sw.Stop();
            Info("Read text for {0} out of {1} files in {2} ms.", results.Count(r => r.Value.Length > 0), results.Count, sw.ElapsedMilliseconds);
            return(results);
        }
Ejemplo n.º 2
0
        protected override Dictionary <string, IEnumerable <Package> > GetModules()
        {
            Stopwatch sw = new Stopwatch();

            if (this.ApplicationBinary == null)
            {
                this.AuditEnvironment.Warning("The .NET assembly application binary was not specified so application modules and version cannot be detected.");
                this.ModulesInitialised = false;
                return(null);
            }
            sw.Start();
            LocalAuditFileInfo      ab       = this.ApplicationBinary.GetAsLocalFile();
            DefaultAssemblyResolver resolver = new DefaultAssemblyResolver();

            resolver.AddSearchDirectory(ab.Directory.FullName);
            ReaderParameters reader = new ReaderParameters()
            {
                AssemblyResolver = resolver
            };

            this.AppModuleDefinition   = ModuleDefinition.ReadModule(ab.FullName, reader);
            this.AppAssemblyDefinition = AssemblyDefinition.ReadAssembly(ab.FullName, reader);
            this.Modules = this.AppModuleDefinition;
            List <AssemblyNameReference> references = this.AppModuleDefinition.AssemblyReferences.ToList();

            references.RemoveAll(r => r.Name == "mscorlib" || r.Name.StartsWith("System"));
            sw.Stop();
            this.AuditEnvironment.Info("Got {0} referenced assemblies in {1} ms", references.Count(), sw.ElapsedMilliseconds);
            IEnumerable <Package> modules = references.Select(r => new Package("netfx", r.Name, r.Version.ToString()));

            this.ModulePackages = new Dictionary <string, IEnumerable <Package> >
            {
                { "references", modules }
            };

            this.ModulesInitialised = true;
            return(this.ModulePackages);
        }
Ejemplo n.º 3
0
        public DirectoryInfo GetDirectoryAsLocal(string remote_path, string local_path)
        {
            CallerInformation   here = this.Here();
            Stopwatch           sw   = new Stopwatch();
            string              dir_archive_filename = string.Format("_devaudit_{0}.tgz", this.GetTimestamp());
            SshCommandSpawanble cs = new SshCommandSpawanble(this.SshClient.CreateCommand(string.Format("tar -czf {0} -C {1} . && stat {0} || echo Failed", dir_archive_filename, remote_path)));

            sw.Start();
            ExpectNet.Session cmd_session = Expect.Spawn(cs, this.LineTerminator);
            List <IResult>    r           = cmd_session.Expect.RegexEither("Size:\\s+([0-9]+)", null, "Failed", null);

            sw.Stop();
            long dir_archive_size;

            cs.Dispose();
            if (r[0].IsMatch)
            {
                Match m = r[0].Result as Match;
                dir_archive_size = long.Parse(m.Groups[1].Value);
                Debug(here, "Archive file {0} created with size {1} bytes in {2} ms.", dir_archive_filename, dir_archive_size, sw.ElapsedMilliseconds);
            }
            else
            {
                Error(here, "Archive file {0} could not be created, command output: {1}", dir_archive_filename, r[1].Text);
                return(null);
            }
            sw.Restart();
            SshAuditFileInfo   dir_archive_file = new SshAuditFileInfo(this, dir_archive_filename);
            LocalAuditFileInfo lf = dir_archive_file.GetAsLocalFile();

            sw.Stop();
            if (lf == null)
            {
                Error(here, "Failed to get archive file {0} as local file.", dir_archive_filename);
                return(null);
            }
            Info("Downloaded archive file {0} in to local file {1} in {2} ms.", dir_archive_file.FullName, lf.FullName, sw.ElapsedMilliseconds);
            cs          = new SshCommandSpawanble(this.SshClient.CreateCommand(string.Format("rm {0} && echo Succeded || echo Failed", dir_archive_filename)));
            cmd_session = Expect.Spawn(cs, this.LineTerminator);
            r           = cmd_session.Expect.RegexEither("Succeded", null, "Failed", null);
            if (r[0].IsMatch)
            {
                Debug("Deleted archive file {0} from remote server.", dir_archive_file.FullName);
            }
            else
            {
                Debug("Failed to delete archive file {0} from remote server. It is safe to delete this file manually.", dir_archive_file.FullName);
            }
            sw.Restart();
            try
            {
                using (Stream fs = File.OpenRead(lf.FullName))
                {
                    ReaderFactory.Open(fs).WriteAllToDirectory(this.WorkDirectory.FullName,
                                                               new ExtractionOptions()
                    {
                        Overwrite       = true,
                        ExtractFullPath = true
                    });
                }

                /*
                 * ArchiveFactory.WriteToDirectory(lf.FullName, , new ExtractionOptions()
                 * {
                 *  Overwrite = true,
                 *
                 * });*/
                sw.Stop();
                Debug(here, "Extracting archive file {0} to work directory {1} in {2} ms.", lf.FullName, this.WorkDirectory.FullName, sw.ElapsedMilliseconds);
                return(this.WorkDirectory);
            }
            catch (Exception e)
            {
                Error(here, e);
                return(null);
            }
            finally
            {
                if (sw != null && sw.IsRunning)
                {
                    sw.Stop();
                }
            }
        }