2022-01-11 18:08:59 +04:00
|
|
|
import enum
|
2021-10-14 17:56:01 +04:00
|
|
|
import json
|
|
|
|
from requests import Response
|
2022-03-18 18:38:36 +04:00
|
|
|
from typing import Optional
|
2021-10-08 19:35:20 +04:00
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
from .atconnect import AternosConnect
|
|
|
|
from .aterrors import ServerError
|
|
|
|
from .atfm import FileManager
|
|
|
|
from .atconf import AternosConfig
|
|
|
|
from .atplayers import PlayersList
|
2022-01-22 15:10:30 +04:00
|
|
|
from .atwss import AternosWss
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
class Edition(enum.IntEnum):
|
|
|
|
java = 0
|
|
|
|
bedrock = 1
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-01-11 18:08:59 +04:00
|
|
|
class Status(enum.IntEnum):
|
|
|
|
off = 0
|
|
|
|
on = 1
|
2022-04-06 11:44:14 +04:00
|
|
|
starting = 2
|
2022-01-11 18:08:59 +04:00
|
|
|
shutdown = 3
|
2022-01-22 15:10:30 +04:00
|
|
|
unknown = 6
|
2022-01-11 18:08:59 +04:00
|
|
|
error = 7
|
2022-01-22 15:10:30 +04:00
|
|
|
confirm = 10
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
class AternosServer:
|
|
|
|
|
2022-01-11 18:08:59 +04:00
|
|
|
def __init__(
|
|
|
|
self, servid:str,
|
2022-03-30 19:40:58 +04:00
|
|
|
atconn:AternosConnect,
|
|
|
|
reqinfo:bool=True) -> None:
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
self.servid = servid
|
|
|
|
self.atconn = atconn
|
2022-03-30 19:40:58 +04:00
|
|
|
if reqinfo:
|
|
|
|
self.fetch()
|
2022-03-25 16:45:38 +04:00
|
|
|
|
|
|
|
def fetch(self) -> None:
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
servreq = self.atserver_request(
|
2022-03-25 16:45:38 +04:00
|
|
|
'https://aternos.org/panel/ajax/status.php',
|
|
|
|
'GET', sendtoken=True
|
2021-10-08 19:35:20 +04:00
|
|
|
)
|
2022-03-25 16:45:38 +04:00
|
|
|
self._info = json.loads(servreq.content)
|
2021-10-08 19:35:20 +04:00
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
def wss(self, autoconfirm:bool=False) -> AternosWss:
|
2021-10-08 19:35:20 +04:00
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
return AternosWss(self, autoconfirm)
|
2022-01-11 18:08:59 +04:00
|
|
|
|
|
|
|
def start(self, headstart:bool=False, accepteula:bool=True) -> None:
|
2021-10-08 19:35:20 +04:00
|
|
|
|
|
|
|
startreq = self.atserver_request(
|
|
|
|
'https://aternos.org/panel/ajax/start.php',
|
2022-03-18 18:38:36 +04:00
|
|
|
'GET', params={'headstart': int(headstart)},
|
2022-01-11 18:08:59 +04:00
|
|
|
sendtoken=True
|
2021-10-08 19:35:20 +04:00
|
|
|
)
|
|
|
|
startresult = startreq.json()
|
|
|
|
|
|
|
|
if startresult['success']:
|
|
|
|
return
|
|
|
|
error = startresult['error']
|
2022-01-11 18:08:59 +04:00
|
|
|
|
2021-10-08 19:35:20 +04:00
|
|
|
if error == 'eula' and accepteula:
|
|
|
|
self.eula()
|
2021-10-14 17:56:01 +04:00
|
|
|
self.start(accepteula=False)
|
2022-01-11 18:08:59 +04:00
|
|
|
|
2021-10-08 19:35:20 +04:00
|
|
|
elif error == 'eula':
|
2022-03-18 18:38:36 +04:00
|
|
|
raise ServerError(
|
2021-10-08 19:35:20 +04:00
|
|
|
'EULA was not accepted. Use start(accepteula=True)'
|
|
|
|
)
|
2022-01-11 18:08:59 +04:00
|
|
|
|
2021-10-08 19:35:20 +04:00
|
|
|
elif error == 'already':
|
2022-03-18 18:38:36 +04:00
|
|
|
raise ServerError(
|
2021-10-08 19:35:20 +04:00
|
|
|
'Server is already running'
|
|
|
|
)
|
2022-01-11 18:08:59 +04:00
|
|
|
|
|
|
|
elif error == 'wrongversion':
|
2022-03-18 18:38:36 +04:00
|
|
|
raise ServerError(
|
2022-01-11 18:08:59 +04:00
|
|
|
'Incorrect software version installed'
|
|
|
|
)
|
|
|
|
|
|
|
|
elif error == 'file':
|
2022-03-18 18:38:36 +04:00
|
|
|
raise ServerError(
|
2022-03-25 16:45:38 +04:00
|
|
|
'File server is unavailbale, view https://status.aternos.gmbh'
|
2022-01-11 18:08:59 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
elif error == 'size':
|
2022-03-18 18:38:36 +04:00
|
|
|
raise ServerError(
|
2022-01-11 18:08:59 +04:00
|
|
|
f'Available storage size is 4GB, ' + \
|
|
|
|
f'your server used: {startresult["size"]}'
|
|
|
|
)
|
|
|
|
|
2021-10-08 19:35:20 +04:00
|
|
|
else:
|
2022-03-18 18:38:36 +04:00
|
|
|
raise ServerError(
|
2022-01-11 18:08:59 +04:00
|
|
|
f'Unable to start server, code: {error}'
|
2021-10-08 19:35:20 +04:00
|
|
|
)
|
|
|
|
|
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',
|
2022-03-18 18:38:36 +04:00
|
|
|
'GET', 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',
|
2022-03-18 18:38:36 +04:00
|
|
|
'GET', 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',
|
2022-03-18 18:38:36 +04:00
|
|
|
'GET', 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',
|
2022-03-18 18:38:36 +04:00
|
|
|
'GET', 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',
|
2022-03-18 18:38:36 +04:00
|
|
|
'GET', sendtoken=True
|
2021-10-08 19:35:20 +04:00
|
|
|
)
|
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
def files(self) -> FileManager:
|
2021-10-08 19:35:20 +04:00
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
return FileManager(self)
|
2021-10-08 19:35:20 +04:00
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
def config(self) -> AternosConfig:
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
return AternosConfig(self)
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
def players(self, lst:str) -> PlayersList:
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
return PlayersList(lst, self)
|
2021-10-15 15:12:45 +04:00
|
|
|
|
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-15 15:12:45 +04:00
|
|
|
def subdomain(self) -> str:
|
|
|
|
atdomain = self.domain
|
|
|
|
return atdomain[:atdomain.find('.')]
|
|
|
|
|
|
|
|
@subdomain.setter
|
|
|
|
def subdomain(self, value:str) -> None:
|
|
|
|
self.atserver_request(
|
|
|
|
'https://aternos.org/panel/ajax/options/subdomain.php',
|
2022-03-18 18:38:36 +04:00
|
|
|
'GET', params={'subdomain': value},
|
2022-01-11 18:08:59 +04:00
|
|
|
sendtoken=True
|
2021-10-15 15:12:45 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def motd(self) -> str:
|
|
|
|
return self._info['motd']
|
|
|
|
|
|
|
|
@motd.setter
|
|
|
|
def motd(self, value:str) -> None:
|
|
|
|
self.atserver_request(
|
|
|
|
'https://aternos.org/panel/ajax/options/motd.php',
|
2022-03-18 18:38:36 +04:00
|
|
|
'POST', data={'motd': value},
|
2021-11-01 18:04:19 +04:00
|
|
|
sendtoken=True
|
2021-10-15 15:12:45 +04:00
|
|
|
)
|
2021-10-14 17:56:01 +04:00
|
|
|
|
|
|
|
@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:
|
2021-10-15 15:12:45 +04:00
|
|
|
return self._info['ip']
|
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
|
|
|
|
2021-10-15 15:12:45 +04:00
|
|
|
@property
|
|
|
|
def edition(self) -> int:
|
|
|
|
soft_type = self._info['bedrock']
|
2022-01-11 18:08:59 +04:00
|
|
|
return int(soft_type)
|
2021-10-15 15:12:45 +04:00
|
|
|
|
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']
|
2022-03-25 16:45:38 +04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def status(self) -> str:
|
|
|
|
return self._info['class']
|
2021-10-15 15:12:45 +04:00
|
|
|
|
|
|
|
@property
|
2022-03-25 16:45:38 +04:00
|
|
|
def status_num(self) -> int:
|
2021-10-15 15:12:45 +04:00
|
|
|
return int(self._info['status'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ram(self) -> int:
|
2022-03-25 16:45:38 +04:00
|
|
|
return int(self._info['ram'])
|