Пример #1
0
        /// <summary>
        ///     Protects the stub using original project settings replace the current output with the protected stub.
        /// </summary>
        /// <param name="context">The working context.</param>
        /// <param name="fileName">The result file name.</param>
        /// <param name="module">The stub module.</param>
        /// <param name="snKey">The strong name key.</param>
        /// <param name="prot">The packer protection that applies to the stub.</param>
        protected void ProtectStub(ConfuserContext context, string fileName, byte[] module, StrongNameKey snKey, Protection prot = null)
        {
            string tmpDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            string outDir = Path.Combine(tmpDir, Path.GetRandomFileName());

            Directory.CreateDirectory(tmpDir);

            for (int i = 0; i < context.OutputModules.Count; i++)
            {
                string path = Path.GetFullPath(Path.Combine(tmpDir, context.OutputPaths[i]));
                var    dir  = Path.GetDirectoryName(path);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                File.WriteAllBytes(path, context.OutputModules[i]);
            }
            File.WriteAllBytes(Path.Combine(tmpDir, fileName), module);

            var proj = new ConfuserProject();

            proj.Seed = context.Project.Seed;
            foreach (Rule rule in context.Project.Rules)
            {
                proj.Rules.Add(rule);
            }
            proj.Add(new ProjectModule
            {
                Path = fileName
            });
            proj.BaseDirectory   = tmpDir;
            proj.OutputDirectory = outDir;
            foreach (var path in context.Project.ProbePaths)
            {
                proj.ProbePaths.Add(path);
            }
            proj.ProbePaths.Add(context.Project.BaseDirectory);

            PluginDiscovery discovery = null;

            if (prot != null)
            {
                var rule = new Rule
                {
                    Preset  = ProtectionPreset.None,
                    Inherit = true,
                    Pattern = "true"
                };
                rule.Add(new SettingItem <Protection>
                {
                    Id     = prot.Id,
                    Action = SettingItemAction.Add
                });
                proj.Rules.Add(rule);
                discovery = new PackerDiscovery(prot);
            }

            try
            {
                ConfuserEngine.Run(new ConfuserParameters
                {
                    Logger          = new PackerLogger(context.Logger),
                    PluginDiscovery = discovery,
                    Marker          = new PackerMarker(snKey),
                    Project         = proj,
                    PackerInitiated = true
                }, context.token).Wait();
            }
            catch (AggregateException ex)
            {
                context.Logger.Error("Failed to protect packer stub.");
                throw new ConfuserException(ex);
            }

            context.OutputModules = new[] { File.ReadAllBytes(Path.Combine(outDir, fileName)) };
            context.OutputPaths   = new[] { fileName };
        }
Пример #2
0
 protected override void GetPluginsInternal(ConfuserContext context, IList <Protection> protections, IList <Packer> packers, IList <ConfuserComponent> components)
 {
     base.GetPluginsInternal(context, protections, packers, components);
     protections.Add(prot);
 }
Пример #3
0
 /// <summary>
 ///     Executes the packer.
 /// </summary>
 /// <param name="context">The working context.</param>
 /// <param name="parameters">The parameters of packer.</param>
 protected internal abstract void Pack(ConfuserContext context, ProtectionParameters parameters);
Пример #4
0
 /// <summary>
 ///     Execute the specified pipeline stage with pre-processing and post-processing.
 /// </summary>
 /// <param name="stage">The pipeline stage.</param>
 /// <param name="func">The stage function.</param>
 /// <param name="targets">The target list of the stage.</param>
 /// <param name="context">The working context.</param>
 internal void ExecuteStage(PipelineStage stage, Action <ConfuserContext> func, Func <IList <ISDnlibDef> > targets, ConfuserContext context)
 {
     foreach (ProtectionPhase pre in preStage[stage])
     {
         context.CheckCancellation();
         context.Logger.DebugFormat("Executing '{0}' phase...", pre.Name);
         pre.Execute(context, new ProtectionParameters(pre.Parent, Filter(context, targets(), pre)));
     }
     context.CheckCancellation();
     func(context);
     context.CheckCancellation();
     foreach (ProtectionPhase post in postStage[stage])
     {
         context.Logger.DebugFormat("Executing '{0}' phase...", post.Name);
         post.Execute(context, new ProtectionParameters(post.Parent, Filter(context, targets(), post)));
         context.CheckCancellation();
     }
 }
 /// <summary>
 ///     Gets the protection parameters of the specified target.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="target">The protection target.</param>
 /// <returns>The parameters.</returns>
 public static ProtectionSettings GetParameters(
     ConfuserContext context, ISDnlibDef target)
 {
     return(context.Annotations.Get <ProtectionSettings>(target, ParametersKey));
 }
 /// <summary>
 ///     Sets the protection parameters of the specified target.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="target">The protection target.</param>
 /// <param name="parameters">The parameters.</param>
 public static void SetParameters(
     ConfuserContext context, ISDnlibDef target, ProtectionSettings parameters)
 {
     context.Annotations.Set(target, ParametersKey, parameters);
 }