Add files via upload

This commit is contained in:
Andrey 2021-10-08 19:35:20 +04:00 committed by GitHub
parent c4fa20fd14
commit b010b8a4c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 420 additions and 305 deletions

View file

@ -1,6 +1,6 @@
[build-system] [build-system]
requires = [ requires = [
"setuptools>=42", "setuptools>=42",
"wheel" "wheel"
] ]
build-backend = "setuptools.build_meta" build-backend = "setuptools.build_meta"

View file

@ -0,0 +1,58 @@
import hashlib
import lxml.html
from . import atserver
from . import atconnect
from . import aterrors
class Client:
def __init__(self, username, md5=None, password=None):
if (password == None) and (md5 == None):
raise AttributeError('Password was not specified')
if (password != None):
self.__init__(
username,
md5=hashlib.md5(password.encode('utf-8'))\
.hexdigest().lower()
)
return
self.atconn = atconnect.AternosConnect()
self.token = self.atconn.get_token()
self.sec = self.atconn.generate_sec()
self.credentials = {
'user': username,
'password': md5
}
loginreq = self.atconn.request_cloudflare(
f'https://aternos.org/panel/ajax/account/login.php?' + \
f'SEC={self.sec}&TOKEN={self.token}',
self.atconn.REQPOST, data=self.credentials
)
if loginreq.cookies.get('ATERNOS_SESSION', None) == None:
raise aterrors.AternosCredentialsError(
'Check your username and password'
)
def get_servers(self):
serverspage = self.atconn.request_cloudflare(
'https://aternos.org/servers/',
self.atconn.REQGET
)
serverstree = lxml.html.fromstring(serverspage.content)
serverslist = serverstree.xpath('//div[@class="servers"]/div')
servers = []
for server in serverslist:
servid = server.xpath('./div[@class="server-body"]/@data-id')[0]
servers.append(atserver.AternosServer(servid, self.atconn))
return servers

View file

@ -1,146 +1,147 @@
import re import re
import time import time
import random import random
import lxml.html import lxml.html
from cloudscraper import CloudScraper from cloudscraper import CloudScraper
from aterrors import AternosCredentialsError
from . import aterrors
class AternosConnect:
class AternosConnect:
REQGET = 0
REQPOST = 1 REQGET = 0
REQUA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Goanna/4.8 Firefox/68.0 PaleMoon/29.4.0.2' REQPOST = 1
REQUA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Goanna/4.8 Firefox/68.0 PaleMoon/29.4.0.2'
def __init__(self):
def __init__(self):
pass
pass
def get_token(self, response=None):
def get_token(self, response=None):
if response == None:
loginpage = self.request_cloudflare( if response == None:
f'https://aternos.org/go/', loginpage = self.request_cloudflare(
self.REQGET f'https://aternos.org/go/',
).content self.REQGET
pagetree = lxml.html.fromstring(loginpage) ).content
else: pagetree = lxml.html.fromstring(loginpage)
pagetree = lxml.html.fromstring(response) else:
pagetree = lxml.html.fromstring(response)
try:
pagehead = pagetree.head try:
self.token = re.search( pagehead = pagetree.head
r'const\s+AJAX_TOKEN\s*=\s*["\'](\w+)["\']', self.token = re.search(
pagehead.text_content() r'const\s+AJAX_TOKEN\s*=\s*["\'](\w+)["\']',
)[1] pagehead.text_content()
except (IndexError, TypeError): )[1]
raise AternosCredentialsError( except (IndexError, TypeError):
'Unable to parse TOKEN from the page' raise aterrors.AternosCredentialsError(
) 'Unable to parse TOKEN from the page'
)
return self.token
return self.token
def generate_sec(self):
def generate_sec(self):
randkey = self.generate_aternos_rand()
randval = self.generate_aternos_rand() randkey = self.generate_aternos_rand()
self.sec = f'{randkey}:{randval}' randval = self.generate_aternos_rand()
self.session.cookies.set( self.sec = f'{randkey}:{randval}'
f'ATERNOS_SEC_{randkey}', randval, self.session.cookies.set(
domain='aternos.org' f'ATERNOS_SEC_{randkey}', randval,
) domain='aternos.org'
)
return self.sec
return self.sec
def generate_aternos_rand(self, randlen=16):
def generate_aternos_rand(self, randlen=16):
rand_arr = []
for i in range(randlen+1): rand_arr = []
rand_arr.append('') for i in range(randlen+1):
rand_arr.append('')
rand_alphanum = \
self.convert_num(random.random(),36) + \ rand_alphanum = \
'00000000000000000' self.convert_num(random.random(),36) + \
return (rand_alphanum[2:18].join(rand_arr)[:randlen]) '00000000000000000'
return (rand_alphanum[2:18].join(rand_arr)[:randlen])
def convert_num(self, num, base):
def convert_num(self, num, base):
result = ''
while num > 0: result = ''
result = str(num % base) + result while num > 0:
num //= base result = str(num % base) + result
return result num //= base
return result
def request_cloudflare(
self, url, method, retries=10, def request_cloudflare(
params=None, data=None, headers=None, self, url, method, retries=10,
reqcookies=None, sendtoken=False): params=None, data=None, headers=None,
reqcookies=None, sendtoken=False):
cftitle = '<title>Please Wait... | Cloudflare</title>'
cftitle = '<title>Please Wait... | Cloudflare</title>'
if sendtoken:
if params == None: if sendtoken:
params = {} if params == None:
params['SEC'] = self.sec params = {}
params['TOKEN'] = self.token params['SEC'] = self.sec
params['TOKEN'] = self.token
if headers == None:
headers = {} if headers == None:
headers['User-Agent'] = self.REQUA headers = {}
headers['User-Agent'] = self.REQUA
try:
cookies = self.session.cookies try:
except AttributeError: cookies = self.session.cookies
cookies = None except AttributeError:
cookies = None
self.session = CloudScraper()
if cookies != None: self.session = CloudScraper()
self.session.cookies = cookies if cookies != None:
self.session.cookies = cookies
if method == self.REQPOST:
req = self.session.post( if method == self.REQPOST:
url, req = self.session.post(
data=data, url,
headers=headers, data=data,
cookies=reqcookies headers=headers,
) cookies=reqcookies
else: )
req = self.session.get( else:
url, req = self.session.get(
params=params, url,
headers=headers, params=params,
cookies=reqcookies headers=headers,
) cookies=reqcookies
)
countdown = retries
while cftitle in req.text \ countdown = retries
and (countdown > 0): while cftitle in req.text \
and (countdown > 0):
self.session = CloudScraper()
if cookies != None: self.session = CloudScraper()
self.session.cookies = cookies if cookies != None:
if reqcookies != None: self.session.cookies = cookies
for cookiekey in reqcookies: if reqcookies != None:
self.session.cookies.set(cookiekey, reqcookies[cookiekey]) for cookiekey in reqcookies:
self.session.cookies.set(cookiekey, reqcookies[cookiekey])
time.sleep(1)
if method == self.REQPOST: time.sleep(1)
req = self.session.post( if method == self.REQPOST:
url, req = self.session.post(
data=data, url,
headers=headers, data=data,
cookies=reqcookies headers=headers,
) cookies=reqcookies
else: )
req = self.session.get( else:
url, req = self.session.get(
params=params, url,
headers=headers, params=params,
cookies=reqcookies headers=headers,
) cookies=reqcookies
countdown -= 1 )
countdown -= 1
return req
return req
def get_session(self):
def get_session(self):
return self.session
return self.session

View file

@ -1,11 +1,11 @@
class AternosError(Exception): class AternosError(Exception):
pass pass
class AternosCredentialsError(AternosError): class AternosCredentialsError(AternosError):
pass pass
class AternosServerStartError(AternosError): class AternosServerStartError(AternosError):
pass pass

28
python_aternos/atfiles.py Normal file
View file

@ -0,0 +1,28 @@
from . import atconnect
class AternosFileManager:
def __init__(atserv):
self.atserv = atserv
def listdir(self, path=''):
self.atserv.atserver_request(
f'https://aternos.org/files/{path}',
atconnect.AternosConnect.REQGET
)
def get_file(self, path):
self.atserv.atserver_request(
f'https://aternos.org/panel/ajax/files/download.php?file={path}',
atconnect.AternosConnect.REQGET
)
def get_world(self, world):
self.atserv.atserver_request(
f'https://aternos.org/panel/ajax/worlds/download.php?world={world}',
atconnect.AternosConnect.REQGET
)

View file

@ -1,110 +1,136 @@
import lxml.html import lxml.html
import aterrors
from . import aterrors
class AternosServer: from . import atfiles
def __init__(self, servid, atconn): class AternosServer:
self.servid = servid def __init__(self, servid, atconn):
self.atconn = atconn
self.servid = servid
servreq = self._atserver_request( self.atconn = atconn
'https://aternos.org/server',
self.atconn.REQGET servreq = self.atserver_request(
) 'https://aternos.org/server',
servtree = lxml.html.fromstring(servreq.content) self.atconn.REQGET
)
servinfo = servtree.xpath( servtree = lxml.html.fromstring(servreq.content)
'//div[@class="server-bottom-info server-info"]' + \
'/div[@class="server-info-container"]' + \ servinfo = servtree.xpath(
'/div[@class="server-info-box"]' + \ '//div[@class="server-bottom-info server-info"]' + \
'/div[@class="server-info-box-body"]' + \ '/div[@class="server-info-container"]' + \
'/div[@class="server-info-box-value"]/span' '/div[@class="server-info-box"]' + \
) '/div[@class="server-info-box-body"]' + \
self._address = servinfo[0].text '/div[@class="server-info-box-value"]/span'
self._software = servinfo[1].text )
self._version = servinfo[2].text
fullip = servinfo[0].text
self.atconn.get_token(servreq.content) self._address = fullip
self.atconn.generate_sec() self._domain = fullip[:fullip.rfind(':')]
self._port = fullip[fullip.rfind(':')+1:]
def start(self, accepteula=True):
self._software = servinfo[1].text
startreq = self._atserver_request( self._version = servinfo[2].text
'https://aternos.org/panel/ajax/start.php',
self.atconn.REQGET, sendtoken=True self.atconn.get_token(servreq.content)
) self.atconn.generate_sec()
startresult = startreq.json()
def start(self, accepteula=True):
if startresult['success']:
return startreq = self.atserver_request(
'https://aternos.org/panel/ajax/start.php',
error = startresult['error'] self.atconn.REQGET, sendtoken=True
if error == 'eula' and accepteula: )
self.eula() startresult = startreq.json()
elif error == 'eula':
raise aterrors.AternosServerStartError( if startresult['success']:
'EULA was not accepted. Use start(accepteula=True)' return
)
elif error == 'already': error = startresult['error']
raise aterrors.AternosServerStartError( if error == 'eula' and accepteula:
'Server is already running' self.eula()
) elif error == 'eula':
else: raise aterrors.AternosServerStartError(
raise aterrors.AternosServerStartError( 'EULA was not accepted. Use start(accepteula=True)'
f'Unable to start server. Code: {error}' )
) elif error == 'already':
raise aterrors.AternosServerStartError(
def stop(self): 'Server is already running'
)
self._atserver_request( else:
'https://aternos.org/panel/ajax/stop.php', raise aterrors.AternosServerStartError(
self.atconn.REQGET, sendtoken=True f'Unable to start server. Code: {error}'
) )
def cancel(self): def confirm(self):
self._atserver_request( self.atserver_request(
'https://aternos.org/panel/ajax/cancel.php', 'https://aternos.org/panel/ajax/confirm.php',
self.atconn.REQGET, sendtoken=True self.atconn.REQGET, sendtoken=True
) )
def restart(self): def stop(self):
self._atserver_request( self.atserver_request(
'https://aternos.org/panel/ajax/restart.php', 'https://aternos.org/panel/ajax/stop.php',
self.atconn.REQGET, sendtoken=True self.atconn.REQGET, sendtoken=True
) )
def eula(self): def cancel(self):
self._atserver_request( self.atserver_request(
'https://aternos.org/panel/ajax/eula.php', 'https://aternos.org/panel/ajax/cancel.php',
self.atconn.REQGET, sendtoken=True self.atconn.REQGET, sendtoken=True
) )
def _atserver_request( def restart(self):
self, url, method, params=None,
data=None, headers=None, sendtoken=False): self.atserver_request(
'https://aternos.org/panel/ajax/restart.php',
return self.atconn.request_cloudflare( self.atconn.REQGET, sendtoken=True
url=url, method=method, )
params=params, data=data,
headers=headers, def eula(self):
reqcookies={
'ATERNOS_SERVER': self.servid self.atserver_request(
}, 'https://aternos.org/panel/ajax/eula.php',
sendtoken=sendtoken self.atconn.REQGET, sendtoken=True
) )
@property def files(self):
def address(self):
return self._address return AternosFileManager(self)
@property def atserver_request(
def software(self): self, url, method, params=None,
return self._software data=None, headers=None, sendtoken=False):
@property return self.atconn.request_cloudflare(
def version(self): url=url, method=method,
return self._version params=params, data=data,
headers=headers,
reqcookies={
'ATERNOS_SERVER': self.servid
},
sendtoken=sendtoken
)
@property
def address(self):
return self._address
@property
def domain(self):
return self._domain
@property
def port(self):
return self._port
@property
def software(self):
return self._software
@property
def version(self):
return self._version

View file

@ -1,3 +1,3 @@
lxml==4.6.2 lxml==4.6.2
requests==2.25.1 requests==2.25.1
cloudscraper==1.2.58 cloudscraper==1.2.58

View file

@ -1,29 +1,31 @@
import setuptools import setuptools
with open('README.md', 'rt') as readme: with open('README.md', 'rt') as readme:
long_description = readme.read() long_description = readme.read()
setuptools.setup( setuptools.setup(
name='python-aternos', name='python-aternos',
version='0.1', version='0.2',
author='Chechkenev Andrey (@DarkCat09)', author='Chechkenev Andrey (@DarkCat09)',
author_email='aacd0709@mail.ru', author_email='aacd0709@mail.ru',
description='An unofficial Aternos API', description='An unofficial Aternos API',
long_description=long_description, long_description=long_description,
long_description_content_type='text/markdown', long_description_content_type='text/markdown',
url='https://github.com/DarkCat09/python-aternos', url='https://github.com/DarkCat09/python-aternos',
project_urls={ project_urls={
'Bug Tracker': 'https://github.com/DarkCat09/python-aternos/issues', 'Bug Tracker': 'https://github.com/DarkCat09/python-aternos/issues',
}, 'Documentation': 'https://github.com/DarkCat09/python-aternos/wiki/Client-(entry-point)',
classifiers=[ },
'Programming Language :: Python :: 3', classifiers=[
'License :: OSI Approved :: Apache Software License', 'Programming Language :: Python :: 3',
'Operating System :: OS Independent' 'License :: OSI Approved :: Apache Software License',
], 'Operating System :: OS Independent'
install_requires=[ ],
'lxml==4.6.2', install_requires=[
'requests==2.25.1', 'lxml==4.6.2',
'cloudscraper==1.2.58' 'requests==2.25.1',
], 'cloudscraper==1.2.58'
python_requires=">=3.6", ],
) packages=['python_aternos'],
python_requires=">=3.6",
)