Debian Codegraph @

cloudscraper/interpreters/nodejs.py

raw
1 import base64
2 import subprocess
3 import sys
4
5 from . import JavaScriptInterpreter
6 from .encapsulated import template
7
8 # ------------------------------------------------------------------------------- #
9
10
11 class ChallengeInterpreter(JavaScriptInterpreter):
12
13 # ------------------------------------------------------------------------------- #
14
15 def __init__(self):
16 super(ChallengeInterpreter, self).__init__('nodejs')
17
18 # ------------------------------------------------------------------------------- #
19
20 def eval(self, body, domain):
21 try:
22 js = 'var atob = function(str) {return Buffer.from(str, "base64").toString("binary");};' \
23 'var challenge = atob("%s");' \
24 'var context = {atob: atob};' \
25 'var options = {filename: "iuam-challenge.js", timeout: 4000};' \
26 'var answer = require("vm").runInNewContext(challenge, context, options);' \
27 'process.stdout.write(String(answer));' \
28 % base64.b64encode(template(body, domain).encode('UTF-8')).decode('ascii')
29
30 return subprocess.check_output(['node', '-e', js])
31
32 except OSError as e:
33 if e.errno == 2:
34 raise EnvironmentError(
35 'Missing Node.js runtime. Node is required and must be in the PATH (check with `node -v`).\n\n'
36 'Your Node binary may be called `nodejs` rather than `node`, '
37 'in which case you may need to run `apt-get install nodejs-legacy` on some Debian-based systems.\n\n'
38 '(Please read the cloudscraper README\'s Dependencies section: '
39 'https://github.com/VeNoMouS/cloudscraper#dependencies.)'
40 )
41 raise
42 except Exception:
43 sys.tracebacklimit = 0
44 raise RuntimeError('Error executing Cloudflare IUAM Javascript in nodejs')
45
46
47 # ------------------------------------------------------------------------------- #
48
49 ChallengeInterpreter()