using System;
using System.Collections.Generic;
using System.IO;
namespace Rainbow.Services;
/// <summary>Linux 发行版适配工具。处理 Ubuntu/CentOS/Debian 等发行版差异</summary>
public static class DistroAdapter
{
/// <summary>检测当前发行版</summary>
public static String DetectDistro()
{
try
{
// 优先读取 /etc/os-release
if (File.Exists("/etc/os-release"))
{
var lines = File.ReadAllLines("/etc/os-release");
foreach (var line in lines)
{
if (line.StartsWith("ID="))
{
var id = line.Substring(3).Trim('"', '\'');
if (id == "ubuntu") return "Ubuntu";
if (id == "debian") return "Debian";
if (id == "centos") return "CentOS";
if (id == "rhel") return "RHEL";
if (id == "fedora") return "Fedora";
return id;
}
}
}
// 回退到 lsb_release
if (File.Exists("/etc/lsb-release"))
{
var lines = File.ReadAllLines("/etc/lsb-release");
foreach (var line in lines)
if (line.StartsWith("DISTRIB_ID="))
return line.Substring(12).Trim('"', '\'');
}
}
catch { }
return "Unknown";
}
/// <summary>获取发行版对应的包管理器命令</summary>
public static String GetPackageManager(String distro)
{
return distro switch
{
"Ubuntu" or "Debian" => "apt",
"CentOS" or "RHEL" or "Fedora" => "dnf",
_ => "apt",
};
}
/// <summary>获取发行版对应的服务管理器</summary>
public static String GetServiceManager() => "systemctl";
/// <summary>获取发行版适配的包名映射</summary>
public static Dictionary<String, String> GetPackageNames(String distro)
{
var packages = new Dictionary<String, String>
{
["pppoe"] = "pppoe",
["dnsmasq"] = "dnsmasq",
["iptables"] = "iptables",
["wireguard"] = "wireguard-tools",
["openvpn"] = "openvpn",
["bind9"] = "bind9",
};
// CentOS/RHEL 使用不同包名
if (distro == "CentOS" || distro == "RHEL")
{
packages["dnsmasq"] = "dnsmasq";
packages["bind9"] = "bind";
}
return packages;
}
}
|