| 1 |
# -*- coding: utf-8 -*- |
| 2 |
import hashlib |
| 3 |
import responses |
| 4 |
|
| 5 |
from os import path |
| 6 |
from io import open |
| 7 |
|
| 8 |
try: |
| 9 |
from urlparse import parse_qsl |
| 10 |
except ImportError: |
| 11 |
from urllib.parse import parse_qsl |
| 12 |
|
| 13 |
# Fake URL, network requests are not allowed by default when using the decorator |
| 14 |
url = 'http://www.evildomain.com' |
| 15 |
|
| 16 |
# These kwargs will be passed to tests by the decorator |
| 17 |
cloudscraper_kwargs = dict(delay=0.01, debug=False) |
| 18 |
|
| 19 |
# Cloudflare challenge fixtures are only read from the FS once |
| 20 |
cache = {} |
| 21 |
|
| 22 |
# ------------------------------------------------------------------------------- # |
| 23 |
|
| 24 |
|
| 25 |
def fixtures(filename): |
| 26 |
""" |
| 27 |
Read and cache a challenge fixture |
| 28 |
|
| 29 |
Returns: HTML (bytes): The HTML challenge fixture |
| 30 |
""" |
| 31 |
if not cache.get(filename): |
| 32 |
print('reading...') |
| 33 |
with open(path.join(path.dirname(__file__), 'fixtures', filename), 'r') as fp: |
| 34 |
cache[filename] = fp.read() |
| 35 |
return cache[filename] |
| 36 |
|
| 37 |
# ------------------------------------------------------------------------------- # |
| 38 |
|
| 39 |
|
| 40 |
def mockCloudflare(fixture, payload): |
| 41 |
def responses_decorator(test): |
| 42 |
@responses.activate |
| 43 |
def wrapper(self): |
| 44 |
def post_callback(request): |
| 45 |
postPayload = dict(parse_qsl(request.body)) |
| 46 |
postPayload['r'] = hashlib.sha256(postPayload.get('r', '').encode('ascii')).hexdigest() |
| 47 |
|
| 48 |
for param in payload: |
| 49 |
if param not in postPayload or postPayload[param] != payload[param]: |
| 50 |
return ( |
| 51 |
503, |
| 52 |
{'Server': 'cloudflare'}, |
| 53 |
fixtures(fixture) |
| 54 |
) |
| 55 |
|
| 56 |
# ------------------------------------------------------------------------------- # |
| 57 |
|
| 58 |
return ( |
| 59 |
200, |
| 60 |
[ |
| 61 |
( |
| 62 |
'Set-Cookie', '__cfduid=d5927a7cbaa96ec536939f93648e3c08a1576098703; Domain=.evildomain.com; path=/' |
| 63 |
), |
| 64 |
( |
| 65 |
'Set-Cookie', |
| 66 |
'__cfduid=d5927a7cbaa96ec536939f93648e3c08a1576098703; domain=.evildomain.com; path=/' |
| 67 |
), |
| 68 |
('Server', 'cloudflare') |
| 69 |
], |
| 70 |
'Solved OK' |
| 71 |
) |
| 72 |
|
| 73 |
# ------------------------------------------------------------------------------- # |
| 74 |
|
| 75 |
def challengeCallback(request): |
| 76 |
status_code = 503 |
| 77 |
|
| 78 |
if 'reCaptcha' in fixture or '1020' in fixture: |
| 79 |
status_code = 403 |
| 80 |
return ( |
| 81 |
status_code, |
| 82 |
[ |
| 83 |
( |
| 84 |
'Set-Cookie', |
| 85 |
'__cfduid=d5927a7cbaa96ec536939f93648e3c08a1576098703; Domain=.evildomain.com; path=/' |
| 86 |
), |
| 87 |
('Server', 'cloudflare') |
| 88 |
], |
| 89 |
fixtures(fixture) |
| 90 |
) |
| 91 |
|
| 92 |
# ------------------------------------------------------------------------------- # |
| 93 |
|
| 94 |
responses.add_callback( |
| 95 |
responses.POST, |
| 96 |
url, |
| 97 |
callback=post_callback, |
| 98 |
content_type='text/html', |
| 99 |
) |
| 100 |
|
| 101 |
responses.add_callback( |
| 102 |
responses.GET, |
| 103 |
url, |
| 104 |
callback=challengeCallback, |
| 105 |
content_type='text/html', |
| 106 |
) |
| 107 |
|
| 108 |
# ------------------------------------------------------------------------------- # |
| 109 |
|
| 110 |
return test(self, **cloudscraper_kwargs) |
| 111 |
|
| 112 |
# ------------------------------------------------------------------------------- # |
| 113 |
|
| 114 |
return wrapper |
| 115 |
|
| 116 |
# ------------------------------------------------------------------------------- # |
| 117 |
|
| 118 |
return responses_decorator |