LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
10.02k stars 1.41k forks source link

Switching from IPv4 to IPv6 breaks existing authorization data #3196

Open lilydjwg opened 3 years ago

lilydjwg commented 3 years ago

Checklist

When I enabled IPv6 for the client, reusing an existing session that has been connecting via IPv4, I got this error:

[I 10-29 20:50:30.003 mtprotosender:221] Connecting to 2001:67c:4e8:f002::a:443/TcpFull...
[I 10-29 20:50:30.012 mtprotosender:272] Connection to 2001:67c:4e8:f002::a:443/TcpFull complete!
[I 10-29 20:50:30.025 mtprotosender:524] Broken authorization key; resetting
[I 10-29 20:50:30.028 mtprotosender:312] Disconnecting from 2001:67c:4e8:f002::a:443/TcpFull...
Traceback (most recent call last):
  File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/lilydjwg/workspace/luoxu/luoxu/__main__.py", line 174, in <module>
    run_until_sigint(indexer.run())
  File "/home/lilydjwg/workspace/luoxu/luoxu/util.py", line 24, in run_until_sigint
    loop.run_until_complete(fu)
  File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/home/lilydjwg/workspace/luoxu/luoxu/__main__.py", line 97, in run
    await client.start(tg_config['account'])
  File "/usr/lib/python3.9/site-packages/telethon/client/auth.py", line 140, in _start
    await self.connect()
  File "/usr/lib/python3.9/site-packages/telethon/client/telegrambaseclient.py", line 529, in connect
    await self._sender.send(functions.InvokeWithLayerRequest(
  File "/usr/lib/python3.9/site-packages/telethon/network/mtprotosender.py", line 510, in _recv_loop
    message = self._state.decrypt_message_data(body)
  File "/usr/lib/python3.9/site-packages/telethon/network/mtprotostate.py", line 138, in decrypt_message_data
    raise InvalidBufferError(body)
telethon.errors.common.InvalidBufferError: Invalid response buffer (HTTP code 404)

Then I reran the program, and it asked to authorize:

[I 10-29 20:50:40.012 mtprotosender:221] Connecting to 2001:67c:4e8:f002::a:443/TcpFull...
[I 10-29 20:50:40.823 mtprotosender:272] Connection to 2001:67c:4e8:f002::a:443/TcpFull complete!
[I 10-29 20:50:41.137 users:115] Phone migrated to 1
[I 10-29 20:50:41.268 telegrambaseclient:660] Reconnecting to new data center 1
[I 10-29 20:50:41.425 mtprotosender:312] Disconnecting from 2001:67c:4e8:f002::a:443/TcpFull...
[I 10-29 20:50:41.425 mtprotosender:332] Disconnection from 2001:67c:4e8:f002::a:443/TcpFull complete!
[I 10-29 20:50:41.426 mtprotosender:221] Connecting to 2001:0b28:f23d:f001:0000:0000:0000:000a:443/TcpFull...
[I 10-29 20:50:42.638 mtprotosender:272] Connection to 2001:0b28:f23d:f001:0000:0000:0000:000a:443/TcpFull complete!
Please enter the code you received: 

It seems that the authorization data doesn't work with different protocols? There is also #1661 which sounds similar but I don't use a proxy.

Update: the reverse is also true: switching from IPv6 to IPv4 broke the session again.

Lonami commented 2 years ago

This is not very high priority because it is unlikely someone will keep switching back-and-forth between IPv4 and IPv6.

lilydjwg commented 2 years ago

Maybe raise an error on switching so the user knows they need to auth again? It is not always easy to re-auth.

w1ld32 commented 2 years ago

This is not very high priority because it is unlikely someone will keep switching back-and-forth between IPv4 and IPv6.

For me, this problem is relevant. I register accounts on ipv4, and for further work they use IPV6, and in this case, these accounts do not work (

Lonami commented 2 years ago

You're welcome to review if this is still an issue in the current master version and try to correct it.

w1ld32 commented 2 years ago

You're welcome to review if this is still an issue in the current master version and try to correct it.

I tried it in the current version master (pip3 install -U https://github.com/LonamiWebs/Telethon/archive/master.zip ) the error has been preserved. I do not know how to solve this problem.

w1ld32 commented 2 years ago

Sorry, maybe I'm digging absolutely in the wrong place, and I'm wrong in my judgments and searches. I also don't know how transmission protocols work in general, but the only thing I have unearthed is that the response body from the server when switching to IPV6 is very short. Perhaps there are ideas why this is so? There is no such problem in the pyrogram, it works on layer 138, attempts to change the layer in telethon did not give results. I tried to pull out the contents of the packages (in which this error occurs) telethon ipv4-ipv4

data b'\xc3\xf6\x1f\r\xad\xc7\xfc\x00\xa9\xfc\xafpbQ;\xfc\x1d\x99{\xbc\xa9B\xecV\t\xe4\x03\xc6\xd2\x17b\x7f\x84\xb6\x84\xae\xa1\xe0\x7f\xfc\xb6\xb6\x8fY[\xc2\xe1y_\x0e4&D\xbb\xcfe\xe8\xf9\xa6\x84|\x16rv\xf1\xf9\xdd\xf5t\x90Bp\x1f\x17\x88a\x8dc\xaa\xd3\x0f\xffF\xcb\xca\x01u\x8a\x99\xabH\xef\n\xe2\x0b\xa0\xbfe\xfcH\xec\x85\xc7#\xebi\x13\xca\xc4\x81\xf8\xef\xdd%)#Q-\x1b=\xc5\xc2gn\xfbw\xf8T5\x8fQU\x83\xc6\xbdX'
body b'\xc3\xf6\x1f\r\xad\xc7\xfc\x00\xe5\x7f\xf1ox\xa8\xe5"b\xee\xce\xd6\xefK3\xee\xa9\xaf\x02\xcb\xaf\x99\x1c\xcd6\xadr\'\xe5\x9d\xae\x8d\xda\x94Tn\x87\xac\\\xbaAh4\xdb\x9b\xe7W.z\xd0\x0e\xb4\xb2\xe0\xdc\x98\x0f:3\x99a*(\x97J\xe7\x9b7Q\xe0>\x9d\x92\x02\x02\xc2\x9e\xd2i\xc6\xa4\xdb~t\x84\x96\xba\x00G\x1c\x12\x14\xc8\xe4\x17\xad'

teleton ipv4-ipv6
data b'\xf4\xce4\xab\xe55f\x9c\x94K\xda\xd3 \xbb\x13\x8c\xfa\x8el\xa5\xb5\xc2\xa6\xab\xc9\x8a\xfe\xa4\x80\x18\xd7\x02\x04@G\x188[ \xef\x17#hB\xeb\x1aT\x9fpX\x18v?\xf9\t;\'\x84\xf4U\xbbNDb_H\x9d\x91\xa5\xa8\xd9\xad\x041\xa4T\x05\x9fn\xcdG\xd6\x98|\'\x8an\x96h\xbc"\x1c.\xa2W1\xf3Q\xdcKBe\xd4$\x921\x95\x01\xf4\x15\x12\x9f\xf2\xbeQ`\x82\x00f/\x96F\xfc\xf1\xcc\xf590\xcc\xc9W\x08gk\x05\xb1z3\x7f\x8eU\x96\x0e\x9c\xa1*/\xb9\xdb\xa2\x00\xbb'
body b'l\xfe\xff\xff'

pyrogram ipv4-ipv6
data b'K\xe4\x10 \xd3\xdb&a\xa8%\x18\xf6\\e\xed\x87Y{T\x9d\xac\xce\xdb\xcft\xdb\x8d\xac\xf0\xee\x98\xd3\x92[\xbc\x9f\xd9\xc4N\x1f7\x14\x95S\xed@1\xab\xcc\x0cmIs\xdd|Ib\xd31.m\x83\x81\xce\x0fW\xd8\xd2\x19\xd2f}\x18y\x84$\xf7#\xf9HG\xf9dZ\x88=S"\x99\xc55gb\x14\xa2\xf2\x90\xf9\xc3\xd1\x7fQz\xef'
body b'K\xe4\x10 \xd3\xdb&a\xae\x8e+\xbc.\xd2uF)M\x80V5\xe4\xb3\xf7\xb1\rMn\xbc\xcd\x8a\n\x05\xf8~\xf5I\xff\x9d\xaa]\xcb\xcd\xd7\x03\x1dO\xdb\xc3i~\x8ba6\xe0#\xa7\x92\x1f\xa2\xf8\x89\x92\xff\x12?\xf8\x0c\xb7\x9f\x12\xe7\x89\xbd\xf3\xce\x03\x17\x15Q\xe1\xfdRs\x94\xb0 \xf0\xd1M\xcd\xbf\x06T3\xed\xf2\xbb\x15\x80ru\xe5\xd2'

maybe this will help

w1ld32 commented 2 years ago

@Lonami How save and use SessionsString on V2? On main i save SessionString=client.session.save() or StringSession.save(client.session) but on v2 dont work this(

Lonami commented 2 years ago

@w1ld32 that's offtopic here.

JuniorJPDJ commented 4 months ago

still happens, but for me doesnt show any error message, just stops on connecting

w1ld32 commented 4 months ago

still happens, but for me doesnt show any error message, just stops on connecting

This problem lies in the addresses of the TG servers. IPV4 and IPV6 are different servers, and when switching between them, the server address does not change automatically. I solved this issue by manually changing the DC server in the session file.

JuniorJPDJ commented 4 months ago

It does change, but server migration is not being handled like on first startup.

JuniorJPDJ commented 4 months ago

@divadsn

divadsn commented 4 months ago

From what I have so far figured out is this one of the root causes: https://github.com/LonamiWebs/Telethon/blob/a5c98aec50891e973b1ae4f38b8c080444aae664/telethon/client/telegrambaseclient.py#L310

When switching from IPv4 to IPv6 or vice versa the data center ID gets reset in the session file to the DEFAULT_DC_ID along with the new IP address, but for some reason Telethon does not handle the migration again like on first time.

Continuing my research from few weeks ago, it looks like it's skipping the migration due to the fact that we are already authorized, which I believe can be the reason why it's not re-migrating after a switch here: https://github.com/LonamiWebs/Telethon/blob/a5c98aec50891e973b1ae4f38b8c080444aae664/telethon/client/users.py#L127

I would look at the issue the next days during my vacation and open a PR if I find a fix.

JuniorJPDJ commented 3 months ago

@divadsn bump :D