/// <summary> /// 所有服务控制中心 /// </summary> public static void Init() { //基础服务 BaseHost.Init(false); //特巡报告服务 ReportHost.Init(false); //用户信息服务 UserHost.Init(false); //Web管理后台特巡报告服务 PatrolHost.Init(false); }
private bool CheckUrlReferrer(int userId) { if (RequiredCheckUrlReferrer) { if (Request.UrlReferrer == null) { return(false); } var hostBLL = new UserHost(); return(hostBLL.Exists(userId, Request.UrlReferrer.Host)); } return(true); }
static bool CheckUrlReferrer(int uid) { if (Bank.Utility.RequiredCheckUrlReferrer) { if (HttpContext.Current.Request.UrlReferrer == null) { return(false); } var hostBLL = new UserHost(); return(hostBLL.Exists(uid, HttpContext.Current.Request.UrlReferrer.Host)); } return(true); }
static void Main(string[] args) { NormalizedPath userHostPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); userHostPath = userHostPath.AppendPart("CKli"); LogFile.RootLogPath = userHostPath.AppendPart("Logs"); var logConfig = new GrandOutputConfiguration().AddHandler( new CK.Monitoring.Handlers.TextFileConfiguration() { Path = "Text" }); GrandOutput.EnsureActiveDefault(logConfig); var monitor = new ActivityMonitor(); monitor.Output.RegisterClient(new ColoredActivityMonitorConsoleClient()); monitor.MinimalFilter = LogFilter.Debug; IBasicApplicationLifetime appLife = new FakeApplicationLifetime(); try { using (var host = new UserHost(appLife, userHostPath)) { host.Initialize(monitor); OpenKeyVault(monitor, host); if (host.UserKeyVault.IsKeyVaultOpened) { host.Initialize(monitor); } DumpWorlds(host.WorldStore.ReadWorlds(monitor)); InteractiveRun(monitor, host); } } catch (Exception ex) { monitor.Fatal(ex); Console.ReadLine(); } }
static void OpenKeyVault(IActivityMonitor monitor, UserHost host) { string prompt; if (host.UserKeyVault.KeyVaultFileExists) { Console.WriteLine("Your personal KeyVault should be opened."); prompt = "Enter the passphrase to open it: "; } else { Console.WriteLine($"Personal KeyVault not found at: '{host.UserKeyVault.KeyVaultPath}'"); Console.WriteLine("It will contain encrypted secrets required by the different operations on all stacks."); prompt = "It should be created. Enter its passphrase (and memorize it!): "; } Console.Write(prompt); var s = ReadSecret(); if (s != null) { host.UserKeyVault.OpenKeyVault(monitor, s); } else { Console.WriteLine("KeyVault opening cancelled."); } if (host.UserKeyVault.IsKeyVaultOpened) { Console.WriteLine("KeyVault opened."); } else { Console.WriteLine("KeyVault not opened. You may open it later by using the 'secret' command."); Console.WriteLine("Some features will be unavailable or fail miserably when secrets are unavailable."); } Console.WriteLine("Note:"); Console.WriteLine(" - You can always use 'secret set NAME' or 'secret clear NAME' commands at anytime to upsert or remove secrets."); Console.WriteLine(" - You can also change the KeyVault passphrase thanks to 'secret save'."); }
/// <summary> /// Create a <see cref="TestUniverse"/> in a given folder. /// </summary> /// <param name="path"> The path where the <see cref="TestUniverse"/> will be. The Directory will be deleted when disposed.</param> /// <returns></returns> public static TestUniverse Create(IActivityMonitor m, NormalizedPath path) { m.Info($"Creating TestUniverse from {path}."); NormalizedPath ckliPath = path.AppendPart(_ckliMapping); if (Directory.Exists(ckliPath)) { ReplaceInDirectoriesPaths( ckliPath: ckliPath, oldString: GitWorldStore.CleanPathDirName(PlaceHolderString), newString: GitWorldStore.CleanPathDirName(path) ); } PlaceHolderSwapEverything(m, path, PlaceHolderString, path); var userHost = new UserHost(new FakeApplicationLifetime(), ckliPath); var output = new TestUniverse(m, path, userHost); userHost.Initialize(m); userHost.WorldStore.DeleteStackDefinition(m, "CK"); userHost.WorldStore.DeleteStackDefinition(m, "CK-Build"); return(output); }
/// <summary> /// Instantiate a new <see cref="TestUniverse"/>. /// </summary> /// <param name="tempPath">Path of the TestHost.</param> /// <param name="userHost">The UserHost instantied with this path.</param> TestUniverse(IActivityMonitor m, NormalizedPath tempPath, UserHost userHost) { _m = m; UniversePath = tempPath; UserHost = userHost; }
public void Dispose() { UserHost.Dispose(); FileHelper.RawDeleteLocalDirectory(_m, UniversePath); }
static void InteractiveRun(IActivityMonitor monitor, UserHost host) { const string textCommands = "[run <globbed command name> | list [<globbed command name>] | cls | secret [clear NAME|set NAME|save] | exit]"; for (; ;) { if (host.ApplicationLifetime.CanCancelStopRequest) { host.ApplicationLifetime.CancelStopRequest(); } bool hasWorld = host.WorldSelector.CurrentWorld != null; Console.WriteLine(); if (hasWorld) { Console.WriteLine($"> World: {host.WorldSelector.CurrentWorld.WorldName.FullName} - {textCommands }"); } else { Console.WriteLine($"> {textCommands}"); } Console.Write("> "); string rep = Console.ReadLine().Trim(); if (rep.Length == 0) { if (hasWorld) { host.CommandRegister["World/DumpWorldState"].Execute(monitor, null); } else { DumpWorlds(host.WorldStore.ReadWorlds(monitor)); } continue; } if (rep == "cls") { Console.Clear(); continue; } if (rep.StartsWith("secret")) { if (!host.UserKeyVault.IsKeyVaultOpened) { Console.WriteLine("Your personal KeyVault is not opened."); OpenKeyVault(monitor, host); } rep = rep.Substring(6).Trim(); if (rep.Length > 0) { var two = rep.Split(' ').Where(t => t.Length > 0).ToArray(); if (two.Length == 1) { if (two[0].Equals("save", StringComparison.OrdinalIgnoreCase)) { Console.Write($"Enter new passphrase (empty to cancel): "); var s = ReadSecret(); if (s != null) { host.UserKeyVault.SaveKeyVault(monitor, s); } } else { two = null; } } else if (two.Length == 2) { if (two[0].Equals("clear", StringComparison.OrdinalIgnoreCase)) { host.UserKeyVault.UpdateSecret(monitor, two[1], null); } else if (two[0].Equals("set", StringComparison.OrdinalIgnoreCase)) { Console.Write($"Enter '{two[1]}' secret (empty to cancel): "); var s = ReadSecret(); if (s != null) { host.UserKeyVault.UpdateSecret(monitor, two[1], s); } } else { two = null; } } if (two == null) { Console.WriteLine("Invalid syntax. Expected: 'clear NAME', 'set NAME' or 'save'."); } } DumpSecrets(host.UserKeyVault); continue; } if (rep.StartsWith("list")) { rep = rep.Substring(4).Trim(); IEnumerable <ICommandHandler> commands; if (rep.Length == 0) { Console.Write($"Available Commands:"); commands = host.CommandRegister.GetAllCommands(); } else { Console.Write($"Available Commands matching '{rep}':"); commands = host.CommandRegister.GetCommands(rep); } bool atLeastOne = false; foreach (var c in commands.OrderBy(c => c.UniqueName)) { if (!atLeastOne) { Console.WriteLine(); atLeastOne = true; } Console.Write(" "); Console.WriteLine(c.UniqueName); } if (!atLeastOne) { Console.WriteLine(" (none)"); } continue; } if (rep.StartsWith("run ")) { rep = rep.Substring(4).Trim(); RunCommand(monitor, host, rep); continue; } if (rep == "exit") { return; } if (!hasWorld && Int32.TryParse(rep, out var idx)) { host.WorldSelector.Open(monitor, idx.ToString()); continue; } Console.WriteLine("Unrecognized command."); } }
static void RunCommand(IActivityMonitor m, UserHost host, string rep) { var handlers = host.CommandRegister.GetCommands(rep).ToList(); var handlersBySig = handlers .GroupBy(c => c.PayloadSignature) .ToList(); if (handlersBySig.Count == 0) { m.Warn($"Pattern '{rep}' has no match."); return; } if (handlersBySig.Count != 1) { using (m.OpenWarn($"Pattern '{rep}' matches require {handlersBySig.Count} different payloads.")) { foreach (var c in handlersBySig) { m.Warn($"{c.Key ?? "<No payload>"}: {c.Select( n => n.UniqueName.ToString() ).Concatenate() }"); } } return; } var firstHandler = handlers[0]; bool?parallelRun = firstHandler.ParallelRun; bool?globalConfirm = firstHandler.ConfirmationRequired; bool?backgroundRun = firstHandler.BackgroundRun; if (handlers.Any(h => h.ConfirmationRequired != globalConfirm.Value)) { m.Error("Different ConfirmationRequired found among commands. All commands must have the same ConfirmationRequired "); return; } if (handlers.Any(h => h.BackgroundRun != backgroundRun)) { m.Error("Different BackgroundCommandAttribute found among commands. All commands must have the same attributes and identically configured attribute."); return; } if (handlers.Any(h => h.ParallelRun != parallelRun)) { m.Error("Different ParallelCommandAttribute found among commands. All commands must have the same attributes and identically configured attribute."); return; } var payload = firstHandler.CreatePayload(); if (!ReadPayload(m, payload)) { return; } if (parallelRun == null) { Console.WriteLine("The selected command(s) can run in parallel. Do you want to run in parallel ?"); parallelRun = YesOrNo(); } if (backgroundRun == null) { Console.WriteLine("The selected command(s) can run in background. Do you want to run in background ?"); backgroundRun = YesOrNo(); } if (globalConfirm == true) { Console.WriteLine("Confirm execution of:"); foreach (var h in handlers) { Console.WriteLine(h.UniqueName); } DumpPayLoad(payload); if (!YesOrNo()) { return; } } if (backgroundRun.Value) { throw new NotImplementedException(); } if (!parallelRun.Value) { foreach (var h in handlers) { if (host.ApplicationLifetime.StopRequested(m)) { break; } h.Execute(m, payload); } } else { var tasks = handlers.Select(h => Task.Run(() => { ActivityMonitor monitor = new ActivityMonitor(); h.Execute(monitor, payload); })).ToArray(); Task.WaitAll(tasks); } }