From 6b15437abd55d4122ee8b7369b9dac4595af6f79 Mon Sep 17 00:00:00 2001
From: xljiulang <366193849@qq.com>
Date: Thu, 15 Jul 2021 00:21:11 +0800
Subject: [PATCH] fix bug
---
FastGithub.Dns/DnsHostedService.cs | 45 ++++++++++++---
.../KestrelServerOptionsExtensions.cs | 56 +++++++++----------
2 files changed, 65 insertions(+), 36 deletions(-)
diff --git a/FastGithub.Dns/DnsHostedService.cs b/FastGithub.Dns/DnsHostedService.cs
index 1817ce6..88393ff 100644
--- a/FastGithub.Dns/DnsHostedService.cs
+++ b/FastGithub.Dns/DnsHostedService.cs
@@ -12,7 +12,7 @@ namespace FastGithub.Dns
///
/// dns后台服务
///
- sealed class DnsHostedService : IHostedService
+ sealed class DnsHostedService : BackgroundService
{
private readonly DnsServer dnsServer;
private readonly IOptions options;
@@ -31,22 +31,51 @@ namespace FastGithub.Dns
ILogger logger)
{
this.dnsServer = new DnsServer(githubRequestResolver, options.Value.UpStream);
+ this.dnsServer.Listening += DnsServer_Listening;
+ this.dnsServer.Errored += DnsServer_Errored;
this.options = options;
this.logger = logger;
}
///
- /// 启动dns服务
+ /// 监听后
///
- ///
- ///
- public Task StartAsync(CancellationToken cancellationToken)
+ ///
+ ///
+ private void DnsServer_Listening(object? sender, EventArgs e)
{
- this.dnsServer.Listen();
this.logger.LogInformation("dns服务启动成功");
this.dnsAddresses = this.SetNameServers(IPAddress.Loopback, this.options.Value.UpStream);
+ }
- return Task.CompletedTask;
+ ///
+ /// dns异常
+ ///
+ ///
+ ///
+ private void DnsServer_Errored(object? sender, DnsServer.ErroredEventArgs e)
+ {
+ if (e.Exception is not OperationCanceledException)
+ {
+ this.logger.LogError($"dns服务异常:{e.Exception.Message}");
+ }
+ }
+
+ ///
+ /// 启动dns
+ ///
+ ///
+ ///
+ protected async override Task ExecuteAsync(CancellationToken stoppingToken)
+ {
+ try
+ {
+ await this.dnsServer.Listen();
+ }
+ catch (Exception ex)
+ {
+ this.logger.LogWarning($"dns服务启动失败:{ex.Message}");
+ }
}
///
@@ -54,7 +83,7 @@ namespace FastGithub.Dns
///
///
///
- public Task StopAsync(CancellationToken cancellationToken)
+ public override Task StopAsync(CancellationToken cancellationToken)
{
this.dnsServer.Dispose();
this.logger.LogInformation("dns服务已终止");
diff --git a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs
index b15a742..262bbb2 100644
--- a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs
+++ b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs
@@ -6,6 +6,7 @@ using Microsoft.Extensions.Logging;
using System;
using System.Collections.Concurrent;
using System.Security.Cryptography.X509Certificates;
+using System.Threading;
namespace FastGithub
{
@@ -14,6 +15,11 @@ namespace FastGithub
///
public static class KestrelServerOptionsExtensions
{
+ ///
+ /// 域名与证书
+ ///
+ private static readonly ConcurrentDictionary> domainCerts = new();
+
///
/// 监听github的反向代理
///
@@ -26,8 +32,29 @@ namespace FastGithub
var logger = loggerFactory.CreateLogger($"{nameof(FastGithub)}{nameof(ReverseProxy)}");
TryInstallCaCert(caPublicCerPath, logger);
- kestrel.ListenAnyIP(443, listen => listen.UseGithubHttps(caPublicCerPath, caPrivateKeyPath));
+ kestrel.ListenAnyIP(443, listen =>
+ listen.UseHttps(https =>
+ https.ServerCertificateSelector = (ctx, domain) =>
+ GetOrCreateCert(domain)));
+
logger.LogInformation("反向代理服务启动成功");
+
+
+ X509Certificate2 GetOrCreateCert(string key)
+ {
+ if (key == string.Empty)
+ {
+ key = "github.com";
+ }
+
+ return domainCerts.GetOrAdd(key, domain => new Lazy(() =>
+ {
+ var domains = new[] { domain };
+ var validFrom = DateTime.Today.AddYears(-1);
+ var validTo = DateTime.Today.AddYears(10);
+ return CertGenerator.Generate(domains, 2048, validFrom, validTo, caPublicCerPath, caPrivateKeyPath);
+ }, LazyThreadSafetyMode.ExecutionAndPublication)).Value;
+ }
}
///
@@ -57,32 +84,5 @@ namespace FastGithub
}
}
- ///
- /// 应用fastGihub的https
- ///
- ///
- ///
- ///
- ///
- private static ListenOptions UseGithubHttps(this ListenOptions listenOptions, string caPublicCerPath, string caPrivateKeyPath)
- {
- return listenOptions.UseHttps(https =>
- {
- var certs = new ConcurrentDictionary();
- https.ServerCertificateSelector = (ctx, domain) => certs.GetOrAdd(domain, CreateCert);
- });
-
- X509Certificate2 CreateCert(string domain)
- {
- if (domain == string.Empty)
- {
- domain = "github.com";
- }
- var domains = new[] { domain };
- var validFrom = DateTime.Today.AddYears(-1);
- var validTo = DateTime.Today.AddYears(10);
- return CertGenerator.Generate(domains, 2048, validFrom, validTo, caPublicCerPath, caPrivateKeyPath);
- }
- }
}
}