diff --git a/FastGithub.Dns/DnsHostedService.cs b/FastGithub.Dns/DnsHostedService.cs index 844e4a7..1817ce6 100644 --- a/FastGithub.Dns/DnsHostedService.cs +++ b/FastGithub.Dns/DnsHostedService.cs @@ -43,7 +43,7 @@ namespace FastGithub.Dns public Task StartAsync(CancellationToken cancellationToken) { this.dnsServer.Listen(); - this.logger.LogInformation("dns服务启用成功"); + this.logger.LogInformation("dns服务启动成功"); this.dnsAddresses = this.SetNameServers(IPAddress.Loopback, this.options.Value.UpStream); return Task.CompletedTask; diff --git a/FastGithub.ReverseProxy/GithubDnsHttpHandler.cs b/FastGithub.ReverseProxy/GithubDnsHttpHandler.cs index e042e3d..2eb04a0 100644 --- a/FastGithub.ReverseProxy/GithubDnsHttpHandler.cs +++ b/FastGithub.ReverseProxy/GithubDnsHttpHandler.cs @@ -1,4 +1,5 @@ using FastGithub.Scanner; +using Microsoft.Extensions.Logging; using System; using System.Net.Http; using System.Threading; @@ -13,16 +14,21 @@ namespace FastGithub.ReverseProxy sealed class GithubDnsHttpHandler : DelegatingHandler { private readonly IGithubScanResults scanResults; + private readonly ILogger logger; /// /// Github的dns解析的httpHandler /// /// /// - public GithubDnsHttpHandler(IGithubScanResults scanResults, HttpMessageHandler innerHandler) + public GithubDnsHttpHandler( + IGithubScanResults scanResults, + HttpMessageHandler innerHandler, + ILogger logger) : base(innerHandler) { this.scanResults = scanResults; + this.logger = logger; } /// @@ -39,6 +45,7 @@ namespace FastGithub.ReverseProxy var address = this.scanResults.FindBestAddress(uri.Host); if (address != null) { + this.logger.LogInformation($"使用{address}请求{uri.Host}"); var builder = new UriBuilder(uri) { Host = address.ToString() diff --git a/FastGithub.ReverseProxy/ListenOptionsHttpsExtensions.cs b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs similarity index 57% rename from FastGithub.ReverseProxy/ListenOptionsHttpsExtensions.cs rename to FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs index e1dd115..5755cdf 100644 --- a/FastGithub.ReverseProxy/ListenOptionsHttpsExtensions.cs +++ b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs @@ -5,23 +5,45 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Collections.Concurrent; +using System.IO; using System.Security.Cryptography.X509Certificates; namespace FastGithub { /// - /// ListenOptions扩展 + /// Kestrel扩展 /// - public static class ListenOptionsHttpsExtensions + public static class KestrelServerOptionsExtensions { /// - /// 应用fastGihub的https + /// 监听github的反向代理 /// - /// + /// /// /// - /// - public static ListenOptions UseGithubHttps(this ListenOptions listenOptions, string caPublicCerPath, string caPrivateKeyPath) + public static void ListenGithubReverseProxy(this KestrelServerOptions kestrel, string caPublicCerPath, string caPrivateKeyPath) + { + var loggerFactory = kestrel.ApplicationServices.GetRequiredService(); + var logger = loggerFactory.CreateLogger($"{nameof(FastGithub)}{nameof(ReverseProxy)}"); + TryInstallCaCert(caPublicCerPath, logger); + + try + { + kestrel.ListenAnyIP(443, listen => listen.UseGithubHttps(caPublicCerPath, caPrivateKeyPath)); + logger.LogInformation("反向代理服务启动成功"); + } + catch (IOException ex) + { + logger.LogError($"无法开启反向代理功能:{ex.Message}"); + } + } + + /// + /// 安装根证书 + /// + /// + /// + private static void TryInstallCaCert(string caPublicCerPath, ILogger logger) { if (OperatingSystem.IsWindows()) { @@ -38,12 +60,20 @@ namespace FastGithub } catch (Exception ex) { - var loggerFactory = listenOptions.ApplicationServices.GetRequiredService(); - var logger = loggerFactory.CreateLogger($"{nameof(FastGithub)}{nameof(ReverseProxy)}"); logger.LogError($"安装根证书{caPublicCerPath}失败:{ex.Message}"); } } + } + /// + /// 应用fastGihub的https + /// + /// + /// + /// + /// + private static ListenOptions UseGithubHttps(this ListenOptions listenOptions, string caPublicCerPath, string caPrivateKeyPath) + { return listenOptions.UseHttps(https => { var certs = new ConcurrentDictionary(); diff --git a/FastGithub.ReverseProxy/NoneSniHttpClientFactory.cs b/FastGithub.ReverseProxy/NoneSniHttpClientFactory.cs index fd44d17..59ab8cd 100644 --- a/FastGithub.ReverseProxy/NoneSniHttpClientFactory.cs +++ b/FastGithub.ReverseProxy/NoneSniHttpClientFactory.cs @@ -1,5 +1,6 @@ using FastGithub.Scanner; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using System; using System.Net.Http; using System.Net.Security; @@ -14,6 +15,7 @@ namespace FastGithub.ReverseProxy sealed class NoneSniHttpClientFactory { private readonly IGithubScanResults githubScanResults; + private readonly ILogger logger; /// /// 生命周期 @@ -35,9 +37,12 @@ namespace FastGithub.ReverseProxy /// 禁用tls sni的HttpClient工厂 /// /// - public NoneSniHttpClientFactory(IGithubScanResults githubScanResults) + public NoneSniHttpClientFactory( + IGithubScanResults githubScanResults, + ILogger logger) { this.githubScanResults = githubScanResults; + this.logger = logger; this.lifeTimeHttpHandlerLazy = new Lazy(this.CreateHttpHandler, true); } @@ -78,7 +83,7 @@ namespace FastGithub.ReverseProxy } }; - var dnsHandler = new GithubDnsHttpHandler(this.githubScanResults, noneSniHandler); + var dnsHandler = new GithubDnsHttpHandler(this.githubScanResults, noneSniHandler, this.logger); return new LifetimeHttpHandler(dnsHandler, this.lifeTime, this.OnHttpHandlerDeactivate); } diff --git a/FastGithub/FastGithub_CA.cer b/FastGithub/FastGithub.cer similarity index 100% rename from FastGithub/FastGithub_CA.cer rename to FastGithub/FastGithub.cer diff --git a/FastGithub/FastGithub.csproj b/FastGithub/FastGithub.csproj index 5ba4faf..806f03b 100644 --- a/FastGithub/FastGithub.csproj +++ b/FastGithub/FastGithub.csproj @@ -25,19 +25,16 @@ - - PreserveNewest - PreserveNewest PreserveNewest - + PreserveNewest - + PreserveNewest diff --git a/FastGithub/FastGithub_CA.key b/FastGithub/FastGithub.key similarity index 100% rename from FastGithub/FastGithub_CA.key rename to FastGithub/FastGithub.key diff --git a/FastGithub/Program.cs b/FastGithub/Program.cs index 3d5197b..7c701ec 100644 --- a/FastGithub/Program.cs +++ b/FastGithub/Program.cs @@ -43,15 +43,8 @@ namespace FastGithub }) .ConfigureWebHostDefaults(web => { - web.Configure(app => - { - app.UseGithubReverseProxy(); - }); - - web.UseKestrel(kestrel => - { - kestrel.ListenAnyIP(443, listen => listen.UseGithubHttps("FastGithub_CA.cer", "FastGithub_CA.key")); - }); + web.Configure(app => app.UseGithubReverseProxy()); + web.UseKestrel(kestrel => kestrel.ListenGithubReverseProxy("FastGithub.cer", "FastGithub.key")); }); } } diff --git a/FastGithub/Properties/launchSettings.json b/FastGithub/Properties/launchSettings.json index 20866f8..b100c0a 100644 --- a/FastGithub/Properties/launchSettings.json +++ b/FastGithub/Properties/launchSettings.json @@ -7,7 +7,7 @@ "launchBrowser": true, "environmentVariables": { "DOTNET_ENVIRONMENT": "Development", - //"Logging__LogLevel__Default": "Trace" + "Logging__LogLevel__Default": "Trace" } } } diff --git a/FastGithub/README.MD b/FastGithub/README.MD index 29a7938..db68f4a 100644 --- a/FastGithub/README.MD +++ b/FastGithub/README.MD @@ -3,7 +3,7 @@ github定制版的dns服务,解析github最优的ip ### 安装根证书 默认开启github反向代理功能,开启之后dns返回github的ip指向FastGithub自身。 -需要在浏览器所在的设备安装FastGithub_CA.cer到`将所有的证书都放入下载存储\受信任的根证书颁发机构` +需要在浏览器所在的设备安装FastGithub.cer到`将所有的证书都放入下载存储\受信任的根证书颁发机构` ### 本机使用 * 运行FastGithub程序,本机的网络适配器的dns会自动变成127.0.0.1 diff --git a/FastGithub/appsettings.domain.json.example b/FastGithub/appsettings.domain.json.example deleted file mode 100644 index 450c534..0000000 --- a/FastGithub/appsettings.domain.json.example +++ /dev/null @@ -1,23 +0,0 @@ -{ - "Lookup": { // ip - "Domains": [ // ҵҪٵ - "domain.com", - "xx.domain.com", - "yy.domain.com" - ] - }, - "Scan": { // ɨ - "HttpsScan": { - "Rules": { // ɨȱʧĬHEADĸ· - "xx.domain.com": { - "Method": "HEAD", - "Path": "/xx/" - }, - "yy.domain.com": { - "Method": "HEAD", - "Path": "/yy/" - } - } - } - } -} diff --git a/FastGithub/appsettings.json b/FastGithub/appsettings.json index 1e4debe..0341e50 100644 --- a/FastGithub/appsettings.json +++ b/FastGithub/appsettings.json @@ -21,7 +21,6 @@ "8.8.8.8", "223.5.5.5", "123.125.81.6", - "180.76.76.76", "119.29.29.29", "208.67.220.220", "114.114.114.114" @@ -43,8 +42,10 @@ "Logging": { "LogLevel": { "Default": "Information", + "Yarp": "Warning", "System": "Warning", - "Microsoft": "Warning" + "Microsoft": "Warning", + "Microsoft.AspNetCore.Server.Kestrel": "Error" } } }