mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-25 19:03:46 +03:00
Move connection parameters to the session
This commit is contained in:
parent
1119a2737a
commit
a73be04da7
|
@ -70,8 +70,7 @@ class TelegramBareClient:
|
||||||
|
|
||||||
# region Connecting
|
# region Connecting
|
||||||
|
|
||||||
def connect(self, device_model, system_version, app_version, lang_code,
|
def connect(self, exported_auth=None):
|
||||||
exported_auth=None):
|
|
||||||
"""Connects to the Telegram servers, executing authentication if
|
"""Connects to the Telegram servers, executing authentication if
|
||||||
required. Note that authenticating to the Telegram servers is
|
required. Note that authenticating to the Telegram servers is
|
||||||
not the same as authenticating the desired user itself, which
|
not the same as authenticating the desired user itself, which
|
||||||
|
@ -103,10 +102,10 @@ class TelegramBareClient:
|
||||||
|
|
||||||
request = InitConnectionRequest(
|
request = InitConnectionRequest(
|
||||||
api_id=self.api_id,
|
api_id=self.api_id,
|
||||||
device_model=device_model,
|
device_model=self.session.device_model,
|
||||||
system_version=system_version,
|
system_version=self.session.system_version,
|
||||||
app_version=app_version,
|
app_version=self.session.app_version,
|
||||||
lang_code=lang_code,
|
lang_code=self.session.lang_code,
|
||||||
query=query)
|
query=query)
|
||||||
|
|
||||||
result = self.invoke(
|
result = self.invoke(
|
||||||
|
@ -136,8 +135,7 @@ class TelegramBareClient:
|
||||||
self.sender.disconnect()
|
self.sender.disconnect()
|
||||||
self.sender = None
|
self.sender = None
|
||||||
|
|
||||||
def reconnect(self, device_model, system_version, app_version, lang_code,
|
def reconnect(self, new_dc=None):
|
||||||
new_dc=None):
|
|
||||||
"""Disconnects and connects again (effectively reconnecting).
|
"""Disconnects and connects again (effectively reconnecting).
|
||||||
|
|
||||||
If 'new_dc' is not None, the current authorization key is
|
If 'new_dc' is not None, the current authorization key is
|
||||||
|
@ -152,7 +150,7 @@ class TelegramBareClient:
|
||||||
self.session.port = dc.port
|
self.session.port = dc.port
|
||||||
self.session.save()
|
self.session.save()
|
||||||
|
|
||||||
self.connect(device_model, system_version, app_version, lang_code)
|
self.connect()
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
|
@ -194,10 +192,7 @@ class TelegramBareClient:
|
||||||
except ConnectionResetError:
|
except ConnectionResetError:
|
||||||
self._logger.info('Server disconnected us. Reconnecting and '
|
self._logger.info('Server disconnected us. Reconnecting and '
|
||||||
'resending request...')
|
'resending request...')
|
||||||
|
self.reconnect()
|
||||||
# TODO Don't actually use these values
|
|
||||||
import platform
|
|
||||||
self.reconnect(platform.node(), platform.system(), self.__version__, 'en')
|
|
||||||
return self.invoke(request, timeout=timeout)
|
return self.invoke(request, timeout=timeout)
|
||||||
|
|
||||||
except FloodWaitError:
|
except FloodWaitError:
|
||||||
|
|
|
@ -27,8 +27,7 @@ from .tl.functions.auth import (CheckPasswordRequest, LogOutRequest,
|
||||||
SignUpRequest, ImportBotAuthorizationRequest)
|
SignUpRequest, ImportBotAuthorizationRequest)
|
||||||
|
|
||||||
# Required to work with different data centers
|
# Required to work with different data centers
|
||||||
from .tl.functions.auth import (ExportAuthorizationRequest,
|
from .tl.functions.auth import ExportAuthorizationRequest
|
||||||
ImportAuthorizationRequest)
|
|
||||||
|
|
||||||
# Easier access to common methods
|
# Easier access to common methods
|
||||||
from .tl.functions.messages import (
|
from .tl.functions.messages import (
|
||||||
|
@ -103,14 +102,17 @@ class TelegramClient(TelegramBareClient):
|
||||||
self._updates_thread_receiving = Event()
|
self._updates_thread_receiving = Event()
|
||||||
|
|
||||||
# Used on connection - the user may modify these and reconnect
|
# Used on connection - the user may modify these and reconnect
|
||||||
self.device_model = \
|
if device_model:
|
||||||
device_model if device_model else platform.node()
|
self.session.device_model = device_model
|
||||||
|
|
||||||
self.system_version = \
|
if system_version:
|
||||||
system_version if system_version else platform.system()
|
self.session.system_version = system_version
|
||||||
|
|
||||||
self.app_version = app_version if app_version else self.__version__
|
self.session.app_version = \
|
||||||
self.lang_code = lang_code if lang_code else 'en'
|
app_version if app_version else self.__version__
|
||||||
|
|
||||||
|
if lang_code:
|
||||||
|
self.session.lang_code = lang_code
|
||||||
|
|
||||||
# Cache "exported" senders 'dc_id: MtProtoSender' and
|
# Cache "exported" senders 'dc_id: MtProtoSender' and
|
||||||
# their corresponding sessions not to recreate them all
|
# their corresponding sessions not to recreate them all
|
||||||
|
@ -131,12 +133,7 @@ class TelegramClient(TelegramBareClient):
|
||||||
|
|
||||||
*args will be ignored.
|
*args will be ignored.
|
||||||
"""
|
"""
|
||||||
return super(TelegramClient, self).connect(
|
return super(TelegramClient, self).connect()
|
||||||
device_model=self.device_model,
|
|
||||||
system_version=self.system_version,
|
|
||||||
app_version=self.app_version,
|
|
||||||
lang_code=self.lang_code
|
|
||||||
)
|
|
||||||
|
|
||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
"""Disconnects from the Telegram server
|
"""Disconnects from the Telegram server
|
||||||
|
@ -150,7 +147,7 @@ class TelegramClient(TelegramBareClient):
|
||||||
|
|
||||||
self._cached_clients.clear()
|
self._cached_clients.clear()
|
||||||
|
|
||||||
def reconnect(self, new_dc=None, *args):
|
def reconnect(self, new_dc=None):
|
||||||
"""Disconnects and connects again (effectively reconnecting).
|
"""Disconnects and connects again (effectively reconnecting).
|
||||||
|
|
||||||
If 'new_dc' is not None, the current authorization key is
|
If 'new_dc' is not None, the current authorization key is
|
||||||
|
@ -158,13 +155,7 @@ class TelegramClient(TelegramBareClient):
|
||||||
|
|
||||||
*args will be ignored.
|
*args will be ignored.
|
||||||
"""
|
"""
|
||||||
super(TelegramClient, self).reconnect(
|
super(TelegramClient, self).reconnect(new_dc=new_dc)
|
||||||
device_model=self.device_model,
|
|
||||||
system_version=self.system_version,
|
|
||||||
app_version=self.app_version,
|
|
||||||
lang_code=self.lang_code,
|
|
||||||
new_dc=new_dc
|
|
||||||
)
|
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
|
@ -186,13 +177,7 @@ class TelegramClient(TelegramBareClient):
|
||||||
client = self._cached_clients.get(dc_id)
|
client = self._cached_clients.get(dc_id)
|
||||||
if client:
|
if client:
|
||||||
if init_connection:
|
if init_connection:
|
||||||
client.reconnect(
|
client.reconnect()
|
||||||
device_model=self.device_model,
|
|
||||||
system_version=self.system_version,
|
|
||||||
app_version=self.app_version,
|
|
||||||
lang_code=self.lang_code
|
|
||||||
)
|
|
||||||
|
|
||||||
return client
|
return client
|
||||||
else:
|
else:
|
||||||
dc = self._get_dc(dc_id)
|
dc = self._get_dc(dc_id)
|
||||||
|
@ -206,9 +191,7 @@ class TelegramClient(TelegramBareClient):
|
||||||
session.server_address = dc.ip_address
|
session.server_address = dc.ip_address
|
||||||
session.port = dc.port
|
session.port = dc.port
|
||||||
client = TelegramBareClient(session, self.api_id, self.api_hash)
|
client = TelegramBareClient(session, self.api_id, self.api_hash)
|
||||||
client.connect(self.device_model, self.system_version,
|
client.connect(exported_auth=export_auth)
|
||||||
self.app_version, self.lang_code,
|
|
||||||
exported_auth=export_auth)
|
|
||||||
|
|
||||||
# Don't go through this expensive process every time.
|
# Don't go through this expensive process every time.
|
||||||
self._cached_clients[dc_id] = client
|
self._cached_clients[dc_id] = client
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import pickle
|
import pickle
|
||||||
|
import platform
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
|
@ -98,7 +99,19 @@ class JsonSession:
|
||||||
through an official Telegram client to revoke the authorization.
|
through an official Telegram client to revoke the authorization.
|
||||||
"""
|
"""
|
||||||
def __init__(self, session_user_id):
|
def __init__(self, session_user_id):
|
||||||
|
# These values will NOT be saved
|
||||||
self.session_user_id = session_user_id
|
self.session_user_id = session_user_id
|
||||||
|
|
||||||
|
# For connection purposes
|
||||||
|
self.device_model = platform.node()
|
||||||
|
self.system_version = platform.system()
|
||||||
|
self.app_version = '0'
|
||||||
|
self.lang_code = 'en'
|
||||||
|
|
||||||
|
# Cross-thread safety
|
||||||
|
self._lock = Lock()
|
||||||
|
|
||||||
|
# These values will be saved
|
||||||
self.server_address = '91.108.56.165'
|
self.server_address = '91.108.56.165'
|
||||||
self.port = 443
|
self.port = 443
|
||||||
self.auth_key = None
|
self.auth_key = None
|
||||||
|
@ -108,9 +121,6 @@ class JsonSession:
|
||||||
self.time_offset = 0
|
self.time_offset = 0
|
||||||
self.last_message_id = 0 # Long
|
self.last_message_id = 0 # Long
|
||||||
|
|
||||||
# Cross-thread safety
|
|
||||||
self._lock = Lock()
|
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
"""Saves the current session object as session_user_id.session"""
|
"""Saves the current session object as session_user_id.session"""
|
||||||
if self.session_user_id:
|
if self.session_user_id:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user