diff --git a/Protest/Database/DeviceConfiguration.cs b/Protest/Database/DeviceConfiguration.cs index 791e64d2..4a2e03fc 100644 --- a/Protest/Database/DeviceConfiguration.cs +++ b/Protest/Database/DeviceConfiguration.cs @@ -39,13 +39,23 @@ public static byte[] Save(HttpListenerContext ctx, Dictionary pa return Data.CODE_INVALID_ARGUMENT.Array; } - StreamReader reader = new StreamReader(ctx.Request.InputStream, ctx.Request.ContentEncoding); - string payload = reader.ReadToEnd(); + try { + if (!Directory.Exists(Data.DIR_CONFIG)) { + Directory.CreateDirectory(Data.DIR_CONFIG); + } + + StreamReader reader = new StreamReader(ctx.Request.InputStream, ctx.Request.ContentEncoding); + string payload = reader.ReadToEnd(); - byte[] plain = Encoding.UTF8.GetBytes(FormatRouterOs(payload)); - byte[] gzip = Http.Cache.GZip(plain); - byte[] cipher = Cryptography.Encrypt(gzip, Configuration.DB_KEY, Configuration.DB_KEY_IV); - File.WriteAllBytes($"{Data.DIR_CONFIG}{Data.DELIMITER}{file}", cipher); + byte[] plain = Encoding.UTF8.GetBytes(FormatRouterOs(payload)); + byte[] gzip = Http.Cache.GZip(plain); + byte[] cipher = Cryptography.Encrypt(gzip, Configuration.DB_KEY, Configuration.DB_KEY_IV); + File.WriteAllBytes($"{Data.DIR_CONFIG}{Data.DELIMITER}{file}", cipher); + } + catch (Exception ex){ + Logger.Error(ex); + return Data.CODE_FAILED.ToArray(); + } Logger.Action(origin, $"Modify the device conficuration for file: {file}"); diff --git a/Protest/Front/watchdog.js b/Protest/Front/watchdog.js index 84c1ce45..fd1053b7 100644 --- a/Protest/Front/watchdog.js +++ b/Protest/Front/watchdog.js @@ -585,7 +585,7 @@ class Watchdog extends Window { this.Seek(); } catch (ex) { - setTimeout(()=>this.ConfirmBox(ex, true, "mono/error.svg"), 200); + setTimeout(()=>this.ConfirmBox(ex, true, "mono/error.svg"), 250); } cancelButton.onclick(); diff --git a/Protest/Misc/Backup.cs b/Protest/Misc/Backup.cs index 00cc7f73..2b2a9d15 100644 --- a/Protest/Misc/Backup.cs +++ b/Protest/Misc/Backup.cs @@ -16,13 +16,17 @@ static Backup() { } internal static byte[] Create(Dictionary parameters) { - if (parameters is null || !parameters.TryGetValue("name", out string name)) { + if (parameters is null || !parameters.TryGetValue("name", out string name) || String.IsNullOrEmpty(name)) { name = $"backup-{DateTime.UtcNow.ToString(Data.DATE_FORMAT_FILE)}"; } + Console.WriteLine(name); + foreach (char c in Path.GetInvalidFileNameChars()) { name = name.Replace (c, '_'); } + + Console.WriteLine(name); try { DirectoryInfo backupDirectory = new DirectoryInfo(Data.DIR_BACKUP); diff --git a/Protest/Misc/Data.cs b/Protest/Misc/Data.cs index a99e2159..97c65f62 100644 --- a/Protest/Misc/Data.cs +++ b/Protest/Misc/Data.cs @@ -1,5 +1,6 @@ using System.IO; using System.Reflection; +using System.Security.Cryptography; using System.Text; namespace Protest; @@ -68,20 +69,15 @@ public static class Data { public static readonly string FILE_CONFIG = $"{DIR_ROOT}{DELIMITER}protest.cfg"; + private static readonly byte[] DEBIT_TEMPLATE = "The aforementioned items are company property and must be returned in pristine condition at the conclusion of the contract. Any loss, damage, or failure to return these items will result in their value being deducted from the employee's salary."u8.ToArray(); + public static void InitializeDirectories() { DirectoryInfo[] dirs = new DirectoryInfo[] { new DirectoryInfo(DIR_LOG), new DirectoryInfo(DIR_LASTSEEN), new DirectoryInfo(DIR_LIFELINE), - new DirectoryInfo(DIR_WATCHDOG), - new DirectoryInfo(DIR_DOCUMENTATION), new DirectoryInfo(DIR_DEBIT), - new DirectoryInfo(DIR_DEBIT_SHORT), - new DirectoryInfo(DIR_DEBIT_LONG), - new DirectoryInfo(DIR_DEBIT_RETURNED), - new DirectoryInfo(DIR_DEBIT_TEMPLATE), - new DirectoryInfo(DIR_CONFIG), - new DirectoryInfo(DIR_DATA), + //new DirectoryInfo(DIR_DEBIT_TEMPLATE), new DirectoryInfo(DIR_DEVICES), new DirectoryInfo(DIR_USERS) }; @@ -96,6 +92,11 @@ public static void InitializeDirectories() { Logger.Error(ex); } } + + if (!Directory.Exists(DIR_DEBIT_TEMPLATE)) { + Directory.CreateDirectory(DIR_DEBIT_TEMPLATE); + File.WriteAllBytes($"{DIR_DEBIT_TEMPLATE}{DELIMITER}Company.txt", DEBIT_TEMPLATE); + } } public static string SizeToString(long size) { diff --git a/Protest/Tasks/Watchdog.cs b/Protest/Tasks/Watchdog.cs index b10e711e..178dffba 100644 --- a/Protest/Tasks/Watchdog.cs +++ b/Protest/Tasks/Watchdog.cs @@ -467,6 +467,10 @@ public static byte[] Create(HttpListenerContext ctx, Dictionary byte[] content = JsonSerializer.SerializeToUtf8Bytes(watcher, watcherSerializerOptions); + if (!Directory.Exists(Data.DIR_WATCHDOG)) { + Directory.CreateDirectory(Data.DIR_WATCHDOG); + } + File.WriteAllBytes($"{Data.DIR_WATCHDOG}{Data.DELIMITER}{file}", content); DirectoryInfo dirInfo = new DirectoryInfo($"{Data.DIR_WATCHDOG}{Data.DELIMITER}{file}_"); diff --git a/Protest/Tools/DebitNotes.cs b/Protest/Tools/DebitNotes.cs index ad4bbaa4..0faa2f44 100644 --- a/Protest/Tools/DebitNotes.cs +++ b/Protest/Tools/DebitNotes.cs @@ -320,6 +320,10 @@ public static byte[] Return(Dictionary parameters, string origin public static byte[] ListTemplate() { DirectoryInfo dir = new DirectoryInfo(Data.DIR_DEBIT_TEMPLATE); + if (!dir.Exists) { + return "{}"u8.ToArray(); + } + StringBuilder builder = new StringBuilder(); builder.Append('['); FileInfo[] files = dir.GetFiles(); diff --git a/Protest/Tools/Documentation.cs b/Protest/Tools/Documentation.cs index 0e29ed3e..04edcc3e 100644 --- a/Protest/Tools/Documentation.cs +++ b/Protest/Tools/Documentation.cs @@ -16,9 +16,10 @@ public static byte[] List(Dictionary parameters) { string[] keywordsArray = keywords.Split(' ').Where(o=>o.Length > 0).ToArray(); DirectoryInfo dir = new DirectoryInfo(Data.DIR_DOCUMENTATION); - if (!dir.Exists) - return Data.CODE_FILE_NOT_FOUND.Array; - + if (!dir.Exists) { + return "[]"u8.ToArray(); + } + List files = dir.GetFiles().ToList(); files.Sort((a, b) => String.Compare(a.Name, b.Name)); @@ -167,18 +168,23 @@ public static byte[] Create(HttpListenerContext ctx, string origin) { keywords.Sort(); idx = 0; - if (keywords.Count > 1) - while (idx < keywords.Count - 1) - if (keywords[idx + 1].StartsWith(keywords[idx])) + if (keywords.Count > 1) { + while (idx < keywords.Count - 1) { + if (keywords[idx + 1].StartsWith(keywords[idx])) { keywords.RemoveAt(idx); - else + } + else { idx++; + } + } + } - lock (mutex) + lock (mutex) { try { DirectoryInfo dir = new DirectoryInfo(Data.DIR_DOCUMENTATION); - if (!dir.Exists) + if (!dir.Exists) { dir.Create(); + } FileInfo html = new FileInfo($"{Data.DIR_DOCUMENTATION}\\{filename}.html.gz"); @@ -190,6 +196,7 @@ public static byte[] Create(HttpListenerContext ctx, string origin) { catch { return Data.CODE_FILE_NOT_FOUND.Array; } + } Logger.Action(origin, $"Create documentation: {filename}");