diff --git a/FastGithub.HttpServer/CaCertInstallerOfLinux.cs b/FastGithub.HttpServer/CaCertInstallerOfLinux.cs index 12af24a..a716742 100644 --- a/FastGithub.HttpServer/CaCertInstallerOfLinux.cs +++ b/FastGithub.HttpServer/CaCertInstallerOfLinux.cs @@ -8,32 +8,40 @@ namespace FastGithub.HttpServer { abstract class CaCertInstallerOfLinux : ICaCertInstaller { - const string OS_RELEASE_FILE = "/etc/os-release"; + private readonly ILogger logger; /// /// 更新工具文件名 /// - public abstract string CertUpdateFileName { get; } + protected abstract string CertToolName { get; } /// /// 证书根目录 /// - public abstract string RootCertPath { get; } + protected abstract string CertStorePath { get; } + + + public CaCertInstallerOfLinux(ILogger logger) + { + this.logger = logger; + } /// /// 是否支持 /// /// - public abstract bool IsSupported(); + public bool IsSupported() + { + return OperatingSystem.IsLinux() && File.Exists(this.CertToolName); + } /// /// 安装ca证书 /// /// 证书文件路径 - /// - public void Install(string caCertFilePath, ILogger logger) + public void Install(string caCertFilePath) { - var destCertFilePath = Path.Combine(this.RootCertPath, "fastgithub.crt"); + var destCertFilePath = Path.Combine(this.CertStorePath, "fastgithub.crt"); if (File.Exists(destCertFilePath) && File.ReadAllBytes(caCertFilePath).SequenceEqual(File.ReadAllBytes(destCertFilePath))) { return; @@ -41,45 +49,21 @@ namespace FastGithub.HttpServer if (Environment.UserName != "root") { - logger.LogWarning($"无法自动安装CA证书{caCertFilePath},因为没有root权限"); + this.logger.LogWarning($"无法自动安装CA证书{caCertFilePath},因为没有root权限"); return; } try { - Directory.CreateDirectory(this.RootCertPath); + Directory.CreateDirectory(this.CertStorePath); File.Copy(caCertFilePath, destCertFilePath, overwrite: true); - Process.Start(this.CertUpdateFileName).WaitForExit(); + Process.Start(this.CertToolName).WaitForExit(); } catch (Exception ex) { File.Delete(destCertFilePath); - logger.LogWarning(ex.Message, "自动安装证书异常"); + this.logger.LogWarning(ex.Message, "自动安装证书异常"); } } - - - /// - /// 是否为某个发行版 - /// - /// - /// - protected static bool IsReleasName(string name) - { - if (File.Exists(OS_RELEASE_FILE) == false) - { - return false; - } - - foreach (var line in File.ReadAllLines(OS_RELEASE_FILE)) - { - if (line.StartsWith("NAME=") && line.Contains(name)) - { - return true; - } - } - - return false; - } } } \ No newline at end of file diff --git a/FastGithub.HttpServer/CaCertInstallerOfLinuxCentOS.cs b/FastGithub.HttpServer/CaCertInstallerOfLinuxCentOS.cs deleted file mode 100644 index 3ed7031..0000000 --- a/FastGithub.HttpServer/CaCertInstallerOfLinuxCentOS.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; - -namespace FastGithub.HttpServer -{ - sealed class CaCertInstallerOfLinuxCentOS : CaCertInstallerOfLinuxRedHat - { - /// - /// 是否支持 - /// - /// - public override bool IsSupported() - { - return OperatingSystem.IsLinux() && IsReleasName("CentOS"); - } - } -} \ No newline at end of file diff --git a/FastGithub.HttpServer/CaCertInstallerOfLinuxDebian.cs b/FastGithub.HttpServer/CaCertInstallerOfLinuxDebian.cs index 802fdf9..36af3fb 100644 --- a/FastGithub.HttpServer/CaCertInstallerOfLinuxDebian.cs +++ b/FastGithub.HttpServer/CaCertInstallerOfLinuxDebian.cs @@ -1,20 +1,16 @@ -using System; +using Microsoft.Extensions.Logging; namespace FastGithub.HttpServer { - class CaCertInstallerOfLinuxDebian : CaCertInstallerOfLinux + sealed class CaCertInstallerOfLinuxDebian : CaCertInstallerOfLinux { - public override string RootCertPath => "/usr/local/share/ca-certificates"; + protected override string CertToolName => "update-ca-certificates"; - public override string CertUpdateFileName => "update-ca-certificates"; + protected override string CertStorePath => "/usr/local/share/ca-certificates"; - /// - /// 是否支持 - /// - /// - public override bool IsSupported() + public CaCertInstallerOfLinuxDebian(ILogger logger) + : base(logger) { - return OperatingSystem.IsLinux() && IsReleasName("Debian"); } } } \ No newline at end of file diff --git a/FastGithub.HttpServer/CaCertInstallerOfLinuxRedHat.cs b/FastGithub.HttpServer/CaCertInstallerOfLinuxRedHat.cs index cfa0441..c98f53f 100644 --- a/FastGithub.HttpServer/CaCertInstallerOfLinuxRedHat.cs +++ b/FastGithub.HttpServer/CaCertInstallerOfLinuxRedHat.cs @@ -1,20 +1,16 @@ -using System; +using Microsoft.Extensions.Logging; namespace FastGithub.HttpServer { - class CaCertInstallerOfLinuxRedHat : CaCertInstallerOfLinux + sealed class CaCertInstallerOfLinuxRedHat : CaCertInstallerOfLinux { - public override string RootCertPath => "/etc/pki/ca-trust/source/anchors"; + protected override string CertToolName => "update-ca-trust"; - public override string CertUpdateFileName => "update-ca-trust"; + protected override string CertStorePath => "/etc/pki/ca-trust/source/anchors"; - /// - /// 是否支持 - /// - /// - public override bool IsSupported() + public CaCertInstallerOfLinuxRedHat(ILogger logger) + : base(logger) { - return OperatingSystem.IsLinux() && IsReleasName("Red Hat"); } } } \ No newline at end of file diff --git a/FastGithub.HttpServer/CaCertInstallerOfLinuxUbuntu.cs b/FastGithub.HttpServer/CaCertInstallerOfLinuxUbuntu.cs deleted file mode 100644 index fa0371a..0000000 --- a/FastGithub.HttpServer/CaCertInstallerOfLinuxUbuntu.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; - -namespace FastGithub.HttpServer -{ - sealed class CaCertInstallerOfLinuxUbuntu : CaCertInstallerOfLinuxDebian - { - /// - /// 是否支持 - /// - /// - public override bool IsSupported() - { - return OperatingSystem.IsLinux() && IsReleasName("Ubuntu"); - } - } -} \ No newline at end of file diff --git a/FastGithub.HttpServer/CaCertInstallerOfMacOS.cs b/FastGithub.HttpServer/CaCertInstallerOfMacOS.cs index 580d3b5..8aeb009 100644 --- a/FastGithub.HttpServer/CaCertInstallerOfMacOS.cs +++ b/FastGithub.HttpServer/CaCertInstallerOfMacOS.cs @@ -5,6 +5,13 @@ namespace FastGithub.HttpServer { sealed class CaCertInstallerOfMacOS : ICaCertInstaller { + private readonly ILogger logger; + + public CaCertInstallerOfMacOS(ILogger logger) + { + this.logger = logger; + } + /// /// 是否支持 /// @@ -18,10 +25,9 @@ namespace FastGithub.HttpServer /// 安装ca证书 /// /// 证书文件路径 - /// - public void Install(string caCertFilePath, ILogger logger) + public void Install(string caCertFilePath) { - logger.LogWarning($"请手动安装CA证书然后设置信任CA证书{caCertFilePath}"); + this.logger.LogWarning($"请手动安装CA证书然后设置信任CA证书{caCertFilePath}"); } } } diff --git a/FastGithub.HttpServer/CaCertInstallerOfWindows.cs b/FastGithub.HttpServer/CaCertInstallerOfWindows.cs index b4299e2..d6d152f 100644 --- a/FastGithub.HttpServer/CaCertInstallerOfWindows.cs +++ b/FastGithub.HttpServer/CaCertInstallerOfWindows.cs @@ -6,6 +6,13 @@ namespace FastGithub.HttpServer { sealed class CaCertInstallerOfWindows : ICaCertInstaller { + private readonly Logger logger; + + public CaCertInstallerOfWindows(Logger logger) + { + this.logger = logger; + } + /// /// 是否支持 /// @@ -19,8 +26,7 @@ namespace FastGithub.HttpServer /// 安装ca证书 /// /// 证书文件路径 - /// - public void Install(string caCertFilePath, ILogger logger) + public void Install(string caCertFilePath) { try { @@ -44,7 +50,7 @@ namespace FastGithub.HttpServer } catch (Exception) { - logger.LogWarning($"请手动安装CA证书{caCertFilePath}到“将所有的证书都放入下列存储”\\“受信任的根证书颁发机构”"); + this.logger.LogWarning($"请手动安装CA证书{caCertFilePath}到“将所有的证书都放入下列存储”\\“受信任的根证书颁发机构”"); } } } diff --git a/FastGithub.HttpServer/CertService.cs b/FastGithub.HttpServer/CertService.cs index 6b66f32..8b8574e 100644 --- a/FastGithub.HttpServer/CertService.cs +++ b/FastGithub.HttpServer/CertService.cs @@ -76,7 +76,7 @@ namespace FastGithub.HttpServer var installer = this.certInstallers.FirstOrDefault(item => item.IsSupported()); if (installer != null) { - installer.Install(this.CaCerFilePath, this.logger); + installer.Install(this.CaCerFilePath); } else { diff --git a/FastGithub.HttpServer/ICaCertInstaller.cs b/FastGithub.HttpServer/ICaCertInstaller.cs index 9fe3fe9..7e7e6f5 100644 --- a/FastGithub.HttpServer/ICaCertInstaller.cs +++ b/FastGithub.HttpServer/ICaCertInstaller.cs @@ -1,6 +1,4 @@ -using Microsoft.Extensions.Logging; - -namespace FastGithub.HttpServer +namespace FastGithub.HttpServer { /// /// CA证书安装器 @@ -17,7 +15,6 @@ namespace FastGithub.HttpServer /// 安装ca证书 /// /// 证书文件路径 - /// - void Install(string caCertFilePath,ILogger logger); + void Install(string caCertFilePath); } } diff --git a/FastGithub.HttpServer/ServiceCollectionExtensions.cs b/FastGithub.HttpServer/ServiceCollectionExtensions.cs index e622f28..a3ba4d5 100644 --- a/FastGithub.HttpServer/ServiceCollectionExtensions.cs +++ b/FastGithub.HttpServer/ServiceCollectionExtensions.cs @@ -21,9 +21,7 @@ namespace FastGithub .AddSingleton() .AddSingleton() .AddSingleton() - .AddSingleton() .AddSingleton() - .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton();