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 diff --git a/FastGithub.Dns.DnscryptProxy/DnscryptProxyHostedService.cs b/FastGithub.Dns.DnscryptProxy/DnscryptProxyHostedService.cs index 8dfe005..3a8974c 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; @@ -13,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后台服务 @@ -35,27 +33,20 @@ namespace FastGithub.Dns.DnscryptProxy { try { - var fileName = dnscryptFile; if (OperatingSystem.IsWindows()) { - fileName = $"{dnscryptFile}.exe"; + StartDnscryptProxy("-service install", waitForExit: true); + StartDnscryptProxy("-service start", waitForExit: true); } - - 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}启动成功"); + StartDnscryptProxy(string.Empty, waitForExit: false); } + this.logger.LogInformation($"{dnscryptProxyFile}启动成功"); } catch (Exception ex) { - this.logger.LogWarning($"{dnscryptFile}启动失败:{ex.Message}"); + this.logger.LogWarning($"{dnscryptProxyFile}启动失败:{ex.Message}"); } return Task.CompletedTask; } @@ -67,12 +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}已停止"); + } + catch (Exception ex) + { + this.logger.LogWarning($"{dnscryptProxyFile}停止失败:{ex.Message}"); } return Task.CompletedTask; } + + + /// + /// 启动DnscryptProxy进程 + /// + /// + /// + private static void StartDnscryptProxy(string arguments, bool waitForExit) + { + var process = Process.Start(new ProcessStartInfo + { + FileName = OperatingSystem.IsWindows() ? $"{dnscryptProxyFile}.exe" : dnscryptProxyFile, + Arguments = arguments, + UseShellExecute = true, + CreateNoWindow = true, + WindowStyle = ProcessWindowStyle.Hidden + }); + + if (waitForExit && process != null) + { + process.WaitForExit(); + } + } } } 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..ecf9c65 100644 --- a/FastGithub/appsettings.json +++ b/FastGithub/appsettings.json @@ -28,19 +28,24 @@ "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 + "TlsSni": false, + "TlsIgnoreNameMismatch": true }, "ajax.googleapis.com": { "TlsSni": true,