From 05422437df5ef11af7b54b4de9150d40cff34774 Mon Sep 17 00:00:00 2001 From: marco Date: Wed, 29 Apr 2020 11:35:18 +0200 Subject: [PATCH] aggiornato cloudscraper (fix piratestreaming) --- lib/cloudscraper/__init__.py | 30 ++++++++++++++++++++++- lib/cloudscraper/exceptions.py | 6 +++++ lib/cloudscraper/interpreters/native.py | 18 ++++++++------ lib/cloudscraper/reCaptcha/9kw.py | 17 ++++++------- lib/cloudscraper/reCaptcha/anticaptcha.py | 2 +- 5 files changed, 55 insertions(+), 18 deletions(-) diff --git a/lib/cloudscraper/__init__.py b/lib/cloudscraper/__init__.py index 3a056269..389788bf 100644 --- a/lib/cloudscraper/__init__.py +++ b/lib/cloudscraper/__init__.py @@ -28,6 +28,7 @@ from .exceptions import ( CloudflareLoopProtection, CloudflareCode1020, CloudflareIUAMError, + CloudflareChallengeError, CloudflareReCaptchaError, CloudflareReCaptchaProvider ) @@ -54,7 +55,7 @@ except ImportError: # ------------------------------------------------------------------------------- # -__version__ = '1.2.34' +__version__ = '1.2.35' # ------------------------------------------------------------------------------- # @@ -265,6 +266,27 @@ class CloudScraper(Session): return False + # ------------------------------------------------------------------------------- # + # check if the response contains new Cloudflare challenge + # ------------------------------------------------------------------------------- # + + @staticmethod + def is_New_IUAM_Challenge(resp): + try: + return ( + resp.headers.get('Server', '').startswith('cloudflare') + and resp.status_code in [429, 503] + and re.search( + r'cpo.src="/cdn-cgi/challenge-platform/orchestrate/jsch/v1"', + resp.text, + re.M | re.DOTALL + ) + ) + except AttributeError: + pass + + return False + # ------------------------------------------------------------------------------- # # check if the response contains a valid Cloudflare reCaptcha challenge # ------------------------------------------------------------------------------- # @@ -318,6 +340,12 @@ class CloudScraper(Session): 'Cloudflare has blocked this request (Code 1020 Detected).' ) + if self.is_New_IUAM_Challenge(resp): + self.simpleException( + CloudflareChallengeError, + 'Detected the new Cloudflare challenge.' + ) + if self.is_reCaptcha_Challenge(resp) or self.is_IUAM_Challenge(resp): return True diff --git a/lib/cloudscraper/exceptions.py b/lib/cloudscraper/exceptions.py index 525b1743..c9fb7ca0 100644 --- a/lib/cloudscraper/exceptions.py +++ b/lib/cloudscraper/exceptions.py @@ -36,6 +36,12 @@ class CloudflareIUAMError(CloudflareException): """ +class CloudflareChallengeError(CloudflareException): + """ + Raise an error when detected new Cloudflare challenge + """ + + class CloudflareSolveError(CloudflareException): """ Raise an error when issue with solving Cloudflare challenge diff --git a/lib/cloudscraper/interpreters/native.py b/lib/cloudscraper/interpreters/native.py index 9c0694de..69828705 100644 --- a/lib/cloudscraper/interpreters/native.py +++ b/lib/cloudscraper/interpreters/native.py @@ -3,6 +3,7 @@ from __future__ import absolute_import import re import operator as op +from ..exceptions import CloudflareSolveError from . import JavaScriptInterpreter # ------------------------------------------------------------------------------- # @@ -62,13 +63,16 @@ class ChallengeInterpreter(JavaScriptInterpreter): def challengeSolve(body, domain): jschl_answer = 0 - jsfuckChallenge = re.search( - r"setTimeout\(function\(\){\s+var.*?f,\s*(?P\w+).*?:(?P\S+)};" - r".*?\('challenge-form'\);\s+;(?P.*?a\.value)" - r"(?:.*id=\"cf-dn-.*?>(?P\S+)<)?", - body, - re.DOTALL | re.MULTILINE - ).groupdict() + try: + jsfuckChallenge = re.search( + r"setTimeout\(function\(\){\s+var.*?f,\s*(?P\w+).*?:(?P\S+)};" + r".*?\('challenge-form'\);\s+;(?P.*?a\.value)" + r"(?:.*id=\"cf-dn-.*?>(?P\S+)<)?", + body, + re.DOTALL | re.MULTILINE + ).groupdict() + except AttributeError: + raise CloudflareSolveError('There was an issue extracting the Cloudflare challenge.') jsfuckChallenge['challenge'] = re.finditer( r'{}.*?([+\-*/])=(.*?);(?=a\.value|{})'.format( diff --git a/lib/cloudscraper/reCaptcha/9kw.py b/lib/cloudscraper/reCaptcha/9kw.py index 2404bfe5..e1a7e6a0 100644 --- a/lib/cloudscraper/reCaptcha/9kw.py +++ b/lib/cloudscraper/reCaptcha/9kw.py @@ -12,7 +12,6 @@ except ImportError: ) from ..exceptions import ( - reCaptchaException, reCaptchaServiceUnavailable, reCaptchaAPIError, reCaptchaTimeout, @@ -144,7 +143,7 @@ class captchaSolver(reCaptcha): # ------------------------------------------------------------------------------- # - def requestSolve(self, url, siteKey): + def requestSolve(self, captchaType, url, siteKey): def _checkRequest(response): if response.ok and response.text.startswith('{') and response.json().get('captchaid'): return response @@ -153,6 +152,11 @@ class captchaSolver(reCaptcha): return None + captchaMap = { + 'reCaptcha': 'recaptchav2', + 'hCaptcha': 'hcaptcha' + } + response = polling.poll( lambda: self.session.post( self.host, @@ -161,7 +165,7 @@ class captchaSolver(reCaptcha): 'action': 'usercaptchaupload', 'interactive': 1, 'file-upload-01': siteKey, - 'oldsource': 'recaptchav2', + 'oldsource': captchaMap[captchaType], 'pageurl': url, 'maxtimeout': self.maxtimeout, 'json': 1 @@ -186,11 +190,6 @@ class captchaSolver(reCaptcha): if not reCaptchaParams.get('api_key'): raise reCaptchaParameter("9kw: Missing api_key parameter.") - if captchaType == 'hCaptcha': - raise reCaptchaException( - 'Provider does not support hCaptcha.' - ) - self.api_key = reCaptchaParams.get('api_key') if reCaptchaParams.get('maxtimeout'): @@ -200,7 +199,7 @@ class captchaSolver(reCaptcha): self.session.proxies = reCaptchaParams.get('proxies') try: - jobID = self.requestSolve(url, siteKey) + jobID = self.requestSolve(captchaType, url, siteKey) return self.requestJob(jobID) except polling.TimeoutException: raise reCaptchaTimeout( diff --git a/lib/cloudscraper/reCaptcha/anticaptcha.py b/lib/cloudscraper/reCaptcha/anticaptcha.py index 4a95111f..b25e4e14 100644 --- a/lib/cloudscraper/reCaptcha/anticaptcha.py +++ b/lib/cloudscraper/reCaptcha/anticaptcha.py @@ -26,7 +26,7 @@ from . import reCaptcha class captchaSolver(reCaptcha): def __init__(self): - if sys.modules['python_anticaptcha'].__version__ < 0.6: + if sys.modules['python_anticaptcha'].__version__ < '0.6': raise ImportError( "Please upgrade the python module 'python_anticaptcha' via " "pip install -U python-anticaptcha or https://github.com/ad-m/python-anticaptcha/"