From 9086bf79f98121a2702f66aac05a12bfa7a37bf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E4=B9=9D?= <366193849@qq.com> Date: Sun, 26 Sep 2021 20:45:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=90=86=E7=9A=84http=E6=B5=81?= =?UTF-8?q?=E9=87=8F=E5=BA=94=E7=94=A8=E5=9F=9F=E5=90=8D=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Configuration/FastGithubConfig.cs | 7 ++++ FastGithub.DomainResolve/DomainResolver.cs | 4 +- FastGithub.DomainResolve/IDomainResolver.cs | 2 +- FastGithub.HttpServer/HttpProxyMiddleware.cs | 42 +++++++++++-------- .../SshReverseProxyHandler.cs | 8 +--- FastGithub/Program.cs | 8 ++-- 6 files changed, 39 insertions(+), 32 deletions(-) diff --git a/FastGithub.Configuration/FastGithubConfig.cs b/FastGithub.Configuration/FastGithubConfig.cs index 173fd40..17fcb40 100644 --- a/FastGithub.Configuration/FastGithubConfig.cs +++ b/FastGithub.Configuration/FastGithubConfig.cs @@ -15,6 +15,11 @@ namespace FastGithub.Configuration private SortedDictionary domainConfigs; private ConcurrentDictionary domainConfigCache; + /// + /// http代理端口 + /// + public int HttpProxyPort { get; set; } + /// /// 回退的dns /// @@ -29,6 +34,7 @@ namespace FastGithub.Configuration { var opt = options.CurrentValue; + this.HttpProxyPort = opt.HttpProxyPort; this.FallbackDns = ConvertToIPEndPoints(opt.FallbackDns).ToArray(); this.domainConfigs = ConvertDomainConfigs(opt.DomainConfigs); this.domainConfigCache = new ConcurrentDictionary(); @@ -42,6 +48,7 @@ namespace FastGithub.Configuration /// private void Update(FastGithubOptions options) { + this.HttpProxyPort = options.HttpProxyPort; this.FallbackDns = ConvertToIPEndPoints(options.FallbackDns).ToArray(); this.domainConfigs = ConvertDomainConfigs(options.DomainConfigs); this.domainConfigCache = new ConcurrentDictionary(); diff --git a/FastGithub.DomainResolve/DomainResolver.cs b/FastGithub.DomainResolve/DomainResolver.cs index 7c8003a..fa88a8a 100644 --- a/FastGithub.DomainResolve/DomainResolver.cs +++ b/FastGithub.DomainResolve/DomainResolver.cs @@ -38,13 +38,13 @@ namespace FastGithub.DomainResolve /// 域名 /// /// - public async Task ResolveAsync(string domain, CancellationToken cancellationToken = default) + public async Task ResolveAsync(string domain, CancellationToken cancellationToken = default) { await foreach (var address in this.ResolveAllAsync(domain, cancellationToken)) { return address; } - return default; + throw new FastGithubException($"解析不到{domain}的IP"); } /// diff --git a/FastGithub.DomainResolve/IDomainResolver.cs b/FastGithub.DomainResolve/IDomainResolver.cs index 691ab3c..384e6ee 100644 --- a/FastGithub.DomainResolve/IDomainResolver.cs +++ b/FastGithub.DomainResolve/IDomainResolver.cs @@ -16,7 +16,7 @@ namespace FastGithub.DomainResolve /// 域名 /// /// - Task ResolveAsync(string domain, CancellationToken cancellationToken = default); + Task ResolveAsync(string domain, CancellationToken cancellationToken = default); /// /// 解析所有ip diff --git a/FastGithub.HttpServer/HttpProxyMiddleware.cs b/FastGithub.HttpServer/HttpProxyMiddleware.cs index f6518dc..69bdcc9 100644 --- a/FastGithub.HttpServer/HttpProxyMiddleware.cs +++ b/FastGithub.HttpServer/HttpProxyMiddleware.cs @@ -3,7 +3,6 @@ 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; @@ -25,8 +24,9 @@ namespace FastGithub.HttpServer private readonly FastGithubConfig fastGithubConfig; private readonly IDomainResolver domainResolver; private readonly IHttpForwarder httpForwarder; - private readonly IOptions options; - private readonly HttpMessageInvoker httpClient; + private readonly HttpReverseProxyMiddleware httpReverseProxy; + + private readonly HttpMessageInvoker defaultHttpClient; /// /// http代理中间件 @@ -34,18 +34,19 @@ namespace FastGithub.HttpServer /// /// /// - /// + /// public HttpProxyMiddleware( FastGithubConfig fastGithubConfig, IDomainResolver domainResolver, IHttpForwarder httpForwarder, - IOptions options) + HttpReverseProxyMiddleware httpReverseProxy) { this.fastGithubConfig = fastGithubConfig; this.domainResolver = domainResolver; this.httpForwarder = httpForwarder; - this.options = options; - this.httpClient = new HttpMessageInvoker(CreateHttpHandler(), disposeHandler: false); + this.httpReverseProxy = httpReverseProxy; + + this.defaultHttpClient = new HttpMessageInvoker(CreateDefaultHttpHandler(), disposeHandler: false); } /// @@ -84,12 +85,14 @@ namespace FastGithub.HttpServer } else { - var destinationPrefix = $"{context.Request.Scheme}://{context.Request.Host}"; - await this.httpForwarder.SendAsync(context, destinationPrefix, this.httpClient); + await this.httpReverseProxy.InvokeAsync(context, async ctx => + { + var destinationPrefix = $"{ctx.Request.Scheme}://{ctx.Request.Host}"; + await this.httpForwarder.SendAsync(ctx, destinationPrefix, this.defaultHttpClient); + }); } } - /// /// 是否为fastgithub服务 /// @@ -99,7 +102,7 @@ namespace FastGithub.HttpServer { if (host.Host == LOOPBACK || host.Host == LOCALHOST) { - return host.Port == this.options.Value.HttpProxyPort; + return host.Port == this.fastGithubConfig.HttpProxyPort; } return false; } @@ -130,6 +133,7 @@ namespace FastGithub.HttpServer /// private async Task GetTargetEndPointAsync(HostString host) { + const int HTTP_PORT = 80; const int HTTPS_PORT = 443; var targetHost = host.Host; var targetPort = host.Port ?? HTTPS_PORT; @@ -145,24 +149,26 @@ namespace FastGithub.HttpServer return new DnsEndPoint(targetHost, targetPort); } - // 目标端口为443,走https代理中间人 - if (targetPort == HTTPS_PORT && targetHost != "ssh.github.com") + if (targetPort == HTTP_PORT) + { + return new IPEndPoint(IPAddress.Loopback, ReverseProxyPort.Http); + } + + if (targetPort == HTTPS_PORT) { return new IPEndPoint(IPAddress.Loopback, ReverseProxyPort.Https); } - // dns优选 + // 不使用系统dns address = await this.domainResolver.ResolveAsync(targetHost); - return address == null - ? throw new FastGithubException($"解析不到{targetHost}的IP") - : new IPEndPoint(address, targetPort); + return new IPEndPoint(address, targetPort); } /// /// 创建httpHandler /// /// - private static SocketsHttpHandler CreateHttpHandler() + private static SocketsHttpHandler CreateDefaultHttpHandler() { return new() { diff --git a/FastGithub.HttpServer/SshReverseProxyHandler.cs b/FastGithub.HttpServer/SshReverseProxyHandler.cs index 0691802..16055e2 100644 --- a/FastGithub.HttpServer/SshReverseProxyHandler.cs +++ b/FastGithub.HttpServer/SshReverseProxyHandler.cs @@ -1,5 +1,4 @@ -using FastGithub.Configuration; -using FastGithub.DomainResolve; +using FastGithub.DomainResolve; using Microsoft.AspNetCore.Connections; using System.IO.Pipelines; using System.Net.Sockets; @@ -33,11 +32,6 @@ namespace FastGithub.HttpServer public override async Task OnConnectedAsync(ConnectionContext context) { var address = await this.domainResolver.ResolveAsync(SSH_GITHUB_COM); - if (address == null) - { - throw new FastGithubException($"解析不到{SSH_GITHUB_COM}的IP"); - } - using var socket = new Socket(SocketType.Stream, ProtocolType.Tcp); await socket.ConnectAsync(address, SSH_OVER_HTTPS_PORT); var targetStream = new NetworkStream(socket, ownsSocket: false); diff --git a/FastGithub/Program.cs b/FastGithub/Program.cs index d798dfb..f7bfd0c 100644 --- a/FastGithub/Program.cs +++ b/FastGithub/Program.cs @@ -27,7 +27,7 @@ namespace FastGithub { return Host .CreateDefaultBuilder(args) - .UseWindowsService() + .UseWindowsService() .UseDefaultServiceProvider(c => { c.ValidateOnBuild = false; @@ -51,15 +51,15 @@ namespace FastGithub webBuilder.UseKestrel(kestrel => { kestrel.NoLimit(); + kestrel.ListenHttpsReverseProxy(); + kestrel.ListenHttpReverseProxy(); + if (OperatingSystem.IsWindows()) { - kestrel.ListenHttpsReverseProxy(); - kestrel.ListenHttpReverseProxy(); kestrel.ListenSshReverseProxy(); } else { - kestrel.ListenHttpsReverseProxy(); kestrel.ListenHttpProxy(); } });