|
@@ -8,32 +8,40 @@ namespace FastGithub.HttpServer
|
|
{
|
|
{
|
|
abstract class CaCertInstallerOfLinux : ICaCertInstaller
|
|
abstract class CaCertInstallerOfLinux : ICaCertInstaller
|
|
{
|
|
{
|
|
- const string OS_RELEASE_FILE = "/etc/os-release";
|
|
|
|
|
|
+ private readonly ILogger logger;
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 更新工具文件名
|
|
/// 更新工具文件名
|
|
/// </summary>
|
|
/// </summary>
|
|
- public abstract string CertUpdateFileName { get; }
|
|
|
|
|
|
+ protected abstract string CertToolName { get; }
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 证书根目录
|
|
/// 证书根目录
|
|
/// </summary>
|
|
/// </summary>
|
|
- public abstract string RootCertPath { get; }
|
|
|
|
|
|
+ protected abstract string CertStorePath { get; }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public CaCertInstallerOfLinux(ILogger logger)
|
|
|
|
+ {
|
|
|
|
+ this.logger = logger;
|
|
|
|
+ }
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 是否支持
|
|
/// 是否支持
|
|
/// </summary>
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
- public abstract bool IsSupported();
|
|
|
|
|
|
+ public bool IsSupported()
|
|
|
|
+ {
|
|
|
|
+ return OperatingSystem.IsLinux() && File.Exists(this.CertToolName);
|
|
|
|
+ }
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 安装ca证书
|
|
/// 安装ca证书
|
|
/// </summary>
|
|
/// </summary>
|
|
/// <param name="caCertFilePath">证书文件路径</param>
|
|
/// <param name="caCertFilePath">证书文件路径</param>
|
|
- /// <param name="logger"></param>
|
|
|
|
- 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)))
|
|
if (File.Exists(destCertFilePath) && File.ReadAllBytes(caCertFilePath).SequenceEqual(File.ReadAllBytes(destCertFilePath)))
|
|
{
|
|
{
|
|
return;
|
|
return;
|
|
@@ -41,45 +49,21 @@ namespace FastGithub.HttpServer
|
|
|
|
|
|
if (Environment.UserName != "root")
|
|
if (Environment.UserName != "root")
|
|
{
|
|
{
|
|
- logger.LogWarning($"无法自动安装CA证书{caCertFilePath},因为没有root权限");
|
|
|
|
|
|
+ this.logger.LogWarning($"无法自动安装CA证书{caCertFilePath},因为没有root权限");
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- Directory.CreateDirectory(this.RootCertPath);
|
|
|
|
|
|
+ Directory.CreateDirectory(this.CertStorePath);
|
|
File.Copy(caCertFilePath, destCertFilePath, overwrite: true);
|
|
File.Copy(caCertFilePath, destCertFilePath, overwrite: true);
|
|
- Process.Start(this.CertUpdateFileName).WaitForExit();
|
|
|
|
|
|
+ Process.Start(this.CertToolName).WaitForExit();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
catch (Exception ex)
|
|
{
|
|
{
|
|
File.Delete(destCertFilePath);
|
|
File.Delete(destCertFilePath);
|
|
- logger.LogWarning(ex.Message, "自动安装证书异常");
|
|
|
|
|
|
+ this.logger.LogWarning(ex.Message, "自动安装证书异常");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
- /// <summary>
|
|
|
|
- /// 是否为某个发行版
|
|
|
|
- /// </summary>
|
|
|
|
- /// <param name="name"></param>
|
|
|
|
- /// <returns></returns>
|
|
|
|
- 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;
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|