diff --git a/.vscode/launch.json b/.vscode/launch.json index e231309..4941fcd 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -13,15 +13,16 @@ "args": [ "s", "X", - "L:/", - "Day-Helper-2024-07-11", - "config", - "refs/remotes/gitea", - "http", - "gitea", - "https://51f44975c8734522b2dec36c6d9a116276c6bded@gitea.ddns.net/phares3757/", - ".git", - "master" + "D:/5-Other-Small/Proxmox/Day-Helper-2024-07-28", + "Day-Helper-2024-07-28", + "ddns.net", + "dashkiosk,gitea,ha,immich,music,odoo,pihole,phares,pgadmin,quartz,slideshow,umbrel,vaultwarden", + "C:/Program Files/Git/mingw64/bin/openssl.exe", + "12345", + "-----BEGIN CERTIFICATE", + "END CERTIFICATE-----", + "certutil -addstore", + "Root" ], "cwd": "${workspaceFolder}", "console": "integratedTerminal", diff --git a/Day/2024-Q2/Helper-2024-06-23.cs b/Day/2024-Q2/Helper-2024-06-23.cs index 8323499..527a8f6 100644 --- a/Day/2024-Q2/Helper-2024-06-23.cs +++ b/Day/2024-Q2/Helper-2024-06-23.cs @@ -6,6 +6,7 @@ namespace File_Folder_Helper.Day; internal static partial class Helper20240623 { + private record SubTaskLine(string Text, bool Done, long? Ticks, int? Line); private record Record(int? CodeInsidersLine, string File, string[] Lines, int? StopLine, int? SubTasksLine); private static List GetRecords(string sourceDirectory, string searchPattern, string codeInsiders, string subTasks) @@ -53,15 +54,18 @@ internal static partial class Helper20240623 return results; } - private static ReadOnlyCollection GetSubTasks(string subTasks, string[] tasks, bool? foundDone, string fallbackLine, string checkFile) + private static ReadOnlyCollection GetSubTasks(string subTasks, string[] tasks, bool? foundDone, string fallbackLine, FileInfo fileInfo) { - List results = []; + List results = []; + string line; + bool doneValue; string? h1 = null; bool foundSubTasks = false; int tasksZeroLength = tasks[0].Length; - string[] lines = File.ReadAllLines(checkFile); - foreach (string line in lines) + string[] lines = File.ReadAllLines(fileInfo.FullName); + for (int i = 0; i < lines.Length; i++) { + line = lines[i]; if (line.StartsWith("# ")) h1 = line[2..]; if (!foundSubTasks && line == subTasks) @@ -70,36 +74,40 @@ internal static partial class Helper20240623 continue; if (line.Length <= tasksZeroLength || !line.StartsWith(tasks[0]) || line[tasksZeroLength] is not ' ' and not 'x' || line[tasksZeroLength + 1] != ']') continue; - results.Add($" {line}"); + doneValue = foundDone is not null && foundDone.Value; + results.Add(new($" {line}", doneValue, fileInfo.LastWriteTime.Ticks, i)); } + doneValue = foundDone is not null && foundDone.Value; if (h1 is null) - results.Insert(0, fallbackLine); + results.Add(new(fallbackLine, doneValue, fileInfo.LastWriteTime.Ticks, Line: null)); else - results.Insert(0, foundDone is null || !foundDone.Value ? $"- [ ] {h1}" : $"- [x] {h1}"); + results.Add(new(foundDone is null || !foundDone.Value ? $"- [ ] {fileInfo.LastWriteTime.Ticks} ~~~ {h1}" : $"- [x] {fileInfo.LastWriteTime.Ticks} ~~~ {h1}", doneValue, fileInfo.LastWriteTime.Ticks, Line: 0)); return new(results); } internal static void UpdateSubTasksInMarkdownFiles(ILogger logger, List args) { int lineCheck; + bool doneValue; bool? foundDone; - string checkFile; + FileInfo fileInfo; + string[] newLines; string[] segments; List lines; - string[] indexLines; string fallbackLine; + string[] indexLines; string checkDirectory; string done = args[7]; List indexFiles; string subTasks = args[3]; - List newLines = []; List oldLines = []; string indexFile = args[5]; string searchPattern = args[2]; string directoryFilter = args[8]; - ReadOnlyCollection useLines; string[] tasks = args[6].Split(','); string codeInsiders = $"{args[4]} \""; + List allSubTaskLines = []; + ReadOnlyCollection subTaskLines; string sourceDirectory = Path.GetFullPath(args[0]); List records = GetRecords(sourceDirectory, searchPattern, codeInsiders, subTasks); foreach (Record record in from l in records orderby l.SubTasksLine is null, l.CodeInsidersLine is null select l) @@ -136,8 +144,8 @@ internal static partial class Helper20240623 } } foundDone = null; - newLines.Clear(); oldLines.Clear(); + allSubTaskLines.Clear(); indexLines = File.ReadAllLines(indexFiles[0]); checkDirectory = Path.GetDirectoryName(indexFiles[0]) ?? throw new Exception(); for (int i = 0; i < indexLines.Length; i++) @@ -145,33 +153,38 @@ internal static partial class Helper20240623 if (indexLines[i] == done) foundDone = true; segments = indexLines[i].Split(tasks[1]); + doneValue = foundDone is not null && foundDone.Value; if (segments.Length > 2 || !segments[0].StartsWith(tasks[0])) continue; fallbackLine = foundDone is null || !foundDone.Value ? $"- [ ] {segments[0][tasks[0].Length..]}" : $"- [x] {segments[0][tasks[0].Length..]}"; - checkFile = Path.GetFullPath(Path.Combine(checkDirectory, segments[1][..^1])); - if (!File.Exists(checkFile)) + fileInfo = new(Path.GetFullPath(Path.Combine(checkDirectory, segments[1][..^1]))); + if (!fileInfo.Exists) { - newLines.Add(fallbackLine); + allSubTaskLines.Add(new(fallbackLine, doneValue, Ticks: null, Line: null)); continue; } - useLines = GetSubTasks(subTasks, tasks, foundDone, fallbackLine, checkFile); - newLines.AddRange(useLines); + subTaskLines = GetSubTasks(subTasks, tasks, doneValue, fallbackLine, fileInfo); + for (int j = subTaskLines.Count - 1; j >= 0; j--) + allSubTaskLines.Add(subTaskLines[j]); } - if (newLines.Count == 0) + if (allSubTaskLines.Count == 0) continue; lineCheck = 0; - newLines.Insert(0, string.Empty); for (int i = record.SubTasksLine.Value + 1; i < record.StopLine.Value - 1; i++) oldLines.Add(record.Lines[i]); - if (newLines.Count == oldLines.Count) + if (allSubTaskLines.Any(l => l.Ticks is null)) + newLines = (from l in allSubTaskLines select l.Text).ToArray(); + else + newLines = (from l in allSubTaskLines orderby l.Done descending, l.Ticks, l.Line select l.Text).ToArray(); + if (newLines.Length == oldLines.Count) { - for (int i = 0; i < newLines.Count; i++) + for (int i = 0; i < newLines.Length; i++) { if (newLines[i] != record.Lines[record.SubTasksLine.Value + 1 + i]) continue; lineCheck++; } - if (lineCheck == newLines.Count) + if (lineCheck == newLines.Length) continue; } checkDirectory = Path.Combine(checkDirectory, DateTime.Now.Ticks.ToString()); @@ -183,8 +196,9 @@ internal static partial class Helper20240623 lines.RemoveAt(i); if (record.StopLine.Value == record.Lines.Length && lines[^1].Length == 0) lines.RemoveAt(lines.Count - 1); - for (int i = 0; i < newLines.Count; i++) + for (int i = 0; i < newLines.Length; i++) lines.Insert(record.SubTasksLine.Value + 1 + i, newLines[i]); + lines.Insert(record.SubTasksLine.Value + 1, string.Empty); File.WriteAllLines(record.File, lines); } } diff --git a/Day/2024-Q3/Helper-2024-07-28.cs b/Day/2024-Q3/Helper-2024-07-28.cs new file mode 100644 index 0000000..b78b930 --- /dev/null +++ b/Day/2024-Q3/Helper-2024-07-28.cs @@ -0,0 +1,64 @@ +using Microsoft.Extensions.Logging; +using System.Diagnostics; + +namespace File_Folder_Helper.Day; + +internal static partial class Helper20240728 +{ + + internal static void DownloadSslCertificates(ILogger logger, List args) + { + string file; + Process? process; + string[] segments; + string standardError; + string standardOutput; + string argumentSegment; + string store = args[9]; + string domain = args[2]; + List lines = []; + string logSegment = args[8]; + string endCertificate = args[7]; + string beginCertificate = args[6]; + int waitForExit = int.Parse(args[5]); + string[] subdomains = args[3].Split(','); + string sourceDirectory = Path.GetFullPath(args[0]); + ProcessStartInfo processStartInfo = new() + { + CreateNoWindow = true, + RedirectStandardError = true, + RedirectStandardOutput = true, + UseShellExecute = false, + FileName = args[4], + WorkingDirectory = sourceDirectory + }; + foreach (string subdomain in subdomains) + { + argumentSegment = $"{subdomain}.{domain}:443 -servername {subdomain}.{domain}"; + processStartInfo.Arguments = $"s_client -connect {subdomain}.{domain}:443 -servername {subdomain}.{domain}"; + process = Process.Start(processStartInfo); + if (process is null) + continue; + _ = process.WaitForExit(waitForExit); + process.Kill(entireProcessTree: true); + standardOutput = process.StandardOutput.ReadToEnd(); + if (!standardOutput.Contains(beginCertificate) || !standardOutput.Contains(endCertificate)) + { + standardError = process.StandardError.ReadToEnd(); + logger.LogWarning($"Error: {subdomain}{Environment.NewLine}{standardOutput}{Environment.NewLine}{standardError}"); + continue; + } + segments = standardOutput.Split(beginCertificate); + if (segments.Length != 2) + break; + segments = segments[1].Split(endCertificate); + if (segments.Length != 2) + break; + lines.Add($"{logSegment} \"{store}\" {subdomain}.{domain}.cert"); + file = Path.Combine(sourceDirectory, $"{subdomain}.{domain}.cert"); + File.WriteAllText(file, $"{beginCertificate}{segments[0]}{endCertificate}{Environment.NewLine}"); + } + File.WriteAllLines(Path.Combine(sourceDirectory, $"{DateTime.Now.Ticks}.txt"), lines); + } + +} \ No newline at end of file diff --git a/Day/HelperDay.cs b/Day/HelperDay.cs index 6e57626..b5cfbe4 100644 --- a/Day/HelperDay.cs +++ b/Day/HelperDay.cs @@ -83,6 +83,8 @@ internal static class HelperDay Day.Helper20240718.JsonToMarkdown(logger, args); else if (args[1] == "Day-Helper-2024-07-24") Day.Helper20240724.CopyDirectories(logger, args); + else if (args[1] == "Day-Helper-2024-07-28") + Day.Helper20240728.DownloadSslCertificates(logger, args); else throw new Exception(appSettings.Company); }