diff --git a/telethon/_client/telegrambaseclient.py b/telethon/_client/telegrambaseclient.py index 1add1abb..eb8b6cd3 100644 --- a/telethon/_client/telegrambaseclient.py +++ b/telethon/_client/telegrambaseclient.py @@ -323,7 +323,7 @@ async def connect(self: 'TelegramClient') -> None: self._sender.auth_key.key = dc.auth if not await self._sender.connect(self._connection( - str(ipaddress.ip_address(dc.ipv6 or dc.ipv4)), + str(ipaddress.ip_address((self._use_ipv6 and dc.ipv6) or dc.ipv4)), dc.port, dc.id, loggers=self._log, diff --git a/telethon/sessions/sqlite.py b/telethon/sessions/sqlite.py index 5bfc0433..00cc3895 100644 --- a/telethon/sessions/sqlite.py +++ b/telethon/sessions/sqlite.py @@ -107,8 +107,8 @@ class SQLiteSession(Session): if old == 7: self._mk_tables(c) c.execute(''' - insert into datacenter (id, ip, port, auth) - select dc_id, server_address, port, auth_key + insert into datacenter (id, ipv4, ipv6, port, auth) + select dc_id, server_address, server_address, port, auth_key from sessions ''') c.execute(''' @@ -152,7 +152,8 @@ class SQLiteSession(Session): c, '''datacenter ( id integer primary key, - ip text not null, + ipv4 text not null, + ipv6 text, port integer not null, auth blob not null )''', @@ -179,9 +180,10 @@ class SQLiteSession(Session): async def insert_dc(self, dc: DataCenter): self._execute( - 'insert or replace into datacenter values (?,?,?,?)', + 'insert or replace into datacenter values (?,?,?,?,?)', dc.id, - str(ipaddress.ip_address(dc.ipv6 or dc.ipv4)), + str(ipaddress.ip_address(dc.ipv4)), + str(ipaddress.ip_address(dc.ipv6)) if dc.ipv6 else None, dc.port, dc.auth ) @@ -189,12 +191,11 @@ class SQLiteSession(Session): async def get_all_dc(self) -> List[DataCenter]: c = self._cursor() res = [] - for (id, ip, port, auth) in c.execute('select * from datacenter'): - ip = ipaddress.ip_address(ip) + for (id, ipv4, ipv6, port, auth) in c.execute('select * from datacenter'): res.append(DataCenter( id=id, - ipv4=int(ip) if ip.version == 4 else None, - ipv6=int(ip) if ip.version == 6 else None, + ipv4=int(ipaddress.ip_address(ipv4)), + ipv6=int(ipaddress.ip_address(ipv6)) if ipv6 else None, port=port, auth=auth, )) diff --git a/telethon/sessions/types.py b/telethon/sessions/types.py index 39033a1f..5fb0d608 100644 --- a/telethon/sessions/types.py +++ b/telethon/sessions/types.py @@ -15,7 +15,7 @@ class DataCenter: def __init__( self, id: int, - ipv4: Optional[int], + ipv4: int, ipv6: Optional[int], port: int, auth: bytes