Skip to content

Commit 28abcaa

Browse files
committed
Fix ipv6 with ipv4 proxies
1 parent 6a89c7e commit 28abcaa

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

  • pyrogram/connection/transport/tcp

pyrogram/connection/transport/tcp/tcp.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,33 @@
3434

3535
class TCP(socks.socksocket):
3636
def __init__(self, ipv6: bool, proxy: dict):
37-
super().__init__(family=socket.AF_INET6 if ipv6 else socket.AF_INET)
37+
if proxy.get("enabled", False):
38+
hostname = proxy.get("hostname", None)
39+
port = proxy.get("port", None)
3840

39-
self.settimeout(10)
40-
self.proxy_enabled = proxy.get("enabled", False)
41+
try:
42+
socket.inet_aton(hostname)
43+
except socket.error:
44+
super().__init__(socket.AF_INET6)
45+
else:
46+
super().__init__(socket.AF_INET)
4147

42-
if proxy and self.proxy_enabled:
4348
self.set_proxy(
4449
proxy_type=socks.SOCKS5,
45-
addr=proxy.get("hostname", None),
46-
port=proxy.get("port", None),
50+
addr=hostname,
51+
port=port,
4752
username=proxy.get("username", None),
4853
password=proxy.get("password", None)
4954
)
5055

51-
log.info("Using proxy {}:{}".format(
52-
proxy.get("hostname", None),
53-
proxy.get("port", None)
54-
))
56+
log.info("Using proxy {}:{}".format(hostname, port))
57+
else:
58+
super().__init__(
59+
socket.AF_INET6 if ipv6
60+
else socket.AF_INET
61+
)
62+
63+
self.settimeout(10)
5564

5665
def close(self):
5766
try:

0 commit comments

Comments
 (0)