This commit is contained in:
xljiulang 2021-07-21 20:30:19 +08:00
commit 9f20fbeab8
7 changed files with 135 additions and 82 deletions

View File

@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>1.0.5</Version>
<Version>1.0.6</Version>
<Nullable>enable</Nullable>
<Description>github加速神器</Description>
<Copyright>https://github.com/xljiulang/FastGithub</Copyright>

View File

@ -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
/// </summary>
sealed class DnscryptProxyHostedService : IHostedService
{
private const string dnscryptFile = "dnscrypt-proxy";
private const string dnscryptProxyFile = "dnscrypt-proxy";
private readonly ILogger<DnscryptProxyHostedService> logger;
private Process? dnscryptProcess;
/// <summary>
/// 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
/// <returns></returns>
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;
}
/// <summary>
/// 启动DnscryptProxy进程
/// </summary>
/// <param name="arguments"></param>
/// <param name="waitForExit"></param>
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();
}
}
}
}

View File

@ -1,50 +0,0 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub.ReverseProxy
{
/// <summary>
/// YARP的HttpClient
/// </summary>
class HttpClient : HttpMessageInvoker
{
private readonly TlsSniPattern tlsSniPattern;
private readonly bool tlsIgnoreNameMismatch;
/// <summary>
/// YARP的HttpClient
/// </summary>
/// <param name="handler"></param>
/// <param name="tlsSniPattern"></param>
/// <param name="disposeHandler"></param>
public HttpClient(
HttpMessageHandler handler,
TlsSniPattern tlsSniPattern,
bool tlsIgnoreNameMismatch,
bool disposeHandler = false) : base(handler, disposeHandler)
{
this.tlsSniPattern = tlsSniPattern;
this.tlsIgnoreNameMismatch = tlsIgnoreNameMismatch;
}
/// <summary>
/// 发送数据
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override Task<HttpResponseMessage> 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);
}
}
}

View File

@ -0,0 +1,73 @@
using Microsoft.Extensions.Options;
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub.ReverseProxy
{
/// <summary>
/// HttpClient工厂
/// </summary>
sealed class HttpClientFactory
{
private HttpClientHanlder httpClientHanlder;
/// <summary>
/// HttpClient工厂
/// </summary>
/// <param name="domainResolver"></param>
/// <param name="options"></param>
public HttpClientFactory(
DomainResolver domainResolver,
IOptionsMonitor<FastGithubOptions> options)
{
this.httpClientHanlder = new HttpClientHanlder(domainResolver);
options.OnChange(opt => this.httpClientHanlder = new HttpClientHanlder(domainResolver));
}
/// <summary>
/// 创建httpClient
/// </summary>
/// <param name="domainConfig"></param>
/// <returns></returns>
public HttpMessageInvoker CreateHttpClient(DomainConfig domainConfig)
{
return new HttpClient(this.httpClientHanlder, domainConfig, disposeHandler: false);
}
/// <summary>
/// http客户端
/// </summary>
private class HttpClient : HttpMessageInvoker
{
private readonly DomainConfig domainConfig;
public HttpClient(
HttpMessageHandler handler,
DomainConfig domainConfig,
bool disposeHandler = false) : base(handler, disposeHandler)
{
this.domainConfig = domainConfig;
}
/// <summary>
/// 发送数据
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override Task<HttpResponseMessage> 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);
}
}
}
}

View File

@ -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<ReverseProxyMiddleware> logger;
public ReverseProxyMiddleware(
IHttpForwarder httpForwarder,
HttpClientHanlder httpClientHanlder,
HttpClientFactory httpClientFactory,
FastGithubConfig fastGithubConfig,
ILogger<ReverseProxyMiddleware> 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);
}

View File

@ -19,7 +19,7 @@ namespace FastGithub
.AddMemoryCache()
.AddHttpForwarder()
.AddSingleton<DomainResolver>()
.AddTransient<HttpClientHanlder>()
.AddTransient<HttpClientFactory>()
.AddSingleton<RequestLoggingMilldeware>()
.AddSingleton<ReverseProxyMiddleware>();
}

View File

@ -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,