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-15 15:12:45 +04:00
|
|
|
from . import atconf
|
|
|
|
from . import atplayers
|
|
|
|
|
|
|
|
SOFTWARE_JAVA = 0
|
|
|
|
SOFTWARE_BEDROCK = 1
|
|
|
|
|
|
|
|
PLAYERS_ALLOWED = 'whitelist'
|
|
|
|
PLAYERS_OPS = 'ops'
|
|
|
|
PLAYERS_BANNED = 'banned-players'
|
|
|
|
PLAYERS_IPS = 'banned-ips'
|
|
|
|
|
|
|
|
STATUS_OFFLINE = 0
|
|
|
|
STATUS_ONLINE = 1
|
|
|
|
STATUS_LOADING = 2
|
|
|
|
STATUS_SHUTDOWN = 3
|
|
|
|
STATUS_ERROR = 7
|
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*({.*})',
|
2021-10-15 19:31:47 +04:00
|
|
|
servtree.head.text_content()
|
2021-10-14 17:56:01 +04:00
|
|
|
)[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
|
|
|
|
2021-10-15 15:12:45 +04:00
|
|
|
def config(self) -> atconf.AternosConfig:
|
|
|
|
|
|
|
|
return atconf.AternosConfig(self)
|
|
|
|
|
|
|
|
def players(self, lst:str) -> atplayers.AternosPlayersList:
|
|
|
|
|
|
|
|
return atplayers.AternosPlayersList(lst, 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-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',
|
|
|
|
atconnect.REQGET, params={'subdomain': value}
|
|
|
|
)
|
|
|
|
|
|
|
|
@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',
|
|
|
|
atconnect.REQPOST, data={'motd': value}
|
|
|
|
)
|
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']
|
|
|
|
if soft_type == True:
|
|
|
|
return SOFTWARE_BEDROCK
|
|
|
|
else:
|
|
|
|
return SOFTWARE_JAVA
|
|
|
|
|
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']
|
2021-10-15 15:12:45 +04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def status(self) -> int:
|
|
|
|
return int(self._info['status'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ram(self) -> int:
|
|
|
|
return int(self._info['ram'])
|