Added support of ipv6 address (boolean "use_v6" parameter in TelegramClient constructor)

This commit is contained in:
Man197 2017-11-12 12:38:38 +03:00
parent 5a57a8a498
commit 237583ea21
3 changed files with 12 additions and 6 deletions

View File

@ -69,6 +69,7 @@ class TelegramBareClient:
def __init__(self, session, api_id, api_hash, def __init__(self, session, api_id, api_hash,
connection_mode=ConnectionMode.TCP_FULL, connection_mode=ConnectionMode.TCP_FULL,
use_v6=False,
proxy=None, proxy=None,
update_workers=None, update_workers=None,
spawn_read_thread=False, spawn_read_thread=False,
@ -82,7 +83,7 @@ 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) session = Session.try_load_or_create_new(session, use_v6)
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.'

View File

@ -69,6 +69,7 @@ class TelegramClient(TelegramBareClient):
def __init__(self, session, api_id, api_hash, def __init__(self, session, api_id, api_hash,
connection_mode=ConnectionMode.TCP_FULL, connection_mode=ConnectionMode.TCP_FULL,
use_v6=False,
proxy=None, proxy=None,
update_workers=None, update_workers=None,
timeout=timedelta(seconds=5), timeout=timedelta(seconds=5),
@ -113,6 +114,7 @@ class TelegramClient(TelegramBareClient):
super().__init__( super().__init__(
session, api_id, api_hash, session, api_id, api_hash,
connection_mode=connection_mode, connection_mode=connection_mode,
use_v6=use_v6,
proxy=proxy, proxy=proxy,
update_workers=update_workers, update_workers=update_workers,
spawn_read_thread=spawn_read_thread, spawn_read_thread=spawn_read_thread,

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): def __init__(self, session_user_id, use_v6=False):
"""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,6 +64,9 @@ 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 = '[2001:67c:4e8:f002::a]'
else:
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
@ -109,15 +112,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): def try_load_or_create_new(session_user_id, use_v6=False):
"""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) return Session(None, use_v6)
else: else:
path = '{}.session'.format(session_user_id) path = '{}.session'.format(session_user_id)
result = Session(session_user_id) result = Session(session_user_id, use_v6)
if not file_exists(path): if not file_exists(path):
return result return result