diff --git a/FastGithub.Configuration/LocalMachine.cs b/FastGithub.Configuration/LocalMachine.cs index f4520f7..94d3ea5 100644 --- a/FastGithub.Configuration/LocalMachine.cs +++ b/FastGithub.Configuration/LocalMachine.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Net.NetworkInformation; using System.Net.Sockets; @@ -10,12 +9,7 @@ namespace FastGithub.Configuration /// 提供本机设备信息 /// public static class LocalMachine - { - /// - /// 获取设备名 - /// - public static string Name => Environment.MachineName; - + { /// /// 获取可用的随机Tcp端口 /// diff --git a/FastGithub.HttpServer/CertService.cs b/FastGithub.HttpServer/CertService.cs index 679d7b0..ae9b7cf 100644 --- a/FastGithub.HttpServer/CertService.cs +++ b/FastGithub.HttpServer/CertService.cs @@ -181,7 +181,7 @@ namespace FastGithub.HttpServer yield break; } - yield return LocalMachine.Name; + yield return Environment.MachineName; yield return IPAddress.Loopback.ToString(); } } diff --git a/FastGithub.HttpServer/HttpProxyMiddleware.cs b/FastGithub.HttpServer/HttpProxyMiddleware.cs index 54b78b1..5ed2365 100644 --- a/FastGithub.HttpServer/HttpProxyMiddleware.cs +++ b/FastGithub.HttpServer/HttpProxyMiddleware.cs @@ -3,6 +3,7 @@ using FastGithub.DomainResolve; using Microsoft.AspNetCore.Connections.Features; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Features; +using Microsoft.Extensions.Options; using System.IO.Pipelines; using System.Net; using System.Net.Http; @@ -18,9 +19,13 @@ namespace FastGithub.HttpServer /// sealed class HttpProxyMiddleware { + private const string LOOPBACK = "127.0.0.1"; + private const string LOCALHOST = "localhost"; + private readonly FastGithubConfig fastGithubConfig; private readonly IDomainResolver domainResolver; private readonly IHttpForwarder httpForwarder; + private readonly IOptions options; private readonly HttpMessageInvoker httpClient; /// @@ -29,14 +34,17 @@ namespace FastGithub.HttpServer /// /// /// + /// public HttpProxyMiddleware( FastGithubConfig fastGithubConfig, IDomainResolver domainResolver, - IHttpForwarder httpForwarder) + IHttpForwarder httpForwarder, + IOptions options) { this.fastGithubConfig = fastGithubConfig; this.domainResolver = domainResolver; this.httpForwarder = httpForwarder; + this.options = options; this.httpClient = new HttpMessageInvoker(CreateHttpHandler(), disposeHandler: false); } @@ -47,15 +55,17 @@ namespace FastGithub.HttpServer /// public async Task InvokeAsync(HttpContext context) { - if (context.Request.Method == HttpMethods.Get && context.Request.Path == "/proxy.pac") + var host = context.Request.Host; + if (this.IsFastGithubServer(host) == true) { + var proxyPac = this.GetProxyPacString(host); context.Response.ContentType = "application/x-ns-proxy-autoconfig"; - var pacString = this.GetProxyPacString(context); - await context.Response.WriteAsync(pacString); + context.Response.Headers.Add("Content-Disposition", $"attachment;filename=proxy.pac"); + await context.Response.WriteAsync(proxyPac); } else if (context.Request.Method == HttpMethods.Connect) { - var endpoint = await this.GetTargetEndPointAsync(context.Request); + var endpoint = await this.GetTargetEndPointAsync(host); using var targetSocket = new Socket(SocketType.Stream, ProtocolType.Tcp); await targetSocket.ConnectAsync(endpoint); @@ -80,16 +90,30 @@ namespace FastGithub.HttpServer } + /// + /// 是否为fastgithub服务 + /// + /// + /// + private bool IsFastGithubServer(HostString host) + { + if (host.Host == LOOPBACK || host.Host == LOCALHOST) + { + return host.Port == this.options.Value.HttpProxyPort; + } + return false; + } + /// /// 获取proxypac脚本 /// - /// + /// /// - private string GetProxyPacString(HttpContext context) + private string GetProxyPacString(HostString host) { var buidler = new StringBuilder(); buidler.AppendLine("function FindProxyForURL(url, host){"); - buidler.AppendLine($" var proxy = 'PROXY {context.Request.Host}';"); + buidler.AppendLine($" var proxy = 'PROXY {host.Host}';"); foreach (var domain in this.fastGithubConfig.GetDomainPatterns()) { buidler.AppendLine($" if (shExpMatch(host, '{domain}')) return proxy;"); @@ -102,13 +126,13 @@ namespace FastGithub.HttpServer /// /// 获取目标终节点 /// - /// + /// /// - private async Task GetTargetEndPointAsync(HttpRequest request) + private async Task GetTargetEndPointAsync(HostString host) { const int HTTPS_PORT = 443; - var targetHost = request.Host.Host; - var targetPort = request.Host.Port ?? HTTPS_PORT; + var targetHost = host.Host; + var targetPort = host.Port ?? HTTPS_PORT; if (IPAddress.TryParse(targetHost, out var address) == true) { diff --git a/FastGithub/AppHostedService.cs b/FastGithub/AppHostedService.cs index 7e62349..dde9fc8 100644 --- a/FastGithub/AppHostedService.cs +++ b/FastGithub/AppHostedService.cs @@ -62,7 +62,7 @@ namespace FastGithub await Task.Delay(TimeSpan.FromSeconds(1d), stoppingToken); if (await this.UseFastGithubProxyAsync() == false) { - this.logger.LogWarning($"请手工设置系统http/https代理为127.0.0.1:38457或自动代理为http://127.0.0.1:38457/proxy.pac"); + this.logger.LogWarning($"请手工设置系统http/https代理为127.0.0.1:38457或自动代理为http://127.0.0.1:38457"); } } } diff --git a/README.html b/README.html index f2891e4..ff28097 100644 --- a/README.html +++ b/README.html @@ -380,13 +380,13 @@ code {
  • 执行sudo ./fastgithub
  • 手工安装cacert/fastgithub.cer到受信任的根证书颁发机构
  • -
  • 手工设置系统http/https代理为127.0.0.1:38457或自动代理为http://127.0.0.1:38457/proxy.pac
  • +
  • 手工设置系统http/https代理为127.0.0.1:38457或自动代理为http://127.0.0.1:38457

2.3 macOS-x64

  • 双击运行fastgithub程序
  • 手工安装cacert/fastgithub.cer并设置信任
  • -
  • 手工设置系统http/https代理为127.0.0.1:38457或自动代理为http://127.0.0.1:38457/proxy.pac
  • +
  • 手工设置系统http/https代理为127.0.0.1:38457或自动代理为http://127.0.0.1:38457

3 证书验证

3.1 git

diff --git a/README.md b/README.md index e710f71..13fffb0 100644 --- a/README.md +++ b/README.md @@ -13,12 +13,12 @@ github加速神器,解决github打不开、用户头像无法加载、releases #### 2.2 linux-x64 * 执行`sudo ./fastgithub` * 手工安装cacert/fastgithub.cer到受信任的根证书颁发机构 -* 手工设置系统http/https代理为`127.0.0.1:38457`或自动代理为`http://127.0.0.1:38457/proxy.pac` +* 手工设置系统http/https代理为`127.0.0.1:38457`或自动代理为`http://127.0.0.1:38457` #### 2.3 macOS-x64 * 双击运行fastgithub程序 * 手工安装cacert/fastgithub.cer并设置信任 -* 手工设置系统http/https代理为`127.0.0.1:38457`或自动代理为`http://127.0.0.1:38457/proxy.pac` +* 手工设置系统http/https代理为`127.0.0.1:38457`或自动代理为`http://127.0.0.1:38457` ### 3 证书验证