using Microsoft.Extensions.Logging; using System; using System.Diagnostics; using System.IO; using System.Linq; namespace FastGithub.HttpServer { abstract class CaCertInstallerOfLinux : ICaCertInstaller { private readonly ILogger logger; /// /// 更新工具文件名 /// protected abstract string CertToolName { get; } /// /// 证书根目录 /// protected abstract string CertStorePath { get; } public CaCertInstallerOfLinux(ILogger logger) { this.logger = logger; } /// /// 是否支持 /// /// public bool IsSupported() { return OperatingSystem.IsLinux() && File.Exists(this.CertToolName); } /// /// 安装ca证书 /// /// 证书文件路径 public void Install(string caCertFilePath) { var destCertFilePath = Path.Combine(this.CertStorePath, "fastgithub.crt"); if (File.Exists(destCertFilePath) && File.ReadAllBytes(caCertFilePath).SequenceEqual(File.ReadAllBytes(destCertFilePath))) { return; } if (Environment.UserName != "root") { this.logger.LogWarning($"无法自动安装CA证书{caCertFilePath},因为没有root权限"); return; } try { Directory.CreateDirectory(this.CertStorePath); File.Copy(caCertFilePath, destCertFilePath, overwrite: true); Process.Start(this.CertToolName).WaitForExit(); } catch (Exception ex) { File.Delete(destCertFilePath); this.logger.LogWarning(ex.Message, "自动安装证书异常"); } } } }