Debian Codegraph @

cloudscraper/interpreters/encapsulated.py

raw
1 import logging
2 import re
3
4 # ------------------------------------------------------------------------------- #
5
6
7 def template(body, domain):
8 BUG_REPORT = 'Cloudflare may have changed their technique, or there may be a bug in the script.'
9
10 try:
11 js = re.search(
12 r'setTimeout\(function\(\){\s+(.*?a\.value\s*=\s*\S+toFixed\(10\);)',
13 body,
14 re.M | re.S
15 ).group(1)
16 except Exception:
17 raise ValueError('Unable to identify Cloudflare IUAM Javascript on website. {}'.format(BUG_REPORT))
18
19 jsEnv = '''String.prototype.italics=function(str) {{return "<i>" + this + "</i>";}};
20 var subVars= {{{subVars}}};
21 var document = {{
22 createElement: function () {{
23 return {{ firstChild: {{ href: "https://{domain}/" }} }}
24 }},
25 getElementById: function (str) {{
26 return {{"innerHTML": subVars[str]}};
27 }}
28 }};
29 '''
30
31 try:
32 js = js.replace(
33 r"(setInterval(function(){}, 100),t.match(/https?:\/\//)[0]);",
34 r"t.match(/https?:\/\//)[0];"
35 )
36
37 k = re.search(r" k\s*=\s*'(?P<k>\S+)';", body).group('k')
38 r = re.compile(r'<div id="{}(?P<id>\d+)">\s*(?P<jsfuck>[^<>]*)</div>'.format(k))
39
40 subVars = ''
41 for m in r.finditer(body):
42 subVars = '{}\n\t\t{}{}: {},\n'.format(subVars, k, m.group('id'), m.group('jsfuck'))
43 subVars = subVars[:-2]
44
45 except: # noqa
46 logging.error('Error extracting Cloudflare IUAM Javascript. {}'.format(BUG_REPORT))
47 raise
48
49 return '{}{}'.format(
50 re.sub(
51 r'\s{2,}',
52 ' ',
53 jsEnv.format(
54 domain=domain,
55 subVars=subVars
56 ),
57 re.MULTILINE | re.DOTALL
58 ),
59 js
60 )
61
62 # ------------------------------------------------------------------------------- #