mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-29 05:18:10 +00:00
Make proxy loading simpler
This commit is contained in:
parent
9dc767b88e
commit
6dcdeda244
@ -54,15 +54,6 @@ from .style import Markdown, HTML
|
|||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Proxy:
|
|
||||||
def __init__(self, enabled: bool, hostname: str, port: int, username: str, password: str):
|
|
||||||
self.enabled = enabled
|
|
||||||
self.hostname = hostname
|
|
||||||
self.port = port
|
|
||||||
self.username = username
|
|
||||||
self.password = password
|
|
||||||
|
|
||||||
|
|
||||||
class Client:
|
class Client:
|
||||||
"""This class represents a Client, the main mean for interacting with Telegram.
|
"""This class represents a Client, the main mean for interacting with Telegram.
|
||||||
It exposes bot-like methods for an easy access to the API as well as a simple way to
|
It exposes bot-like methods for an easy access to the API as well as a simple way to
|
||||||
@ -134,7 +125,7 @@ class Client:
|
|||||||
session_name: str,
|
session_name: str,
|
||||||
api_id: int or str = None,
|
api_id: int or str = None,
|
||||||
api_hash: str = None,
|
api_hash: str = None,
|
||||||
proxy: dict or Proxy = None,
|
proxy: dict = None,
|
||||||
test_mode: bool = False,
|
test_mode: bool = False,
|
||||||
phone_number: str = None,
|
phone_number: str = None,
|
||||||
phone_code: str or callable = None,
|
phone_code: str or callable = None,
|
||||||
@ -830,22 +821,17 @@ class Client:
|
|||||||
else:
|
else:
|
||||||
raise AttributeError("No API Key found")
|
raise AttributeError("No API Key found")
|
||||||
|
|
||||||
if self.proxy is not None:
|
if self.proxy:
|
||||||
self.proxy = Proxy(
|
pass
|
||||||
enabled=True,
|
else:
|
||||||
hostname=self.proxy["hostname"],
|
self.proxy = {}
|
||||||
port=int(self.proxy["port"]),
|
|
||||||
username=self.proxy.get("username", None),
|
if parser.has_section("proxy"):
|
||||||
password=self.proxy.get("password", None)
|
self.proxy["enabled"] = parser.getboolean("proxy", "enabled")
|
||||||
)
|
self.proxy["hostname"] = parser.get("proxy", "hostname")
|
||||||
elif parser.has_section("proxy"):
|
self.proxy["port"] = parser.getint("proxy", "port")
|
||||||
self.proxy = Proxy(
|
self.proxy["username"] = parser.get("proxy", "username", fallback=None) or None
|
||||||
enabled=parser.getboolean("proxy", "enabled"),
|
self.proxy["password"] = parser.get("proxy", "password", fallback=None) or None
|
||||||
hostname=parser.get("proxy", "hostname"),
|
|
||||||
port=parser.getint("proxy", "port"),
|
|
||||||
username=parser.get("proxy", "username", fallback=None) or None,
|
|
||||||
password=parser.get("proxy", "password", fallback=None) or None
|
|
||||||
)
|
|
||||||
|
|
||||||
def load_session(self, session_name):
|
def load_session(self, session_name):
|
||||||
try:
|
try:
|
||||||
|
@ -32,7 +32,7 @@ class Connection:
|
|||||||
2: TCPIntermediate
|
2: TCPIntermediate
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, address: tuple, proxy: type, mode: int = 1):
|
def __init__(self, address: tuple, proxy: dict, mode: int = 1):
|
||||||
self.address = address
|
self.address = address
|
||||||
self.proxy = proxy
|
self.proxy = proxy
|
||||||
self.mode = self.MODES.get(mode, TCPAbridged)
|
self.mode = self.MODES.get(mode, TCPAbridged)
|
||||||
|
@ -18,7 +18,6 @@
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
import socket
|
import socket
|
||||||
from collections import namedtuple
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import socks
|
import socks
|
||||||
@ -32,29 +31,25 @@ except ImportError as e:
|
|||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
Proxy = namedtuple("Proxy", ["enabled", "hostname", "port", "username", "password"])
|
|
||||||
|
|
||||||
|
|
||||||
class TCP(socks.socksocket):
|
class TCP(socks.socksocket):
|
||||||
def __init__(self, proxy: Proxy):
|
def __init__(self, proxy: dict):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.settimeout(10)
|
self.settimeout(10)
|
||||||
self.proxy_enabled = False
|
self.proxy_enabled = proxy.get("enabled", False)
|
||||||
|
|
||||||
if proxy and proxy.enabled:
|
|
||||||
self.proxy_enabled = True
|
|
||||||
|
|
||||||
|
if proxy and self.proxy_enabled:
|
||||||
self.set_proxy(
|
self.set_proxy(
|
||||||
proxy_type=socks.SOCKS5,
|
proxy_type=socks.SOCKS5,
|
||||||
addr=proxy.hostname,
|
addr=proxy["hostname"],
|
||||||
port=proxy.port,
|
port=proxy["port"],
|
||||||
username=proxy.username,
|
username=proxy["username"],
|
||||||
password=proxy.password
|
password=proxy["password"]
|
||||||
)
|
)
|
||||||
|
|
||||||
log.info("Using proxy {}:{}".format(
|
log.info("Using proxy {}:{}".format(
|
||||||
proxy.hostname,
|
proxy["hostname"],
|
||||||
proxy.port
|
proxy["port"]
|
||||||
))
|
))
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
@ -24,7 +24,7 @@ log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class TCPAbridged(TCP):
|
class TCPAbridged(TCP):
|
||||||
def __init__(self, proxy: type):
|
def __init__(self, proxy: dict):
|
||||||
super().__init__(proxy)
|
super().__init__(proxy)
|
||||||
self.is_first_packet = None
|
self.is_first_packet = None
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class TCPFull(TCP):
|
class TCPFull(TCP):
|
||||||
def __init__(self, proxy: type):
|
def __init__(self, proxy: dict):
|
||||||
super().__init__(proxy)
|
super().__init__(proxy)
|
||||||
self.seq_no = None
|
self.seq_no = None
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class TCPIntermediate(TCP):
|
class TCPIntermediate(TCP):
|
||||||
def __init__(self, proxy: type):
|
def __init__(self, proxy: dict):
|
||||||
super().__init__(proxy)
|
super().__init__(proxy)
|
||||||
self.is_first_packet = None
|
self.is_first_packet = None
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ class Auth:
|
|||||||
16
|
16
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self, dc_id: int, test_mode: bool, proxy: type):
|
def __init__(self, dc_id: int, test_mode: bool, proxy: dict):
|
||||||
self.dc_id = dc_id
|
self.dc_id = dc_id
|
||||||
self.test_mode = test_mode
|
self.test_mode = test_mode
|
||||||
|
|
||||||
|
@ -86,7 +86,7 @@ class Session:
|
|||||||
def __init__(self,
|
def __init__(self,
|
||||||
dc_id: int,
|
dc_id: int,
|
||||||
test_mode: bool,
|
test_mode: bool,
|
||||||
proxy: type,
|
proxy: dict,
|
||||||
auth_key: bytes,
|
auth_key: bytes,
|
||||||
api_id: int,
|
api_id: int,
|
||||||
is_cdn: bool = False,
|
is_cdn: bool = False,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user