Skip to content

Commit 6dcdeda

Browse files
committed
Make proxy loading simpler
1 parent 9dc767b commit 6dcdeda

File tree

8 files changed

+27
-46
lines changed

8 files changed

+27
-46
lines changed

pyrogram/client/client.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,6 @@
5454
log = logging.getLogger(__name__)
5555

5656

57-
class Proxy:
58-
def __init__(self, enabled: bool, hostname: str, port: int, username: str, password: str):
59-
self.enabled = enabled
60-
self.hostname = hostname
61-
self.port = port
62-
self.username = username
63-
self.password = password
64-
65-
6657
class Client:
6758
"""This class represents a Client, the main mean for interacting with Telegram.
6859
It exposes bot-like methods for an easy access to the API as well as a simple way to
@@ -134,7 +125,7 @@ def __init__(self,
134125
session_name: str,
135126
api_id: int or str = None,
136127
api_hash: str = None,
137-
proxy: dict or Proxy = None,
128+
proxy: dict = None,
138129
test_mode: bool = False,
139130
phone_number: str = None,
140131
phone_code: str or callable = None,
@@ -830,22 +821,17 @@ def load_config(self):
830821
else:
831822
raise AttributeError("No API Key found")
832823

833-
if self.proxy is not None:
834-
self.proxy = Proxy(
835-
enabled=True,
836-
hostname=self.proxy["hostname"],
837-
port=int(self.proxy["port"]),
838-
username=self.proxy.get("username", None),
839-
password=self.proxy.get("password", None)
840-
)
841-
elif parser.has_section("proxy"):
842-
self.proxy = Proxy(
843-
enabled=parser.getboolean("proxy", "enabled"),
844-
hostname=parser.get("proxy", "hostname"),
845-
port=parser.getint("proxy", "port"),
846-
username=parser.get("proxy", "username", fallback=None) or None,
847-
password=parser.get("proxy", "password", fallback=None) or None
848-
)
824+
if self.proxy:
825+
pass
826+
else:
827+
self.proxy = {}
828+
829+
if parser.has_section("proxy"):
830+
self.proxy["enabled"] = parser.getboolean("proxy", "enabled")
831+
self.proxy["hostname"] = parser.get("proxy", "hostname")
832+
self.proxy["port"] = parser.getint("proxy", "port")
833+
self.proxy["username"] = parser.get("proxy", "username", fallback=None) or None
834+
self.proxy["password"] = parser.get("proxy", "password", fallback=None) or None
849835

850836
def load_session(self, session_name):
851837
try:

pyrogram/connection/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Connection:
3232
2: TCPIntermediate
3333
}
3434

35-
def __init__(self, address: tuple, proxy: type, mode: int = 1):
35+
def __init__(self, address: tuple, proxy: dict, mode: int = 1):
3636
self.address = address
3737
self.proxy = proxy
3838
self.mode = self.MODES.get(mode, TCPAbridged)

pyrogram/connection/transport/tcp/tcp.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import logging
2020
import socket
21-
from collections import namedtuple
2221

2322
try:
2423
import socks
@@ -32,29 +31,25 @@
3231

3332
log = logging.getLogger(__name__)
3433

35-
Proxy = namedtuple("Proxy", ["enabled", "hostname", "port", "username", "password"])
36-
3734

3835
class TCP(socks.socksocket):
39-
def __init__(self, proxy: Proxy):
36+
def __init__(self, proxy: dict):
4037
super().__init__()
4138
self.settimeout(10)
42-
self.proxy_enabled = False
43-
44-
if proxy and proxy.enabled:
45-
self.proxy_enabled = True
39+
self.proxy_enabled = proxy.get("enabled", False)
4640

41+
if proxy and self.proxy_enabled:
4742
self.set_proxy(
4843
proxy_type=socks.SOCKS5,
49-
addr=proxy.hostname,
50-
port=proxy.port,
51-
username=proxy.username,
52-
password=proxy.password
44+
addr=proxy["hostname"],
45+
port=proxy["port"],
46+
username=proxy["username"],
47+
password=proxy["password"]
5348
)
5449

5550
log.info("Using proxy {}:{}".format(
56-
proxy.hostname,
57-
proxy.port
51+
proxy["hostname"],
52+
proxy["port"]
5853
))
5954

6055
def close(self):

pyrogram/connection/transport/tcp/tcp_abridged.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
class TCPAbridged(TCP):
27-
def __init__(self, proxy: type):
27+
def __init__(self, proxy: dict):
2828
super().__init__(proxy)
2929
self.is_first_packet = None
3030

pyrogram/connection/transport/tcp/tcp_full.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727

2828
class TCPFull(TCP):
29-
def __init__(self, proxy: type):
29+
def __init__(self, proxy: dict):
3030
super().__init__(proxy)
3131
self.seq_no = None
3232

pyrogram/connection/transport/tcp/tcp_intermediate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626

2727
class TCPIntermediate(TCP):
28-
def __init__(self, proxy: type):
28+
def __init__(self, proxy: dict):
2929
super().__init__(proxy)
3030
self.is_first_packet = None
3131

pyrogram/session/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Auth:
4646
16
4747
)
4848

49-
def __init__(self, dc_id: int, test_mode: bool, proxy: type):
49+
def __init__(self, dc_id: int, test_mode: bool, proxy: dict):
5050
self.dc_id = dc_id
5151
self.test_mode = test_mode
5252

pyrogram/session/session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class Session:
8686
def __init__(self,
8787
dc_id: int,
8888
test_mode: bool,
89-
proxy: type,
89+
proxy: dict,
9090
auth_key: bytes,
9191
api_id: int,
9292
is_cdn: bool = False,

0 commit comments

Comments
 (0)