Attempt at supporting IPv6 (#211 and #112)

This commit is contained in:
Lonami Exo 2017-08-29 13:49:41 +02:00
parent 592e107f52
commit 82304b18eb
2 changed files with 17 additions and 10 deletions

View File

@ -19,12 +19,12 @@ class TcpClient:
self.delay = 0.1 # Read delay when there was no data available
self._lock = Lock()
def _recreate_socket(self):
def _recreate_socket(self, mode):
if self._proxy is None:
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._socket = socket.socket(mode, socket.SOCK_STREAM)
else:
import socks
self._socket = socks.socksocket(socket.AF_INET, socket.SOCK_STREAM)
self._socket = socks.socksocket(mode, socket.SOCK_STREAM)
if type(self._proxy) is dict:
self._socket.set_proxy(**self._proxy)
else: # tuple, list, etc.
@ -35,9 +35,14 @@ class TcpClient:
'timeout' must be given in seconds
"""
if not self.connected:
self._recreate_socket()
self._socket.settimeout(timeout)
self._socket.connect((ip, port))
if ':' in ip: # IPv6
self._recreate_socket(socket.AF_INET6)
self._socket.settimeout(timeout)
self._socket.connect((ip, port, 0, 0))
else:
self._recreate_socket(socket.AF_INET)
self._socket.settimeout(timeout)
self._socket.connect((ip, port))
self._socket.setblocking(False)
def _get_connected(self):

View File

@ -230,7 +230,7 @@ class TelegramBareClient:
# region Working with different Data Centers
def _get_dc(self, dc_id, cdn=False):
def _get_dc(self, dc_id, ipv6=True, cdn=False):
"""Gets the Data Center (DC) associated to 'dc_id'"""
if not self.dc_options:
raise ConnectionError(
@ -238,8 +238,10 @@ class TelegramBareClient:
'Stabilise a successful initial connection first.')
try:
return next(dc for dc in self.dc_options
if dc.id == dc_id and bool(dc.cdn) == cdn)
return next(
dc for dc in self.dc_options if dc.id == dc_id and
bool(dc.ipv6) == ipv6 and bool(dc.cdn) == cdn
)
except StopIteration:
if not cdn:
raise
@ -248,7 +250,7 @@ class TelegramBareClient:
rsa.add_key(pk.public_key)
self.dc_options = self(GetConfigRequest()).dc_options
return self._get_dc(dc_id, cdn=cdn)
return self._get_dc(dc_id, ipv6=ipv6, cdn=cdn)
def _get_exported_client(self, dc_id,
init_connection=False,