From 414f63b40b47d0ed3879b0a31a6874b93eea9a0b Mon Sep 17 00:00:00 2001 From: loveyousomuch Date: Thu, 28 Oct 2021 00:06:18 +0300 Subject: [PATCH] WORK! fetch token, start and stop server --- js2py_test.py | 44 +++++++++++++++++++++++++++ python_aternos/atconnect.py | 60 +++++++++++++------------------------ racer_test.py | 45 ---------------------------- requirements.txt | 2 +- 4 files changed, 65 insertions(+), 86 deletions(-) create mode 100755 js2py_test.py delete mode 100755 racer_test.py diff --git a/js2py_test.py b/js2py_test.py new file mode 100755 index 0000000..48f2699 --- /dev/null +++ b/js2py_test.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +import base64 +import js2py + +# Emulate 'atob' function +#print(base64.standard_b64decode('MmlYaDVXNXVFWXE1ZldKSWF6UTY=')) + +# Test cases +tests = [ + """(() => {window[("A" + "J" + "AX_T" + "OKE" + "N")]=("2iXh5W5u" + "EYq" + "5fWJIa" + "zQ6");})();""", + """ (() => {window[["N","TOKE","AJAX_"].reverse().join('')]=["IazQ6","fWJ","h5W5uEYq5","2iX"].reverse().join('');})();""", + """(() => {window["AJAX_TOKEN"] = atob("SGVsbG8sIHdvcmxk")})();""", + """(() => {window[atob('QUpBWF9UT0tFTg==')]=atob('MmlYaDVXNXVFWXE1ZldKSWF6UTY=');})();""", + """(() => {window["AJAX_TOKEN"] = "1234" })();""", + """(() => {window[atob('QUpBWF9UT0tFTg==')]="2iXh5W5uEYq5fWJIazQ6";})();""", +] + +# Array function to ECMAScript 5.1 +def code(f): + return "(function() { " + f[f.index("{")+1 : f.index("}")] + "})();" + +# Emulation atob V8 +def atob(arg): + return base64.standard_b64decode(str(arg)).decode("utf-8") + +presettings = """ +let window = {}; +""" + +ctx = js2py.EvalJs({ 'atob': atob }) + +''' +ctx.execute(presettings + code(tests[3])) +print(ctx.window) +''' + +for f in tests: + try: + c = code(f) + ctx.execute(presettings + c) + print(ctx.window['AJAX_TOKEN']) + except Exception as e: + print(c, '\n', e) + diff --git a/python_aternos/atconnect.py b/python_aternos/atconnect.py index d349244..a05f948 100644 --- a/python_aternos/atconnect.py +++ b/python_aternos/atconnect.py @@ -8,18 +8,22 @@ from typing import Optional, Union from . import aterrors -#TEST -from py_mini_racer import MiniRacer +# TEST +import js2py import base64 -presettings = """ -let window = {1: null, 2: null, AJAX_TOKEN: null}; -let i = 1; -function __log() { return {win_var: window["AJAX_TOKEN"], 1: window[1], 2: window[2]} }; -function atob(arg) {window[i++] = arg;}; -""" -postsettings = """__log();""" +# Set obj for js +presettings = """ +let window = {}; +""" +# Convert array function to CMAScript 5 function +def toECMAScript5Function(f): + return "(function() { " + f[f.index("{")+1 : f.index("}")] + "})();" + +# Emulation of atob - https://developer.mozilla.org/en-US/docs/Web/API/atob +def atob(s): + return base64.standard_b64decode(str(s)).decode("utf-8") REQGET = 0 REQPOST = 1 @@ -45,41 +49,17 @@ class AternosConnect: # fetch text pagehead = pagetree.head text = pagehead.text_content() - #print(text) #search - token_js_func = text[ - text.index("const COOKIE_PREFIX = \"ATERNOS\";") + - len("const COOKIE_PREFIX = \"ATERNOS\";") : - text.index("(function(i,s,o,g,r,a,m)") - ].strip() - print(token_js_func) - + js_funcs = re.findall(r"\(\(\)(.*?)\)\(\);", text) + token_js_func = js_funcs[1] if len(js_funcs) > 1 else js_funcs[0] # run js - ctx = MiniRacer() - result = ctx.eval(presettings + token_js_func) - result = ctx.call('__log') - - print(result) - - if 'win_var' in result and result['win_var']: - result = result['win_var'] - elif '1' in result and ('2' in result and not result['2']): - result = base64.standard_b64decode(result['1']) - else: - result = base64.standard_b64decode(result['2']) - - - print(result) - self.token = result - - """ - self.token = re.search( - r'const\s+AJAX_TOKEN\s*=\s*["\'](\w+)["\']', - text - )[1] - """ + ctx = js2py.EvalJs({ 'atob': atob }) + jsf = toECMAScript5Function(token_js_func) + ctx.execute(presettings + jsf) + + self.token = ctx.window['AJAX_TOKEN'] except (IndexError, TypeError): raise aterrors.AternosCredentialsError( 'Unable to parse TOKEN from the page' diff --git a/racer_test.py b/racer_test.py deleted file mode 100755 index 23b50ec..0000000 --- a/racer_test.py +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env python3 - -from py_mini_racer import MiniRacer -import base64 - -# Set function for manage global vars -presettings = """ -let window = {1: null, 2: null, AJAX_TOKEN: null}; -let i = 1; -function __log() { return {win_var: window["AJAX_TOKEN"], 1: window[1], 2: window[2]} }; -function atob(arg) {window[i++] = arg;}; -""" - -# Test cases -tests = [ - """(() => {window[("A" + "J" + "AX_T" + "OKE" + "N")]=("2iXh5W5u" + "EYq" + "5fWJIa" + "zQ6");})();""", - """ (() => {window[["N","TOKE","AJAX_"].reverse().join('')]=["IazQ6","fWJ","h5W5uEYq5","2iX"].reverse().join('');})();""", - """(() => {window["AJAX_TOKEN"] = atob("SGVsbG8sIHdvcmxk")})();""", - """(() => {window[atob('QUpBWF9UT0tFTg==')]=atob('MmlYaDVXNXVFWXE1ZldKSWF6UTY=');})();""", - """(() => {window["AJAX_TOKEN"] = "1234" })();""", - """(() => {window[atob('QUpBWF9UT0tFTg==')]="2iXh5W5uEYq5fWJIazQ6";})();""", -] - -# Emulate 'atob' function -#print(base64.standard_b64decode('MmlYaDVXNXVFWXE1ZldKSWF6UTY=')) - -for js in tests: - ctx = MiniRacer() - result = ctx.eval(presettings + js) - result = ctx.call('__log') - - print(result) - ''' - if 'win_var' in result and result['win_var']: - result = result['win_var'] - elif '1' in result and ('2' in result and not result['2']): - result = base64.standard_b64decode(result['1']) - else: - result = base64.standard_b64decode(result['2']) - ''' - print('Case:\n', js, '\n') - print('Result: \n', result, '\n') - print('-' * 30, '\n') - - diff --git a/requirements.txt b/requirements.txt index 8c37f68..defa9b8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ lxml==4.6.2 requests==2.25.1 cloudscraper==1.2.58 -py-mini-racer==0.6.0 +Js2Py==0.71