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