From 4008943aeb342187dd10720570b223211122a3dc Mon Sep 17 00:00:00 2001 From: ErXBout Date: Fri, 22 Oct 2021 08:09:55 +0200 Subject: [PATCH] Basic Implementation --- .../Models/CertificateInfo.cs | 47 +++++++++++++++ SynologyCertificateExporter/Program.cs | 19 +++++- .../Services/CertificateExporter.cs | 58 +++++++++++++++++++ .../Services/SynologyInfoJsonParser.cs | 26 +++++++++ .../SynologyCertificateExporter.csproj | 4 ++ 5 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 SynologyCertificateExporter/Models/CertificateInfo.cs create mode 100644 SynologyCertificateExporter/Services/CertificateExporter.cs create mode 100644 SynologyCertificateExporter/Services/SynologyInfoJsonParser.cs diff --git a/SynologyCertificateExporter/Models/CertificateInfo.cs b/SynologyCertificateExporter/Models/CertificateInfo.cs new file mode 100644 index 0000000..3b1a62d --- /dev/null +++ b/SynologyCertificateExporter/Models/CertificateInfo.cs @@ -0,0 +1,47 @@ +using Newtonsoft.Json; +using System.Collections.Generic; + +namespace SynologyCertificateExporter.Models +{ + internal class CertificateInfo + { + [JsonIgnore] + internal string Name { get; set; } + + [JsonProperty("desc")] + internal string Description { get; set; } + + [JsonProperty("services")] + internal List Services { get; set; } + + [JsonProperty("user_deletable")] + internal bool UserDeletable { get; set; } + } + + internal class Service + { + [JsonProperty("display_name")] + internal string DisplayName { get; set; } + + [JsonProperty("isPkg")] + internal bool IsPkg { get; set; } + + [JsonProperty("owner")] + internal string Owner { get; set; } + + [JsonProperty("service")] + internal string ServiceName { get; set; } + + [JsonProperty("subscriber")] + internal string Subscriber { get; set; } + + [JsonProperty("display_name_i18n")] + internal string DisplayNameI18n { get; set; } + + [JsonProperty("multiple_cert")] + internal bool? MultipleCert { get; set; } + + [JsonProperty("user_setable")] + internal bool? UserSetable { get; set; } + } +} diff --git a/SynologyCertificateExporter/Program.cs b/SynologyCertificateExporter/Program.cs index a0d47d0..4450d02 100644 --- a/SynologyCertificateExporter/Program.cs +++ b/SynologyCertificateExporter/Program.cs @@ -1,4 +1,5 @@ -using System; +using SynologyCertificateExporter.Services; +using System; namespace SynologyCertificateExporter { @@ -6,7 +7,21 @@ namespace SynologyCertificateExporter { static void Main(string[] args) { - Console.WriteLine("Hello World!"); + if (args.Length == 0) + { + Console.WriteLine("Error, you have to specify the outputDirectory in first argument"); + return; + } + + var outputDirectory = args[0]; + Console.WriteLine($"Setting {outputDirectory} as outputDirectory"); + + var synoCertificatePath = "/usr/syno/etc/certificate/_archive"; + + var exporter = new CertificateExporter(synoCertificatePath, outputDirectory); + exporter.Export(); + + Console.WriteLine("Finished!"); } } } diff --git a/SynologyCertificateExporter/Services/CertificateExporter.cs b/SynologyCertificateExporter/Services/CertificateExporter.cs new file mode 100644 index 0000000..81e7d2d --- /dev/null +++ b/SynologyCertificateExporter/Services/CertificateExporter.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace SynologyCertificateExporter.Services +{ + internal class CertificateExporter + { + private readonly string _synoDirectory; + private readonly string _outputDirectory; + + private List Filenames = new List() + { + "cert.pem", + "chain.pem", + "privkey.pem" + }; + + internal CertificateExporter(string synoDirectory, string outputDirectory) + { + _synoDirectory = synoDirectory; + _outputDirectory = outputDirectory; + } + + internal void Export() + { + var infoJsonPath = _synoDirectory + "/INFO"; + var fileContent = File.ReadAllText(infoJsonPath); + + var certificates = SynologyInfoJsonParser.Parse(fileContent); + + CreateDirectoryIfNotExisting(_outputDirectory); + + foreach (var certificate in certificates) + { + var exportDirectoryName = !string.IsNullOrEmpty(certificate.Description) ? certificate.Description : certificate.Name; + var exportDirectory = _outputDirectory + "/" + exportDirectoryName; + + CreateDirectoryIfNotExisting(exportDirectory); + + foreach (var fileName in Filenames) + { + var cpFrom = _synoDirectory + "/" + certificate.Name + "/" + fileName; + var cpTo = exportDirectory + "/" + fileName; + + Console.WriteLine($"Copy file from {cpFrom} to {cpTo}"); + File.Copy(cpFrom, cpTo); + } + } + } + + private void CreateDirectoryIfNotExisting(string path) + { + if (!Directory.Exists(path)) + Directory.CreateDirectory(path); + } + } +} diff --git a/SynologyCertificateExporter/Services/SynologyInfoJsonParser.cs b/SynologyCertificateExporter/Services/SynologyInfoJsonParser.cs new file mode 100644 index 0000000..010c0b4 --- /dev/null +++ b/SynologyCertificateExporter/Services/SynologyInfoJsonParser.cs @@ -0,0 +1,26 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using SynologyCertificateExporter.Models; +using System.Collections.Generic; + +namespace SynologyCertificateExporter.Services +{ + internal class SynologyInfoJsonParser + { + internal static List Parse(string content) + { + var result = new List(); + var parsed = (JObject)JsonConvert.DeserializeObject(content); + + foreach (JProperty child in parsed.Children()) + { + var info = child.First.ToObject(); + info.Name = child.Name; + + result.Add(info); + } + + return result; + } + } +} diff --git a/SynologyCertificateExporter/SynologyCertificateExporter.csproj b/SynologyCertificateExporter/SynologyCertificateExporter.csproj index 2082704..b84e63f 100644 --- a/SynologyCertificateExporter/SynologyCertificateExporter.csproj +++ b/SynologyCertificateExporter/SynologyCertificateExporter.csproj @@ -5,4 +5,8 @@ net5.0 + + + +