From 4433754662e972c887af522b8a0f6e3fe67e2932 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com>
Date: Mon, 1 Nov 2021 17:38:18 +0800
Subject: [PATCH] AppOptions
---
FastGithub.UI/App.xaml.cs | 8 ++--
FastGithub/AppHostedService.cs | 69 +++++++++++++++++++++++++++-------
FastGithub/AppOptions.cs | 13 +++++++
FastGithub/Startup.cs | 1 +
4 files changed, 74 insertions(+), 17 deletions(-)
create mode 100644 FastGithub/AppOptions.cs
diff --git a/FastGithub.UI/App.xaml.cs b/FastGithub.UI/App.xaml.cs
index f6853f7..1331e33 100644
--- a/FastGithub.UI/App.xaml.cs
+++ b/FastGithub.UI/App.xaml.cs
@@ -14,7 +14,6 @@ namespace FastGithub.UI
public partial class App : Application
{
private Mutex globalMutex;
- private Process fastGithub;
///
/// 程序启动
@@ -77,21 +76,22 @@ namespace FastGithub.UI
/// 启动fastgithub
///
///
- private static Process StartFastGithub()
+ private static void StartFastGithub()
{
const string fileName = "fastgithub.exe";
if (File.Exists(fileName) == false)
{
- return default;
+ return;
}
var startInfo = new ProcessStartInfo
{
FileName = fileName,
+ Arguments = $"ParentProcessId {Process.GetCurrentProcess().Id}",
UseShellExecute = false,
CreateNoWindow = true
};
- return Process.Start(startInfo);
+ Process.Start(startInfo);
}
///
diff --git a/FastGithub/AppHostedService.cs b/FastGithub/AppHostedService.cs
index 7baf2fc..1900129 100644
--- a/FastGithub/AppHostedService.cs
+++ b/FastGithub/AppHostedService.cs
@@ -3,6 +3,7 @@ using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
+using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
@@ -16,14 +17,20 @@ namespace FastGithub
///
sealed class AppHostedService : BackgroundService
{
- private readonly IOptions options;
+ private readonly IHost host;
+ private readonly IOptions appOptions;
+ private readonly IOptions fastGithubOptions;
private readonly ILogger logger;
public AppHostedService(
- IOptions options,
+ IHost host,
+ IOptions appOptions,
+ IOptions fastGithubOptions,
ILogger logger)
{
- this.options = options;
+ this.host = host;
+ this.appOptions = appOptions;
+ this.fastGithubOptions = fastGithubOptions;
this.logger = logger;
}
@@ -46,19 +53,30 @@ namespace FastGithub
///
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
- if (OperatingSystem.IsWindows())
- {
- return;
- }
-
await Task.Delay(TimeSpan.FromSeconds(1d), stoppingToken);
- if (await this.UseFastGithubProxyAsync() == false)
+ await this.CheckFastGithubProxyAsync(stoppingToken);
+ await this.WaitForParentProcessExitAsync(stoppingToken);
+ }
+
+
+ ///
+ /// 检测fastgithub代理设置
+ ///
+ ///
+ ///
+ private async Task CheckFastGithubProxyAsync(CancellationToken cancellationToken)
+ {
+ if (OperatingSystem.IsWindows() == false)
{
- var httpProxyPort = this.options.Value.HttpProxyPort;
- this.logger.LogWarning($"请设置系统自动代理为http://{IPAddress.Loopback}:{httpProxyPort},或手动代理http/https为{IPAddress.Loopback}:{httpProxyPort}");
+ if (await this.UseFastGithubProxyAsync() == false)
+ {
+ var httpProxyPort = this.fastGithubOptions.Value.HttpProxyPort;
+ this.logger.LogWarning($"请设置系统自动代理为http://{IPAddress.Loopback}:{httpProxyPort},或手动代理http/https为{IPAddress.Loopback}:{httpProxyPort}");
+ }
}
}
+
///
/// 应用fastgithub代理
///
@@ -73,7 +91,7 @@ namespace FastGithub
return false;
}
- var domain = this.options.Value.DomainConfigs.Keys.FirstOrDefault();
+ var domain = this.fastGithubOptions.Value.DomainConfigs.Keys.FirstOrDefault();
if (domain == null)
{
return true;
@@ -86,7 +104,7 @@ namespace FastGithub
return false;
}
- var httpProxyPort = this.options.Value.HttpProxyPort;
+ var httpProxyPort = this.fastGithubOptions.Value.HttpProxyPort;
if (proxyServer.Port != httpProxyPort)
{
return false;
@@ -107,5 +125,30 @@ namespace FastGithub
return false;
}
}
+
+ ///
+ /// 等待父进程退出
+ ///
+ ///
+ ///
+ private async Task WaitForParentProcessExitAsync(CancellationToken cancellationToken)
+ {
+ var parentId = this.appOptions.Value.ParentProcessId;
+ if (parentId <= 0)
+ {
+ return;
+ }
+
+ try
+ {
+ Process.GetProcessById(parentId).WaitForExit();
+ await this.host.StopAsync(cancellationToken);
+ }
+ catch (Exception ex)
+ {
+ this.logger.LogError(ex, $"获取进程{parentId}异常");
+ }
+ }
+
}
}
diff --git a/FastGithub/AppOptions.cs b/FastGithub/AppOptions.cs
new file mode 100644
index 0000000..2a15c98
--- /dev/null
+++ b/FastGithub/AppOptions.cs
@@ -0,0 +1,13 @@
+namespace FastGithub
+{
+ ///
+ /// app选项
+ ///
+ public class AppOptions
+ {
+ ///
+ /// 父进程id
+ ///
+ public int ParentProcessId { get; set; }
+ }
+}
diff --git a/FastGithub/Startup.cs b/FastGithub/Startup.cs
index 2bfae71..ab8919d 100644
--- a/FastGithub/Startup.cs
+++ b/FastGithub/Startup.cs
@@ -32,6 +32,7 @@ namespace FastGithub
///
public void ConfigureServices(IServiceCollection services)
{
+ services.Configure(this.Configuration);
services.Configure(this.Configuration.GetSection(nameof(FastGithub)));
services.AddConfiguration();