private static async Task <string> DecodeResponse(IHtmlDocument htmlDocument, HttpClient httpClient, HttpResponseMessage httpResponseMessage)
    {
        string responseString = await httpResponseMessage.Content.ReadAsStringAsync();

        string responseJson = string.Empty;

        if (responseString.StartsWith("{"))
        {
            responseJson = responseString;
        }
        else
        {
            lock (DecodeResponseLock)
            {
                if (JintEngine == null)
                {
                    IHtmlScriptElement appJsScript = htmlDocument.Scripts.FirstOrDefault(s =>
                                                                                         s.Source?.Contains("app.js") == true ||
                                                                                         s.Source?.Contains("app.min.js") == true ||
                                                                                         s.Source?.Contains("app.obf.js") == true ||
                                                                                         s.Source?.Contains("app.obf.min.js") == true
                                                                                         );

                    Obfuscated = appJsScript.Source.Contains("obf.");

                    string appJsSource = httpClient.GetStringAsync(appJsScript.Source.Replace("obf.", string.Empty)).GetAwaiter().GetResult();
                    List <JavaScriptHelper.Function> functions = JavaScriptHelper.Parse(appJsSource);

                    JintEngine = new Engine();

                    JintEngine.Execute(functions.FirstOrDefault(f => f.Name == "read").Body);

                    if (Obfuscated)
                    {
                        Func <string, string> atob = str => Encoding.Latin1.GetString(Convert.FromBase64String(str));
                        JintEngine.SetValue("atob", atob);

                        JintEngine.Execute(functions.FirstOrDefault(f => f.Name == "gdidecode").Body);
                    }
                }

                JsValue jsValue = JintEngine.Invoke("read", responseString);

                if (Obfuscated)
                {
                    jsValue      = JintEngine.Invoke("gdidecode", jsValue.ToString());
                    responseJson = jsValue.ToString();
                }
                else
                {
                    responseJson = Encoding.UTF8.GetString(Convert.FromBase64String(jsValue.ToString()));
                }
            }
        }

        return(responseJson);
    }
예제 #2
0
        /// <summary>
        /// Call a Js function by string, passing a single string argument to it (the argument can be a Json object parsed inside the function)
        /// </summary>
        public void JsRun(string function, string argument = null)
        {
            if (function is null)
            {
                throw new ArgumentNullException(nameof(function));
            }
            switch (Type)
            {
            case JsScriptRunnerType.Jint:
            {
                try
                {
                    JintEngine.Invoke(function, argument);          // execute the function passing the argument
                    break;
                }
                catch (Esprima.ParserException ex)
                {
                    throw new ApplicationException($"{ex.Error} (Line {ex.LineNumber}, Column {ex.Column})", ex);
                }
                catch (Jint.Runtime.JavaScriptException ex)          // from https://github.com/sebastienros/jint/issues/112
                {
                    throw new ApplicationException($"{ex.Error} (Line {ex.LineNumber}, Column {ex.Column})", ex);
                }
            }

            case JsScriptRunnerType.ClearScriptDebugMode:      // fallback to JsScriptRunnerType.ClearScript
            case JsScriptRunnerType.ClearScript:
            {
                try
                {
                    ClearScriptEngine.Invoke(function, argument);          // execute the function passing the argument
                    break;
                }
                catch (Microsoft.ClearScript.ScriptEngineException ex)          // from https://github.com/microsoft/ClearScript/issues/16
                {
                    throw new ApplicationException($"{ex.ErrorDetails}", ex);
                }
            }
            }
        }