瀏覽代碼

better solution to certificate check, so it cannot spill over into other implementations

Scrublord1336 6 年之前
父節點
當前提交
9e9bdd0315

+ 1 - 0
src/XUnity.AutoTranslator.Plugin.Core/Web/BaiduTranslateEndpoint.cs

@@ -52,6 +52,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
 
       public override void ConfigureServicePointManager()
       {
+         ServicePointManager.ServerCertificateValidationCallback += Security.AlwaysAllowByHosts( "api.fanyi.baidu.com" );
       }
 
       public override bool TryExtractTranslated( string result, out string translated )

+ 1 - 12
src/XUnity.AutoTranslator.Plugin.Core/Web/GoogleTranslateEndpoint.cs

@@ -18,7 +18,6 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
 {
    public class GoogleTranslateEndpoint : KnownEndpoint
    {
-      private static readonly string CertificateIssuer = "CN=Google Internet Authority G3, O=Google Trust Services, C=US";
       private static readonly string HttpsServicePointTemplateUrl = "https://translate.googleapis.com/translate_a/single?client=t&dt=t&sl={0}&tl={1}&ie=UTF-8&oe=UTF-8&tk={2}&q={3}";
       private static readonly string FallbackHttpsServicePointTemplateUrl = "https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}";
       private static readonly string HttpsTranslateUserSite = "https://translate.google.com";
@@ -187,17 +186,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
 
       public override void ConfigureServicePointManager()
       {
-         try
-         {
-            ServicePointManager.ServerCertificateValidationCallback += ( sender, certificate, chain, sslPolicyErrors ) =>
-            {
-               return certificate.Issuer == CertificateIssuer;
-            };
-
-         }
-         catch
-         {
-         }
+         ServicePointManager.ServerCertificateValidationCallback += Security.AlwaysAllowByHosts( "translate.google.com", "translate.googleapis.com" );
       }
 
       public override bool TryExtractTranslated( string result, out string translated )

+ 28 - 0
src/XUnity.AutoTranslator.Plugin.Core/Web/Security.cs

@@ -0,0 +1,28 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Net.Security;
+using System.Security.Cryptography.X509Certificates;
+using System.Text;
+
+namespace XUnity.AutoTranslator.Plugin.Core.Web
+{
+   public static class Security
+   {
+      public static RemoteCertificateValidationCallback AlwaysAllowByHosts( params string[] hosts )
+      {
+         var lookup = new HashSet<string>( hosts, StringComparer.OrdinalIgnoreCase );
+
+         return ( sender, certificate, chain, sslPolicyErrors ) =>
+         {
+            var request = sender as HttpWebRequest;
+            if( request != null )
+            {
+               return lookup.Contains( request.Address.Host );
+            }
+            return false;
+         };
+      }
+   }
+}