diff --git a/FastGithub.Dns/DnsOverUdpHostedService.cs b/FastGithub.Dns/DnsOverUdpHostedService.cs index d452941..45a543e 100644 --- a/FastGithub.Dns/DnsOverUdpHostedService.cs +++ b/FastGithub.Dns/DnsOverUdpHostedService.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Net; +using System.Text; using System.Threading; using System.Threading.Tasks; @@ -30,7 +31,7 @@ namespace FastGithub.Dns { this.dnsOverUdpServer = dnsOverUdpServer; this.conflictValidators = conflictValidators; - this.logger = logger; + this.logger = logger; } /// @@ -48,7 +49,12 @@ namespace FastGithub.Dns } catch (Exception ex) { - this.logger.LogError($"DNS服务启动失败:{ex.Message}{Environment.NewLine}请配置系统或浏览器使用{nameof(FastGithub)}的DoH:https://127.0.0.1/dns-query,或向系统hosts文件添加github相关域名的ip为127.0.0.1"); + var builder = new StringBuilder().AppendLine($"DNS服务启动失败({ex.Message}),你可以选择如下的一种操作:"); + builder.AppendLine($"1. 关闭占用udp53端口的进程然后重新打开本程序"); + builder.AppendLine($"2. 向系统hosts文件添加要加速的域名的ip为127.0.0.1"); + builder.AppendLine($"3. 配置系统或浏览器使用DNS over HTTPS:https://127.0.0.1/dns-query"); + builder.Append($"4. 在局域网其它设备上运行本程序,然后将本机DNS设置为局域网设备的IP"); + this.logger.LogError(builder.ToString()); } foreach (var item in this.conflictValidators) diff --git a/FastGithub.DomainResolve/DnscryptProxyHostedService.cs b/FastGithub.DomainResolve/DnscryptProxyHostedService.cs index fdf70d9..95872a9 100644 --- a/FastGithub.DomainResolve/DnscryptProxyHostedService.cs +++ b/FastGithub.DomainResolve/DnscryptProxyHostedService.cs @@ -37,7 +37,7 @@ namespace FastGithub.DomainResolve try { await this.dnscryptProxy.StartAsync(cancellationToken); - this.logger.LogInformation($"{this.dnscryptProxy}启动完成"); + this.logger.LogInformation($"已监听端口{this.dnscryptProxy.LocalEndPoint?.Port},{this.dnscryptProxy}启动完成"); } catch (Exception ex) { diff --git a/FastGithub.ReverseProxy/ReverseProxyApplicationBuilderExtensions.cs b/FastGithub.ReverseProxy/ApplicationBuilderExtensions.cs similarity index 89% rename from FastGithub.ReverseProxy/ReverseProxyApplicationBuilderExtensions.cs rename to FastGithub.ReverseProxy/ApplicationBuilderExtensions.cs index 8fcad59..cd8ff39 100644 --- a/FastGithub.ReverseProxy/ReverseProxyApplicationBuilderExtensions.cs +++ b/FastGithub.ReverseProxy/ApplicationBuilderExtensions.cs @@ -5,9 +5,9 @@ using Microsoft.Extensions.DependencyInjection; namespace FastGithub { /// - /// https反向代理的中间件扩展 + /// ApplicationBuilder扩展 /// - public static class ReverseProxyApplicationBuilderExtensions + public static class ApplicationBuilderExtensions { /// /// 使用请求日志中间件 @@ -28,7 +28,7 @@ namespace FastGithub public static IApplicationBuilder UseReverseProxy(this IApplicationBuilder app) { var middleware = app.ApplicationServices.GetRequiredService(); - return app.Use(next => context => middleware.InvokeAsync(context, next)); + return app.Use(next => context => middleware.InvokeAsync(context)); } } } diff --git a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs index be65491..a6c8c43 100644 --- a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs +++ b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs @@ -16,30 +16,48 @@ namespace FastGithub public static class KestrelServerOptionsExtensions { /// - /// 监听http的反向代理 + /// 无限制 /// /// - public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel) + public static void NoLimit(this KestrelServerOptions kestrel) { - const int HTTP_PORT = 80; - var logger = kestrel.GetLogger(); + kestrel.Limits.MaxRequestBodySize = null; + } - if (LocalMachine.CanListenTcp(HTTP_PORT) == false) + /// + /// 监听ssh + /// + /// + public static void ListenSsh(this KestrelServerOptions kestrel) + { + const int SSH_PORT = 22; + if (LocalMachine.CanListenTcp(SSH_PORT) == true) { - logger.LogWarning($"由于tcp端口{HTTP_PORT}已经被其它进程占用,http反向代理功能将受限"); - } - else - { - kestrel.Listen(IPAddress.Any, HTTP_PORT); - logger.LogInformation($"已监听tcp端口{HTTP_PORT},http反向代理启动完成"); + kestrel.Listen(IPAddress.Any, SSH_PORT, listen => listen.UseConnectionHandler()); + kestrel.GetLogger().LogInformation($"已监听tcp端口{SSH_PORT},github的ssh代理启动完成"); } } /// - /// 监听https的反向代理 + /// 监听http /// /// - public static void ListenHttpsReverseProxy(this KestrelServerOptions kestrel) + public static void ListenHttp(this KestrelServerOptions kestrel) + { + const int HTTP_PORT = 80; + if (LocalMachine.CanListenTcp(HTTP_PORT) == true) + { + kestrel.Listen(IPAddress.Any, HTTP_PORT); + kestrel.GetLogger().LogInformation($"已监听tcp端口{HTTP_PORT},http反向代理启动完成"); + } + } + + /// + /// 监听https + /// + /// + /// + public static void ListenHttps(this KestrelServerOptions kestrel) { const int HTTPS_PORT = 443; if (OperatingSystem.IsWindows()) @@ -49,7 +67,7 @@ namespace FastGithub if (LocalMachine.CanListenTcp(HTTPS_PORT) == false) { - throw new FastGithubException($"由于tcp端口{HTTPS_PORT}已经被其它进程占用,{nameof(FastGithub)}无法进行必须的https反向代理"); + throw new FastGithubException($"tcp端口{HTTPS_PORT}已经被其它进程占用"); } var certService = kestrel.ApplicationServices.GetRequiredService(); @@ -65,25 +83,6 @@ namespace FastGithub logger.LogInformation($"已监听tcp端口{HTTPS_PORT},https反向代理启动完成"); } - /// - /// 监听github的ssh的代理 - /// - /// - public static void ListenGithubSshProxy(this KestrelServerOptions kestrel) - { - const int SSH_PORT = 22; - var logger = kestrel.GetLogger(); - - if (LocalMachine.CanListenTcp(SSH_PORT) == false) - { - logger.LogWarning($"由于tcp端口{SSH_PORT}已经被其它进程占用,github的ssh代理功能将受限"); - } - else - { - kestrel.Listen(IPAddress.Any, SSH_PORT, listen => listen.UseConnectionHandler()); - logger.LogInformation($"已监听tcp端口{SSH_PORT},github的ssh代理启动完成"); - } - } /// /// 获取日志 diff --git a/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs b/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs index 02965bb..6354098 100644 --- a/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs +++ b/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs @@ -17,6 +17,7 @@ namespace FastGithub.ReverseProxy private readonly IHttpClientFactory httpClientFactory; private readonly FastGithubConfig fastGithubConfig; private readonly ILogger logger; + private readonly DomainConfig defaultDomainConfig = new() { TlsSni = true }; public ReverseProxyMiddleware( IHttpForwarder httpForwarder, @@ -34,16 +35,24 @@ namespace FastGithub.ReverseProxy /// 处理请求 /// /// - /// /// - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async Task InvokeAsync(HttpContext context) { var host = context.Request.Host.Host; if (this.fastGithubConfig.TryGetDomainConfig(host, out var domainConfig) == false) { - await next(context); + domainConfig = this.defaultDomainConfig; } - else if (domainConfig.Response != null) + + if (domainConfig.Response == null) + { + var scheme = context.Request.Scheme; + var destinationPrefix = GetDestinationPrefix(scheme, host, domainConfig.Destination); + var httpClient = this.httpClientFactory.CreateHttpClient(domainConfig); + var error = await httpForwarder.SendAsync(context, destinationPrefix, httpClient); + await HandleErrorAsync(context, error); + } + else { context.Response.StatusCode = domainConfig.Response.StatusCode; context.Response.ContentType = domainConfig.Response.ContentType; @@ -52,14 +61,6 @@ namespace FastGithub.ReverseProxy await context.Response.WriteAsync(domainConfig.Response.ContentValue); } } - else - { - var scheme = context.Request.Scheme; - var destinationPrefix = GetDestinationPrefix(scheme, host, domainConfig.Destination); - var httpClient = this.httpClientFactory.CreateHttpClient(domainConfig); - var error = await httpForwarder.SendAsync(context, destinationPrefix, httpClient); - await HandleErrorAsync(context, error); - } } /// @@ -91,13 +92,7 @@ namespace FastGithub.ReverseProxy /// private static async Task HandleErrorAsync(HttpContext context, ForwarderError error) { - if (error == ForwarderError.None) - { - return; - } - - var errorFeature = context.GetForwarderErrorFeature(); - if (errorFeature == null || context.Response.HasStarted) + if (error == ForwarderError.None || context.Response.HasStarted) { return; } @@ -105,7 +100,7 @@ namespace FastGithub.ReverseProxy await context.Response.WriteAsJsonAsync(new { error = error.ToString(), - message = errorFeature.Exception?.Message + message = context.GetForwarderErrorFeature()?.Exception?.Message }); } } diff --git a/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs b/FastGithub.ReverseProxy/ServiceCollectionExtensions.cs similarity index 91% rename from FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs rename to FastGithub.ReverseProxy/ServiceCollectionExtensions.cs index ea7bb81..bd9598f 100644 --- a/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs +++ b/FastGithub.ReverseProxy/ServiceCollectionExtensions.cs @@ -6,7 +6,7 @@ namespace FastGithub /// /// https反向代理的服务注册扩展 /// - public static class ReverseProxyServiceCollectionExtensions + public static class ServiceCollectionExtensions { /// /// 添加https反向代理 diff --git a/FastGithub/Controllers/CertController.cs b/FastGithub/Controllers/CertController.cs deleted file mode 100644 index 4654a0f..0000000 --- a/FastGithub/Controllers/CertController.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using System.Threading.Tasks; - -namespace FastGithub.Controllers -{ - /// - /// 证书控制器 - /// - public class CertController : Controller - { - /// - /// 下载CA证书 - /// - /// - public async Task Index() - { - var certFile = $"CACert/{nameof(FastGithub)}.cer"; - this.Response.ContentType = "application/x-x509-ca-cert"; - this.Response.Headers.Add("Content-Disposition", $"attachment;filename={nameof(FastGithub)}.cer"); - await this.Response.SendFileAsync(certFile); - return new EmptyResult(); - } - } -} diff --git a/FastGithub/Controllers/HomeController.cs b/FastGithub/Controllers/HomeController.cs deleted file mode 100644 index cce46c5..0000000 --- a/FastGithub/Controllers/HomeController.cs +++ /dev/null @@ -1,27 +0,0 @@ -using FastGithub.Configuration; -using FastGithub.Models; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Options; - -namespace FastGithub.Controllers -{ - /// - /// 首页控制器 - /// - public class HomeController : Controller - { - /// - /// 首页 - /// - /// - public IActionResult Index() - { - var model = new Home - { - Version = ProductionVersion.Current?.ToString(), - ProjectUri = "https://github.com/dotnetcore/FastGithub" - }; - return View(model); - } - } -} diff --git a/FastGithub/Models/Home.cs b/FastGithub/Models/Home.cs deleted file mode 100644 index 2c2435e..0000000 --- a/FastGithub/Models/Home.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace FastGithub.Models -{ - /// - /// 首页模型 - /// - public class Home - { - /// - /// 获取版本号 - /// - public string? Version { get; set; } - - /// - /// 请求域名或ip - /// - public string? ProjectUri { get; set; } - } -} diff --git a/FastGithub/Program.cs b/FastGithub/Program.cs index e422d99..5acacfc 100644 --- a/FastGithub/Program.cs +++ b/FastGithub/Program.cs @@ -51,10 +51,10 @@ namespace FastGithub webBuilder.UseShutdownTimeout(TimeSpan.FromSeconds(2d)); webBuilder.UseKestrel(kestrel => { - kestrel.Limits.MaxRequestBodySize = null; - kestrel.ListenHttpsReverseProxy(); - kestrel.ListenHttpReverseProxy(); - kestrel.ListenGithubSshProxy(); + kestrel.NoLimit(); + kestrel.ListenSsh(); + kestrel.ListenHttp(); + kestrel.ListenHttps(); }); webBuilder.UseSerilog((hosting, logger) => { diff --git a/FastGithub/Startup.cs b/FastGithub/Startup.cs index 185753a..7198386 100644 --- a/FastGithub/Startup.cs +++ b/FastGithub/Startup.cs @@ -29,13 +29,11 @@ namespace FastGithub { services.Configure(this.Configuration.GetSection(nameof(FastGithub))); - services.AddConfiguration(); - services.AddDnsServer(); + services.AddConfiguration(); services.AddDomainResolve(); + services.AddDnsServer(); services.AddHttpClient(); services.AddReverseProxy(); - - services.AddControllersWithViews(); services.AddHostedService(); } @@ -48,11 +46,6 @@ namespace FastGithub app.UseRequestLogging(); app.UseDnsOverHttps(); app.UseReverseProxy(); - app.UseRouting(); - app.UseEndpoints(endpoints => - { - endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}"); - }); } } } diff --git a/FastGithub/Views/Home/Index.cshtml b/FastGithub/Views/Home/Index.cshtml deleted file mode 100644 index 5ba930e..0000000 --- a/FastGithub/Views/Home/Index.cshtml +++ /dev/null @@ -1,44 +0,0 @@ - -@{ - Layout = null; -} -@model Home - - - - - - - - - FastGithub - - - -
-
-
- - -

FastGithub

-

github加速神器,解决github打不开、用户头像无法加载、releases无法上传下载、git-clone、git-pull、git-push失败等问题

- -

软件信息

-

- 安装版本: - v@(Model?.Version) -

-

- 项目地址: - @(Model?.ProjectUri) -

- -

CA证书

-

- 你可能需要在客户端设备下载FastGithub自颁发的CA证书FastGithub.cer,导入到受信任的根证书颁发机构或浏览器 -

-
-
-
- - diff --git a/FastGithub/Views/_ViewImports.cshtml b/FastGithub/Views/_ViewImports.cshtml deleted file mode 100644 index 654e8fd..0000000 --- a/FastGithub/Views/_ViewImports.cshtml +++ /dev/null @@ -1,3 +0,0 @@ -@using FastGithub -@using FastGithub.Models -@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers \ No newline at end of file diff --git a/FastGithub/Views/_ViewStart.cshtml b/FastGithub/Views/_ViewStart.cshtml deleted file mode 100644 index 5f28270..0000000 --- a/FastGithub/Views/_ViewStart.cshtml +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file