public static (PackageDatabaseTableVersionItem res, bool status) ToDatabaseFormat(this Command.AddverOption ori) { var obj = new PackageDatabaseTableVersionItem() { name = ori.Name, parent = ori.Parent, additional_desc = ori.AdditionalDesc, dependency = ori.Dependency, conflict = ori.Conflict, require_decompress = ori.RequireDecompress, hash = SignVerifyHelper.GetFileHash(ori.PackagePath) }; try { if (ori.Timestamp == "+") { obj.timestamp = DateTime.Now.ToUNIXTimestamp(); } else { obj.timestamp = long.Parse(ori.Timestamp); } obj.suit_os = (OSType)(int.Parse(ori.SuitOS)); obj.reverse_conflict = bool.Parse(ori.ReverseConflict); obj.internal_script = bool.Parse(ori.ReverseConflict); } catch { return(obj, false); } return(obj, true); }
public static bool Process(string command) { return(parser.ParseArguments <ExitOption, ConfigOption, SwitchOption, ClientOption, ImportOption, LsOption, ShowOption, AddpkgOption, EditpkgOption, DelpkgOption, AddverOption, EditverOption, DelverOption, HelpOption>( CommandSplitter.Split(command)) .MapResult( (ExitOption opt) => { if (opt.IsForce) { return true; } else { if (!General.IsMaintaining) { General.CoreTcpProcessor.StopListen(); Console.WriteLine("Waiting the release of resources..."); if (General.ManualResetEventList.Count != 0) { WaitHandle.WaitAll(General.ManualResetEventList.ToArray()); } } else { General.GeneralDatabase.Close(); } General.RecordFileManager.Close(); return true; } }, (ConfigOption opt) => { if (opt.Key is null) { foreach (var item in General.ConfigManager.Configuration.Keys) { Console.Write($"{item}: "); Console.Write($"{General.ConfigManager.Configuration[item]}\n"); } } else { if (opt.NewValue is null) { if (General.ConfigManager.Configuration.Keys.Contains(opt.Key)) { Console.WriteLine(General.ConfigManager.Configuration[opt.Key]); } } else { if (General.ConfigManager.Configuration.Keys.Contains(opt.Key)) { General.ConfigManager.Configuration[opt.Key] = opt.NewValue; General.ConfigManager.Save(); Console.WriteLine("New value has been applied"); } } } return false; }, (SwitchOption opt) => { if (!General.IsMaintaining) { General.CoreTcpProcessor.StopListen(); Console.WriteLine("Waiting the release of resources..."); if (General.ManualResetEventList.Count != 0) { WaitHandle.WaitAll(General.ManualResetEventList.ToArray()); } General.GeneralDatabase.Open(); General.IsMaintaining = true; ConsoleAssistance.WriteLine("Switch to maintain mode successfully.", ConsoleColor.Yellow); } else { General.GeneralDatabase.Close(); //force update verify code ConsoleAssistance.WriteLine("Updating verify code....", ConsoleColor.White); General.VerifyBytes = SignVerifyHelper.SignData(Information.WorkPath.Enter("package.db").Path, Information.WorkPath.Enter("pri.key").Path); General.ConfigManager.Configuration["VerifyBytes"] = Convert.ToBase64String(General.VerifyBytes); General.ConfigManager.Save(); General.CoreTcpProcessor.StartListen(); General.IsMaintaining = false; ConsoleAssistance.WriteLine("Switch to running mode successfully.", ConsoleColor.Yellow); } return false; }, (ClientOption opt) => { if (!CheckStatus(false)) { return false; } ConsoleAssistance.WriteLine($"Current client: {General.ManualResetEventList.Count}", ConsoleColor.Yellow); return false; }, (ImportOption opt) => { if (!CheckStatus(true)) { return false; } ConsoleAssistance.WriteLine("import is a dangerous command. It will load all script and run it without any error judgement! It couldn't be stopped before all of commands has been executed!", ConsoleColor.Yellow); var confirm = new Random().Next(100, 9999); ConsoleAssistance.WriteLine($"Type this random number to confirm your operation: {confirm}", ConsoleColor.Yellow); if (Console.ReadLine() == confirm.ToString()) { if (System.IO.File.Exists(opt.FilePath)) { ImportStack.AppendImportedCommands(opt.FilePath); } else { ConsoleAssistance.WriteLine("Cannot find specific file", ConsoleColor.Red); } } return false; }, (LsOption opt) => { if (!CheckStatus(true)) { return false; } if (opt.Condition is null) { PackageManager.Ls(General.GeneralDatabase, ""); } else { PackageManager.Ls(General.GeneralDatabase, opt.Condition); } return false; }, (ShowOption opt) => { if (!CheckStatus(true)) { return false; } PackageManager.Show(General.GeneralDatabase, opt.FullPackageName); return false; }, (AddpkgOption opt) => { if (!CheckStatus(true)) { return false; } PackageManager.AddPackage(General.GeneralDatabase, opt); return false; }, (EditpkgOption opt) => { if (!CheckStatus(true)) { return false; } PackageManager.EditPackage(General.GeneralDatabase, opt); return false; }, (DelpkgOption opt) => { if (!CheckStatus(true)) { return false; } PackageManager.RemovePackage(General.GeneralDatabase, opt.Name); return false; }, (AddverOption opt) => { if (!CheckStatus(true)) { return false; } PackageManager.AddVersion(General.GeneralDatabase, opt); return false; }, (EditverOption opt) => { if (!CheckStatus(true)) { return false; } PackageManager.EditVersion(General.GeneralDatabase, opt); return false; }, (DelverOption opt) => { if (!CheckStatus(true)) { return false; } PackageManager.RemoveVersion(General.GeneralDatabase, opt.Name); return false; }, (HelpOption opt) => { OutputHelp(); return false; }, errs => { ConsoleAssistance.WriteLine("Unknow command. Use help to find the correct command", ConsoleColor.Red); return false; })); }
static void Main(string[] args) { #region comfirm necessary file ConsoleAssistance.WriteLine("Server is checking necessary files...", ConsoleColor.Yellow); //package storage if (!Directory.Exists(Information.WorkPath.Enter("package").Path)) { Directory.CreateDirectory(Information.WorkPath.Enter("package").Path); } if (!Directory.Exists(Information.WorkPath.Enter("logs").Path)) { Directory.CreateDirectory(Information.WorkPath.Enter("logs").Path); } var databasePath = Information.WorkPath.Enter("package.db").Path; var rsaPublic = Information.WorkPath.Enter("pub.key").Path; var rsaPrivate = Information.WorkPath.Enter("pri.key").Path; //detect database if (!File.Exists(databasePath)) { ConsoleAssistance.WriteLine("No existing database file. A empty database file will be created.", ConsoleColor.Yellow); General.GeneralDatabase.Open(); General.GeneralDatabase.Close(); } //detect encrypt file if (!File.Exists(rsaPublic) || !File.Exists(rsaPrivate)) { //ensure there are no file. ConsoleAssistance.WriteLine("No existing RSA key. A new RSA key will be created.", ConsoleColor.Yellow); File.Delete(rsaPublic); File.Delete(rsaPrivate); SignVerifyHelper.GenerateKey(rsaPublic, rsaPrivate); } #endregion //init config and file pool General.ConfigManager = new ShareLib.Config("config.cfg", new Dictionary <string, string>() { { "IPv4Port", "3850" }, { "IPv6Port", "3851" }, { "VerifyBytes", "" }, { "EnableRecordFile", "True" } }); General.CoreFileReader = new FileReaderManager(); General.RecordFileManager = new RecordFile(bool.Parse(General.ConfigManager.Configuration["EnableRecordFile"])); General.CoreTcpProcessor = new TcpProcessor(int.Parse(General.ConfigManager.Configuration["IPv4Port"]), int.Parse(General.ConfigManager.Configuration["IPv6Port"])); //check parameter if (args.Length != 0 && args[0] == "maintain") { General.GeneralDatabase.Open(); General.IsMaintaining = true; ConsoleAssistance.WriteLine("Start with maintain mode", ConsoleColor.Yellow); } else { //force update verify code ConsoleAssistance.WriteLine("Updating verify code....", ConsoleColor.White); General.VerifyBytes = SignVerifyHelper.SignData(databasePath, rsaPrivate); General.ConfigManager.Configuration["VerifyBytes"] = Convert.ToBase64String(General.VerifyBytes); General.ConfigManager.Save(); General.CoreTcpProcessor.StartListen(); General.IsMaintaining = false; ConsoleAssistance.WriteLine("Start with running mode", ConsoleColor.Yellow); } //================================================read circle string command = ""; while (true) { ConsoleAssistance.Write($"BPMServer ({(General.IsMaintaining ? "Maintaining" : "Running")})> ", ConsoleColor.Green); command = ImportStack.ReadLine(); if (CommandProcessor.Process(command)) { break; } } ConsoleAssistance.WriteLine("Server exit!", ConsoleColor.Yellow); //Environment.Exit(0); }