浏览代码

增加远程证书验证

xljiulang 4 年之前
父节点
当前提交
f5698ef1e1
共有 1 个文件被更改,包括 8 次插入1 次删除
  1. 8 1
      FastGithub.ReverseProxy/HttpClientHanlder.cs

+ 8 - 1
FastGithub.ReverseProxy/HttpClientHanlder.cs

@@ -2,6 +2,7 @@
 using System.Net.Http;
 using System.Net.Security;
 using System.Net.Sockets;
+using System.Security.Cryptography.X509Certificates;
 using System.Threading;
 using System.Threading.Tasks;
 
@@ -51,9 +52,15 @@ namespace FastGithub.ReverseProxy
                     await sslStream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions
                     {
                         TargetHost = tlsSniContext.TlsSniPattern.Value,
-                        RemoteCertificateValidationCallback = delegate { return true; }
+                        RemoteCertificateValidationCallback = ValidateServerCertificate
                     }, cancellationToken);
                     return sslStream;
+
+                    // 这里最好需要验证证书的使用者和所有使用者可选名称
+                    static bool ValidateServerCertificate(object sender, X509Certificate? cert, X509Chain? chain, SslPolicyErrors errors)
+                    {
+                        return errors == SslPolicyErrors.None || errors == SslPolicyErrors.RemoteCertificateNameMismatch;
+                    }
                 }
             };
         }