Merge pull request #5 from lusm554/main

AJAX_TOKEN parser
This commit is contained in:
Andrey 2021-10-29 17:07:53 +04:00 committed by GitHub
commit 6ca89bf959
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 198 additions and 110 deletions

5
.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
# Python
__pycache__
#Vim
*.swp

2
NOTICE
View file

@ -1,4 +1,4 @@
Copyright 2021 Chechkenev Andrey
Copyright 2021 Chechkenev Andrey, lusm554
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

View file

@ -1,6 +1,7 @@
# Python Aternos API
An unofficial Aternos API written in Python.
It uses requests, cloudscraper and lxml to parse data from [aternos.org](https://aternos.org/).
> Note for vim: if u have problem like this `IndentationError: unindent does not match any outer indentation level`, try out `retab`.
## Using
First you need to install the module:
@ -55,7 +56,7 @@ You can find full documentation on the [Project Wiki](https://github.com/DarkCat
## License
[License Notice](NOTICE):
```
Copyright 2021 Chechkenev Andrey
Copyright 2021 Chechkenev Andrey, lusm554
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

11
connect_test.py Normal file
View file

@ -0,0 +1,11 @@
from python_aternos import Client as AternosClient
aternos = AternosClient('', password='')
srvs = aternos.servers
print(srvs)
s = srvs[0]
s.start()

44
js2py_test.py Executable file
View file

@ -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)

View file

@ -8,6 +8,23 @@ from typing import Optional, Union
from . import aterrors
# TEST
import js2py
import base64
# 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
REQUA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Goanna/4.8 Firefox/68.0 PaleMoon/29.4.0.2'
@ -29,11 +46,20 @@ class AternosConnect:
pagetree = lxml.html.fromstring(response)
try:
# fetch text
pagehead = pagetree.head
self.token = re.search(
r'const\s+AJAX_TOKEN\s*=\s*["\'](\w+)["\']',
pagehead.text_content()
)[1]
text = pagehead.text_content()
#search
js_funcs = re.findall(r"\(\(\)(.*?)\)\(\);", text)
token_js_func = js_funcs[1] if len(js_funcs) > 1 else js_funcs[0]
# run js
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'

View file

@ -1,3 +1,4 @@
lxml==4.6.2
requests==2.25.1
cloudscraper==1.2.58
Js2Py==0.71