Download SSL Certificates

Sort Subtasks of Markdown files
This commit is contained in:
Mike Phares 2024-07-30 14:00:28 -07:00
parent aa6a3405fd
commit 440b0d2290
4 changed files with 113 additions and 32 deletions

19
.vscode/launch.json vendored
View File

@ -13,15 +13,16 @@
"args": [ "args": [
"s", "s",
"X", "X",
"L:/", "D:/5-Other-Small/Proxmox/Day-Helper-2024-07-28",
"Day-Helper-2024-07-11", "Day-Helper-2024-07-28",
"config", "ddns.net",
"refs/remotes/gitea", "dashkiosk,gitea,ha,immich,music,odoo,pihole,phares,pgadmin,quartz,slideshow,umbrel,vaultwarden",
"http", "C:/Program Files/Git/mingw64/bin/openssl.exe",
"gitea", "12345",
"https://51f44975c8734522b2dec36c6d9a116276c6bded@gitea.ddns.net/phares3757/", "-----BEGIN CERTIFICATE",
".git", "END CERTIFICATE-----",
"master" "certutil -addstore",
"Root"
], ],
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
"console": "integratedTerminal", "console": "integratedTerminal",

View File

@ -6,6 +6,7 @@ namespace File_Folder_Helper.Day;
internal static partial class Helper20240623 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 record Record(int? CodeInsidersLine, string File, string[] Lines, int? StopLine, int? SubTasksLine);
private static List<Record> GetRecords(string sourceDirectory, string searchPattern, string codeInsiders, string subTasks) private static List<Record> GetRecords(string sourceDirectory, string searchPattern, string codeInsiders, string subTasks)
@ -53,15 +54,18 @@ internal static partial class Helper20240623
return results; return results;
} }
private static ReadOnlyCollection<string> GetSubTasks(string subTasks, string[] tasks, bool? foundDone, string fallbackLine, string checkFile) private static ReadOnlyCollection<SubTaskLine> GetSubTasks(string subTasks, string[] tasks, bool? foundDone, string fallbackLine, FileInfo fileInfo)
{ {
List<string> results = []; List<SubTaskLine> results = [];
string line;
bool doneValue;
string? h1 = null; string? h1 = null;
bool foundSubTasks = false; bool foundSubTasks = false;
int tasksZeroLength = tasks[0].Length; int tasksZeroLength = tasks[0].Length;
string[] lines = File.ReadAllLines(checkFile); string[] lines = File.ReadAllLines(fileInfo.FullName);
foreach (string line in lines) for (int i = 0; i < lines.Length; i++)
{ {
line = lines[i];
if (line.StartsWith("# ")) if (line.StartsWith("# "))
h1 = line[2..]; h1 = line[2..];
if (!foundSubTasks && line == subTasks) if (!foundSubTasks && line == subTasks)
@ -70,36 +74,40 @@ internal static partial class Helper20240623
continue; continue;
if (line.Length <= tasksZeroLength || !line.StartsWith(tasks[0]) || line[tasksZeroLength] is not ' ' and not 'x' || line[tasksZeroLength + 1] != ']') if (line.Length <= tasksZeroLength || !line.StartsWith(tasks[0]) || line[tasksZeroLength] is not ' ' and not 'x' || line[tasksZeroLength + 1] != ']')
continue; 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) if (h1 is null)
results.Insert(0, fallbackLine); results.Add(new(fallbackLine, doneValue, fileInfo.LastWriteTime.Ticks, Line: null));
else 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); return new(results);
} }
internal static void UpdateSubTasksInMarkdownFiles(ILogger<Worker> logger, List<string> args) internal static void UpdateSubTasksInMarkdownFiles(ILogger<Worker> logger, List<string> args)
{ {
int lineCheck; int lineCheck;
bool doneValue;
bool? foundDone; bool? foundDone;
string checkFile; FileInfo fileInfo;
string[] newLines;
string[] segments; string[] segments;
List<string> lines; List<string> lines;
string[] indexLines;
string fallbackLine; string fallbackLine;
string[] indexLines;
string checkDirectory; string checkDirectory;
string done = args[7]; string done = args[7];
List<string> indexFiles; List<string> indexFiles;
string subTasks = args[3]; string subTasks = args[3];
List<string> newLines = [];
List<string> oldLines = []; List<string> oldLines = [];
string indexFile = args[5]; string indexFile = args[5];
string searchPattern = args[2]; string searchPattern = args[2];
string directoryFilter = args[8]; string directoryFilter = args[8];
ReadOnlyCollection<string> useLines;
string[] tasks = args[6].Split(','); string[] tasks = args[6].Split(',');
string codeInsiders = $"{args[4]} \""; string codeInsiders = $"{args[4]} \"";
List<SubTaskLine> allSubTaskLines = [];
ReadOnlyCollection<SubTaskLine> subTaskLines;
string sourceDirectory = Path.GetFullPath(args[0]); string sourceDirectory = Path.GetFullPath(args[0]);
List<Record> records = GetRecords(sourceDirectory, searchPattern, codeInsiders, subTasks); List<Record> 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) 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; foundDone = null;
newLines.Clear();
oldLines.Clear(); oldLines.Clear();
allSubTaskLines.Clear();
indexLines = File.ReadAllLines(indexFiles[0]); indexLines = File.ReadAllLines(indexFiles[0]);
checkDirectory = Path.GetDirectoryName(indexFiles[0]) ?? throw new Exception(); checkDirectory = Path.GetDirectoryName(indexFiles[0]) ?? throw new Exception();
for (int i = 0; i < indexLines.Length; i++) for (int i = 0; i < indexLines.Length; i++)
@ -145,33 +153,38 @@ internal static partial class Helper20240623
if (indexLines[i] == done) if (indexLines[i] == done)
foundDone = true; foundDone = true;
segments = indexLines[i].Split(tasks[1]); segments = indexLines[i].Split(tasks[1]);
doneValue = foundDone is not null && foundDone.Value;
if (segments.Length > 2 || !segments[0].StartsWith(tasks[0])) if (segments.Length > 2 || !segments[0].StartsWith(tasks[0]))
continue; continue;
fallbackLine = foundDone is null || !foundDone.Value ? $"- [ ] {segments[0][tasks[0].Length..]}" : $"- [x] {segments[0][tasks[0].Length..]}"; 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])); fileInfo = new(Path.GetFullPath(Path.Combine(checkDirectory, segments[1][..^1])));
if (!File.Exists(checkFile)) if (!fileInfo.Exists)
{ {
newLines.Add(fallbackLine); allSubTaskLines.Add(new(fallbackLine, doneValue, Ticks: null, Line: null));
continue; continue;
} }
useLines = GetSubTasks(subTasks, tasks, foundDone, fallbackLine, checkFile); subTaskLines = GetSubTasks(subTasks, tasks, doneValue, fallbackLine, fileInfo);
newLines.AddRange(useLines); for (int j = subTaskLines.Count - 1; j >= 0; j--)
allSubTaskLines.Add(subTaskLines[j]);
} }
if (newLines.Count == 0) if (allSubTaskLines.Count == 0)
continue; continue;
lineCheck = 0; lineCheck = 0;
newLines.Insert(0, string.Empty);
for (int i = record.SubTasksLine.Value + 1; i < record.StopLine.Value - 1; i++) for (int i = record.SubTasksLine.Value + 1; i < record.StopLine.Value - 1; i++)
oldLines.Add(record.Lines[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]) if (newLines[i] != record.Lines[record.SubTasksLine.Value + 1 + i])
continue; continue;
lineCheck++; lineCheck++;
} }
if (lineCheck == newLines.Count) if (lineCheck == newLines.Length)
continue; continue;
} }
checkDirectory = Path.Combine(checkDirectory, DateTime.Now.Ticks.ToString()); checkDirectory = Path.Combine(checkDirectory, DateTime.Now.Ticks.ToString());
@ -183,8 +196,9 @@ internal static partial class Helper20240623
lines.RemoveAt(i); lines.RemoveAt(i);
if (record.StopLine.Value == record.Lines.Length && lines[^1].Length == 0) if (record.StopLine.Value == record.Lines.Length && lines[^1].Length == 0)
lines.RemoveAt(lines.Count - 1); 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 + i, newLines[i]);
lines.Insert(record.SubTasksLine.Value + 1, string.Empty);
File.WriteAllLines(record.File, lines); File.WriteAllLines(record.File, lines);
} }
} }

View File

@ -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<Worker> logger, List<string> args)
{
string file;
Process? process;
string[] segments;
string standardError;
string standardOutput;
string argumentSegment;
string store = args[9];
string domain = args[2];
List<string> 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);
}
}

View File

@ -83,6 +83,8 @@ internal static class HelperDay
Day.Helper20240718.JsonToMarkdown(logger, args); Day.Helper20240718.JsonToMarkdown(logger, args);
else if (args[1] == "Day-Helper-2024-07-24") else if (args[1] == "Day-Helper-2024-07-24")
Day.Helper20240724.CopyDirectories(logger, args); Day.Helper20240724.CopyDirectories(logger, args);
else if (args[1] == "Day-Helper-2024-07-28")
Day.Helper20240728.DownloadSslCertificates(logger, args);
else else
throw new Exception(appSettings.Company); throw new Exception(appSettings.Company);
} }