Add support for connecting through IPv6 (#425 for #112)

This commit is contained in:
Vladislav Kolesnichenko 2017-11-16 15:30:18 +03:00 committed by Lonami
parent 959e824c1c
commit ee5915e86d
3 changed files with 20 additions and 5 deletions

View File

@ -39,6 +39,11 @@ from .update_state import UpdateState
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:
"""Bare Telegram Client with just the minimum -
@ -69,6 +74,7 @@ class TelegramBareClient:
def __init__(self, session, api_id, api_hash,
connection_mode=ConnectionMode.TCP_FULL,
use_ipv6=False,
proxy=None,
update_workers=None,
spawn_read_thread=False,
@ -80,6 +86,8 @@ class TelegramBareClient:
"Your API ID or Hash cannot be empty or None. "
"Refer to Telethon's README.rst for more information.")
self._use_ipv6 = use_ipv6
# Determine what session object we have
if isinstance(session, str) or session is None:
session = Session.try_load_or_create_new(session)
@ -88,6 +96,11 @@ class TelegramBareClient:
'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 self._use_ipv6 else DEFAULT_IPV4_IP
self.session = session
self.api_id = int(api_id)
self.api_hash = api_hash
@ -282,7 +295,7 @@ class TelegramBareClient:
return self._recv_thread is not None and \
threading.get_ident() == self._recv_thread.ident
def _get_dc(self, dc_id, ipv6=False, cdn=False):
def _get_dc(self, dc_id, cdn=False):
"""Gets the Data Center (DC) associated to 'dc_id'"""
if not TelegramBareClient._config:
TelegramBareClient._config = self(GetConfigRequest())
@ -295,7 +308,7 @@ class TelegramBareClient:
return next(
dc for dc in TelegramBareClient._config.dc_options
if dc.id == dc_id and bool(dc.ipv6) == ipv6 and bool(dc.cdn) == cdn
if dc.id == dc_id and bool(dc.ipv6) == self._use_ipv6 and bool(dc.cdn) == cdn
)
except StopIteration:
if not cdn:
@ -303,7 +316,7 @@ class TelegramBareClient:
# New configuration, perhaps a new CDN was added?
TelegramBareClient._config = self(GetConfigRequest())
return self._get_dc(dc_id, ipv6=ipv6, cdn=cdn)
return self._get_dc(dc_id, cdn=cdn)
def _get_exported_client(self, dc_id):
"""Creates and connects a new TelegramBareClient for the desired DC.

View File

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

View File

@ -64,8 +64,8 @@ class Session:
self._last_msg_id = 0 # Long
# These values will be saved
self.server_address = '91.108.56.165'
self.port = 443
self.server_address = None
self.port = None
self.auth_key = None
self.layer = 0
self.salt = 0 # Unsigned long