2022-04-01 17:00:02 +04:00
|
|
|
import os
|
|
|
|
import re
|
2021-10-08 19:35:20 +04:00
|
|
|
import hashlib
|
|
|
|
import lxml.html
|
2022-03-17 10:24:58 +04:00
|
|
|
from typing import List
|
2022-01-11 18:08:59 +04:00
|
|
|
|
2022-03-17 10:24:58 +04:00
|
|
|
from .atserver import AternosServer
|
|
|
|
from .atconnect import AternosConnect
|
2022-03-18 18:38:36 +04:00
|
|
|
from .aterrors import CredentialsError
|
|
|
|
|
|
|
|
__all__ = ['Client', 'atconf', 'atconnect', 'aterrors', 'atfile', 'atfm', 'atjsparse', 'atplayers', 'atserver', 'atwss']
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
class Client:
|
|
|
|
|
2022-01-06 19:57:26 +04:00
|
|
|
def __init__(self, atconn:atconnect.AternosConnect) -> None:
|
2021-10-08 19:35:20 +04:00
|
|
|
|
2022-01-06 19:57:26 +04:00
|
|
|
self.atconn = atconn
|
2021-10-08 19:35:20 +04:00
|
|
|
|
2022-01-06 19:57:26 +04:00
|
|
|
@classmethod
|
|
|
|
def from_hashed(cls, username:str, md5:str):
|
2021-10-08 19:35:20 +04:00
|
|
|
|
2022-03-17 10:24:58 +04:00
|
|
|
atconn = AternosConnect()
|
2022-01-11 18:08:59 +04:00
|
|
|
atconn.parse_token()
|
|
|
|
atconn.generate_sec()
|
|
|
|
|
|
|
|
credentials = {
|
|
|
|
'user': username,
|
|
|
|
'password': md5
|
|
|
|
}
|
2021-10-08 19:35:20 +04:00
|
|
|
|
2022-01-11 18:08:59 +04:00
|
|
|
loginreq = atconn.request_cloudflare(
|
2022-01-06 19:57:26 +04:00
|
|
|
f'https://aternos.org/panel/ajax/account/login.php',
|
2022-04-01 17:00:02 +04:00
|
|
|
'POST', data=credentials, sendtoken=True
|
2021-10-08 19:35:20 +04:00
|
|
|
)
|
|
|
|
|
2022-04-01 17:00:02 +04:00
|
|
|
if 'ATERNOS_SESSION' not in loginreq.cookies:
|
2022-03-18 18:38:36 +04:00
|
|
|
raise CredentialsError(
|
2021-10-08 19:35:20 +04:00
|
|
|
'Check your username and password'
|
|
|
|
)
|
|
|
|
|
2022-01-11 18:08:59 +04:00
|
|
|
return cls(atconn)
|
2022-01-06 19:57:26 +04:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_credentials(cls, username:str, password:str):
|
2022-01-11 18:08:59 +04:00
|
|
|
|
2022-04-01 17:00:02 +04:00
|
|
|
md5 = Client.md5encode(password)
|
2022-01-11 18:08:59 +04:00
|
|
|
return cls.from_hashed(username, md5)
|
2022-01-06 19:57:26 +04:00
|
|
|
|
|
|
|
@classmethod
|
2022-01-11 18:08:59 +04:00
|
|
|
def from_session(cls, session:str):
|
|
|
|
|
2022-03-17 10:24:58 +04:00
|
|
|
atconn = AternosConnect()
|
2022-03-25 16:45:38 +04:00
|
|
|
atconn.session.cookies['ATERNOS_SESSION'] = session
|
2022-01-11 18:08:59 +04:00
|
|
|
atconn.parse_token()
|
|
|
|
atconn.generate_sec()
|
|
|
|
|
|
|
|
return cls(atconn)
|
2022-04-01 17:00:02 +04:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def restore_session(cls, file:str='~/.aternos'):
|
2022-01-11 18:08:59 +04:00
|
|
|
|
2022-04-01 17:00:02 +04:00
|
|
|
file = os.path.expanduser(file)
|
|
|
|
with open(file, 'rt') as f:
|
|
|
|
session = f.read().strip()
|
|
|
|
return cls.from_session(session)
|
|
|
|
|
2022-01-11 18:08:59 +04:00
|
|
|
@staticmethod
|
2022-04-01 17:00:02 +04:00
|
|
|
def md5encode(passwd:str) -> str:
|
2022-01-11 18:08:59 +04:00
|
|
|
|
2022-04-01 17:00:02 +04:00
|
|
|
encoded = hashlib.md5(passwd.encode('utf-8'))
|
|
|
|
return encoded.hexdigest().lower()
|
|
|
|
|
|
|
|
def save_session(self, file:str='~/.aternos') -> None:
|
|
|
|
|
|
|
|
file = os.path.expanduser(file)
|
|
|
|
with open(file, 'wt') as f:
|
|
|
|
f.write(self.atconn.atsession)
|
|
|
|
|
|
|
|
def list_servers(self) -> List[AternosServer]:
|
2022-01-06 19:57:26 +04:00
|
|
|
|
2021-10-08 19:35:20 +04:00
|
|
|
serverspage = self.atconn.request_cloudflare(
|
2022-03-25 16:45:38 +04:00
|
|
|
'https://aternos.org/servers/', 'GET'
|
2021-10-08 19:35:20 +04:00
|
|
|
)
|
|
|
|
serverstree = lxml.html.fromstring(serverspage.content)
|
2021-11-01 18:04:19 +04:00
|
|
|
serverslist = serverstree.xpath('//div[contains(@class,"servers ")]/div')
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
servers = []
|
|
|
|
for server in serverslist:
|
|
|
|
servid = server.xpath('./div[@class="server-body"]/@data-id')[0]
|
2022-03-17 10:24:58 +04:00
|
|
|
servers.append(AternosServer(servid, self.atconn))
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
return servers
|
2022-04-01 17:00:02 +04:00
|
|
|
|
|
|
|
def get_server(self, servid:str) -> AternosServer:
|
|
|
|
|
|
|
|
return AternosServer(servid, self.atconn)
|
|
|
|
|
|
|
|
def change_username(self, value:str) -> None:
|
|
|
|
|
|
|
|
self.atconn.request_cloudflare(
|
|
|
|
'https://aternos.org/panel/ajax/account/username.php',
|
|
|
|
'POST', data={'username': value}
|
|
|
|
)
|
|
|
|
|
|
|
|
def change_email(self, value:str) -> None:
|
|
|
|
|
|
|
|
email = re.compile(r'^[A-Za-z0-9\-_+.]+@[A-Za-z0-9\-_+.]+\.[A-Za-z0-9\-]+$|^$')
|
|
|
|
if not email.match(value):
|
|
|
|
raise ValueError('Invalid e-mail!')
|
|
|
|
|
|
|
|
self.atconn.request_cloudflare(
|
|
|
|
'https://aternos.org/panel/ajax/account/email.php',
|
|
|
|
'POST', data={'email': value}
|
|
|
|
)
|
|
|
|
|
|
|
|
def change_password(self, old:str, new:str) -> None:
|
|
|
|
|
|
|
|
old = Client.md5encode(old)
|
|
|
|
new = Client.md5encode(new)
|
|
|
|
self.atconn.request_cloudflare(
|
|
|
|
'https://aternos.org/panel/ajax/account/password.php',
|
|
|
|
'POST', data={
|
|
|
|
'oldpassword': old,
|
|
|
|
'newpassword': new
|
|
|
|
}
|
|
|
|
)
|