浏览代码

custom user agent

Scrublord1336 6 年之前
父节点
当前提交
333018a370

+ 3 - 0
README.md

@@ -57,6 +57,9 @@ TrimAllText=True                 ;Indicates whether spaces in front and behind t
 UseStaticTranslations=True       ;Indicates whether or not to use translations from the included static translation cache
 OverrideFont=                    ;Overrides the fonts used for texts when updating text components. NOTE: Only works for UGUI
 
+[Http]
+UserAgent=                       ;Override the user agent used by APIs requiring a user agent
+
 [GoogleLegitimate]
 GoogleAPIKey=                    ;OPTIONAL, needed if GoogleTranslateLegitimate is configured
 

+ 13 - 3
src/XUnity.AutoTranslator.Plugin.Core/AutoTranslationPlugin.cs

@@ -434,6 +434,15 @@ namespace XUnity.AutoTranslator.Plugin.Core
          _frameForLastQueuedTranslation = currentFrame;
       }
 
+      public void PeriodicResetFrameCheck()
+      {
+         var currentSecond = (int)Time.time;
+         if( currentSecond % 100 == 0 )
+         {
+            _consecutiveFramesTranslated = 0;
+         }
+      }
+
       private void CheckStaggerText( string untranslatedText )
       {
          if( _previouslyQueuedText != null )
@@ -949,7 +958,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
                      _ongoingOperations.Remove( ui );
                   }
                }
-               else if ( !isSpammer || ( isSpammer && IsShortText( text ) ) )
+               else if( !isSpammer || ( isSpammer && IsShortText( text ) ) )
                {
                   // Lets try not to spam a service that might not be there...
                   if( _endpoint != null )
@@ -1072,6 +1081,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
             if( !Settings.IsShutdown )
             {
+               PeriodicResetFrameCheck();
                IncrementBatchOperations();
                ResetThresholdTimerIfRequired();
                KickoffTranslations();
@@ -1278,7 +1288,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
                if( _endpoint.ShouldGetSecondChanceAfterFailure() )
                {
                   Logger.Current.Warn( $"More than {Settings.MaxErrors} consecutive errors occurred. Entering fallback mode." );
-                  _consecutiveErrors = 0;
+                  _consecutiveErrors = Settings.MaxErrors;
                }
                else
                {
@@ -1310,7 +1320,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
                if( _endpoint.ShouldGetSecondChanceAfterFailure() )
                {
                   Logger.Current.Warn( $"More than {Settings.MaxErrors} consecutive errors occurred. Entering fallback mode." );
-                  _consecutiveErrors = 0;
+                  _consecutiveErrors = Settings.MaxErrors;
                }
                else
                {

+ 12 - 0
src/XUnity.AutoTranslator.Plugin.Core/Configuration/Settings.cs

@@ -69,6 +69,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
       public static string GoogleAPIKey;
       public static bool UseStaticTranslations;
       public static string OverrideFont;
+      public static string UserAgent;
 
       public static bool CopyToClipboard;
       public static int MaxClipboardCopyCharacters;
@@ -120,6 +121,8 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
          UseStaticTranslations = Config.Current.Preferences[ "Behaviour" ][ "UseStaticTranslations" ].GetOrDefault( true );
          OverrideFont = Config.Current.Preferences[ "Behaviour" ][ "OverrideFont" ].GetOrDefault( string.Empty );
 
+         UserAgent = Config.Current.Preferences[ "Http" ][ "UserAgent" ].GetOrDefault( string.Empty );
+
          GoogleAPIKey = Config.Current.Preferences[ "GoogleLegitimate" ][ "GoogleAPIKey" ].GetOrDefault( "" );
 
          BaiduAppId = Config.Current.Preferences[ "Baidu" ][ "BaiduAppId" ].GetOrDefault( "" );
@@ -165,5 +168,14 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
             }
          }
       }
+
+      public static string GetUserAgent( string defaultUserAgent )
+      {
+         if( !string.IsNullOrEmpty( UserAgent ) )
+         {
+            return UserAgent;
+         }
+         return defaultUserAgent;
+      }
    }
 }

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

@@ -38,7 +38,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
 
       public override void ApplyHeaders( WebHeaderCollection headers )
       {
-         headers[ HttpRequestHeader.UserAgent ] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36";
+         headers[ HttpRequestHeader.UserAgent ] = Settings.GetUserAgent( "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" );
          headers[ HttpRequestHeader.AcceptCharset ] = "UTF-8";
       }
 

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

@@ -22,7 +22,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
 
       public override void ApplyHeaders( Dictionary<string, string> headers )
       {
-         headers[ "User-Agent" ] = "Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D257 Safari/9537.53";
+         headers[ "User-Agent" ] = Settings.GetUserAgent( "Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D257 Safari/9537.53" );
          headers[ "Accept" ] = "text/html";
          //headers[ "Accept-Charset" ] = "UTF-8";
          //headers[ "DNT" ] = "1";

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

@@ -18,7 +18,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
 {
    public class GoogleTranslateEndpoint : KnownHttpEndpoint
    {
-      protected static readonly ConstructorInfo WwwConstructor = Constants.Types.WWW?.GetConstructor( new[] { typeof( string ), typeof( byte[] ), typeof( Dictionary<string, string> ) } );
+      //protected static readonly ConstructorInfo WwwConstructor = Constants.Types.WWW?.GetConstructor( new[] { typeof( string ), typeof( byte[] ), typeof( Dictionary<string, string> ) } );
       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";
@@ -29,7 +29,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
       private bool _hasFallenBack = false;
       private bool _hasSetup = false;
       private bool _hasSetupCustomUserAgent = false;
-      private string _popularUserAgent;
+      //private string _popularUserAgent;
       private long m = 425635;
       private long s = 1953544246;
 
@@ -46,7 +46,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
 
       public override void ApplyHeaders( WebHeaderCollection headers )
       {
-         headers[ HttpRequestHeader.UserAgent ] = _popularUserAgent ?? DefaultUserAgent;
+         headers[ HttpRequestHeader.UserAgent ] = Settings.GetUserAgent( DefaultUserAgent );
          headers[ HttpRequestHeader.AcceptLanguage ] = "en-US,en;q=0.9";
          headers[ HttpRequestHeader.Accept ] = "*/*";
          headers[ HttpRequestHeader.Referer ] = "https://translate.google.com/";
@@ -54,18 +54,18 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
 
       public override IEnumerator OnBeforeTranslate( int translationCount )
       {
-         if( !_hasSetupCustomUserAgent )
-         {
-            _hasSetupCustomUserAgent = true;
-
-            // setup dynamic user agent
-            var enumerator = SetupDynamicUserAgent();
-            while( enumerator.MoveNext() )
-            {
-               var current = enumerator.Current;
-               if( current != null ) yield return current;
-            }
-         }
+         //if( !_hasSetupCustomUserAgent )
+         //{
+         //   _hasSetupCustomUserAgent = true;
+
+         //   // setup dynamic user agent
+         //   var enumerator = SetupDynamicUserAgent();
+         //   while( enumerator.MoveNext() )
+         //   {
+         //      var current = enumerator.Current;
+         //      if( current != null ) yield return current;
+         //   }
+         //}
 
          if( !_hasSetup || translationCount % 100 == 0 )
          {
@@ -82,62 +82,62 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
          }
       }
 
-      public IEnumerator SetupDynamicUserAgent()
-      {
-         // have to use WWW for this because unity mono is broken
-
-         if( WwwConstructor != null )
-         {
-            object www;
-            try
-            {
-               var headers = new Dictionary<string, string>();
-               www = WwwConstructor.Invoke( new object[] { UserAgentRepository, null, headers } );
-            }
-            catch( Exception e )
-            {
-               Logger.Current.Warn( e, "An error occurred while retrieving dynamic user agent." );
-               yield break;
-            }
-
-            yield return www;
-
-            try
-            {
-               var error = (string)AccessTools.Property( Constants.Types.WWW, "error" ).GetValue( www, null );
-               if( error == null )
-               {
-                  var text = (string)AccessTools.Property( Constants.Types.WWW, "text" ).GetValue( www, null );
-                  var userAgents = text.GetBetween( "<textarea rows=\"10\" class=\"get-the-list\" onclick=\"this.select()\" readonly=\"readonly\">", "</textarea>" );
-                  if( userAgents.Length > 42 )
-                  {
-                     var reader = new StringReader( userAgents );
-                     var popularUserAgent = reader.ReadLine();
-                     if( popularUserAgent.Length > 30 && popularUserAgent.Length < 300 && popularUserAgent.StartsWith( "Mozilla/" ) )
-                     {
-                        _popularUserAgent = popularUserAgent;
-                     }
-                     else
-                     {
-                        Logger.Current.Warn( "An error occurred while retrieving dynamic user agent. Could not find a user agent in returned html." );
-                     }
-                  }
-                  else
-                  {
-                     Logger.Current.Warn( "An error occurred while retrieving dynamic user agent. Could not find a user agent in returned html." );
-                  }
-               }
-               else
-               {
-                  Logger.Current.Warn( "An error occurred while retrieving dynamic user agent. Request failed: " + Environment.NewLine + error );
-               }
-            }
-            catch( Exception e )
-            {
-               Logger.Current.Warn( e, "An error occurred while retrieving dynamic user agent." );
-            }
-         }
-      }
+      //public IEnumerator SetupDynamicUserAgent()
+      //{
+      //   // have to use WWW for this because unity mono is broken
+
+      //   if( WwwConstructor != null )
+      //   {
+      //      object www;
+      //      try
+      //      {
+      //         var headers = new Dictionary<string, string>();
+      //         www = WwwConstructor.Invoke( new object[] { UserAgentRepository, null, headers } );
+      //      }
+      //      catch( Exception e )
+      //      {
+      //         Logger.Current.Warn( e, "An error occurred while retrieving dynamic user agent." );
+      //         yield break;
+      //      }
+
+      //      yield return www;
+
+      //      try
+      //      {
+      //         var error = (string)AccessTools.Property( Constants.Types.WWW, "error" ).GetValue( www, null );
+      //         if( error == null )
+      //         {
+      //            var text = (string)AccessTools.Property( Constants.Types.WWW, "text" ).GetValue( www, null );
+      //            var userAgents = text.GetBetween( "<textarea rows=\"10\" class=\"get-the-list\" onclick=\"this.select()\" readonly=\"readonly\">", "</textarea>" );
+      //            if( userAgents.Length > 42 )
+      //            {
+      //               var reader = new StringReader( userAgents );
+      //               var popularUserAgent = reader.ReadLine();
+      //               if( popularUserAgent.Length > 30 && popularUserAgent.Length < 300 && popularUserAgent.StartsWith( "Mozilla/" ) )
+      //               {
+      //                  _popularUserAgent = popularUserAgent;
+      //               }
+      //               else
+      //               {
+      //                  Logger.Current.Warn( "An error occurred while retrieving dynamic user agent. Could not find a user agent in returned html." );
+      //               }
+      //            }
+      //            else
+      //            {
+      //               Logger.Current.Warn( "An error occurred while retrieving dynamic user agent. Could not find a user agent in returned html." );
+      //            }
+      //         }
+      //         else
+      //         {
+      //            Logger.Current.Warn( "An error occurred while retrieving dynamic user agent. Request failed: " + Environment.NewLine + error );
+      //         }
+      //      }
+      //      catch( Exception e )
+      //      {
+      //         Logger.Current.Warn( e, "An error occurred while retrieving dynamic user agent." );
+      //      }
+      //   }
+      //}
 
       public IEnumerator SetupTKK()
       {

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

@@ -22,7 +22,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
 
       public override void ApplyHeaders( WebHeaderCollection headers )
       {
-         headers[ HttpRequestHeader.UserAgent ] = "Opera/9.80 (J2ME/MIDP; Opera Mini/5.1.21214/28.2725; U; en) Presto/2.8.119 Version/11.10";
+         headers[ HttpRequestHeader.UserAgent ] = Settings.GetUserAgent( "Opera/9.80 (J2ME/MIDP; Opera Mini/5.1.21214/28.2725; U; en) Presto/2.8.119 Version/11.10" );
          headers[ HttpRequestHeader.Accept ] = "*/*";
          headers[ HttpRequestHeader.AcceptCharset ] = "UTF-8";
       }

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

@@ -22,7 +22,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
 
       public override void ApplyHeaders( Dictionary<string, string> headers )
       {
-         headers[ "User-Agent" ] = "curl/7.55.1";
+         headers[ "User-Agent" ] = Settings.GetUserAgent( "curl/7.55.1" );
          headers[ "Accept" ] = "application/json";
          headers[ "Accept-Charset" ] = "UTF-8";
          headers[ "Authorization" ] = "Basic " + System.Convert.ToBase64String( System.Text.Encoding.ASCII.GetBytes( Settings.WatsonAPIUsername + ":" + Settings.WatsonAPIPassword ) );

+ 0 - 21
src/XUnity.AutoTranslator.Plugin.Core/Web/WebClientReference.cs

@@ -1,21 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Net;
-using System.Text;
-
-namespace XUnity.AutoTranslator.Plugin.Core.Web
-{
-   public class WebClientReference
-   {
-      public WebClientReference( WebClient client )
-      {
-         Client = client;
-         LastTimestamp = DateTime.UtcNow;
-      }
-
-      public WebClient Client { get; }
-
-      public DateTime LastTimestamp { get; set; }
-   }
-}

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

@@ -21,7 +21,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
 
       public override void ApplyHeaders( WebHeaderCollection headers )
       {
-         headers[ HttpRequestHeader.UserAgent ] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.183 Safari/537.36 Vivaldi/1.96.1147.55";
+         headers[ HttpRequestHeader.UserAgent ] = Settings.GetUserAgent( "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.183 Safari/537.36 Vivaldi/1.96.1147.55" );
          headers[ HttpRequestHeader.Accept ] = "*/*";
          headers[ HttpRequestHeader.AcceptCharset ] = "UTF-8";
       }