From b6b449644b8c985361ef4e40f7d08c9b3ac6b4b8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com>
Date: Wed, 21 Jul 2021 09:56:24 +0800
Subject: [PATCH 1/5] =?UTF-8?q?=E4=BD=BF=E7=94=A8HttpClientFactory?=
=?UTF-8?q?=E5=8F=96=E4=BB=A3HttpClient?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FastGithub.ReverseProxy/HttpClient.cs | 50 -------------
FastGithub.ReverseProxy/HttpClientFactory.cs | 73 +++++++++++++++++++
.../ReverseProxyMiddleware.cs | 10 +--
...ReverseProxyServiceCollectionExtensions.cs | 2 +-
FastGithub/appsettings.json | 3 +-
5 files changed, 80 insertions(+), 58 deletions(-)
delete mode 100644 FastGithub.ReverseProxy/HttpClient.cs
create mode 100644 FastGithub.ReverseProxy/HttpClientFactory.cs
diff --git a/FastGithub.ReverseProxy/HttpClient.cs b/FastGithub.ReverseProxy/HttpClient.cs
deleted file mode 100644
index e2d141f..0000000
--- a/FastGithub.ReverseProxy/HttpClient.cs
+++ /dev/null
@@ -1,50 +0,0 @@
-using System;
-using System.Net.Http;
-using System.Threading;
-using System.Threading.Tasks;
-
-namespace FastGithub.ReverseProxy
-{
- ///
- /// YARP的HttpClient
- ///
- class HttpClient : HttpMessageInvoker
- {
- private readonly TlsSniPattern tlsSniPattern;
- private readonly bool tlsIgnoreNameMismatch;
-
- ///
- /// YARP的HttpClient
- ///
- ///
- ///
- ///
- public HttpClient(
- HttpMessageHandler handler,
- TlsSniPattern tlsSniPattern,
- bool tlsIgnoreNameMismatch,
- bool disposeHandler = false) : base(handler, disposeHandler)
- {
- this.tlsSniPattern = tlsSniPattern;
- this.tlsIgnoreNameMismatch = tlsIgnoreNameMismatch;
- }
-
- ///
- /// 发送数据
- ///
- ///
- ///
- ///
- public override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
- {
- request.SetRequestContext(new RequestContext
- {
- Host = request.RequestUri?.Host,
- IsHttps = request.RequestUri?.Scheme == Uri.UriSchemeHttps,
- TlsSniPattern = this.tlsSniPattern,
- TlsIgnoreNameMismatch = this.tlsIgnoreNameMismatch
- });
- return base.SendAsync(request, cancellationToken);
- }
- }
-}
diff --git a/FastGithub.ReverseProxy/HttpClientFactory.cs b/FastGithub.ReverseProxy/HttpClientFactory.cs
new file mode 100644
index 0000000..57b66d7
--- /dev/null
+++ b/FastGithub.ReverseProxy/HttpClientFactory.cs
@@ -0,0 +1,73 @@
+using Microsoft.Extensions.Options;
+using System;
+using System.Net.Http;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace FastGithub.ReverseProxy
+{
+ ///
+ /// HttpClient工厂
+ ///
+ sealed class HttpClientFactory
+ {
+ private HttpClientHanlder httpClientHanlder;
+
+ ///
+ /// HttpClient工厂
+ ///
+ ///
+ ///
+ public HttpClientFactory(
+ DomainResolver domainResolver,
+ IOptionsMonitor options)
+ {
+ this.httpClientHanlder = new HttpClientHanlder(domainResolver);
+ options.OnChange(opt => this.httpClientHanlder = new HttpClientHanlder(domainResolver));
+ }
+
+ ///
+ /// 创建httpClient
+ ///
+ ///
+ ///
+ public HttpMessageInvoker CreateHttpClient(DomainConfig domainConfig)
+ {
+ return new HttpClient(this.httpClientHanlder, domainConfig, disposeHandler: false);
+ }
+
+ ///
+ /// http客户端
+ ///
+ private class HttpClient : HttpMessageInvoker
+ {
+ private readonly DomainConfig domainConfig;
+
+ public HttpClient(
+ HttpMessageHandler handler,
+ DomainConfig domainConfig,
+ bool disposeHandler = false) : base(handler, disposeHandler)
+ {
+ this.domainConfig = domainConfig;
+ }
+
+ ///
+ /// 发送数据
+ ///
+ ///
+ ///
+ ///
+ public override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
+ {
+ request.SetRequestContext(new RequestContext
+ {
+ Host = request.RequestUri?.Host,
+ IsHttps = request.RequestUri?.Scheme == Uri.UriSchemeHttps,
+ TlsSniPattern = this.domainConfig.GetTlsSniPattern(),
+ TlsIgnoreNameMismatch = this.domainConfig.TlsIgnoreNameMismatch
+ });
+ return base.SendAsync(request, cancellationToken);
+ }
+ }
+ }
+}
diff --git a/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs b/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs
index 934a995..7b28119 100644
--- a/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs
+++ b/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs
@@ -12,18 +12,18 @@ namespace FastGithub.ReverseProxy
sealed class ReverseProxyMiddleware
{
private readonly IHttpForwarder httpForwarder;
- private readonly HttpClientHanlder httpClientHanlder;
+ private readonly HttpClientFactory httpClientFactory;
private readonly FastGithubConfig fastGithubConfig;
private readonly ILogger logger;
public ReverseProxyMiddleware(
IHttpForwarder httpForwarder,
- HttpClientHanlder httpClientHanlder,
+ HttpClientFactory httpClientFactory,
FastGithubConfig fastGithubConfig,
ILogger logger)
{
this.httpForwarder = httpForwarder;
- this.httpClientHanlder = httpClientHanlder;
+ this.httpClientFactory = httpClientFactory;
this.fastGithubConfig = fastGithubConfig;
this.logger = logger;
}
@@ -54,11 +54,9 @@ namespace FastGithub.ReverseProxy
else
{
var destinationPrefix = GetDestinationPrefix(host, domainConfig.Destination);
+ var httpClient = this.httpClientFactory.CreateHttpClient(domainConfig);
var requestConfig = new ForwarderRequestConfig { Timeout = domainConfig.Timeout };
- var tlsSniPattern = domainConfig.GetTlsSniPattern();
- using var httpClient = new HttpClient(this.httpClientHanlder, tlsSniPattern, domainConfig.TlsIgnoreNameMismatch);
-
var error = await httpForwarder.SendAsync(context, destinationPrefix, httpClient, requestConfig);
await HandleErrorAsync(context, error);
}
diff --git a/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs b/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs
index abac1bd..3ec02e4 100644
--- a/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs
+++ b/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs
@@ -19,7 +19,7 @@ namespace FastGithub
.AddMemoryCache()
.AddHttpForwarder()
.AddSingleton()
- .AddTransient()
+ .AddTransient()
.AddSingleton()
.AddSingleton();
}
diff --git a/FastGithub/appsettings.json b/FastGithub/appsettings.json
index fda2e06..9ae34f7 100644
--- a/FastGithub/appsettings.json
+++ b/FastGithub/appsettings.json
@@ -40,7 +40,8 @@
"TlsSni": false
},
"*github*.s3.amazonaws.com": {
- "TlsSni": false
+ "TlsSni": false,
+ "TlsIgnoreNameMismatch": true
},
"ajax.googleapis.com": {
"TlsSni": true,
From 0195535fa10fb2bdddf2c38d93a64ae6814ca6bd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com>
Date: Wed, 21 Jul 2021 11:21:12 +0800
Subject: [PATCH 2/5] TlsIgnoreNameMismatch
---
FastGithub/appsettings.json | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/FastGithub/appsettings.json b/FastGithub/appsettings.json
index 9ae34f7..ecf9c65 100644
--- a/FastGithub/appsettings.json
+++ b/FastGithub/appsettings.json
@@ -28,16 +28,20 @@
"TlsSni": false
},
"*.github.io": {
- "TlsSni": false
+ "TlsSni": false,
+ "TlsIgnoreNameMismatch": true
},
"*.githubapp.com": {
- "TlsSni": false
+ "TlsSni": false,
+ "TlsIgnoreNameMismatch": true
},
"*.githubassets.com": {
- "TlsSni": false
+ "TlsSni": false,
+ "TlsIgnoreNameMismatch": true
},
"*.githubusercontent.com": {
- "TlsSni": false
+ "TlsSni": false,
+ "TlsIgnoreNameMismatch": true
},
"*github*.s3.amazonaws.com": {
"TlsSni": false,
From 5576c602083f58fcef493148eeebdf1dcd8123e9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com>
Date: Wed, 21 Jul 2021 12:49:07 +0800
Subject: [PATCH 3/5] =?UTF-8?q?=E4=BB=A5windows=E6=9C=8D=E5=8A=A1=E8=BF=90?=
=?UTF-8?q?=E8=A1=8C=E4=B9=9F=E4=BB=A5=E6=9C=8D=E5=8A=A1=E8=BF=90=E8=A1=8C?=
=?UTF-8?q?dnscrypt-proxy?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../DnscryptProxyHostedService.cs | 42 ++++++++++++-------
1 file changed, 28 insertions(+), 14 deletions(-)
diff --git a/FastGithub.Dns.DnscryptProxy/DnscryptProxyHostedService.cs b/FastGithub.Dns.DnscryptProxy/DnscryptProxyHostedService.cs
index 8dfe005..b16591f 100644
--- a/FastGithub.Dns.DnscryptProxy/DnscryptProxyHostedService.cs
+++ b/FastGithub.Dns.DnscryptProxy/DnscryptProxyHostedService.cs
@@ -2,7 +2,6 @@
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics;
-using System.IO;
using System.Threading;
using System.Threading.Tasks;
@@ -35,23 +34,16 @@ namespace FastGithub.Dns.DnscryptProxy
{
try
{
- var fileName = dnscryptFile;
- if (OperatingSystem.IsWindows())
+ if (OperatingSystem.IsWindows() && Process.GetCurrentProcess().SessionId == 0)
{
- fileName = $"{dnscryptFile}.exe";
+ StartDnscrypt("-service install")?.WaitForExit();
+ StartDnscrypt("-service start")?.WaitForExit();
}
-
- if (File.Exists(fileName) == true)
+ else
{
- this.dnscryptProcess = Process.Start(new ProcessStartInfo
- {
- FileName = fileName,
- UseShellExecute = true,
- CreateNoWindow = true,
- WindowStyle = ProcessWindowStyle.Hidden
- });
- this.logger.LogInformation($"{dnscryptFile}启动成功");
+ this.dnscryptProcess = StartDnscrypt(string.Empty);
}
+ this.logger.LogInformation($"{dnscryptFile}启动成功");
}
catch (Exception ex)
{
@@ -72,7 +64,29 @@ namespace FastGithub.Dns.DnscryptProxy
this.dnscryptProcess.Kill();
this.logger.LogInformation($"{dnscryptFile}已停止");
}
+ else if (OperatingSystem.IsWindows())
+ {
+ StartDnscrypt("-service stop")?.WaitForExit();
+ StartDnscrypt("-service uninstall")?.WaitForExit();
+ this.logger.LogInformation($"{dnscryptFile}已停止");
+ }
return Task.CompletedTask;
}
+
+ ///
+ /// 启动Dnscrypt
+ ///
+ ///
+ private static Process? StartDnscrypt(string arguments)
+ {
+ return Process.Start(new ProcessStartInfo
+ {
+ FileName = OperatingSystem.IsWindows() ? $"{dnscryptFile}.exe" : dnscryptFile,
+ Arguments = arguments,
+ UseShellExecute = true,
+ CreateNoWindow = true,
+ WindowStyle = ProcessWindowStyle.Hidden
+ });
+ }
}
}
From 32cfe8e56feb66ab295a8922b64075f5286a5c2c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com>
Date: Wed, 21 Jul 2021 12:51:03 +0800
Subject: [PATCH 4/5] 1.0.6
---
Directory.Build.props | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/Directory.Build.props b/Directory.Build.props
index 785b2a7..d72ad6d 100644
--- a/Directory.Build.props
+++ b/Directory.Build.props
@@ -1,6 +1,6 @@
- 1.0.5
+ 1.0.6
enable
github加速神器
https://github.com/xljiulang/FastGithub
From 2882615bade08cd213e1a517d89bfc546780ed2b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com>
Date: Wed, 21 Jul 2021 13:13:44 +0800
Subject: [PATCH 5/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BB=93=E6=9D=9F?=
=?UTF-8?q?=E9=80=BB=E8=BE=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../DnscryptProxyHostedService.cs | 51 ++++++++++++-------
1 file changed, 32 insertions(+), 19 deletions(-)
diff --git a/FastGithub.Dns.DnscryptProxy/DnscryptProxyHostedService.cs b/FastGithub.Dns.DnscryptProxy/DnscryptProxyHostedService.cs
index b16591f..3a8974c 100644
--- a/FastGithub.Dns.DnscryptProxy/DnscryptProxyHostedService.cs
+++ b/FastGithub.Dns.DnscryptProxy/DnscryptProxyHostedService.cs
@@ -12,9 +12,8 @@ namespace FastGithub.Dns.DnscryptProxy
///
sealed class DnscryptProxyHostedService : IHostedService
{
- private const string dnscryptFile = "dnscrypt-proxy";
+ private const string dnscryptProxyFile = "dnscrypt-proxy";
private readonly ILogger logger;
- private Process? dnscryptProcess;
///
/// DnscryptProxy后台服务
@@ -34,20 +33,20 @@ namespace FastGithub.Dns.DnscryptProxy
{
try
{
- if (OperatingSystem.IsWindows() && Process.GetCurrentProcess().SessionId == 0)
+ if (OperatingSystem.IsWindows())
{
- StartDnscrypt("-service install")?.WaitForExit();
- StartDnscrypt("-service start")?.WaitForExit();
+ StartDnscryptProxy("-service install", waitForExit: true);
+ StartDnscryptProxy("-service start", waitForExit: true);
}
else
{
- this.dnscryptProcess = StartDnscrypt(string.Empty);
+ StartDnscryptProxy(string.Empty, waitForExit: false);
}
- this.logger.LogInformation($"{dnscryptFile}启动成功");
+ this.logger.LogInformation($"{dnscryptProxyFile}启动成功");
}
catch (Exception ex)
{
- this.logger.LogWarning($"{dnscryptFile}启动失败:{ex.Message}");
+ this.logger.LogWarning($"{dnscryptProxyFile}启动失败:{ex.Message}");
}
return Task.CompletedTask;
}
@@ -59,34 +58,48 @@ namespace FastGithub.Dns.DnscryptProxy
///
public Task StopAsync(CancellationToken cancellationToken)
{
- if (this.dnscryptProcess != null)
+ try
{
- this.dnscryptProcess.Kill();
- this.logger.LogInformation($"{dnscryptFile}已停止");
+ if (OperatingSystem.IsWindows())
+ {
+ StartDnscryptProxy("-service stop", waitForExit: true);
+ StartDnscryptProxy("-service uninstall", waitForExit: true);
+ }
+
+ foreach (var process in Process.GetProcessesByName(dnscryptProxyFile))
+ {
+ process.Kill();
+ }
+ this.logger.LogInformation($"{dnscryptProxyFile}已停止");
}
- else if (OperatingSystem.IsWindows())
+ catch (Exception ex)
{
- StartDnscrypt("-service stop")?.WaitForExit();
- StartDnscrypt("-service uninstall")?.WaitForExit();
- this.logger.LogInformation($"{dnscryptFile}已停止");
+ this.logger.LogWarning($"{dnscryptProxyFile}停止失败:{ex.Message}");
}
return Task.CompletedTask;
}
+
///
- /// 启动Dnscrypt
+ /// 启动DnscryptProxy进程
///
///
- private static Process? StartDnscrypt(string arguments)
+ ///
+ private static void StartDnscryptProxy(string arguments, bool waitForExit)
{
- return Process.Start(new ProcessStartInfo
+ var process = Process.Start(new ProcessStartInfo
{
- FileName = OperatingSystem.IsWindows() ? $"{dnscryptFile}.exe" : dnscryptFile,
+ FileName = OperatingSystem.IsWindows() ? $"{dnscryptProxyFile}.exe" : dnscryptProxyFile,
Arguments = arguments,
UseShellExecute = true,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
});
+
+ if (waitForExit && process != null)
+ {
+ process.WaitForExit();
+ }
}
}
}