2020-08-03 02:36:38 +00:00
|
|
|
import base64
|
|
|
|
import subprocess
|
2021-06-23 19:54:28 +00:00
|
|
|
import sys
|
2020-08-03 02:36:38 +00:00
|
|
|
|
|
|
|
from . import JavaScriptInterpreter
|
2021-06-23 19:54:28 +00:00
|
|
|
from .encapsulated import template
|
2020-08-03 02:36:38 +00:00
|
|
|
|
2021-06-23 19:54:28 +00:00
|
|
|
# ------------------------------------------------------------------------------- #
|
2020-08-03 02:36:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ChallengeInterpreter(JavaScriptInterpreter):
|
|
|
|
|
2021-06-23 19:54:28 +00:00
|
|
|
# ------------------------------------------------------------------------------- #
|
|
|
|
|
2020-08-03 02:36:38 +00:00
|
|
|
def __init__(self):
|
|
|
|
super(ChallengeInterpreter, self).__init__('nodejs')
|
|
|
|
|
2021-06-23 19:54:28 +00:00
|
|
|
# ------------------------------------------------------------------------------- #
|
|
|
|
|
|
|
|
def eval(self, body, domain):
|
2020-08-03 02:36:38 +00:00
|
|
|
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));' \
|
2021-06-23 19:54:28 +00:00
|
|
|
% base64.b64encode(template(body, domain).encode('UTF-8')).decode('ascii')
|
2020-08-03 02:36:38 +00:00
|
|
|
|
|
|
|
return subprocess.check_output(['node', '-e', js])
|
|
|
|
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno == 2:
|
|
|
|
raise EnvironmentError(
|
2021-06-23 19:54:28 +00:00
|
|
|
'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.)'
|
2020-08-03 02:36:38 +00:00
|
|
|
)
|
|
|
|
raise
|
|
|
|
except Exception:
|
2021-06-23 19:54:28 +00:00
|
|
|
sys.tracebacklimit = 0
|
|
|
|
raise RuntimeError('Error executing Cloudflare IUAM Javascript in nodejs')
|
2020-08-03 02:36:38 +00:00
|
|
|
|
|
|
|
|
2021-06-23 19:54:28 +00:00
|
|
|
# ------------------------------------------------------------------------------- #
|
2020-08-03 02:36:38 +00:00
|
|
|
|
|
|
|
ChallengeInterpreter()
|