支持ip访问
This commit is contained in:
parent
e9439541c3
commit
f736fb6be8
@ -16,6 +16,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using X509Certificate2 = System.Security.Cryptography.X509Certificates.X509Certificate2;
|
using X509Certificate2 = System.Security.Cryptography.X509Certificates.X509Certificate2;
|
||||||
|
|
||||||
@ -114,7 +115,17 @@ namespace FastGithub.ReverseProxy
|
|||||||
certGenerator.AddExtension(X509Extensions.BasicConstraints, extension.IsCritical, extension.GetParsedValue());
|
certGenerator.AddExtension(X509Extensions.BasicConstraints, extension.IsCritical, extension.GetParsedValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
var names = domains.Select(domain => new GeneralName(GeneralName.DnsName, domain)).ToArray();
|
var names = domains.Select(domain =>
|
||||||
|
{
|
||||||
|
var nameType = GeneralName.DnsName;
|
||||||
|
if (IPAddress.TryParse(domain, out _))
|
||||||
|
{
|
||||||
|
nameType = GeneralName.IPAddress;
|
||||||
|
}
|
||||||
|
return new GeneralName(nameType, domain);
|
||||||
|
|
||||||
|
}).ToArray();
|
||||||
|
|
||||||
var subjectAltName = new GeneralNames(names);
|
var subjectAltName = new GeneralNames(names);
|
||||||
certGenerator.AddExtension(X509Extensions.SubjectAlternativeName, false, subjectAltName);
|
certGenerator.AddExtension(X509Extensions.SubjectAlternativeName, false, subjectAltName);
|
||||||
return certGenerator.Generate(signatureFactory);
|
return certGenerator.Generate(signatureFactory);
|
||||||
|
|||||||
@ -5,6 +5,11 @@ using Microsoft.Extensions.DependencyInjection;
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.NetworkInformation;
|
||||||
|
using System.Net.Sockets;
|
||||||
using System.Security.Cryptography.X509Certificates;
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
@ -35,25 +40,58 @@ namespace FastGithub
|
|||||||
kestrel.ListenAnyIP(443, listen =>
|
kestrel.ListenAnyIP(443, listen =>
|
||||||
listen.UseHttps(https =>
|
listen.UseHttps(https =>
|
||||||
https.ServerCertificateSelector = (ctx, domain) =>
|
https.ServerCertificateSelector = (ctx, domain) =>
|
||||||
GetOrCreateCert(domain)));
|
GetDomainCert(domain, caPublicCerPath, caPrivateKeyPath)));
|
||||||
|
|
||||||
logger.LogInformation("https反向代理服务启动成功");
|
logger.LogInformation("https反向代理服务启动成功");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取颁发给指定域名的证书
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="domain"></param>
|
||||||
|
/// <param name="caPublicCerPath"></param>
|
||||||
|
/// <param name="caPrivateKeyPath"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static X509Certificate2 GetDomainCert(string domain, string caPublicCerPath, string caPrivateKeyPath)
|
||||||
|
{
|
||||||
|
return domainCerts.GetOrAdd(domain, GetOrCreateCert).Value;
|
||||||
|
|
||||||
X509Certificate2 GetOrCreateCert(string key)
|
Lazy<X509Certificate2> GetOrCreateCert(string host)
|
||||||
{
|
{
|
||||||
if (key == string.Empty)
|
return new Lazy<X509Certificate2>(() =>
|
||||||
{
|
{
|
||||||
key = "github.com";
|
var domains = GetDomains(host).Distinct();
|
||||||
}
|
|
||||||
|
|
||||||
return domainCerts.GetOrAdd(key, domain => new Lazy<X509Certificate2>(() =>
|
|
||||||
{
|
|
||||||
var domains = new[] { domain };
|
|
||||||
var validFrom = DateTime.Today.AddYears(-1);
|
var validFrom = DateTime.Today.AddYears(-1);
|
||||||
var validTo = DateTime.Today.AddYears(10);
|
var validTo = DateTime.Today.AddYears(10);
|
||||||
return CertGenerator.Generate(domains, 2048, validFrom, validTo, caPublicCerPath, caPrivateKeyPath);
|
return CertGenerator.Generate(domains, 2048, validFrom, validTo, caPublicCerPath, caPrivateKeyPath);
|
||||||
}, LazyThreadSafetyMode.ExecutionAndPublication)).Value;
|
}, LazyThreadSafetyMode.ExecutionAndPublication);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取域名
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="host"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static IEnumerable<string> GetDomains(string host)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(host) == false)
|
||||||
|
{
|
||||||
|
yield return host;
|
||||||
|
}
|
||||||
|
|
||||||
|
yield return Environment.MachineName;
|
||||||
|
yield return IPAddress.Loopback.ToString();
|
||||||
|
|
||||||
|
foreach (var @interface in NetworkInterface.GetAllNetworkInterfaces())
|
||||||
|
{
|
||||||
|
foreach (var addressInfo in @interface.GetIPProperties().UnicastAddresses)
|
||||||
|
{
|
||||||
|
if (addressInfo.Address.AddressFamily == AddressFamily.InterNetwork)
|
||||||
|
{
|
||||||
|
yield return addressInfo.Address.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user