2021-10-14 17:56:01 +04:00
|
|
|
import re
|
|
|
|
import json
|
2021-10-08 19:35:20 +04:00
|
|
|
import lxml.html
|
2021-10-14 17:56:01 +04:00
|
|
|
from requests import Response
|
|
|
|
from typing import Optional, Dict
|
2021-10-08 19:35:20 +04:00
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
from . import atconnect
|
2021-10-08 19:35:20 +04:00
|
|
|
from . import aterrors
|
2021-10-14 17:56:01 +04:00
|
|
|
from . import atfm
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
class AternosServer:
|
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
def __init__(self, servid:str, atconn:atconnect.AternosConnect) -> None:
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
self.servid = servid
|
|
|
|
self.atconn = atconn
|
|
|
|
|
|
|
|
servreq = self.atserver_request(
|
|
|
|
'https://aternos.org/server',
|
2021-10-14 17:56:01 +04:00
|
|
|
atconnect.REQGET
|
2021-10-08 19:35:20 +04:00
|
|
|
)
|
|
|
|
servtree = lxml.html.fromstring(servreq.content)
|
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
self._info = json.loads(
|
|
|
|
re.search(
|
|
|
|
r'var\s*lastStatus\s*=\s*({.*})',
|
|
|
|
servtree.head.text
|
|
|
|
)[1]
|
2021-10-08 19:35:20 +04:00
|
|
|
)
|
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
self.atconn.parse_token(servreq.content)
|
2021-10-08 19:35:20 +04:00
|
|
|
self.atconn.generate_sec()
|
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
def start(self, accepteula:bool=True) -> None:
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
startreq = self.atserver_request(
|
|
|
|
'https://aternos.org/panel/ajax/start.php',
|
2021-10-14 17:56:01 +04:00
|
|
|
atconnect.REQGET, sendtoken=True
|
2021-10-08 19:35:20 +04:00
|
|
|
)
|
|
|
|
startresult = startreq.json()
|
|
|
|
|
|
|
|
if startresult['success']:
|
|
|
|
return
|
|
|
|
|
|
|
|
error = startresult['error']
|
|
|
|
if error == 'eula' and accepteula:
|
|
|
|
self.eula()
|
2021-10-14 17:56:01 +04:00
|
|
|
self.start(accepteula=False)
|
2021-10-08 19:35:20 +04:00
|
|
|
elif error == 'eula':
|
|
|
|
raise aterrors.AternosServerStartError(
|
|
|
|
'EULA was not accepted. Use start(accepteula=True)'
|
|
|
|
)
|
|
|
|
elif error == 'already':
|
|
|
|
raise aterrors.AternosServerStartError(
|
|
|
|
'Server is already running'
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
raise aterrors.AternosServerStartError(
|
|
|
|
f'Unable to start server. Code: {error}'
|
|
|
|
)
|
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
def confirm(self) -> None:
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
self.atserver_request(
|
|
|
|
'https://aternos.org/panel/ajax/confirm.php',
|
2021-10-14 17:56:01 +04:00
|
|
|
atconnect.REQGET, sendtoken=True
|
2021-10-08 19:35:20 +04:00
|
|
|
)
|
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
def stop(self) -> None:
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
self.atserver_request(
|
|
|
|
'https://aternos.org/panel/ajax/stop.php',
|
2021-10-14 17:56:01 +04:00
|
|
|
atconnect.REQGET, sendtoken=True
|
2021-10-08 19:35:20 +04:00
|
|
|
)
|
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
def cancel(self) -> None:
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
self.atserver_request(
|
|
|
|
'https://aternos.org/panel/ajax/cancel.php',
|
2021-10-14 17:56:01 +04:00
|
|
|
atconnect.REQGET, sendtoken=True
|
2021-10-08 19:35:20 +04:00
|
|
|
)
|
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
def restart(self) -> None:
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
self.atserver_request(
|
|
|
|
'https://aternos.org/panel/ajax/restart.php',
|
2021-10-14 17:56:01 +04:00
|
|
|
atconnect.REQGET, sendtoken=True
|
2021-10-08 19:35:20 +04:00
|
|
|
)
|
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
def eula(self) -> None:
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
self.atserver_request(
|
|
|
|
'https://aternos.org/panel/ajax/eula.php',
|
2021-10-14 17:56:01 +04:00
|
|
|
atconnect.REQGET, sendtoken=True
|
2021-10-08 19:35:20 +04:00
|
|
|
)
|
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
def files(self) -> atfm.AternosFileManager:
|
2021-10-08 19:35:20 +04:00
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
return atfm.AternosFileManager(self)
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
def atserver_request(
|
2021-10-14 17:56:01 +04:00
|
|
|
self, url:str, method:int,
|
|
|
|
params:Optional[dict]=None,
|
|
|
|
data:Optional[dict]=None,
|
|
|
|
headers:Optional[dict]=None,
|
|
|
|
sendtoken:bool=False) -> Response:
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
return self.atconn.request_cloudflare(
|
|
|
|
url=url, method=method,
|
|
|
|
params=params, data=data,
|
|
|
|
headers=headers,
|
|
|
|
reqcookies={
|
|
|
|
'ATERNOS_SERVER': self.servid
|
|
|
|
},
|
|
|
|
sendtoken=sendtoken
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
2021-10-14 17:56:01 +04:00
|
|
|
def info(self) -> dict:
|
|
|
|
return self._info
|
|
|
|
|
|
|
|
@property
|
|
|
|
def address(self) -> str:
|
|
|
|
return f'{self.domain}:{self.port}'
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
@property
|
2021-10-14 17:56:01 +04:00
|
|
|
def domain(self) -> str:
|
|
|
|
return self._info['displayAddress']
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
@property
|
2021-10-14 17:56:01 +04:00
|
|
|
def port(self) -> int:
|
|
|
|
return self._info['port']
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
@property
|
2021-10-14 17:56:01 +04:00
|
|
|
def software(self) -> str:
|
|
|
|
return self._info['software']
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
@property
|
2021-10-14 17:56:01 +04:00
|
|
|
def version(self) -> str:
|
|
|
|
return self._info['version']
|