| 1 |
from __future__ import absolute_import |
| 2 |
|
| 3 |
import os |
| 4 |
import sys |
| 5 |
import ctypes.util |
| 6 |
|
| 7 |
from ctypes import c_void_p, c_size_t, byref, create_string_buffer, CDLL |
| 8 |
|
| 9 |
from . import JavaScriptInterpreter |
| 10 |
from .encapsulated import template |
| 11 |
|
| 12 |
# ------------------------------------------------------------------------------- # |
| 13 |
|
| 14 |
|
| 15 |
class ChallengeInterpreter(JavaScriptInterpreter): |
| 16 |
|
| 17 |
# ------------------------------------------------------------------------------- # |
| 18 |
|
| 19 |
def __init__(self): |
| 20 |
super(ChallengeInterpreter, self).__init__('chakracore') |
| 21 |
|
| 22 |
# ------------------------------------------------------------------------------- # |
| 23 |
|
| 24 |
def eval(self, body, domain): |
| 25 |
chakraCoreLibrary = None |
| 26 |
|
| 27 |
# check current working directory. |
| 28 |
for _libraryFile in ['libChakraCore.so', 'libChakraCore.dylib', 'ChakraCore.dll']: |
| 29 |
if os.path.isfile(os.path.join(os.getcwd(), _libraryFile)): |
| 30 |
chakraCoreLibrary = os.path.join(os.getcwd(), _libraryFile) |
| 31 |
continue |
| 32 |
|
| 33 |
if not chakraCoreLibrary: |
| 34 |
chakraCoreLibrary = ctypes.util.find_library('ChakraCore') |
| 35 |
|
| 36 |
if not chakraCoreLibrary: |
| 37 |
sys.tracebacklimit = 0 |
| 38 |
raise RuntimeError( |
| 39 |
'ChakraCore library not found in current path or any of your system library paths, ' |
| 40 |
'please download from https://www.github.com/VeNoMouS/cloudscraper/tree/ChakraCore/, ' |
| 41 |
'or https://github.com/Microsoft/ChakraCore/' |
| 42 |
) |
| 43 |
|
| 44 |
try: |
| 45 |
chakraCore = CDLL(chakraCoreLibrary) |
| 46 |
except OSError: |
| 47 |
sys.tracebacklimit = 0 |
| 48 |
raise RuntimeError('There was an error loading the ChakraCore library {}'.format(chakraCoreLibrary)) |
| 49 |
|
| 50 |
if sys.platform != 'win32': |
| 51 |
chakraCore.DllMain(0, 1, 0) |
| 52 |
chakraCore.DllMain(0, 2, 0) |
| 53 |
|
| 54 |
script = create_string_buffer(template(body, domain).encode('utf-16')) |
| 55 |
|
| 56 |
runtime = c_void_p() |
| 57 |
chakraCore.JsCreateRuntime(0, 0, byref(runtime)) |
| 58 |
|
| 59 |
context = c_void_p() |
| 60 |
chakraCore.JsCreateContext(runtime, byref(context)) |
| 61 |
chakraCore.JsSetCurrentContext(context) |
| 62 |
|
| 63 |
fname = c_void_p() |
| 64 |
chakraCore.JsCreateString( |
| 65 |
'iuam-challenge.js', |
| 66 |
len('iuam-challenge.js'), |
| 67 |
byref(fname) |
| 68 |
) |
| 69 |
|
| 70 |
scriptSource = c_void_p() |
| 71 |
chakraCore.JsCreateExternalArrayBuffer( |
| 72 |
script, |
| 73 |
len(script), |
| 74 |
0, |
| 75 |
0, |
| 76 |
byref(scriptSource) |
| 77 |
) |
| 78 |
|
| 79 |
jsResult = c_void_p() |
| 80 |
chakraCore.JsRun(scriptSource, 0, fname, 0x02, byref(jsResult)) |
| 81 |
|
| 82 |
resultJSString = c_void_p() |
| 83 |
chakraCore.JsConvertValueToString(jsResult, byref(resultJSString)) |
| 84 |
|
| 85 |
stringLength = c_size_t() |
| 86 |
chakraCore.JsCopyString(resultJSString, 0, 0, byref(stringLength)) |
| 87 |
|
| 88 |
resultSTR = create_string_buffer(stringLength.value + 1) |
| 89 |
chakraCore.JsCopyString( |
| 90 |
resultJSString, |
| 91 |
byref(resultSTR), |
| 92 |
stringLength.value + 1, |
| 93 |
0 |
| 94 |
) |
| 95 |
|
| 96 |
chakraCore.JsDisposeRuntime(runtime) |
| 97 |
|
| 98 |
return resultSTR.value |
| 99 |
|
| 100 |
|
| 101 |
# ------------------------------------------------------------------------------- # |
| 102 |
|
| 103 |
ChallengeInterpreter() |