File: chakracore.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 (103 lines) | stat: -rw-r--r-- 3,355 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from __future__ import absolute_import

import os
import sys
import ctypes.util

from ctypes import c_void_p, c_size_t, byref, create_string_buffer, CDLL

from . import JavaScriptInterpreter
from .encapsulated import template

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


class ChallengeInterpreter(JavaScriptInterpreter):

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

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

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

    def eval(self, body, domain):
        chakraCoreLibrary = None

        # check current working directory.
        for _libraryFile in ['libChakraCore.so', 'libChakraCore.dylib', 'ChakraCore.dll']:
            if os.path.isfile(os.path.join(os.getcwd(), _libraryFile)):
                chakraCoreLibrary = os.path.join(os.getcwd(), _libraryFile)
                continue

        if not chakraCoreLibrary:
            chakraCoreLibrary = ctypes.util.find_library('ChakraCore')

        if not chakraCoreLibrary:
            sys.tracebacklimit = 0
            raise RuntimeError(
                'ChakraCore library not found in current path or any of your system library paths, '
                'please download from https://www.github.com/VeNoMouS/cloudscraper/tree/ChakraCore/, '
                'or https://github.com/Microsoft/ChakraCore/'
            )

        try:
            chakraCore = CDLL(chakraCoreLibrary)
        except OSError:
            sys.tracebacklimit = 0
            raise RuntimeError('There was an error loading the ChakraCore library {}'.format(chakraCoreLibrary))

        if sys.platform != 'win32':
            chakraCore.DllMain(0, 1, 0)
            chakraCore.DllMain(0, 2, 0)

        script = create_string_buffer(template(body, domain).encode('utf-16'))

        runtime = c_void_p()
        chakraCore.JsCreateRuntime(0, 0, byref(runtime))

        context = c_void_p()
        chakraCore.JsCreateContext(runtime, byref(context))
        chakraCore.JsSetCurrentContext(context)

        fname = c_void_p()
        chakraCore.JsCreateString(
            'iuam-challenge.js',
            len('iuam-challenge.js'),
            byref(fname)
        )

        scriptSource = c_void_p()
        chakraCore.JsCreateExternalArrayBuffer(
            script,
            len(script),
            0,
            0,
            byref(scriptSource)
        )

        jsResult = c_void_p()
        chakraCore.JsRun(scriptSource, 0, fname, 0x02, byref(jsResult))

        resultJSString = c_void_p()
        chakraCore.JsConvertValueToString(jsResult, byref(resultJSString))

        stringLength = c_size_t()
        chakraCore.JsCopyString(resultJSString, 0, 0, byref(stringLength))

        resultSTR = create_string_buffer(stringLength.value + 1)
        chakraCore.JsCopyString(
            resultJSString,
            byref(resultSTR),
            stringLength.value + 1,
            0
        )

        chakraCore.JsDisposeRuntime(runtime)

        return resultSTR.value


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

ChallengeInterpreter()