Always use session ip address and port when connecting

This commit is contained in:
Lonami Exo 2017-10-01 19:26:20 +02:00
parent 269949595f
commit e717256937
3 changed files with 9 additions and 14 deletions

View File

@ -43,12 +43,9 @@ class Connection:
work on plain bytes, with no further additions.
"""
def __init__(self, ip, port, mode=ConnectionMode.TCP_FULL,
def __init__(self, mode=ConnectionMode.TCP_FULL,
proxy=None, timeout=timedelta(seconds=5)):
self.ip = ip
self.port = port
self._mode = mode
self._send_counter = 0
self._aes_encrypt, self._aes_decrypt = None, None
@ -77,9 +74,9 @@ class Connection:
setattr(self, 'write', self._write_plain)
setattr(self, 'read', self._read_plain)
def connect(self):
def connect(self, ip, port):
try:
self.conn.connect(self.ip, self.port)
self.conn.connect(ip, port)
except OSError as e:
if e.errno == errno.EISCONN:
return # Already connected, no need to re-set everything up
@ -132,10 +129,9 @@ class Connection:
def clone(self):
"""Creates a copy of this Connection"""
return Connection(self.ip, self.port,
mode=self._mode,
proxy=self.conn.proxy,
timeout=self.conn.timeout)
return Connection(
mode=self._mode, proxy=self.conn.proxy, timeout=self.conn.timeout
)
# region Receive message implementations

View File

@ -43,7 +43,7 @@ class MtProtoSender:
def connect(self):
"""Connects to the server"""
self.connection.connect()
self.connection.connect(self.session.server_address, self.session.port)
def is_connected(self):
return self.connection.is_connected()

View File

@ -96,7 +96,6 @@ class TelegramBareClient:
# temporary connection. The connection on this one is always
# kept open so Telegram can send us updates.
self._sender = MtProtoSender(self.session, Connection(
self.session.server_address, self.session.port,
mode=connection_mode, proxy=proxy, timeout=timeout
))
@ -299,8 +298,8 @@ class TelegramBareClient:
self.session.auth_key = None # Force creating new auth_key
dc = self._get_dc(new_dc)
ip = dc.ip_address
self._sender.connection.ip = self.session.server_address = ip
self._sender.connection.port = self.session.port = dc.port
self.session.server_address = ip
self.session.port = dc.port
self.session.save()
return self.connect()