Esempio n. 1
0
        public IActionResult Login(Login login)
        {
            var captchaCode = HttpContext.Session.GetString(sessionCaptchaKey);

            var captchaRequest = new CaptchaRequest()
            {
                CaptchaCode = captchaCode,
                UserInput   = login.CaptchaText
            };

            var jsonRequest   = JsonConvert.SerializeObject(captchaRequest);
            var stringContent = new StringContent(jsonRequest, Encoding.UTF8, "application/json");

            var request = new HttpRequestMessage(HttpMethod.Post, "https://localhost:5001/api/captcha/validate");

            request.Content = stringContent;

            var client = _clientFactory.CreateClient();

            var response = client.SendAsync(request).GetAwaiter().GetResult();

            if (response.IsSuccessStatusCode)
            {
                return(View("Index"));
            }
            else
            {
                ViewBag.InvalidCaptcha = true;
            }

            return(View());
        }
Esempio n. 2
0
        public ActionResult CreateCaptcha()
        {
            var captchaRequest = new CaptchaRequest();

            captchaRequest.PublicKey = ConfigurationManager.AppSettings["ReCaptchaPublicKey"];
            return(PartialView("_Captcha", captchaRequest));
        }
 internal InvalidCaptchaException(CaptchaRequest newCaptcha, string spName)
     : base(new SpCallError()
 {
     ErrorName    = SpCommonExceptionId.InvalidCaptcha.ToString(),
     ErrorId      = (int)SpCommonExceptionId.InvalidCaptcha,
     ErrorMessage = $"Invalid captcha for invoking {spName}",
     ErrorData    = newCaptcha
 })
 {
 }
Esempio n. 4
0
        // CAPTCHA
        public async Task <CaptchaResponse> GetCaptcha(CaptchaRequest captchaArgs)
        {
            var uri = new Uri(_baseAddress + "captcha/Get");

            var message = new HttpRequestMessage(HttpMethod.Post, uri)
            {
                Content = new StringContent(JsonConvert.SerializeObject(captchaArgs), Encoding.UTF8, "application/json")
            };

            AddHeader(message, captchaArgs.UserId.ToString());

            return(await GetResponse <CaptchaResponse>(message));
        }
Esempio n. 5
0
 public Boolean requestSolveCaptcha()
 {
     Console.WriteLine("in requestSolveCaptcha()");
     while (true)
     {
         String         res     = requestSolveCaptchaSingle();
         CaptchaRequest request = JsonConvert.DeserializeObject <CaptchaRequest>(res);
         String         id      = request.request;
         if (id.ToLower().IndexOf("error") != -1)
         {
             return(false);
         }
         else
         {
             taskIdString = id;
             return(true);
         }
     }
 }
Esempio n. 6
0
        public async Task <CaptchaResponse> Validate(string captchaToken)
        {
            using (var httpClient = new HttpClient())
            {
                var captchaRequest = new CaptchaRequest()
                {
                    Secret   = this._recaptchaSettings.SecretKey,
                    Response = captchaToken
                };

                string content = $"secret={captchaRequest.Secret}&response={captchaRequest.Response}";

                HttpResponseMessage result = await httpClient.PostAsync("https://www.google.com/recaptcha/api/siteverify", new StringContent(content, Encoding.UTF8, "application/x-www-form-urlencoded"));

                string body = await result.Content.ReadAsStringAsync();

                return(JsonConvert.DeserializeObject <CaptchaResponse>(body));
            }
        }
Esempio n. 7
0
        public IActionResult Post([FromBody] CaptchaRequest captchaRequest)
        {
            string         methodName     = "Post";
            ValidateResult validateResult = null;

            try
            {
                _logger.LogInformation($"[MethodStart]:{_moduleName}-{methodName}");

                var isValid = _simpleCaptchaService.ValidateCaptcha(captchaRequest.CaptchaCode, captchaRequest.UserInput);

                _logger.LogInformation($"[MethodEnd]:{_moduleName}-{methodName}");

                if (isValid)
                {
                    validateResult = new ValidateResult()
                    {
                        IsValid = isValid, HttpStatusCode = HttpStatusCode.OK
                    }
                }
                ;
                else
                {
                    validateResult = new ValidateResult()
                    {
                        IsValid = isValid, HttpStatusCode = HttpStatusCode.BadRequest
                    }
                };

                return(new ObjectResult(validateResult)
                {
                    StatusCode = (int)validateResult.HttpStatusCode
                });
            }
            catch (System.Exception ex)
            {
                _logger.LogError($"[Error]:{_moduleName}-{methodName}", ex);
                return(new ObjectResult(ex.Message)
                {
                    StatusCode = (int)HttpStatusCode.BadRequest
                });
            }
        }
Esempio n. 8
0
            public Boolean getResult()
            {
                Console.WriteLine("in getResult()");
                int countError = 0, count = 0;

                while (true)
                {
                    String         res     = getResultSingle();
                    CaptchaRequest request = JsonConvert.DeserializeObject <CaptchaRequest>(res);
                    String         result  = request.request;
                    if (result.ToLower().IndexOf("error") != -1)
                    {
                        countError++;
                        if (countError >= 20)
                        {
                            //MyLogger.getInstance(mContext).writeLog("error: " + response);
                            return(false);
                        }

                        Thread.Sleep(7000);
                        //return false;
                    }
                    else if (result.ToLower().IndexOf("captcha_not_ready") != -1 || result.ToLower().IndexOf("capcha_not_ready") != -1)
                    {
                        count++;
                        if (count >= 200)
                        {
                            //MyLogger.getInstance(mContext).writeLog("error: " + "cannot solve captcha after 1000s waiting");
                            return(false);
                        }

                        Thread.Sleep(5000);
                    }
                    else
                    {
                        realResult = result;
                        return(true);
                    }
                }
            }
Esempio n. 9
0
        public JsonResult ValidateCaptcha(CaptchaRequest captcha)
        {
            var cientIP    = Request.ServerVariables["REMOTE_ADDR"];
            var privateKey = _captchaSettings.ReCaptchaPrivateKey;

            string data = string.Format("privatekey={0}&remoteip={1}&challenge={2}&response={3}", privateKey, cientIP, captcha.Challenge, captcha.Response);

            byte[] byteArray = new ASCIIEncoding().GetBytes(data);

            WebRequest request = WebRequest.Create("http://www.google.com/recaptcha/api/verify");

            request.Credentials   = CredentialCache.DefaultCredentials;
            request.Method        = "POST";
            request.ContentType   = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            Stream dataStream = request.GetRequestStream();

            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            WebResponse response = request.GetResponse();
            var         status   = (((HttpWebResponse)response).StatusCode);

            dataStream = response.GetResponseStream();
            StreamReader reader             = new StreamReader(dataStream);
            string       responseFromServer = reader.ReadToEnd();

            reader.Close();
            dataStream.Close();
            response.Close();

            var  responseLines = responseFromServer.Split(new string[] { "\n" }, StringSplitOptions.None);
            bool success       = responseLines[0].Equals("true");

            return(this.Json((object)new
            {
                result = success
            }, JsonRequestBehavior.AllowGet));
        }
        public CaptchaResponse Solve(CaptchaRequest captcha)
        {
            try
            {
                if (!ConfigRefresh())
                {
                    return(new CaptchaResponse(CaptchaStatus.Failed, "Failed to load configuration!"));
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Failed to load configuration!", ex);
                return(new CaptchaResponse(CaptchaStatus.Failed, "Failed to load configuration!"));
            }

            var requestQueueId = new RestRequest("in.php", Method.GET);

            requestQueueId.AddParameter("soft_id", 2370);
            requestQueueId.AddParameter("body", $"data:image/jpg;base64,{captcha.CaptchaImage}");
            requestQueueId.AddParameter("method", "base64");
            AddProxy(requestQueueId, captcha.Proxy);

            return(GetSolution(requestQueueId, false));
        }
Esempio n. 11
0
 public CaptchaResponse Solve(CaptchaRequest request)
 => ImageCaptcha?.Solve(request);
 internal InvalidCaptchaException(CaptchaRequest newCaptcha, DirectSpException baseException) : base(baseException)
 {
     _SpCallError.ErrorData = newCaptcha;
 }
Esempio n. 13
0
        public CaptchaResponse Solve(CaptchaRequest request)
        {
            if (!Config.Load())
            {
                Config.Save();
                return(new CaptchaResponse(SACModuleBase.Enums.Captcha.CaptchaStatus.CannotSolve, "Config load error"));
            }

            var _params = new Dictionary <string, object>()
            {
                { "key", Config?.Running?.ApiKey ?? "" },
                { "soft_id", "2370" },
                { "json", "0" },
                { "method", "base64" },
                { "body", $"data:image/jpg;base64,{request.CaptchaImage}" }
            };

            var _captchaIdResponse = Misc.TwoCaptcha("in.php", _params);
            var _captchaStatus     = _captchaIdResponse?.FirstOrDefault()?.ToUpper() ?? "UNKNOWN";

            switch (_captchaStatus)
            {
            case "OK":
                break;

            case "ERROR_NO_SLOT_AVAILABLE":
                Thread.Sleep(6000);
                return(new CaptchaResponse(SACModuleBase.Enums.Captcha.CaptchaStatus.RetryAvailable, _captchaStatus, null));

            default:
                return(new CaptchaResponse(SACModuleBase.Enums.Captcha.CaptchaStatus.Failed, _captchaStatus, null));
            }

            var _captchaId = _captchaIdResponse.ElementAt(1);

            Thread.Sleep(TimeSpan.FromSeconds(20));

            var solution = string.Empty;

            for (int i = 0; i < 3; i++)
            {
                var _captchaResponse = Misc.TwoCaptcha("res.php",
                                                       new Dictionary <string, object>()
                {
                    { "key", Config?.Running?.ApiKey ?? "" },
                    { "action", "get" },
                    { "id", _captchaId },
                    { "json", "0" },
                });

                var _status = _captchaResponse?.FirstOrDefault()?.ToUpper() ?? "UNKNOWN";
                switch (_status)
                {
                case "OK":
                    return(new CaptchaResponse(_captchaResponse.ElementAt(1), _captchaId));

                case "CAPCHA_NOT_READY":
                case "ERROR_NO_SLOT_AVAILABLE":
                    Thread.Sleep(6000);
                    continue;

                default:
                    return(new CaptchaResponse(SACModuleBase.Enums.Captcha.CaptchaStatus.RetryAvailable, _status));
                }
            }

            return(new CaptchaResponse(SACModuleBase.Enums.Captcha.CaptchaStatus.CannotSolve, "Something went wrong..."));
        }