mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-29 12:53:44 +03:00
Replace the old Session+pickle with JsonSession (name change)
This commit is contained in:
parent
82304b18eb
commit
22673aec0e
|
@ -1,6 +1,6 @@
|
||||||
from hashlib import sha256
|
from hashlib import sha256
|
||||||
|
|
||||||
from ..tl import JsonSession
|
from ..tl import Session
|
||||||
from ..tl.functions.upload import GetCdnFileRequest, ReuploadCdnFileRequest
|
from ..tl.functions.upload import GetCdnFileRequest, ReuploadCdnFileRequest
|
||||||
from ..tl.types.upload import CdnFileReuploadNeeded
|
from ..tl.types.upload import CdnFileReuploadNeeded
|
||||||
from ..crypto import AESModeCTR
|
from ..crypto import AESModeCTR
|
||||||
|
@ -36,7 +36,7 @@ class CdnDecrypter:
|
||||||
|
|
||||||
# Create a new client on said CDN
|
# Create a new client on said CDN
|
||||||
dc = client._get_dc(cdn_redirect.dc_id, cdn=True)
|
dc = client._get_dc(cdn_redirect.dc_id, cdn=True)
|
||||||
session = JsonSession(client.session)
|
session = Session(client.session)
|
||||||
session.server_address = dc.ip_address
|
session.server_address = dc.ip_address
|
||||||
session.port = dc.port
|
session.port = dc.port
|
||||||
cdn_client = client_cls( # Avoid importing TelegramBareClient
|
cdn_client = client_cls( # Avoid importing TelegramBareClient
|
||||||
|
|
|
@ -15,7 +15,7 @@ from .utils import get_appropriated_part_size
|
||||||
from .crypto import rsa, CdnDecrypter
|
from .crypto import rsa, CdnDecrypter
|
||||||
|
|
||||||
# For sending and receiving requests
|
# For sending and receiving requests
|
||||||
from .tl import TLObject, JsonSession
|
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, PingRequest
|
||||||
|
@ -230,7 +230,7 @@ class TelegramBareClient:
|
||||||
|
|
||||||
# region Working with different Data Centers
|
# region Working with different Data Centers
|
||||||
|
|
||||||
def _get_dc(self, dc_id, ipv6=True, cdn=False):
|
def _get_dc(self, dc_id, ipv6=False, cdn=False):
|
||||||
"""Gets the Data Center (DC) associated to 'dc_id'"""
|
"""Gets the Data Center (DC) associated to 'dc_id'"""
|
||||||
if not self.dc_options:
|
if not self.dc_options:
|
||||||
raise ConnectionError(
|
raise ConnectionError(
|
||||||
|
@ -286,7 +286,7 @@ class TelegramBareClient:
|
||||||
#
|
#
|
||||||
# Construct this session with the connection parameters
|
# Construct this session with the connection parameters
|
||||||
# (system version, device model...) from the current one.
|
# (system version, device model...) from the current one.
|
||||||
session = JsonSession(self.session)
|
session = Session(self.session)
|
||||||
session.server_address = dc.ip_address
|
session.server_address = dc.ip_address
|
||||||
session.port = dc.port
|
session.port = dc.port
|
||||||
client = TelegramBareClient(
|
client = TelegramBareClient(
|
||||||
|
|
|
@ -15,7 +15,7 @@ from .errors import (RPCError, UnauthorizedError, InvalidParameterError,
|
||||||
PhoneCodeInvalidError, InvalidChecksumError)
|
PhoneCodeInvalidError, InvalidChecksumError)
|
||||||
|
|
||||||
# For sending and receiving requests
|
# For sending and receiving requests
|
||||||
from .tl import Session, JsonSession, TLObject
|
from .tl import Session, TLObject
|
||||||
|
|
||||||
# Required to get the password salt
|
# Required to get the password salt
|
||||||
from .tl.functions.account import GetPasswordRequest
|
from .tl.functions.account import GetPasswordRequest
|
||||||
|
@ -87,10 +87,9 @@ class TelegramClient(TelegramBareClient):
|
||||||
"Refer to Telethon's README.rst for more information.")
|
"Refer to Telethon's README.rst for more information.")
|
||||||
|
|
||||||
# Determine what session object we have
|
# Determine what session object we have
|
||||||
# TODO JsonSession until migration is complete (by v1.0)
|
|
||||||
if isinstance(session, str) or session is None:
|
if isinstance(session, str) or session is None:
|
||||||
session = JsonSession.try_load_or_create_new(session)
|
session = Session.try_load_or_create_new(session)
|
||||||
elif not isinstance(session, Session) and not isinstance(session, JsonSession):
|
elif not isinstance(session, Session):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
'The given session must be a str or a Session instance.')
|
'The given session must be a str or a Session instance.')
|
||||||
|
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
from .tlobject import TLObject
|
from .tlobject import TLObject
|
||||||
from .session import Session, JsonSession
|
from .session import Session
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import pickle
|
|
||||||
import platform
|
import platform
|
||||||
import time
|
import time
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
|
@ -11,80 +10,6 @@ from .. import helpers as utils
|
||||||
|
|
||||||
|
|
||||||
class Session:
|
class Session:
|
||||||
def __init__(self, session_user_id):
|
|
||||||
self.session_user_id = session_user_id
|
|
||||||
self.server_address = '91.108.56.165'
|
|
||||||
self.port = 443
|
|
||||||
self.auth_key = None
|
|
||||||
self.id = utils.generate_random_long(signed=False)
|
|
||||||
self.sequence = 0
|
|
||||||
self.salt = 0 # Unsigned long
|
|
||||||
self.time_offset = 0
|
|
||||||
self.last_message_id = 0 # Long
|
|
||||||
# TODO Remove this now unused members, left so unpickling is happy
|
|
||||||
self.user = None
|
|
||||||
|
|
||||||
def save(self):
|
|
||||||
"""Saves the current session object as session_user_id.session"""
|
|
||||||
if self.session_user_id:
|
|
||||||
with open('{}.session'.format(self.session_user_id), 'wb') as file:
|
|
||||||
pickle.dump(self, file)
|
|
||||||
|
|
||||||
def delete(self):
|
|
||||||
"""Deletes the current session file"""
|
|
||||||
try:
|
|
||||||
os.remove('{}.session'.format(self.session_user_id))
|
|
||||||
return True
|
|
||||||
except OSError:
|
|
||||||
return False
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def try_load_or_create_new(session_user_id):
|
|
||||||
"""Loads a saved session_user_id session, or creates a new one if none existed before.
|
|
||||||
If the given session_user_id is None, we assume that it is for testing purposes"""
|
|
||||||
if session_user_id is None:
|
|
||||||
return Session(None)
|
|
||||||
else:
|
|
||||||
path = '{}.session'.format(session_user_id)
|
|
||||||
|
|
||||||
if file_exists(path):
|
|
||||||
with open(path, 'rb') as file:
|
|
||||||
return pickle.load(file)
|
|
||||||
else:
|
|
||||||
return Session(session_user_id)
|
|
||||||
|
|
||||||
def generate_sequence(self, confirmed):
|
|
||||||
"""Ported from JsonSession.generate_sequence"""
|
|
||||||
with Lock():
|
|
||||||
if confirmed:
|
|
||||||
result = self.sequence * 2 + 1
|
|
||||||
self.sequence += 1
|
|
||||||
return result
|
|
||||||
else:
|
|
||||||
return self.sequence * 2
|
|
||||||
|
|
||||||
def get_new_msg_id(self):
|
|
||||||
now = time.time()
|
|
||||||
nanoseconds = int((now - int(now)) * 1e+9)
|
|
||||||
# "message identifiers are divisible by 4"
|
|
||||||
new_msg_id = (int(now) << 32) | (nanoseconds << 2)
|
|
||||||
|
|
||||||
if self.last_message_id >= new_msg_id:
|
|
||||||
new_msg_id = self.last_message_id + 4
|
|
||||||
|
|
||||||
self.last_message_id = new_msg_id
|
|
||||||
return new_msg_id
|
|
||||||
|
|
||||||
def update_time_offset(self, correct_msg_id):
|
|
||||||
"""Updates the time offset based on a known correct message ID"""
|
|
||||||
now = int(time.time())
|
|
||||||
correct = correct_msg_id >> 32
|
|
||||||
self.time_offset = correct - now
|
|
||||||
|
|
||||||
|
|
||||||
# Until migration is complete, we need the original 'Session' class
|
|
||||||
# for Pickle to keep working. TODO Replace 'Session' by 'JsonSession' by v1.0
|
|
||||||
class JsonSession:
|
|
||||||
"""This session contains the required information to login into your
|
"""This session contains the required information to login into your
|
||||||
Telegram account. NEVER give the saved JSON file to anyone, since
|
Telegram account. NEVER give the saved JSON file to anyone, since
|
||||||
they would gain instant access to all your messages and contacts.
|
they would gain instant access to all your messages and contacts.
|
||||||
|
@ -98,7 +23,7 @@ class JsonSession:
|
||||||
those required to init a connection will be copied.
|
those required to init a connection will be copied.
|
||||||
"""
|
"""
|
||||||
# These values will NOT be saved
|
# These values will NOT be saved
|
||||||
if isinstance(session_user_id, JsonSession):
|
if isinstance(session_user_id, Session):
|
||||||
self.session_user_id = None
|
self.session_user_id = None
|
||||||
|
|
||||||
# For connection purposes
|
# For connection purposes
|
||||||
|
@ -174,10 +99,10 @@ class JsonSession:
|
||||||
If session_user_id=None, later .save()'s will have no effect.
|
If session_user_id=None, later .save()'s will have no effect.
|
||||||
"""
|
"""
|
||||||
if session_user_id is None:
|
if session_user_id is None:
|
||||||
return JsonSession(None)
|
return Session(None)
|
||||||
else:
|
else:
|
||||||
path = '{}.session'.format(session_user_id)
|
path = '{}.session'.format(session_user_id)
|
||||||
result = JsonSession(session_user_id)
|
result = Session(session_user_id)
|
||||||
if not file_exists(path):
|
if not file_exists(path):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -199,16 +124,7 @@ class JsonSession:
|
||||||
result.auth_key = AuthKey(data=key)
|
result.auth_key = AuthKey(data=key)
|
||||||
|
|
||||||
except (json.decoder.JSONDecodeError, UnicodeDecodeError):
|
except (json.decoder.JSONDecodeError, UnicodeDecodeError):
|
||||||
# TODO Backwards-compatibility code
|
pass
|
||||||
old = Session.try_load_or_create_new(session_user_id)
|
|
||||||
result.id = old.id
|
|
||||||
result.port = old.port
|
|
||||||
result.salt = old.salt
|
|
||||||
result._sequence = old.sequence
|
|
||||||
result.time_offset = old.time_offset
|
|
||||||
result.server_address = old.server_address
|
|
||||||
result.auth_key = old.auth_key
|
|
||||||
result.save()
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user