public CaptchaSolution(bool solved, bool retryAvailable, string message, string solution, string id, Models.CaptchaSolvingConfig config)
 {
     Solved         = solved;
     RetryAvailable = retryAvailable;
     Message        = message ?? string.Empty;
     Solution       = solution ?? string.Empty;
     Id             = id ?? string.Empty;
     Config         = Config;
 }
        public CaptchaDialog(HttpHandler httpHandler, Action <string> updateStatus, Models.CaptchaSolvingConfig config)
        {
            Logger.Debug("Init. solving captcha...");

            Solution = new Web.Captcha.CaptchaSolution(false, "Something went wrong...", config);

            _httpHandler = httpHandler;

            InitializeComponent();

            LoadCaptcha(updateStatus, Config = config);
        }
 private void LoadCaptcha(Action <string> updateStatus, Models.CaptchaSolvingConfig config)
 {
     if (config.Enabled)
     {
         Logger.Debug("Solving captcha using services...");
         Solution = _httpHandler.SolveCaptcha(updateStatus, config);
     }
     else
     {
         Logger.Debug("Solving captcha using dialog box...");
         boxCaptcha.Image = _httpHandler.GetCaptchaImageraw();
     }
 }
Пример #4
0
        public Captcha.CaptchaSolution SolveCaptcha(Action <string> updateStatus, Models.CaptchaSolvingConfig captchaConfig)
        {
            Logger.Debug("Getting captcha...");
            updateStatus?.Invoke("Getting captcha...");

            //Store captcha ID
            SetConfig(JoinUri, Method.GET);
            var response = _client.Execute(_request);

            try
            {
                _captchaGid = CaptchaRegex.Matches(response.Content)[0].Groups[1].Value;
            }
            catch (Exception e)
            {
                Logger.Error("Captcha error.", e);
                updateStatus($"Captcha error: {e.Message}");
                return(new Captcha.CaptchaSolution(true, $"{e}\n\n{response.ResponseStatus}", captchaConfig));
            }

            //download and return captcha image
            SetConfig($"{CaptchaUri}{_captchaGid}", Method.GET);
            var captchaPayload = string.Empty;

            for (int i = 0; i < 3; i++)
            {
                try
                {
                    Logger.Debug($"Downloading captcha: Try {i + 1}/3");
                    updateStatus($"Downloading captcha: Try {i + 1}/3");

                    var _captchaResp = _client.DownloadData(_request);
                    captchaPayload = GetBase64FromImage(_captchaResp);

                    break;
                }
                catch (Exception ex)
                {
                    Logger.Error("Downloading captcha error.", ex);
                    captchaPayload = string.Empty;
                }
            }

            // recognize captcha
            Logger.Debug("Recognizing captcha...");
            updateStatus("Recognizing captcha...");
            switch (captchaConfig.Service)
            {
            case Enums.CaptchaService.Captchasolutions:
            {
                Logger.Debug("Recognizing captcha via Captchasolutions...");
                var _resp = Captchasolutions("solve",
                                             new Dictionary <string, object>()
                    {
                        { "p", "base64" },
                        { "captcha", $"data:image/jpg;base64,{captchaPayload}" },
                        { "key", captchaConfig.CaptchaSolutions.ApiKey },
                        { "secret", captchaConfig.CaptchaSolutions.ApiSecret },
                        { "out", "txt" },
                    });

                if (Regex.IsMatch(_resp, @"Error:\s(.+)", RegexOptions.IgnoreCase))
                {
                    Logger.Warn($"Captchasolutions error:\n{_resp}\n====== END ======");
                    return(new Captcha.CaptchaSolution(true, _resp, captchaConfig));
                }

                var solution = Regex.Replace(_resp, @"\t|\n|\r", "");
                Logger.Debug($"Captchasolutions: {solution}");
                return(new Captcha.CaptchaSolution(solution, null, captchaConfig));
            }

            case Enums.CaptchaService.RuCaptcha:
            {
                Logger.Debug("Recognizing captcha via TwoCaptcha/RuCaptcha");
                var _captchaIdResponse = TwoCaptcha("in.php",
                                                    new Dictionary <string, object>()
                    {
                        { "key", captchaConfig.RuCaptcha.ApiKey },
                        { "body", $"data:image/jpg;base64,{captchaPayload}" },
                        { "method", "base64" },
                        { "soft_id", "2370" },
                        { "json", "0" },
                    });

                var _captchaStatus = _captchaIdResponse?.FirstOrDefault()?.ToUpper() ?? "UNKNOWN";
                Logger.Debug($"TwoCaptcha/RuCaptcha image upload response: {_captchaStatus}");
                switch (_captchaStatus)
                {
                case "OK":
                    break;

                case "ERROR_NO_SLOT_AVAILABLE":
                    Thread.Sleep(6000);
                    return(new Captcha.CaptchaSolution(true, _captchaStatus, captchaConfig));

                default:
                    return(new Captcha.CaptchaSolution(false, _captchaStatus, captchaConfig));
                }

                var _captchaId = _captchaIdResponse.ElementAt(1);
                Logger.Debug($"TwoCaptcha/RuCaptcha ID: {_captchaId}");

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

                var solution = string.Empty;
                for (int i = 0; i < 3; i++)
                {
                    Logger.Debug($"TwoCaptcha/RuCaptcha requesting solution... Try {i} of 3");
                    var _captchaResponse = TwoCaptcha("res.php",
                                                      new Dictionary <string, object>()
                        {
                            { "key", captchaConfig.RuCaptcha.ApiKey },
                            { "action", "get" },
                            { "id", _captchaId },
                            { "json", "0" },
                        });

                    var _status = _captchaResponse?.FirstOrDefault()?.ToUpper() ?? "UNKNOWN";
                    Logger.Debug($"TwoCaptcha/RuCaptcha solving status: {_status}");
                    switch (_status)
                    {
                    case "OK":
                    {
                        var _solution = new Captcha.CaptchaSolution(_captchaResponse.ElementAt(1), _captchaId, captchaConfig);
                        Logger.Debug($"TwoCaptcha/RuCaptcha solution: {_solution.Solution}");
                        return(_solution);
                    }

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

                    default:
                        return(new Captcha.CaptchaSolution(true, _status, captchaConfig));
                    }
                }
            }
                Logger.Debug("TwoCaptcha/RuCaptcha somethig went wrong.");
                return(new Captcha.CaptchaSolution(true, "Something went wrong", captchaConfig));

            default:
            {
                using (var dialog = new CaptchaDialog(this, updateStatus, captchaConfig))
                {
                    if (dialog.ShowDialog() == DialogResult.OK)
                    {
                        return(dialog.Solution);
                    }
                }
            }
                return(new Captcha.CaptchaSolution(false, "Can't solve captcha!", captchaConfig));
            }
        }
 public CaptchaSolution(bool retryAvailable, string message, Models.CaptchaSolvingConfig config) : this(false, retryAvailable, message, null, null, config)
 {
 }
 public CaptchaSolution(string solution, string id, Models.CaptchaSolvingConfig config) : this(true, false, null, solution, id, config)
 {
 }