2021-10-08 18:35:20 +03:00
|
|
|
import hashlib
|
|
|
|
import lxml.html
|
2022-01-06 18:57:26 +03:00
|
|
|
from typing import Optional, Union, List
|
2021-10-08 18:35:20 +03:00
|
|
|
|
|
|
|
from . import atserver
|
|
|
|
from . import atconnect
|
|
|
|
from . import aterrors
|
2022-01-11 17:08:59 +03:00
|
|
|
|
|
|
|
from .aterrors import AternosCredentialsError
|
2021-10-08 18:35:20 +03:00
|
|
|
|
|
|
|
class Client:
|
|
|
|
|
2022-01-06 18:57:26 +03:00
|
|
|
def __init__(self, atconn:atconnect.AternosConnect) -> None:
|
2021-10-08 18:35:20 +03:00
|
|
|
|
2022-01-06 18:57:26 +03:00
|
|
|
self.atconn = atconn
|
2021-10-08 18:35:20 +03:00
|
|
|
|
2022-01-06 18:57:26 +03:00
|
|
|
@classmethod
|
|
|
|
def from_hashed(cls, username:str, md5:str):
|
2021-10-08 18:35:20 +03:00
|
|
|
|
2022-01-06 18:57:26 +03:00
|
|
|
atconn = atconnect.AternosConnect()
|
2022-01-11 17:08:59 +03:00
|
|
|
atconn.parse_token()
|
|
|
|
atconn.generate_sec()
|
|
|
|
|
|
|
|
credentials = {
|
|
|
|
'user': username,
|
|
|
|
'password': md5
|
|
|
|
}
|
2021-10-08 18:35:20 +03:00
|
|
|
|
2022-01-11 17:08:59 +03:00
|
|
|
loginreq = atconn.request_cloudflare(
|
2022-01-06 18:57:26 +03:00
|
|
|
f'https://aternos.org/panel/ajax/account/login.php',
|
2022-01-11 17:08:59 +03:00
|
|
|
atconnect.REQPOST, data=credentials,
|
2022-01-06 18:57:26 +03:00
|
|
|
sendtoken=True
|
2021-10-08 18:35:20 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
if loginreq.cookies.get('ATERNOS_SESSION', None) == None:
|
2022-01-11 17:08:59 +03:00
|
|
|
raise AternosCredentialsError(
|
2021-10-08 18:35:20 +03:00
|
|
|
'Check your username and password'
|
|
|
|
)
|
|
|
|
|
2022-01-11 17:08:59 +03:00
|
|
|
return cls(atconn)
|
2022-01-06 18:57:26 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_credentials(cls, username:str, password:str):
|
2022-01-11 17:08:59 +03:00
|
|
|
|
|
|
|
pswd_bytes = password.encode('utf-8')
|
|
|
|
md5 = hashlib.md5(pswd_bytes).hexdigest().lower()
|
|
|
|
|
|
|
|
return cls.from_hashed(username, md5)
|
2022-01-06 18:57:26 +03:00
|
|
|
|
|
|
|
@classmethod
|
2022-01-11 17:08:59 +03:00
|
|
|
def from_session(cls, session:str):
|
|
|
|
|
|
|
|
atconn = atconnect.AternosConnect()
|
|
|
|
atconn.session.cookies.set('ATERNOS_SESSION', session)
|
|
|
|
atconn.parse_token()
|
|
|
|
atconn.generate_sec()
|
|
|
|
|
|
|
|
return cls(atconn)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def google() -> str:
|
|
|
|
|
|
|
|
atconn = atconnect.AternosConnect()
|
|
|
|
auth = atconn.request_cloudflare(
|
|
|
|
'https://aternos.org/auth/google-login',
|
|
|
|
atconnect.REQGET, redirect=False
|
|
|
|
)
|
|
|
|
return auth.headers['Location']
|
2022-01-06 18:57:26 +03:00
|
|
|
|
2021-10-14 16:56:01 +03:00
|
|
|
@property
|
2021-10-14 17:41:57 +03:00
|
|
|
def servers(self) -> List[atserver.AternosServer]:
|
2021-10-08 18:35:20 +03:00
|
|
|
serverspage = self.atconn.request_cloudflare(
|
|
|
|
'https://aternos.org/servers/',
|
2021-10-14 16:56:01 +03:00
|
|
|
atconnect.REQGET
|
2021-10-08 18:35:20 +03:00
|
|
|
)
|
|
|
|
serverstree = lxml.html.fromstring(serverspage.content)
|
2021-11-01 17:04:19 +03:00
|
|
|
serverslist = serverstree.xpath('//div[contains(@class,"servers ")]/div')
|
2021-10-08 18:35:20 +03:00
|
|
|
|
|
|
|
servers = []
|
|
|
|
for server in serverslist:
|
|
|
|
servid = server.xpath('./div[@class="server-body"]/@data-id')[0]
|
|
|
|
servers.append(atserver.AternosServer(servid, self.atconn))
|
|
|
|
|
|
|
|
return servers
|