forked from WangGithubUser/FastGitHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppHostedService.cs
165 lines (149 loc) · 5.27 KB
/
AppHostedService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using FastGithub.Configuration;
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;
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub
{
/// <summary>
/// app后台服务
/// </summary>
sealed class AppHostedService : BackgroundService
{
private readonly IHost host;
private readonly IOptions<AppOptions> appOptions;
private readonly IOptions<FastGithubOptions> fastGithubOptions;
private readonly ILogger<AppHostedService> logger;
public AppHostedService(
IHost host,
IOptions<AppOptions> appOptions,
IOptions<FastGithubOptions> fastGithubOptions,
ILogger<AppHostedService> logger)
{
this.host = host;
this.appOptions = appOptions;
this.fastGithubOptions = fastGithubOptions;
this.logger = logger;
}
/// <summary>
/// 启动完成
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override Task StartAsync(CancellationToken cancellationToken)
{
var version = ProductionVersion.Current;
this.logger.LogInformation($"{nameof(FastGithub)}启动完成,当前版本为v{version},访问 https://github.com/WangGithubUser/FastGithub 关注新版本");
return base.StartAsync(cancellationToken);
}
/// <summary>
/// 后台任务
/// </summary>
/// <param name="stoppingToken"></param>
/// <returns></returns>
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await Task.Delay(TimeSpan.FromSeconds(1d), stoppingToken);
await this.CheckFastGithubProxyAsync(stoppingToken);
await this.WaitForParentProcessExitAsync(stoppingToken);
}
/// <summary>
/// 检测fastgithub代理设置
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task CheckFastGithubProxyAsync(CancellationToken cancellationToken)
{
if (OperatingSystem.IsWindows() == false)
{
try
{
if (await this.UseFastGithubProxyAsync() == false)
{
var httpProxyPort = this.fastGithubOptions.Value.HttpProxyPort;
this.logger.LogWarning($"请设置系统自动代理为http://{IPAddress.Loopback}:{httpProxyPort},或手动代理http/https为{IPAddress.Loopback}:{httpProxyPort}");
}
}
catch (Exception)
{
this.logger.LogWarning("尝试获取代理信息失败");
}
}
}
/// <summary>
/// 应用fastgithub代理
/// </summary>
/// <param name="proxyServer"></param>
/// <param name="httpProxyPort"></param>
/// <returns></returns>
private async Task<bool> UseFastGithubProxyAsync()
{
var systemProxy = HttpClient.DefaultProxy;
if (systemProxy == null)
{
return false;
}
var domain = this.fastGithubOptions.Value.DomainConfigs.Keys.FirstOrDefault();
if (domain == null)
{
return true;
}
var destination = new Uri($"https://{domain.Replace('*', 'a')}");
var proxyServer = systemProxy.GetProxy(destination);
if (proxyServer == null)
{
return false;
}
var httpProxyPort = this.fastGithubOptions.Value.HttpProxyPort;
if (proxyServer.Port != httpProxyPort)
{
return false;
}
if (IPAddress.TryParse(proxyServer.Host, out var address))
{
return IPAddress.IsLoopback(address);
}
try
{
var addresses = await Dns.GetHostAddressesAsync(proxyServer.Host);
return addresses.Any(item => IPAddress.IsLoopback(item));
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 等待父进程退出
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task WaitForParentProcessExitAsync(CancellationToken cancellationToken)
{
var parentId = this.appOptions.Value.ParentProcessId;
if (parentId <= 0)
{
return;
}
try
{
Process.GetProcessById(parentId).WaitForExit();
}
catch (Exception ex)
{
this.logger.LogError(ex, $"获取进程{parentId}异常");
}
finally
{
this.logger.LogInformation($"正在主动关闭,因为父进程已退出");
await this.host.StopAsync(cancellationToken);
}
}
}
}