Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
Bugfix in SEC generator (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkCat09 committed Oct 5, 2022
1 parent 2a9f287 commit 154b2c2
Showing 1 changed file with 4 additions and 59 deletions.
63 changes: 4 additions & 59 deletions python_aternos/atconnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import re
import time
import random
import secrets
import logging
from functools import partial

from typing import Optional, Union
from typing import Optional
from typing import Dict, Any

import requests
Expand Down Expand Up @@ -97,8 +97,8 @@ def generate_sec(self) -> str:
Random SEC `key:value` string
"""

randkey = self.generate_aternos_rand()
randval = self.generate_aternos_rand()
randkey = secrets.token_hex(8)
randval = secrets.token_hex(8)
self.sec = f'{randkey}:{randval}'
self.session.cookies.set(
f'ATERNOS_SEC_{randkey}', randval,
Expand All @@ -107,61 +107,6 @@ def generate_sec(self) -> str:

return self.sec

def generate_aternos_rand(self, randlen: int = 16) -> str:

"""Generates a random string using
Aternos algorithm from main.js file
Args:
randlen (int, optional): Random string length
Returns:
Random string for SEC token
"""

# a list with randlen+1 empty strings:
# generate a string with spaces,
# then split it by space
rand_arr = (' ' * (randlen + 1)).split(' ')

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:

"""Converts an integer to specified base
Args:
num (Union[int,float,str]): Integer in any base to convert.
If it is a float starting with `0.`,
zero and point will be removed to get int
base (int): New base
frombase (int, optional): Given number base
Returns:
Number converted to a specified base
"""

if isinstance(num, str):
num = int(num, frombase)

if isinstance(num, float):
sliced = str(num)[2:]
num = int(sliced)

symbols = '0123456789abcdefghijklmnopqrstuvwxyz'
basesym = symbols[:base]
result = ''
while num > 0:
rem = num % base
result = str(basesym[rem]) + result
num //= base
return result

def request_cloudflare(
self, url: str, method: str,
params: Optional[Dict[Any, Any]] = None,
Expand Down

0 comments on commit 154b2c2

Please sign in to comment.