1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.PluginInfrastructure.Manager;
|
---|
6 | using System.IO;
|
---|
7 | using System.ComponentModel;
|
---|
8 | using HeuristicLab.PluginInfrastructure.UpdateLocationReference;
|
---|
9 | using System.Security;
|
---|
10 | using System.Security.Permissions;
|
---|
11 | using System.Security.Policy;
|
---|
12 | using System.Reflection;
|
---|
13 | using System.Diagnostics;
|
---|
14 |
|
---|
15 | namespace HeuristicLab.PluginInfrastructure.Sandboxing {
|
---|
16 | public class SandboxManager {
|
---|
17 |
|
---|
18 | private static StrongName CreateStrongName(Assembly assembly) {
|
---|
19 | if (assembly == null)
|
---|
20 | throw new ArgumentNullException("assembly");
|
---|
21 |
|
---|
22 | AssemblyName assemblyName = assembly.GetName();
|
---|
23 | Trace.Assert(assemblyName != null, "Could not get assembly name");
|
---|
24 |
|
---|
25 | // get the public key blob
|
---|
26 | byte[] publicKey = assemblyName.GetPublicKey();
|
---|
27 | if (publicKey == null || publicKey.Length == 0)
|
---|
28 | throw new InvalidOperationException("Assembly is not strongly named");
|
---|
29 |
|
---|
30 | StrongNamePublicKeyBlob keyBlob = new StrongNamePublicKeyBlob(publicKey);
|
---|
31 |
|
---|
32 | // and create the StrongName
|
---|
33 | return new StrongName(keyBlob, assemblyName.Name, assemblyName.Version);
|
---|
34 | }
|
---|
35 |
|
---|
36 | #region ISandboxManager Members
|
---|
37 |
|
---|
38 | public static AppDomain CreateAndInitSandbox(string name) {
|
---|
39 | return CreateAndInitSandbox(name, Enumerable.Empty<byte[]>());
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 | public static AppDomain CreateAndInitSandbox(string name, IEnumerable<byte[]> files) {
|
---|
44 | PermissionSet pset;
|
---|
45 |
|
---|
46 | #region permission set for sandbox
|
---|
47 | // Uncomment code for sandboxed appdomain
|
---|
48 | //pset = new PermissionSet(PermissionState.None);
|
---|
49 | //pset.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
|
---|
50 | //pset.AddPermission(new ReflectionPermission(ReflectionPermissionFlag.MemberAccess));
|
---|
51 | //FileIOPermission fPerm = new FileIOPermission(PermissionState.None);
|
---|
52 |
|
---|
53 | //foreach (IPluginDescription plugin in ApplicationManager.Manager.Plugins) {
|
---|
54 | // fPerm.AddPathList(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, plugin.Files.ToArray());
|
---|
55 | //}
|
---|
56 |
|
---|
57 | //pset.AddPermission(fPerm);
|
---|
58 | #endregion
|
---|
59 |
|
---|
60 | #region permission set of unrestricted appdomain
|
---|
61 | // unrestricted appdomain
|
---|
62 | pset = new PermissionSet(PermissionState.Unrestricted);
|
---|
63 | #endregion
|
---|
64 |
|
---|
65 | AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
|
---|
66 | //setup.PrivateBinPath = pluginDir;
|
---|
67 | setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
|
---|
68 | AppDomain applicationDomain = AppDomain.CreateDomain(name, AppDomain.CurrentDomain.Evidence, setup, pset, CreateStrongName(Assembly.GetExecutingAssembly()));
|
---|
69 | Type applicationManagerType = typeof(ApplicationManager);
|
---|
70 | ApplicationManager applicationManager =
|
---|
71 | (ApplicationManager)applicationDomain.CreateInstanceAndUnwrap(applicationManagerType.Assembly.FullName, applicationManagerType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null, null);
|
---|
72 | ApplicationDescription[] apps = ApplicationManager.Manager.Applications.Cast<ApplicationDescription>().ToArray();
|
---|
73 | PluginDescription[] plugins = ApplicationManager.Manager.Plugins.Cast<PluginDescription>().ToArray();
|
---|
74 | applicationManager.PrepareApplicationDomain(apps, plugins);
|
---|
75 | if (files != null && files.Count() > 0)
|
---|
76 | applicationManager.LoadAssemblies(files);
|
---|
77 | return applicationDomain;
|
---|
78 | }
|
---|
79 |
|
---|
80 | #endregion
|
---|
81 | }
|
---|
82 | }
|
---|