Clean-up use_v6 usage

This commit is contained in:
Lonami Exo 2017-11-12 12:20:57 +01:00
parent 237583ea21
commit f5c5110ce2
2 changed files with 17 additions and 10 deletions

View File

@ -39,6 +39,11 @@ from .update_state import UpdateState
from .utils import get_appropriated_part_size from .utils import get_appropriated_part_size
DEFAULT_IPV4_IP = '149.154.167.51'
DEFAULT_IPV6_IP = '[2001:67c:4e8:f002::a]'
DEFAULT_PORT = 443
class TelegramBareClient: class TelegramBareClient:
"""Bare Telegram Client with just the minimum - """Bare Telegram Client with just the minimum -
@ -83,12 +88,17 @@ class TelegramBareClient:
# Determine what session object we have # Determine what session object we have
if isinstance(session, str) or session is None: if isinstance(session, str) or session is None:
session = Session.try_load_or_create_new(session, use_v6) session = Session.try_load_or_create_new(session)
elif not isinstance(session, Session): 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.'
) )
if not session.server_address:
session.port = DEFAULT_PORT
session.server_address = \
DEFAULT_IPV6_IP if use_v6 else DEFAULT_IPV4_IP
self.session = session self.session = session
self.api_id = int(api_id) self.api_id = int(api_id)
self.api_hash = api_hash self.api_hash = api_hash

View File

@ -18,7 +18,7 @@ class Session:
If you think the session has been compromised, close all the sessions If you think the session has been compromised, close all the sessions
through an official Telegram client to revoke the authorization. through an official Telegram client to revoke the authorization.
""" """
def __init__(self, session_user_id, use_v6=False): def __init__(self, session_user_id):
"""session_user_id should either be a string or another Session. """session_user_id should either be a string or another Session.
Note that if another session is given, only parameters like Note that if another session is given, only parameters like
those required to init a connection will be copied. those required to init a connection will be copied.
@ -64,11 +64,8 @@ class Session:
self._last_msg_id = 0 # Long self._last_msg_id = 0 # Long
# These values will be saved # These values will be saved
if use_v6: self.server_address = None
self.server_address = '[2001:67c:4e8:f002::a]' self.port = None
else:
self.server_address = '91.108.56.165'
self.port = 443
self.auth_key = None self.auth_key = None
self.layer = 0 self.layer = 0
self.salt = 0 # Unsigned long self.salt = 0 # Unsigned long
@ -112,15 +109,15 @@ class Session:
for f in os.listdir('.') if f.endswith('.session')] for f in os.listdir('.') if f.endswith('.session')]
@staticmethod @staticmethod
def try_load_or_create_new(session_user_id, use_v6=False): def try_load_or_create_new(session_user_id):
"""Loads a saved session_user_id.session or creates a new one. """Loads a saved session_user_id.session or creates a new one.
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 Session(None, use_v6) return Session(None)
else: else:
path = '{}.session'.format(session_user_id) path = '{}.session'.format(session_user_id)
result = Session(session_user_id, use_v6) result = Session(session_user_id)
if not file_exists(path): if not file_exists(path):
return result return result