linux服务支持

This commit is contained in:
老九 2021-11-14 03:40:08 +08:00
parent eafcae4b59
commit a4a64531db
4 changed files with 95 additions and 16 deletions

View File

@ -52,7 +52,7 @@ namespace FastGithub.HttpServer
if (geteuid() != 0) if (geteuid() != 0)
{ {
this.logger.LogWarning($"无法自动安装CA证书{caCertFilePath},因为没有root权限"); this.logger.LogWarning($"无法自动安装CA证书{caCertFilePath}没有root权限");
return; return;
} }

View File

@ -4,9 +4,12 @@ using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using PInvoke; using PInvoke;
using System; using System;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Versioning; using System.Runtime.Versioning;
using System.Text;
using System.Threading; using System.Threading;
namespace FastGithub namespace FastGithub
@ -14,7 +17,7 @@ namespace FastGithub
/// <summary> /// <summary>
/// IHostBuilder扩展 /// IHostBuilder扩展
/// </summary> /// </summary>
static class WindowsServiceExtensions static class ServiceExtensions
{ {
/// <summary> /// <summary>
/// 控制命令 /// 控制命令
@ -25,6 +28,10 @@ namespace FastGithub
Stop, Stop,
} }
[SupportedOSPlatform("linux")]
[DllImport("libc", SetLastError = true)]
private static extern uint geteuid();
/// <summary> /// <summary>
/// 使用windows服务 /// 使用windows服务
/// </summary> /// </summary>
@ -48,11 +55,18 @@ namespace FastGithub
/// <param name="singleton"></param> /// <param name="singleton"></param>
public static void Run(this IHost host, bool singleton = true) 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 try
{ {
UseCommand(cmd); if (OperatingSystem.IsWindows())
{
UseCommandAtWindows(cmd);
}
else if (OperatingSystem.IsLinux())
{
UseCommandAtLinux(cmd);
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -86,7 +100,7 @@ namespace FastGithub
/// </summary> /// </summary>
/// <param name="cmd"></param> /// <param name="cmd"></param>
[SupportedOSPlatform("windows")] [SupportedOSPlatform("windows")]
private static void UseCommand(Command cmd) private static void UseCommandAtWindows(Command cmd)
{ {
var binaryPath = Environment.GetCommandLineArgs().First(); var binaryPath = Environment.GetCommandLineArgs().First();
var serviceName = Path.GetFileNameWithoutExtension(binaryPath); var serviceName = Path.GetFileNameWithoutExtension(binaryPath);
@ -106,5 +120,54 @@ namespace FastGithub
} }
} }
/// <summary>
/// 应用控制指令
/// </summary>
/// <param name="cmd"></param>
[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();
}
}
} }
} }

View File

@ -374,25 +374,34 @@ code {
<li>Q群2 <a href="https://qm.qq.com/cgi-bin/qm/qr?k=6BBJ1nrJwe1o1E4-NJfwSOP-C4sMGc4q&amp;jump_from=webapi">742376932</a></li> <li>Q群2 <a href="https://qm.qq.com/cgi-bin/qm/qr?k=6BBJ1nrJwe1o1E4-NJfwSOP-C4sMGc4q&amp;jump_from=webapi">742376932</a></li>
</ul> </ul>
<h3 id="2-%E9%83%A8%E7%BD%B2%E6%96%B9%E5%BC%8F">2 部署方式</h3> <h3 id="2-%E9%83%A8%E7%BD%B2%E6%96%B9%E5%BC%8F">2 部署方式</h3>
<h4 id="21-windows-x64">2.1 windows-x64</h4> <h4 id="21-windows-x64%E6%A1%8C%E9%9D%A2">2.1 windows-x64桌面</h4>
<ul> <ul>
<li>双击运行FastGithub.UI.exe</li> <li>双击运行FastGithub.UI.exe</li>
</ul>
<h4 id="22-windows-x64%E6%9C%8D%E5%8A%A1">2.2 windows-x64服务</h4>
<ul>
<li><code>fastgithub.exe start</code> // 以windows服务安装并启动</li> <li><code>fastgithub.exe start</code> // 以windows服务安装并启动</li>
<li><code>fastgithub.exe stop</code> // 以windows服务卸载并删除</li> <li><code>fastgithub.exe stop</code> // 以windows服务卸载并删除</li>
</ul> </ul>
<h4 id="22-linux-x64">2.2 linux-x64</h4> <h4 id="23-linux-x64%E7%BB%88%E7%AB%AF">2.3 linux-x64终端</h4>
<ul> <ul>
<li>执行<code>sudo ./fastgithub</code></li> <li><code>sudo ./fastgithub</code></li>
<li>设置系统自动代理为<code>http://127.0.0.1:38457</code>或手动代理http/https为<code>127.0.0.1:38457</code></li> <li>设置系统自动代理为<code>http://127.0.0.1:38457</code>或手动代理http/https为<code>127.0.0.1:38457</code></li>
</ul> </ul>
<h4 id="23-macos-x64">2.3 macOS-x64</h4> <h4 id="24-linux-x64%E6%9C%8D%E5%8A%A1">2.4 linux-x64服务</h4>
<ul>
<li><code>sudo ./fastgithub start</code> // 以systemd服务安装并启动</li>
<li><code>sudo ./fastgithub start</code> // 以systemd服务卸载并删除</li>
<li>设置系统自动代理为<code>http://127.0.0.1:38457</code>或手动代理http/https为<code>127.0.0.1:38457</code></li>
</ul>
<h4 id="25-macos-x64">2.5 macOS-x64</h4>
<ul> <ul>
<li>双击运行fastgithub</li> <li>双击运行fastgithub</li>
<li>安装cacert/fastgithub.cer并设置信任</li> <li>安装cacert/fastgithub.cer并设置信任</li>
<li>设置系统自动代理为<code>http://127.0.0.1:38457</code>或手动代理http/https为<code>127.0.0.1:38457</code></li> <li>设置系统自动代理为<code>http://127.0.0.1:38457</code>或手动代理http/https为<code>127.0.0.1:38457</code></li>
<li><a href="https://github.com/dotnetcore/FastGithub/blob/master/MacOSXConfig.md">具体配置详情</a></li> <li><a href="https://github.com/dotnetcore/FastGithub/blob/master/MacOSXConfig.md">具体配置详情</a></li>
</ul> </ul>
<h4 id="24-docker-compose%E4%B8%80%E9%94%AE%E9%83%A8%E7%BD%B2">2.4 docker-compose一键部署</h4> <h4 id="26-docker-compose%E4%B8%80%E9%94%AE%E9%83%A8%E7%BD%B2">2.6 docker-compose一键部署</h4>
<ul> <ul>
<li>准备好docker 18.09, docker-compose.</li> <li>准备好docker 18.09, docker-compose.</li>
<li>在源码目录下有一个docker-compose.yaml 文件专用于在实际项目中临时使用github.com源码而做的demo配置。</li> <li>在源码目录下有一个docker-compose.yaml 文件专用于在实际项目中临时使用github.com源码而做的demo配置。</li>

View File

@ -7,22 +7,29 @@ github加速神器解决github打不开、用户头像无法加载、releases
* Q群2 [742376932](https://qm.qq.com/cgi-bin/qm/qr?k=6BBJ1nrJwe1o1E4-NJfwSOP-C4sMGc4q&jump_from=webapi) * Q群2 [742376932](https://qm.qq.com/cgi-bin/qm/qr?k=6BBJ1nrJwe1o1E4-NJfwSOP-C4sMGc4q&jump_from=webapi)
### 2 部署方式 ### 2 部署方式
#### 2.1 windows-x64 #### 2.1 windows-x64桌面
* 双击运行FastGithub.UI.exe * 双击运行FastGithub.UI.exe
#### 2.2 windows-x64服务
* `fastgithub.exe start` // 以windows服务安装并启动 * `fastgithub.exe start` // 以windows服务安装并启动
* `fastgithub.exe stop` // 以windows服务卸载并删除 * `fastgithub.exe stop` // 以windows服务卸载并删除
#### 2.2 linux-x64 #### 2.3 linux-x64终端
* 执行`sudo ./fastgithub` * `sudo ./fastgithub`
* 设置系统自动代理为`http://127.0.0.1:38457`或手动代理http/https为`127.0.0.1:38457`
#### 2.4 linux-x64服务
* `sudo ./fastgithub start` // 以systemd服务安装并启动
* `sudo ./fastgithub start` // 以systemd服务卸载并删除
* 设置系统自动代理为`http://127.0.0.1:38457`或手动代理http/https为`127.0.0.1:38457` * 设置系统自动代理为`http://127.0.0.1:38457`或手动代理http/https为`127.0.0.1:38457`
#### 2.3 macOS-x64 #### 2.5 macOS-x64
* 双击运行fastgithub * 双击运行fastgithub
* 安装cacert/fastgithub.cer并设置信任 * 安装cacert/fastgithub.cer并设置信任
* 设置系统自动代理为`http://127.0.0.1:38457`或手动代理http/https为`127.0.0.1:38457` * 设置系统自动代理为`http://127.0.0.1:38457`或手动代理http/https为`127.0.0.1:38457`
* [具体配置详情](https://github.com/dotnetcore/FastGithub/blob/master/MacOSXConfig.md) * [具体配置详情](https://github.com/dotnetcore/FastGithub/blob/master/MacOSXConfig.md)
#### 2.4 docker-compose一键部署 #### 2.6 docker-compose一键部署
* 准备好docker 18.09, docker-compose. * 准备好docker 18.09, docker-compose.
* 在源码目录下有一个docker-compose.yaml 文件专用于在实际项目中临时使用github.com源码而做的demo配置。 * 在源码目录下有一个docker-compose.yaml 文件专用于在实际项目中临时使用github.com源码而做的demo配置。
* 根据自己的需要更新docker-compose.yaml中的sample和build镜像即可完成拉github.com源码加速并基于源码做后续的操作。 * 根据自己的需要更新docker-compose.yaml中的sample和build镜像即可完成拉github.com源码加速并基于源码做后续的操作。