diff --git a/README.rst b/README.rst index 14557bd7..3d3190ef 100755 --- a/README.rst +++ b/README.rst @@ -171,3 +171,19 @@ and replacing the one you can find in this same directory by the updated one. Don't forget to run ``python3 tl_generator.py``. If the changes weren't too big, everything should still work the same way as it did before; but with extra features. + +Using proxy +----------- +If you want to use Telethon via proxy, you have to install +`PySocks (via pip or manual) `_ +and pass proxy settings to ``TelegramClient()`` like + +.. code:: python + + >>> from telethon import InteractiveTelegramClient + >>> import socks + >>> client = InteractiveTelegramClient('sessionid', '+34600000000', + ... api_id=12345, api_hash='0123456789abcdef0123456789abcdef', proxy=(socks.SOCKS5, "localhost", 4444)) + +``proxy`` accept a tuple, which contains exactly the same parameters as +`what a set_proxy method accepts `_. diff --git a/telethon/interactive_telegram_client.py b/telethon/interactive_telegram_client.py index c1b73c78..46efa01e 100644 --- a/telethon/interactive_telegram_client.py +++ b/telethon/interactive_telegram_client.py @@ -30,11 +30,11 @@ def bytes_to_string(byte_count): class InteractiveTelegramClient(TelegramClient): - def __init__(self, session_user_id, user_phone, api_id, api_hash): + def __init__(self, session_user_id, user_phone, api_id, api_hash, proxy=None): print_title('Initialization') print('Initializing interactive example...') - super().__init__(session_user_id, api_id, api_hash) + super().__init__(session_user_id, api_id, api_hash, proxy) # Store all the found media in memory here, # so it can be downloaded if the user wants diff --git a/telethon/network/tcp_client.py b/telethon/network/tcp_client.py index 9ff2e99e..2bc9fc9d 100755 --- a/telethon/network/tcp_client.py +++ b/telethon/network/tcp_client.py @@ -9,9 +9,20 @@ from telethon.utils import BinaryWriter class TcpClient: - def __init__(self): + def __init__(self, proxy=None): self.connected = False - self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + if proxy: + try: + import socks + self.socket = socks.socksocket(socket.AF_INET, socket.SOCK_STREAM) + self.socket.set_proxy(*proxy) + except (ImportError, SystemError): + print("Can't import PySocks, fallback to vanilla socket. " + "Proxy settings are ignored. " + "Try to install PySocks via pip") + self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + else: + self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Support for multi-threading advantages and safety self.cancelled = False # Has the read operation been cancelled? diff --git a/telethon/network/tcp_transport.py b/telethon/network/tcp_transport.py index ede0c151..2291d828 100755 --- a/telethon/network/tcp_transport.py +++ b/telethon/network/tcp_transport.py @@ -7,8 +7,8 @@ from telethon.utils import BinaryWriter class TcpTransport: - def __init__(self, ip_address, port): - self.tcp_client = TcpClient() + def __init__(self, ip_address, port, proxy=None): + self.tcp_client = TcpClient(proxy) self.send_counter = 0 self.tcp_client.connect(ip_address, port) diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index 812c55da..fa88687e 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -45,7 +45,7 @@ class TelegramClient: # region Initialization - def __init__(self, session, api_id, api_hash): + def __init__(self, session, api_id, api_hash, proxy=None): """Initializes the Telegram client with the specified API ID and Hash. Session can either be a `str` object (the filename for the loaded/saved .session) @@ -72,7 +72,7 @@ class TelegramClient: 'The given session must either be a string or a Session instance.') self.transport = TcpTransport(self.session.server_address, - self.session.port) + self.session.port, proxy) # These will be set later self.dc_options = None