Basic Implementation
This commit is contained in:
parent
6d9bbef54d
commit
4008943aeb
5 changed files with 152 additions and 2 deletions
47
SynologyCertificateExporter/Models/CertificateInfo.cs
Normal file
47
SynologyCertificateExporter/Models/CertificateInfo.cs
Normal file
|
@ -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<Service> 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; }
|
||||
}
|
||||
}
|
|
@ -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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
58
SynologyCertificateExporter/Services/CertificateExporter.cs
Normal file
58
SynologyCertificateExporter/Services/CertificateExporter.cs
Normal file
|
@ -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<string> Filenames = new List<string>()
|
||||
{
|
||||
"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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<CertificateInfo> Parse(string content)
|
||||
{
|
||||
var result = new List<CertificateInfo>();
|
||||
var parsed = (JObject)JsonConvert.DeserializeObject(content);
|
||||
|
||||
foreach (JProperty child in parsed.Children())
|
||||
{
|
||||
var info = child.First.ToObject<CertificateInfo>();
|
||||
info.Name = child.Name;
|
||||
|
||||
result.Add(info);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,4 +5,8 @@
|
|||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
Loading…
Add table
Reference in a new issue