2022-07-01 14:28:39 +04:00
|
|
|
"""Entry point. Authorizes on Aternos
|
|
|
|
and allows to manage your account"""
|
|
|
|
|
2022-06-16 15:40:10 +04:00
|
|
|
import os
|
|
|
|
import re
|
2022-12-26 15:48:08 +04:00
|
|
|
from typing import Optional, Type
|
2022-06-16 15:40:10 +04:00
|
|
|
|
2023-05-29 11:44:19 +04:00
|
|
|
from .atlog import log, is_debug, set_debug
|
2023-05-24 20:03:09 +04:00
|
|
|
from .atmd5 import md5encode
|
|
|
|
|
|
|
|
from .ataccount import AternosAccount
|
2023-05-24 18:15:18 +04:00
|
|
|
|
2022-06-16 15:40:10 +04:00
|
|
|
from .atconnect import AternosConnect
|
2023-05-24 20:03:09 +04:00
|
|
|
from .atconnect import AJAX_URL
|
2023-05-24 17:41:33 +04:00
|
|
|
|
2022-06-16 15:40:10 +04:00
|
|
|
from .aterrors import CredentialsError
|
2022-09-23 17:21:17 +04:00
|
|
|
from .aterrors import TwoFactorAuthError
|
2022-06-16 15:40:10 +04:00
|
|
|
|
2022-12-26 15:48:08 +04:00
|
|
|
from . import atjsparse
|
|
|
|
from .atjsparse import Interpreter
|
|
|
|
from .atjsparse import Js2PyInterpreter
|
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
|
2022-06-16 15:40:10 +04:00
|
|
|
class Client:
|
2022-09-23 16:53:47 +04:00
|
|
|
"""Aternos API Client class, object
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
of which contains user's auth data"""
|
2022-06-23 15:13:56 +04:00
|
|
|
|
2023-05-24 20:03:09 +04:00
|
|
|
def __init__(self) -> None:
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
|
2023-05-24 20:03:09 +04:00
|
|
|
# Config
|
|
|
|
self.sessions_dir = '~'
|
|
|
|
self.js: Type[Interpreter] = Js2PyInterpreter
|
|
|
|
# ###
|
2022-09-29 20:04:47 +04:00
|
|
|
|
2023-05-29 11:44:19 +04:00
|
|
|
self.saved_session = '~/.aternos' # will be rewritten by login()
|
2023-05-24 20:03:09 +04:00
|
|
|
self.atconn = AternosConnect()
|
|
|
|
self.account = AternosAccount(self)
|
2022-09-29 20:04:47 +04:00
|
|
|
|
2023-05-24 20:03:09 +04:00
|
|
|
def login(
|
|
|
|
self,
|
|
|
|
username: str,
|
|
|
|
password: str,
|
|
|
|
code: Optional[int] = None) -> None:
|
|
|
|
"""Log in to your Aternos account
|
|
|
|
with a username and a plain password
|
2022-07-01 19:02:10 +04:00
|
|
|
|
2023-05-24 20:03:09 +04:00
|
|
|
Args:
|
|
|
|
username (str): Username
|
|
|
|
password (str): Plain-text password
|
|
|
|
code (Optional[int], optional): 2FA code
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.login_hashed(
|
|
|
|
username,
|
|
|
|
md5encode(password),
|
|
|
|
code,
|
|
|
|
)
|
2022-06-23 15:13:56 +04:00
|
|
|
|
2023-05-24 20:03:09 +04:00
|
|
|
def login_hashed(
|
|
|
|
self,
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
username: str,
|
|
|
|
md5: str,
|
2023-05-24 20:03:09 +04:00
|
|
|
code: Optional[int] = None) -> None:
|
|
|
|
"""Log in to your Aternos account
|
|
|
|
with a username and a hashed password
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
|
|
|
|
Args:
|
2023-05-24 20:03:09 +04:00
|
|
|
username (str): Username
|
|
|
|
md5 (str): Password hashed with MD5
|
|
|
|
code (int): 2FA code
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
|
|
|
|
Raises:
|
2023-05-24 20:03:09 +04:00
|
|
|
TwoFactorAuthError: If the 2FA is enabled,
|
|
|
|
but `code` argument was not passed or is incorrect
|
|
|
|
CredentialsError: If the Aternos backend
|
|
|
|
returned empty session cookie
|
|
|
|
(usually because of incorrect credentials)
|
|
|
|
ValueError: _description_
|
2022-06-23 15:13:56 +04:00
|
|
|
"""
|
|
|
|
|
2023-05-24 20:03:09 +04:00
|
|
|
filename = self.session_filename(
|
|
|
|
username, self.sessions_dir
|
2022-09-29 20:04:47 +04:00
|
|
|
)
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
|
|
|
|
try:
|
2023-05-24 20:03:09 +04:00
|
|
|
self.restore_session(filename)
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
except (OSError, CredentialsError):
|
|
|
|
pass
|
|
|
|
|
2023-05-24 20:03:09 +04:00
|
|
|
atjsparse.get_interpreter(create=self.js)
|
|
|
|
self.atconn.parse_token()
|
|
|
|
self.atconn.generate_sec()
|
2022-09-29 18:55:37 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
credentials = {
|
|
|
|
'user': username,
|
2022-09-23 17:21:17 +04:00
|
|
|
'password': md5,
|
2022-06-23 15:13:56 +04:00
|
|
|
}
|
|
|
|
|
2022-09-23 17:21:17 +04:00
|
|
|
if code is not None:
|
2022-09-29 18:18:15 +04:00
|
|
|
credentials['code'] = str(code)
|
2022-09-23 17:21:17 +04:00
|
|
|
|
2023-05-24 20:03:09 +04:00
|
|
|
loginreq = self.atconn.request_cloudflare(
|
2023-05-29 11:44:19 +04:00
|
|
|
f'{AJAX_URL}/account/login',
|
2023-05-24 20:03:09 +04:00
|
|
|
'POST', data=credentials, sendtoken=True,
|
2022-06-23 15:13:56 +04:00
|
|
|
)
|
|
|
|
|
2022-09-23 17:21:17 +04:00
|
|
|
if b'"show2FA":true' in loginreq.content:
|
|
|
|
raise TwoFactorAuthError('2FA code is required')
|
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
if 'ATERNOS_SESSION' not in loginreq.cookies:
|
|
|
|
raise CredentialsError(
|
|
|
|
'Check your username and password'
|
|
|
|
)
|
|
|
|
|
2023-05-24 20:03:09 +04:00
|
|
|
self.saved_session = filename
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
try:
|
2023-05-24 20:03:09 +04:00
|
|
|
self.save_session(filename)
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
except OSError:
|
|
|
|
pass
|
2023-05-29 12:06:21 +04:00
|
|
|
|
2023-05-29 12:02:45 +04:00
|
|
|
def login_with_session(self, session: str) -> None:
|
|
|
|
"""Log in using ATERNOS_SESSION cookie
|
|
|
|
|
|
|
|
Args:
|
|
|
|
session (str): Session cookie value
|
|
|
|
"""
|
|
|
|
|
2023-06-03 16:45:58 +04:00
|
|
|
self.atconn.parse_token()
|
|
|
|
self.atconn.generate_sec()
|
2023-05-29 12:02:45 +04:00
|
|
|
self.atconn.session.cookies['ATERNOS_SESSION'] = session
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
|
2023-05-24 20:03:09 +04:00
|
|
|
def logout(self) -> None:
|
|
|
|
"""Log out from the Aternos account"""
|
2022-06-23 15:13:56 +04:00
|
|
|
|
2023-05-24 20:03:09 +04:00
|
|
|
self.atconn.request_cloudflare(
|
2023-05-29 11:44:19 +04:00
|
|
|
f'{AJAX_URL}/account/logout',
|
2023-05-24 20:03:09 +04:00
|
|
|
'GET', sendtoken=True,
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
)
|
2022-06-23 15:13:56 +04:00
|
|
|
|
2023-05-24 20:03:09 +04:00
|
|
|
self.remove_session(self.saved_session)
|
2022-06-23 15:13:56 +04:00
|
|
|
|
2023-05-29 11:44:19 +04:00
|
|
|
def restore_session(self, file: str = '~/.aternos') -> None:
|
2023-05-24 20:03:09 +04:00
|
|
|
"""Restores ATERNOS_SESSION cookie and,
|
|
|
|
if included, servers list, from a session file
|
2022-06-23 15:13:56 +04:00
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
Args:
|
2023-05-29 11:44:19 +04:00
|
|
|
file (str, optional): Filename
|
2023-05-24 20:03:09 +04:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
FileNotFoundError: If the file cannot be found
|
|
|
|
CredentialsError: If the session cookie
|
|
|
|
(or the file at all) has incorrect format
|
2022-06-23 15:13:56 +04:00
|
|
|
"""
|
|
|
|
|
2023-05-29 11:44:19 +04:00
|
|
|
file = os.path.expanduser(file)
|
|
|
|
log.debug('Restoring session from %s', file)
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
|
2023-05-29 11:44:19 +04:00
|
|
|
if not os.path.exists(file):
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
raise FileNotFoundError()
|
|
|
|
|
2023-05-29 11:44:19 +04:00
|
|
|
with open(file, 'rt', encoding='utf-8') as f:
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
saved = f.read() \
|
|
|
|
.strip() \
|
|
|
|
.replace('\r\n', '\n') \
|
|
|
|
.split('\n')
|
2022-07-01 19:02:10 +04:00
|
|
|
|
|
|
|
session = saved[0].strip()
|
2023-05-24 20:03:09 +04:00
|
|
|
if session == '' or not session.isalnum():
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
raise CredentialsError(
|
2023-05-24 20:03:09 +04:00
|
|
|
'Session cookie is invalid or the file is empty'
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
)
|
2022-07-01 19:02:10 +04:00
|
|
|
|
|
|
|
if len(saved) > 1:
|
2023-05-24 20:03:09 +04:00
|
|
|
self.account.refresh_servers(saved[1:])
|
2022-07-01 19:02:10 +04:00
|
|
|
|
2023-05-24 20:03:09 +04:00
|
|
|
self.atconn.session.cookies['ATERNOS_SESSION'] = session
|
2023-05-29 11:44:19 +04:00
|
|
|
self.saved_session = file
|
2022-09-29 20:04:47 +04:00
|
|
|
|
2022-07-01 19:02:10 +04:00
|
|
|
def save_session(
|
|
|
|
self,
|
|
|
|
file: str = '~/.aternos',
|
|
|
|
incl_servers: bool = True) -> None:
|
2022-06-23 15:13:56 +04:00
|
|
|
"""Saves an ATERNOS_SESSION cookie to a file
|
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
Args:
|
|
|
|
file (str, optional): File where a session cookie must be saved
|
|
|
|
incl_servers (bool, optional): If the function
|
2023-05-24 20:03:09 +04:00
|
|
|
should include the servers IDs in this file
|
|
|
|
to reduce API requests count on the next restoration
|
|
|
|
(recommended)
|
2022-06-23 15:13:56 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
file = os.path.expanduser(file)
|
2023-05-24 18:15:18 +04:00
|
|
|
log.debug('Saving session to %s', file)
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
|
2022-08-22 09:55:08 +04:00
|
|
|
with open(file, 'wt', encoding='utf-8') as f:
|
2022-06-23 15:13:56 +04:00
|
|
|
|
2022-07-01 19:02:10 +04:00
|
|
|
f.write(self.atconn.atsession + '\n')
|
|
|
|
if not incl_servers:
|
|
|
|
return
|
|
|
|
|
2023-05-24 20:03:09 +04:00
|
|
|
for s in self.account.servers:
|
2022-07-01 19:02:10 +04:00
|
|
|
f.write(s.servid + '\n')
|
|
|
|
|
2022-09-29 20:04:47 +04:00
|
|
|
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)
|
2023-05-24 18:15:18 +04:00
|
|
|
log.debug('Removing session file: %s', file)
|
2022-09-29 20:04:47 +04:00
|
|
|
|
|
|
|
try:
|
|
|
|
os.remove(file)
|
|
|
|
except OSError as err:
|
2023-05-24 18:15:18 +04:00
|
|
|
log.warning('Unable to delete session file: %s', err)
|
2022-09-29 20:04:47 +04:00
|
|
|
|
2023-05-24 20:03:09 +04:00
|
|
|
@staticmethod
|
|
|
|
def session_filename(username: str, sessions_dir: str = '~') -> str:
|
|
|
|
"""Generates a session file name
|
2022-07-01 19:02:10 +04:00
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
Args:
|
2023-05-24 20:03:09 +04:00
|
|
|
username (str): Authenticated user
|
|
|
|
sessions_dir (str, optional): Path to directory
|
|
|
|
with automatically saved sessions
|
2022-06-16 15:40:10 +04:00
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
Returns:
|
2023-05-24 20:03:09 +04:00
|
|
|
Filename
|
2022-09-29 19:17:38 +04:00
|
|
|
"""
|
|
|
|
|
2023-05-24 20:03:09 +04:00
|
|
|
# unsafe symbols replacement
|
|
|
|
repl = '_'
|
2022-09-23 17:21:17 +04:00
|
|
|
|
2023-05-24 20:03:09 +04:00
|
|
|
secure = re.sub(
|
|
|
|
r'[^A-Za-z0-9_-]',
|
|
|
|
repl, username,
|
2022-09-23 17:21:17 +04:00
|
|
|
)
|
|
|
|
|
2023-05-24 20:03:09 +04:00
|
|
|
return f'{sessions_dir}/.at_{secure}'
|
2023-05-29 11:44:19 +04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def debug(self) -> bool:
|
|
|
|
return is_debug()
|
|
|
|
|
|
|
|
@debug.setter
|
|
|
|
def debug(self, state: bool) -> None:
|
|
|
|
return set_debug(state)
|