2021-10-08 19:35:20 +04:00
|
|
|
import re
|
|
|
|
import random
|
2022-03-30 19:40:58 +04:00
|
|
|
import logging
|
2021-10-14 17:56:01 +04:00
|
|
|
from requests import Response
|
2021-10-08 19:35:20 +04:00
|
|
|
from cloudscraper import CloudScraper
|
2022-05-13 17:44:00 +04:00
|
|
|
from typing import Optional, Union, Dict
|
2021-10-08 19:35:20 +04:00
|
|
|
|
2021-11-01 18:04:19 +04:00
|
|
|
from . import atjsparse
|
2022-04-28 09:23:02 +04:00
|
|
|
from .aterrors import CredentialsError, CloudflareError
|
2021-10-27 19:42:41 +03:00
|
|
|
|
2022-05-13 17:44:00 +04:00
|
|
|
REQUA = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.47'
|
2022-05-16 18:16:15 +04:00
|
|
|
REQHEADERS = {
|
|
|
|
'Host': 'aternos.org',
|
|
|
|
'User-Agent': REQUA,
|
|
|
|
'Sec-Ch-Ua': '" Not A;Brand";v="99", "Chromium";v="100", "Opera";v="86"',
|
|
|
|
'Sec-Ch-Ua-Mobile': '?0',
|
|
|
|
'Sec-Ch-Ua-Platform': '"Linux"',
|
|
|
|
'Sec-Fetch-Dest': 'document',
|
|
|
|
'Sec-Fetch-Mode': 'navigate',
|
|
|
|
'Sec-Fetch-Site': 'same-origin',
|
|
|
|
'Sec-Fetch-User': '?1',
|
|
|
|
'Upgrade-Insecure-Requests': '1'
|
|
|
|
}
|
2021-10-08 19:35:20 +04:00
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
class AternosConnect:
|
2021-10-08 19:35:20 +04:00
|
|
|
|
2021-11-01 18:04:19 +04:00
|
|
|
def __init__(self) -> None:
|
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
self.session = CloudScraper()
|
2022-03-30 19:40:58 +04:00
|
|
|
self.atsession = ''
|
2021-11-01 18:04:19 +04:00
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
def parse_token(self) -> str:
|
2021-11-01 18:04:19 +04:00
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
loginpage = self.request_cloudflare(
|
|
|
|
f'https://aternos.org/go/', 'GET'
|
|
|
|
).content
|
2021-11-01 18:04:19 +04:00
|
|
|
|
2022-05-16 18:16:15 +04:00
|
|
|
# Using the standard string methods
|
|
|
|
# instead of the expensive xml parsing
|
|
|
|
head = b'<head>'
|
|
|
|
headtag = loginpage.find(head)
|
|
|
|
headend = loginpage.find(b'</head>', headtag + len(head))
|
|
|
|
|
|
|
|
# Some checks
|
|
|
|
if headtag < 0 or headend < 0:
|
|
|
|
pagehead = loginpage
|
|
|
|
raise RuntimeWarning('Unable to find <head> tag, parsing the whole page')
|
2021-11-01 18:04:19 +04:00
|
|
|
|
2022-05-16 18:16:15 +04:00
|
|
|
# Extracting <head> content
|
|
|
|
headtag = headtag + len(head)
|
|
|
|
pagehead = loginpage[headtag:headend]
|
|
|
|
|
|
|
|
try:
|
|
|
|
text = pagehead.decode('utf-8', 'replace')
|
2021-11-01 18:04:19 +04:00
|
|
|
js_code = re.findall(r'\(\(\)(.*?)\)\(\);', text)
|
|
|
|
token_func = js_code[1] if len(js_code) > 1 else js_code[0]
|
|
|
|
|
|
|
|
ctx = atjsparse.exec(token_func)
|
|
|
|
self.token = ctx.window['AJAX_TOKEN']
|
|
|
|
|
|
|
|
except (IndexError, TypeError):
|
2022-03-18 18:38:36 +04:00
|
|
|
raise CredentialsError(
|
2021-11-01 18:04:19 +04:00
|
|
|
'Unable to parse TOKEN from the page'
|
|
|
|
)
|
|
|
|
|
|
|
|
return self.token
|
|
|
|
|
|
|
|
def generate_sec(self) -> str:
|
|
|
|
|
|
|
|
randkey = self.generate_aternos_rand()
|
|
|
|
randval = self.generate_aternos_rand()
|
|
|
|
self.sec = f'{randkey}:{randval}'
|
|
|
|
self.session.cookies.set(
|
|
|
|
f'ATERNOS_SEC_{randkey}', randval,
|
|
|
|
domain='aternos.org'
|
|
|
|
)
|
|
|
|
|
|
|
|
return self.sec
|
|
|
|
|
|
|
|
def generate_aternos_rand(self, randlen:int=16) -> str:
|
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
# a list with randlen+1 empty strings:
|
|
|
|
# generate a string with spaces,
|
|
|
|
# then split it by space
|
|
|
|
rand_arr = (' ' * (randlen+1)).split(' ')
|
2021-11-01 18:04:19 +04:00
|
|
|
|
2022-01-11 18:08:59 +04:00
|
|
|
rand = random.random()
|
|
|
|
rand_alphanum = self.convert_num(rand, 36) + ('0' * 17)
|
|
|
|
|
|
|
|
return (rand_alphanum[:18].join(rand_arr)[:randlen])
|
|
|
|
|
|
|
|
def convert_num(
|
|
|
|
self, num:Union[int,float,str],
|
|
|
|
base:int, frombase:int=10) -> str:
|
2021-11-01 18:04:19 +04:00
|
|
|
|
2022-01-11 18:08:59 +04:00
|
|
|
if isinstance(num, str):
|
|
|
|
num = int(num, frombase)
|
2021-11-01 18:04:19 +04:00
|
|
|
|
2022-01-11 18:08:59 +04:00
|
|
|
if isinstance(num, float):
|
|
|
|
sliced = str(num)[2:]
|
|
|
|
num = int(sliced)
|
|
|
|
|
|
|
|
symbols = '0123456789abcdefghijklmnopqrstuvwxyz'
|
|
|
|
basesym = symbols[:base]
|
2021-11-01 18:04:19 +04:00
|
|
|
result = ''
|
|
|
|
while num > 0:
|
2022-01-11 18:08:59 +04:00
|
|
|
rem = num % base
|
|
|
|
result = str(basesym[rem]) + result
|
2021-11-01 18:04:19 +04:00
|
|
|
num //= base
|
|
|
|
return result
|
|
|
|
|
2022-05-16 18:16:15 +04:00
|
|
|
def add_headers(self, headers:Optional[Dict[str,str]]=None) -> Dict[str,str]:
|
2022-05-13 17:44:00 +04:00
|
|
|
|
|
|
|
headers = headers or {}
|
2022-05-16 18:16:15 +04:00
|
|
|
headers.update(REQHEADERS)
|
|
|
|
return headers
|
2022-05-13 17:44:00 +04:00
|
|
|
|
2021-11-01 18:04:19 +04:00
|
|
|
def request_cloudflare(
|
2022-03-25 16:45:38 +04:00
|
|
|
self, url:str, method:str,
|
2022-01-11 18:08:59 +04:00
|
|
|
params:Optional[dict]=None, data:Optional[dict]=None,
|
|
|
|
headers:Optional[dict]=None, reqcookies:Optional[dict]=None,
|
2022-04-28 09:23:02 +04:00
|
|
|
sendtoken:bool=False, redirect:bool=True, retry:int=0) -> Response:
|
|
|
|
|
2022-05-13 17:44:00 +04:00
|
|
|
if retry > 3:
|
2022-04-28 09:23:02 +04:00
|
|
|
raise CloudflareError('Unable to bypass Cloudflare protection')
|
2021-11-01 18:04:19 +04:00
|
|
|
|
2022-03-30 19:40:58 +04:00
|
|
|
try:
|
|
|
|
self.atsession = self.session.cookies['ATERNOS_SESSION']
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if method not in ('GET', 'POST'):
|
|
|
|
raise NotImplementedError('Only GET and POST are available')
|
|
|
|
|
|
|
|
params = params or {}
|
|
|
|
data = data or {}
|
|
|
|
reqcookies = reqcookies or {}
|
2022-05-16 18:16:15 +04:00
|
|
|
headers = self.add_headers(headers)
|
2021-11-01 18:04:19 +04:00
|
|
|
|
2022-01-11 18:08:59 +04:00
|
|
|
if sendtoken:
|
2022-03-25 16:45:38 +04:00
|
|
|
params['TOKEN'] = self.token
|
|
|
|
params['SEC'] = self.sec
|
2022-05-16 18:16:15 +04:00
|
|
|
headers['X-Requested-With'] = 'XMLHttpRequest'
|
2022-03-25 16:45:38 +04:00
|
|
|
|
|
|
|
# requests.cookies.CookieConflictError bugfix
|
2022-03-30 19:40:58 +04:00
|
|
|
reqcookies['ATERNOS_SESSION'] = self.atsession
|
2022-03-25 16:45:38 +04:00
|
|
|
del self.session.cookies['ATERNOS_SESSION']
|
2022-03-30 19:40:58 +04:00
|
|
|
|
|
|
|
logging.debug(f'Requesting({method})' + url)
|
|
|
|
logging.debug('headers=' + str(headers))
|
|
|
|
logging.debug('params=' + str(params))
|
|
|
|
logging.debug('data=' + str(data))
|
|
|
|
logging.debug('req-cookies=' + str(reqcookies))
|
|
|
|
logging.debug('session-cookies=' + str(self.session.cookies))
|
2022-03-25 16:45:38 +04:00
|
|
|
|
|
|
|
if method == 'POST':
|
|
|
|
req = self.session.post(
|
|
|
|
url, data=data, params=params,
|
|
|
|
headers=headers, cookies=reqcookies,
|
|
|
|
allow_redirects=redirect
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
req = self.session.get(
|
|
|
|
url, params={**params, **data},
|
|
|
|
headers=headers, cookies=reqcookies,
|
|
|
|
allow_redirects=redirect
|
|
|
|
)
|
2022-03-30 19:40:58 +04:00
|
|
|
|
2022-04-28 09:23:02 +04:00
|
|
|
if '<title>Please Wait... | Cloudflare</title>' in req.text:
|
|
|
|
logging.info('Retrying to bypass Cloudflare')
|
|
|
|
self.request_cloudflare(
|
|
|
|
url, method,
|
|
|
|
params, data,
|
|
|
|
headers, reqcookies,
|
2022-05-13 17:44:00 +04:00
|
|
|
sendtoken, redirect,
|
|
|
|
retry - 1
|
2022-04-28 09:23:02 +04:00
|
|
|
)
|
|
|
|
|
2022-03-30 19:40:58 +04:00
|
|
|
logging.info(
|
|
|
|
f'{method} completed with {req.status_code} status'
|
|
|
|
)
|
|
|
|
|
2021-11-01 18:04:19 +04:00
|
|
|
return req
|