DarkCat09 / python-aternos

[UNMAINTAINED] Unofficial Aternos API written in Python
https://pypi.org/project/python-aternos/
Apache License 2.0
92 stars 12 forks source link

python_aternos can`t parse TOKEN #17

Closed ZeynX92 closed 2 years ago

ZeynX92 commented 2 years ago

Hello everyone! I have a problem, my code was broken yesterday. I have written code that got server and then users can start()/stop() server by using discord bot, but now I have an errors:

WARNING:retry.api:Check your username and password, retrying in 0 seconds...
WARNING:retry.api:Unable to parse TOKEN from the page, retrying in 0 seconds...

But my password and username is valid! I don`t know what to do with this... Can you help me, please?

import config  # Importing file with password and username
from python_aternos import Client  # Iporting Client
from python_aternos.aterrors import ServerError 
from retry import retry  # Importing retry.api, because sometimes aternos has a problems with connecting
#Discord API 
from disnake.ext import commands 
from disnake import Embed

# Creating function that will get server using credentials
@retry()
def get_server():
    atern = Client.from_credentials(f'{config.username}', f'{config.password}')
    return atern.list_servers()[0]

class AternosCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.slash_command(
        name='start_server',
        description='Starting server',
        options=[]
    )
    async def start_server(self, ctx):
        print(get_server())  # Debug, here is the problem
        try:
            get_server().start()  # Main, here is the problem too
            await ctx.send(
                embed=Embed(title='🍏 Success!', description='Server is starting, please, wait...'))
        except ServerError:  # Exception
            await ctx.send(
                embed=Embed(title='🍏 Fail!', description='Server has already started...'))

    @commands.has_permissions(administrator=True)
    @commands.slash_command(
        name='stop_server',
        description='Stopping server',
        options=[]
    )
    async def stop_server(self, ctx):
        get_server.stop()
        await ctx.send(
            embed=Embed(title='🍏 Success!', description='Server shutting down...'))

def setup(bot):
    bot.add_cog(AternosCog(bot))

Thanks in advance, I will appreciate any help : )

DarkCat09 commented 2 years ago

@ZeynX92, да, стоит попробовать сбросить юзер-агент.

У меня вот почему-то опять всё работает.
Но если я напишу названия заголовков маленькими буквами
(host: и user-agent: вместо Host: и User-Agent:),
то клаудфлэр будет выдавать 403 Forbidden.

flancast90 commented 2 years ago

I think Cloudflare blocks requests from repl.it due to incorrect headers or IP address explained in https://github.com/DarkCat09/python-aternos/issues/16 by @sleeyax (thanks). Try to change the REQUA field:

Ok, I'll test this out. Also, what is "yourcookies" defined as?

I am using repl.it because I'm creating a wrapper for this project which is hosted on a Flask application. To make it public, I am using my existing pro subscription to replit.

ZeynX92 commented 2 years ago

@ZeynX92, да, стоит попробовать сбросить юзер-агент.

У меня вот почему-то опять всё работает. Но если я напишу названия заголовков маленькими буквами (host: и user-agent: вместо Host: и User-Agent:), то клаудфлэр будет выдавать 403 Forbidden

Понял, а как это сделать? + повторяю вопрос от человека выше)

DarkCat09 commented 2 years ago

yourcookies is ATERNOS_SESSION cookie from your browser. E.g.: from_session('123AbC456yjdt')

как это сделать

Use the code below / Используйте код выше.
aternos.atconn.REQUA = ''

ZeynX92 commented 2 years ago

А откуда взять yourcokies?)

ZeynX92 commented 2 years ago

А откуда взять yourcokies?)

Я имею ввиду как получить эту строку?)

DarkCat09 commented 2 years ago

Говорю ж, из браузера.
В случае с Google Chrome:

  1. Нажимаете на замочек в адресной строке,
  2. выбираете "куки" или что-то в этом роде,
  3. раскрываете ветку "aternos.org",
  4. ищете ATERNOS_SESSION и кликаете по нему,
  5. в строке "Значение" будет сессия.
ZeynX92 commented 2 years ago

WARNING:retry.api:'AternosConnect' object has no attribute 'cookies', retrying in 0 seconds...```
flancast90 commented 2 years ago

from python_aternos import Client, atconnect aternos = Client(atconnect.AternosConnect())

aternos.atconn.REQUA = ''

aternos.atconn.cookies.set('ATERNOS_SESSION', yourcookies) s = aternos.list_servers() print(s)

@DarkCat09 I'm still confused as how to use this method for other functions. For example, how would I use this with the Client.from_credentials() method?

DarkCat09 commented 2 years ago

@ZeynX92, ой, извините. Вот:

from python_aternos import Client, atconnect
aternos = Client(atconnect.AternosConnect())

# empty User-Agent
aternos.atconn.REQUA = ''

aternos.atconn.session.cookies.set('ATERNOS_SESSION', yourcookies)
s = aternos.list_servers()
print(s)

@flancast90, logging in with session can be performed with Client.from_session (but not from_credentials).

ZeynX92 commented 2 years ago
DEBUG:root:req-cookies={'ATERNOS_SESSION': '<мои cokies>'}
DEBUG:root:session-cookies=<RequestsCookieJar[]>
DEBUG:urllib3.connectionpool:Resetting dropped connection: aternos.org
DEBUG:urllib3.connectionpool:https://aternos.org:443 "GET /servers/ HTTP/1.1" 403 None
INFO:root:Retrying to bypass Cloudflare
DEBUG:root:Requesting(GET)https://aternos.org/servers/
DEBUG:root:headers={'Host': 'aternos.org', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.47', 'Sec-Ch-Ua': '" Not A;Brand";v="99", "Chromium";v="100", "Opera";v="86"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"Linux"', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'same-origin', 'Sec-Fetch-User': '?1', 'Upgrade-Insecure-Requests': '1'}
DEBUG:root:params={}
DEBUG:root:data={}
DEBUG:root:req-cookies={'ATERNOS_SESSION': '<мои cokies>'}
DEBUG:root:session-cookies=<RequestsCookieJar[]>
DEBUG:urllib3.connectionpool:Resetting dropped connection: aternos.org
DEBUG:urllib3.connectionpool:https://aternos.org:443 "GET /servers/ HTTP/1.1" 403 None
INFO:root:Retrying to bypass Cloudflare
DEBUG:root:Requesting(GET)https://aternos.org/servers/
DEBUG:root:headers={'Host': 'aternos.org', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.47', 'Sec-Ch-Ua': '" Not A;Brand";v="99", "Chromium";v="100", "Opera";v="86"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"Linux"', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'same-origin', 'Sec-Fetch-User': '?1', 'Upgrade-Insecure-Requests': '1'}
DEBUG:root:params={}
DEBUG:root:data={}
DEBUG:root:req-cookies={'ATERNOS_SESSION': '<мои cokies>'}
DEBUG:root:session-cookies=<RequestsCookieJar[]>
DEBUG:urllib3.connectionpool:Resetting dropped connection: aternos.org
DEBUG:urllib3.connectionpool:https://aternos.org:443 "GET /servers/ HTTP/1.1" 403 None
INFO:root:Retrying to bypass Cloudflare
DEBUG:root:Requesting(GET)https://aternos.org/servers/
DEBUG:root:headers={'Host': 'aternos.org', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.47', 'Sec-Ch-Ua': '" Not A;Brand";v="99", "Chromium";v="100", "Opera";v="86"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"Linux"', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'same-origin', 'Sec-Fetch-User': '?1', 'Upgrade-Insecure-Requests': '1'}
DEBUG:root:params={}
DEBUG:root:data={}
DEBUG:root:req-cookies={'ATERNOS_SESSION': '<мои cokies>'}
DEBUG:root:session-cookies=<RequestsCookieJar[]>
DEBUG:urllib3.connectionpool:Resetting dropped connection: aternos.org
DEBUG:urllib3.connectionpool:https://aternos.org:443 "GET /servers/ HTTP/1.1" 403 None
INFO:root:Retrying to bypass Cloudflare
DEBUG:root:Requesting(GET)https://aternos.org/servers/
DEBUG:root:headers={'Host': 'aternos.org', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.47', 'Sec-Ch-Ua': '" Not A;Brand";v="99", "Chromium";v="100", "Opera";v="86"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"Linux"', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'same-origin', 'Sec-Fetch-User': '?1', 'Upgrade-Insecure-Requests': '1'}
DEBUG:root:params={}
DEBUG:root:data={}
DEBUG:root:req-cookies={'ATERNOS_SESSION': '<мои cokies>'}
DEBUG:root:session-cookies=<RequestsCookieJar[]>
DEBUG:urllib3.connectionpool:Resetting dropped connection: aternos.org
DEBUG:urllib3.connectionpool:https://aternos.org:443 "GET /servers/ HTTP/1.1" 403 None
INFO:root:Retrying to bypass Cloudflare
DEBUG:root:Requesting(GET)https://aternos.org/servers/
DEBUG:root:headers={'Host': 'aternos.org', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.47', 'Sec-Ch-Ua': '" Not A;Brand";v="99", "Chromium";v="100", "Opera";v="86"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"Linux"', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'same-origin', 'Sec-Fetch-User': '?1', 'Upgrade-Insecure-Requests': '1'}
DEBUG:root:params={}
DEBUG:root:data={}
DEBUG:root:req-cookies={'ATERNOS_SESSION': '<мои cokies>'}
DEBUG:root:session-cookies=<RequestsCookieJar[]>
DEBUG:urllib3.connectionpool:Resetting dropped connection: aternos.org
DEBUG:urllib3.connectionpool:https://aternos.org:443 "GET /servers/ HTTP/1.1" 403 None
INFO:root:Retrying to bypass Cloudflare
DEBUG:root:Requesting(GET)https://aternos.org/servers/
DEBUG:root:headers={'Host': 'aternos.org', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.47', 'Sec-Ch-Ua': '" Not A;Brand";v="99", "Chromium";v="100", "Opera";v="86"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"Linux"', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'same-origin', 'Sec-Fetch-User': '?1', 'Upgrade-Insecure-Requests': '1'}
DEBUG:root:params={}
DEBUG:root:data={}
DEBUG:root:req-cookies={'ATERNOS_SESSION': '<мои cokies>'}
DEBUG:root:session-cookies=<RequestsCookieJar[]>
DEBUG:urllib3.connectionpool:Resetting dropped connection: aternos.org
DEBUG:urllib3.connectionpool:https://aternos.org:443 "GET /servers/ HTTP/1.1" 403 None
INFO:root:Retrying to bypass Cloudflare
DEBUG:root:Requesting(GET)https://aternos.org/servers/
DEBUG:root:headers={'Host': 'aternos.org', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.47', 'Sec-Ch-Ua': '" Not A;Brand";v="99", "Chromium";v="100", "Opera";v="86"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"Linux"', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'same-origin', 'Sec-Fetch-User': '?1', 'Upgrade-Insecure-Requests': '1'}
DEBUG:root:params={}
DEBUG:root:data={}
DEBUG:root:req-cookies={'ATERNOS_SESSION': '<мои cokies>'}
DEBUG:root:session-cookies=<RequestsCookieJar[]>
DEBUG:urllib3.connectionpool:Resetting dropped connection: aternos.org
DEBUG:urllib3.connectionpool:https://aternos.org:443 "GET /servers/ HTTP/1.1" 403 None
INFO:root:Retrying to bypass Cloudflare
DEBUG:root:Requesting(GET)https://aternos.org/servers/
DEBUG:root:headers={'Host': 'aternos.org', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.47', 'Sec-Ch-Ua': '" Not A;Brand";v="99", "Chromium";v="100", "Opera";v="86"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"Linux"', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'same-origin', 'Sec-Fetch-User': '?1', 'Upgrade-Insecure-Requests': '1'}
DEBUG:root:params={}
DEBUG:root:data={}
DEBUG:root:req-cookies={'ATERNOS_SESSION': '<мои cokies>'}
DEBUG:root:session-cookies=<RequestsCookieJar[]>
DEBUG:urllib3.connectionpool:Resetting dropped connection: aternos.org
DEBUG:urllib3.connectionpool:https://aternos.org:443 "GET /servers/ HTTP/1.1" 403 None
INFO:root:Retrying to bypass Cloudflare
ZeynX92 commented 2 years ago
@retry()
def get_server():
    yourcookies = <мое значение cokies>

    aternos = Client(atconnect.AternosConnect())

    # empty User-Agent
    aternos.atconn.REQUA = ''

    aternos.atconn.session.cookies.set('ATERNOS_SESSION', yourcookies)
    s = aternos.list_servers()
    print(s)
flancast90 commented 2 years ago

logging in with session can be performed with Client.from_session (but not from_credentials).

How do I have a session if I can't login with from_credentials? The entire premise of my project relies on automating the login on my server to get the necessary data.

Is there a way to use the blank REQUA with the from_credentials method? I don't necessarily need to login with session.

flancast90 commented 2 years ago

@DarkCat09 @ZeynX92 I solved it! After about 5 hours of trial+error (I even wrote my own fork of this trying stuff!), I simply added: atconnect.REQHEADERS = {}

before my from_credentials() call. After that, everything worked perfectly! I want to thank @DarkCat09 for all their help in solving this for my latest project, an auto-website generator/API utilising Aternos. My demo is hosted at https://minecraft.finnsoftware.net if you want to check it out!

EDIT: This solution worked until I restarted my server a third time. Based on what I can see through this behavior, I am wondering if Cloudflare has a ban system based on requests, headers, and timing which could be blocking the API. I will continue experimenting to find a fully-working solution, but I'm believing more and more that the issue is not with this library, but with Cloudscraper.

adxptived commented 2 years ago

python_aternos.aterrors.CredentialsError: Unable to parse TOKEN from the page - For some reason this error appears when running the code on Heroku, does anyone have a solution to this problem?

python_aternos.aterrors.CredentialsError: Unable to parse TOKEN from the page - Эта ошибка почему то появляется при запуске кода на хероку, есть ли у кого то решение данной проблемы?

DarkCat09 commented 2 years ago

Using selenium with the custom driver instead if cloudscraper will probably solve the problem. It is planned in v1.1.0.
This error appears due to Cloudflare protection that knows all Replit/Heroku/etc. IPs and blocks them.


Должно решить проблему использование селениума с пользовательским драйвером вместо cloudscraper-а. Запланировано на версию v1.1.0.
Ошибка появляется из-за системы защиты Cloudflare, которая, похоже, знает все айпишники Replit/Heroku/т.п. и блокирует их.

adxptived commented 2 years ago

Using selenium with the custom driver instead if cloudscraper will probably solve the problem. It is planned in v1.1.0.
This error appears due to Cloudflare protection that knows all Replit/Heroku/etc. IPs and blocks them.


Должно решить проблему использование селениума с пользовательским драйвером вместо cloudscraper-а. Запланировано на версию v1.1.0.
Ошибка появляется из-за системы защиты Cloudflare, которая, похоже, знает все айпишники Replit/Heroku/т.п. и блокирует их.

Thank you for your prompt response. Do not tell me when version 1.1.0 will be released?

Благодарю за ваш быстрый ответ. Не подскажите когда выйдет версия 1.1.0?

flancast90 commented 2 years ago

Using selenium with the custom driver instead if cloudscraper will probably solve the problem. It is planned in v1.1.0. This error appears due to Cloudflare protection that knows all Replit/Heroku/etc. IPs and blocks them.

Должно решить проблему использование селениума с пользовательским драйвером вместо cloudscraper-а. Запланировано на версию v1.1.0. Ошибка появляется из-за системы защиты Cloudflare, которая, похоже, знает все айпишники Replit/Heroku/т.п. и блокирует их.

Good idea to use selenium. I actually started transferring it over when mine started to stop working. Here's some helpful libs for it: https://pypi.org/project/selenium-stealth/.