Catch -404 BrokenAuthKeyError on .connect()'s .do_authentication

This commit is contained in:
Lonami Exo 2017-09-17 18:38:03 +02:00
parent 940dfe6816
commit e7a936222f
4 changed files with 22 additions and 5 deletions

View File

@ -3,7 +3,7 @@ import re
from .common import ( from .common import (
ReadCancelledError, InvalidParameterError, TypeNotFoundError, ReadCancelledError, InvalidParameterError, TypeNotFoundError,
InvalidChecksumError, CdnFileTamperedError InvalidChecksumError, BrokenAuthKeyError, CdnFileTamperedError
) )
from .rpc_errors import ( from .rpc_errors import (

View File

@ -38,6 +38,14 @@ class InvalidChecksumError(Exception):
self.valid_checksum = valid_checksum self.valid_checksum = valid_checksum
class BrokenAuthKeyError(Exception):
def __init__(self):
super().__init__(
self,
'The authorization key is broken, and it must be reset.'
)
class CdnFileTamperedError(Exception): class CdnFileTamperedError(Exception):
def __init__(self): def __init__(self):
super().__init__( super().__init__(

View File

@ -1,5 +1,6 @@
import time import time
from ..errors import BrokenAuthKeyError
from ..extensions import BinaryReader, BinaryWriter from ..extensions import BinaryReader, BinaryWriter
@ -36,6 +37,10 @@ class MtProtoPlainSender:
def receive(self): def receive(self):
"""Receives a plain packet, returning the body of the response""" """Receives a plain packet, returning the body of the response"""
body = self._connection.recv() body = self._connection.recv()
if body == b'l\xfe\xff\xff': # -404 little endian signed
# Broken authorization, must reset the auth key
raise BrokenAuthKeyError()
with BinaryReader(body) as reader: with BinaryReader(body) as reader:
reader.read_long() # auth_key_id reader.read_long() # auth_key_id
reader.read_long() # msg_id reader.read_long() # msg_id

View File

@ -7,13 +7,14 @@ from os import path
from . import helpers as utils from . import helpers as utils
from .crypto import rsa, CdnDecrypter from .crypto import rsa, CdnDecrypter
from .errors import ( from .errors import (
RPCError, FloodWaitError, FileMigrateError, TypeNotFoundError RPCError, BrokenAuthKeyError,
FloodWaitError, FileMigrateError, TypeNotFoundError
) )
from .network import authenticator, MtProtoSender, Connection, ConnectionMode from .network import authenticator, MtProtoSender, Connection, ConnectionMode
from .tl import TLObject, Session from .tl import TLObject, Session
from .tl.all_tlobjects import LAYER from .tl.all_tlobjects import LAYER
from .tl.functions import ( from .tl.functions import (
InitConnectionRequest, InvokeWithLayerRequest, PingRequest InitConnectionRequest, InvokeWithLayerRequest
) )
from .tl.functions.auth import ( from .tl.functions.auth import (
ImportAuthorizationRequest, ExportAuthorizationRequest ImportAuthorizationRequest, ExportAuthorizationRequest
@ -115,8 +116,11 @@ class TelegramBareClient:
if not self.session.auth_key: if not self.session.auth_key:
# New key, we need to tell the server we're going to use # New key, we need to tell the server we're going to use
# the latest layer # the latest layer
try:
self.session.auth_key, self.session.time_offset = \ self.session.auth_key, self.session.time_offset = \
authenticator.do_authentication(connection) authenticator.do_authentication(connection)
except BrokenAuthKeyError:
return False
self.session.layer = LAYER self.session.layer = LAYER
self.session.save() self.session.save()