From a4a64531dbc9b7ef23ab8fa3fe471e567a590cd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E4=B9=9D?= <366193849@qq.com> Date: Sun, 14 Nov 2021 03:40:08 +0800 Subject: [PATCH] =?UTF-8?q?linux=E6=9C=8D=E5=8A=A1=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CaCertInstallerOfLinux.cs | 2 +- ...viceExtensions.cs => ServiceExtensions.cs} | 71 +++++++++++++++++-- README.html | 19 +++-- README.md | 19 +++-- 4 files changed, 95 insertions(+), 16 deletions(-) rename FastGithub/{WindowsServiceExtensions.cs => ServiceExtensions.cs} (51%) diff --git a/FastGithub.HttpServer/CaCertInstallerOfLinux.cs b/FastGithub.HttpServer/CaCertInstallerOfLinux.cs index 51f1d4a..128bddc 100644 --- a/FastGithub.HttpServer/CaCertInstallerOfLinux.cs +++ b/FastGithub.HttpServer/CaCertInstallerOfLinux.cs @@ -52,7 +52,7 @@ namespace FastGithub.HttpServer if (geteuid() != 0) { - this.logger.LogWarning($"无法自动安装CA证书{caCertFilePath},因为没有root权限"); + this.logger.LogWarning($"无法自动安装CA证书{caCertFilePath}:没有root权限"); return; } diff --git a/FastGithub/WindowsServiceExtensions.cs b/FastGithub/ServiceExtensions.cs similarity index 51% rename from FastGithub/WindowsServiceExtensions.cs rename to FastGithub/ServiceExtensions.cs index 917f948..85b3024 100644 --- a/FastGithub/WindowsServiceExtensions.cs +++ b/FastGithub/ServiceExtensions.cs @@ -4,9 +4,12 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using PInvoke; using System; +using System.Diagnostics; using System.IO; using System.Linq; +using System.Runtime.InteropServices; using System.Runtime.Versioning; +using System.Text; using System.Threading; namespace FastGithub @@ -14,7 +17,7 @@ namespace FastGithub /// /// IHostBuilder扩展 /// - static class WindowsServiceExtensions + static class ServiceExtensions { /// /// 控制命令 @@ -25,6 +28,10 @@ namespace FastGithub Stop, } + [SupportedOSPlatform("linux")] + [DllImport("libc", SetLastError = true)] + private static extern uint geteuid(); + /// /// 使用windows服务 /// @@ -48,11 +55,18 @@ namespace FastGithub /// public static void Run(this IHost host, bool singleton = true) { - if (OperatingSystem.IsWindows() && TryGetCommand(out var cmd)) + if (TryGetCommand(out var cmd) && (OperatingSystem.IsWindows() || OperatingSystem.IsLinux())) { try { - UseCommand(cmd); + if (OperatingSystem.IsWindows()) + { + UseCommandAtWindows(cmd); + } + else if (OperatingSystem.IsLinux()) + { + UseCommandAtLinux(cmd); + } } catch (Exception ex) { @@ -86,7 +100,7 @@ namespace FastGithub /// /// [SupportedOSPlatform("windows")] - private static void UseCommand(Command cmd) + private static void UseCommandAtWindows(Command cmd) { var binaryPath = Environment.GetCommandLineArgs().First(); var serviceName = Path.GetFileNameWithoutExtension(binaryPath); @@ -106,5 +120,54 @@ namespace FastGithub } } + /// + /// 应用控制指令 + /// + /// + [SupportedOSPlatform("linux")] + private static void UseCommandAtLinux(Command cmd) + { + if (geteuid() != 0) + { + throw new UnauthorizedAccessException("无法操作服务:没有root权限"); + } + + var binaryPath = Path.GetFullPath(Environment.GetCommandLineArgs().First()); + var serviceName = Path.GetFileNameWithoutExtension(binaryPath); + var serviceFilePath = $"/etc/systemd/system/{serviceName}.service"; + + if (cmd == Command.Start) + { + var serviceBuilder = new StringBuilder() + .AppendLine("[Unit]") + .AppendLine($"Description={serviceName}") + .AppendLine() + .AppendLine("[Service]") + .AppendLine("Type=notify") + .AppendLine($"User={Environment.UserName}") + .AppendLine($"ExecStart={binaryPath}") + .AppendLine($"WorkingDirectory={Path.GetDirectoryName(binaryPath)}") + .AppendLine() + .AppendLine("[Install]") + .AppendLine("WantedBy=multi-user.target"); + File.WriteAllText(serviceFilePath, serviceBuilder.ToString()); + + Process.Start("chcon", $"--type=bin_t {binaryPath}").WaitForExit(); // SELinux + Process.Start("systemctl", "daemon-reload").WaitForExit(); + Process.Start("systemctl", $"start {serviceName}.service").WaitForExit(); + Process.Start("systemctl", $"enable {serviceName}.service").WaitForExit(); + } + else if (cmd == Command.Stop) + { + Process.Start("systemctl", $"stop {serviceName}.service").WaitForExit(); + Process.Start("systemctl", $"disable {serviceName}.service").WaitForExit(); + + if (File.Exists(serviceFilePath)) + { + File.Delete(serviceFilePath); + } + Process.Start("systemctl", "daemon-reload").WaitForExit(); + } + } } } diff --git a/README.html b/README.html index e4577a5..bb74ba5 100644 --- a/README.html +++ b/README.html @@ -374,25 +374,34 @@ code {
  • Q群2 742376932
  • 2 部署方式

    -

    2.1 windows-x64

    +

    2.1 windows-x64桌面

    +

    2.2 windows-x64服务

    + -

    2.2 linux-x64

    +

    2.3 linux-x64终端

    -

    2.3 macOS-x64

    +

    2.4 linux-x64服务

    + +

    2.5 macOS-x64

    -

    2.4 docker-compose一键部署

    +

    2.6 docker-compose一键部署