Improvements in automatic session saving

This commit is contained in:
DarkCat09 2022-09-29 20:04:47 +04:00
parent b0fccc34cd
commit 5919bebccb

View file

@ -39,6 +39,9 @@ class Client:
""" """
self.atconn = atconn self.atconn = atconn
self.saved_session = ''
self.parsed = False self.parsed = False
self.servers: List[AternosServer] = [] self.servers: List[AternosServer] = []
@ -70,8 +73,9 @@ class Client:
atconn = AternosConnect() atconn = AternosConnect()
secure = cls.secure_name(username) filename = cls.session_file(
filename = f'{sessions_dir}/.at_{secure}' username, sessions_dir
)
try: try:
return cls.restore_session(filename) return cls.restore_session(filename)
@ -103,6 +107,7 @@ class Client:
) )
obj = cls(atconn) obj = cls(atconn)
obj.saved_session = filename
try: try:
obj.save_session(filename) obj.save_session(filename)
@ -157,7 +162,8 @@ class Client:
@classmethod @classmethod
def restore_session(cls, file: str = '~/.aternos'): def restore_session(cls, file: str = '~/.aternos'):
"""Log in to Aternos using a saved ATERNOS_SESSION cookie """Log in to Aternos using
a saved ATERNOS_SESSION cookie
Args: Args:
file (str, optional): File where a session cookie was saved file (str, optional): File where a session cookie was saved
@ -188,7 +194,10 @@ class Client:
servers=saved[1:] servers=saved[1:]
) )
return cls.from_session(session) obj = cls.from_session(session)
obj.saved_session = file
return obj
@staticmethod @staticmethod
def md5encode(passwd: str) -> str: def md5encode(passwd: str) -> str:
@ -206,25 +215,30 @@ class Client:
return encoded.hexdigest().lower() return encoded.hexdigest().lower()
@staticmethod @staticmethod
def secure_name(filename: str, repl: str = '_') -> str: def session_file(username: str, sessions_dir: str = '~') -> str:
"""Replaces unsecure characters """Generates session file name
in filename to underscore or `repl` for authenticated user
Args: Args:
filename (str): Filename username (str): Authenticated user
repl (str, optional): Replacement sessions_dir (str, optional): Path to directory
for unsafe characters with automatically saved sessions
Returns: Returns:
str: Secure filename Filename
""" """
return re.sub( # unsafe symbols replacement
repl = '_'
secure = re.sub(
r'[^A-Za-z0-9_-]', r'[^A-Za-z0-9_-]',
repl, filename repl, username
) )
return f'{sessions_dir}/.at_{secure}'
def save_session( def save_session(
self, self,
file: str = '~/.aternos', file: str = '~/.aternos',
@ -251,6 +265,24 @@ class Client:
for s in self.servers: for s in self.servers:
f.write(s.servid + '\n') f.write(s.servid + '\n')
def remove_session(self, file: str = '~/.aternos') -> None:
"""Removes a file which contains
ATERNOS_SESSION cookie saved
with `save_session()`
Args:
file (str, optional): Filename
"""
file = os.path.expanduser(file)
logging.debug(f'Removing session file: {file}')
try:
os.remove(file)
except OSError as err:
logging.warning(f'Unable to remove session file: {err}')
def list_servers(self, cache: bool = True) -> List[AternosServer]: def list_servers(self, cache: bool = True) -> List[AternosServer]:
"""Parses a list of your servers from Aternos website """Parses a list of your servers from Aternos website
@ -276,6 +308,12 @@ class Client:
) )
self.refresh_servers(servers) self.refresh_servers(servers)
# Update session file (add servers)
try:
self.save_session(self.saved_session)
except OSError as err:
logging.warning(f'Unable to save servers list to file: {err}')
return self.servers return self.servers
def refresh_servers(self, ids: List[str]) -> None: def refresh_servers(self, ids: List[str]) -> None:
@ -321,6 +359,8 @@ class Client:
'GET', sendtoken=True 'GET', sendtoken=True
) )
self.remove_session(self.saved_session)
def change_username(self, value: str) -> None: def change_username(self, value: str) -> None:
"""Changes a username in your Aternos account """Changes a username in your Aternos account
@ -399,7 +439,7 @@ class Client:
'https://aternos.org/panel/ajax/account/secret.php', 'https://aternos.org/panel/ajax/account/secret.php',
'GET', sendtoken=True 'GET', sendtoken=True
).json() ).json()
def save_qr(self, qrcode: str, filename: str) -> None: def save_qr(self, qrcode: str, filename: str) -> None:
"""Writes a 2FA QR code into a png-file """Writes a 2FA QR code into a png-file