File: nodejs.py

package info (click to toggle)
python-cloudscraper 1.2.71~git20230426.cbb3c0ea-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,608 kB
  • sloc: python: 2,496; makefile: 37
file content (49 lines) | stat: -rw-r--r-- 2,084 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import base64
import subprocess
import sys

from . import JavaScriptInterpreter
from .encapsulated import template

# ------------------------------------------------------------------------------- #


class ChallengeInterpreter(JavaScriptInterpreter):

    # ------------------------------------------------------------------------------- #

    def __init__(self):
        super(ChallengeInterpreter, self).__init__('nodejs')

    # ------------------------------------------------------------------------------- #

    def eval(self, body, domain):
        try:
            js = 'var atob = function(str) {return Buffer.from(str, "base64").toString("binary");};' \
                 'var challenge = atob("%s");' \
                 'var context = {atob: atob};' \
                 'var options = {filename: "iuam-challenge.js", timeout: 4000};' \
                 'var answer = require("vm").runInNewContext(challenge, context, options);' \
                 'process.stdout.write(String(answer));' \
                 % base64.b64encode(template(body, domain).encode('UTF-8')).decode('ascii')

            return subprocess.check_output(['node', '-e', js])

        except OSError as e:
            if e.errno == 2:
                raise EnvironmentError(
                    'Missing Node.js runtime. Node is required and must be in the PATH (check with `node -v`).\n\n'
                    'Your Node binary may be called `nodejs` rather than `node`, '
                    'in which case you may need to run `apt-get install nodejs-legacy` on some Debian-based systems.\n\n'
                    '(Please read the cloudscraper README\'s Dependencies section: '
                    'https://github.com/VeNoMouS/cloudscraper#dependencies.)'
                )
            raise
        except Exception:
            sys.tracebacklimit = 0
            raise RuntimeError('Error executing Cloudflare IUAM Javascript in nodejs')


# ------------------------------------------------------------------------------- #

ChallengeInterpreter()