mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-07-10 16:12:22 +03:00
Replace python-proxy aiosocks
Although it supports a lot less features, aiosocks' code is much cleaner, and python-proxy has several asyncio-related issues. Furthermore, python-proxy author claims: > "pproxy is mostly used standalone" > (in https://github.com/qwj/python-proxy/issues/43)
This commit is contained in:
parent
0b69d7fd7b
commit
c1a40630a3
|
@ -107,7 +107,7 @@ Signing In behind a Proxy
|
||||||
=========================
|
=========================
|
||||||
|
|
||||||
If you need to use a proxy to access Telegram,
|
If you need to use a proxy to access Telegram,
|
||||||
you will need to `install python-proxy`__ and then change:
|
you will need to `install aiosocks`__ and then change:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
|
@ -117,20 +117,26 @@ with
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
TelegramClient('anon', api_id, api_hash, proxy='socks5://127.0.0.1:4444')
|
|
||||||
|
|
||||||
# You can also use a ``dict`` (it will be translated to the same URI as above)
|
|
||||||
TelegramClient('anon', api_id, api_hash, proxy={
|
TelegramClient('anon', api_id, api_hash, proxy={
|
||||||
'scheme': 'socks5',
|
'host': '127.0.0.1',
|
||||||
'hostname': '127.0.0.1',
|
|
||||||
'port': 4444
|
'port': 4444
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
(of course, replacing the IP and port with the IP and port of the proxy).
|
(of course, replacing the IP and port with the IP and port of the proxy).
|
||||||
|
|
||||||
The ``proxy=`` argument should be a tuple, a list or a dict,
|
The ``proxy=`` argument should be a dictionary
|
||||||
consisting of parameters described `in python-proxy usage`__.
|
where the following keys are allowed:
|
||||||
|
|
||||||
.. __: https://github.com/qwj/python-proxy#quickstart
|
.. code-block:: python
|
||||||
.. __: https://github.com/qwj/python-proxy#uri-syntax
|
|
||||||
|
proxy = {
|
||||||
|
'host': 'localhost', # (mandatory) proxy IP address
|
||||||
|
'port': 42252, # (mandatory) proxy port number
|
||||||
|
'protocol': 'socks5', # (optional) protocol to use, default socks5, allowed values: socks5, socks4
|
||||||
|
'username': 'foo', # (optional) username if the proxy requires auth
|
||||||
|
'password': 'bar', # (optional) password if the proxy requires auth
|
||||||
|
'remote_resolve': True # (optional) whether to use remote or local resolve, default remote
|
||||||
|
}
|
||||||
|
|
||||||
|
.. __: https://github.com/nibrag/aiosocks
|
||||||
|
|
|
@ -67,11 +67,10 @@ class TelegramBaseClient(abc.ABC):
|
||||||
By default this is ``False`` as IPv6 support is not
|
By default this is ``False`` as IPv6 support is not
|
||||||
too widespread yet.
|
too widespread yet.
|
||||||
|
|
||||||
proxy (`str` | `dict`, optional):
|
proxy (`dict`, optional):
|
||||||
The URI with all the required proxy information, or a dictionary
|
A dictionary with information about the proxy to connect to.
|
||||||
with the fields to use (like ``schemepython``, ``hostname``, etc.).
|
|
||||||
|
|
||||||
See https://github.com/qwj/python-proxy#uri-syntax for details.
|
See :ref:`signing-in` for details.
|
||||||
|
|
||||||
timeout (`int` | `float`, optional):
|
timeout (`int` | `float`, optional):
|
||||||
The timeout in seconds to be used when connecting.
|
The timeout in seconds to be used when connecting.
|
||||||
|
@ -253,7 +252,7 @@ class TelegramBaseClient(abc.ABC):
|
||||||
self._request_retries = request_retries
|
self._request_retries = request_retries
|
||||||
self._connection_retries = connection_retries
|
self._connection_retries = connection_retries
|
||||||
self._retry_delay = retry_delay or 0
|
self._retry_delay = retry_delay or 0
|
||||||
self._proxy = helpers._get_proxy_uri(proxy)
|
self._proxy = proxy
|
||||||
self._timeout = timeout
|
self._timeout = timeout
|
||||||
self._auto_reconnect = auto_reconnect
|
self._auto_reconnect = auto_reconnect
|
||||||
|
|
||||||
|
|
|
@ -131,43 +131,6 @@ def _sync_exit(self, *args):
|
||||||
return loop.run_until_complete(self.__aexit__(*args))
|
return loop.run_until_complete(self.__aexit__(*args))
|
||||||
|
|
||||||
|
|
||||||
def _get_proxy_uri(proxy):
|
|
||||||
if not proxy:
|
|
||||||
return None
|
|
||||||
elif isinstance(proxy, str):
|
|
||||||
return proxy
|
|
||||||
elif isinstance(proxy, dict):
|
|
||||||
fmt = {
|
|
||||||
'scheme': 'socks5',
|
|
||||||
'cipher': '',
|
|
||||||
'netloc': '',
|
|
||||||
'localbind': '',
|
|
||||||
'plugins': '',
|
|
||||||
'rules': '',
|
|
||||||
'auth': ''
|
|
||||||
}
|
|
||||||
fmt.update(proxy)
|
|
||||||
if 'hostname' in fmt:
|
|
||||||
fmt['netloc'] = '{}:{}'.format(fmt.pop('hostname'), fmt.pop('port'))
|
|
||||||
if 'username' in fmt:
|
|
||||||
fmt['auth'] = '{}:{}'.format(fmt.pop('username'), fmt.pop('password'))
|
|
||||||
|
|
||||||
fix = {
|
|
||||||
'cipher': '{}@',
|
|
||||||
'localbind': '@{}',
|
|
||||||
'plugins': ',{}',
|
|
||||||
'rules': '?{}',
|
|
||||||
'auth': '#{}'
|
|
||||||
}
|
|
||||||
for k, v in fix.items():
|
|
||||||
if fmt[k]:
|
|
||||||
fmt[k] = v.format(fmt[k])
|
|
||||||
|
|
||||||
return '{scheme}://{cipher}{netloc}/{localbind}{plugins}{rules}{auth}'.format(**fmt)
|
|
||||||
else:
|
|
||||||
raise TypeError('{!r} is not a valid proxy (must be str URI or dict)'.format(proxy))
|
|
||||||
|
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
# region Cryptographic related utils
|
# region Cryptographic related utils
|
||||||
|
|
|
@ -46,11 +46,29 @@ class Connection(abc.ABC):
|
||||||
connect_coroutine = asyncio.open_connection(
|
connect_coroutine = asyncio.open_connection(
|
||||||
self._ip, self._port, loop=self._loop, ssl=ssl)
|
self._ip, self._port, loop=self._loop, ssl=ssl)
|
||||||
else:
|
else:
|
||||||
import pproxy
|
import aiosocks
|
||||||
|
|
||||||
# FIXME https://github.com/qwj/python-proxy/issues/41
|
auth = None
|
||||||
connect_coroutine = pproxy.Connection(
|
proto = self._proxy.get('protocol', 'socks5').lower()
|
||||||
self._proxy).tcp_connect(self._ip, self._port)
|
if proto == 'socks5':
|
||||||
|
proxy = aiosocks.Socks5Addr(self._proxy['host'], self._proxy['port'])
|
||||||
|
if 'username' in self._proxy:
|
||||||
|
auth = aiosocks.Socks5Auth(self._proxy['username'], self._proxy['password'])
|
||||||
|
|
||||||
|
elif proto == 'socks4':
|
||||||
|
proxy = aiosocks.Socks4Addr(self._proxy['host'], self._proxy['port'])
|
||||||
|
if 'username' in self._proxy:
|
||||||
|
auth = aiosocks.Socks4Auth(self._proxy['username'])
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise ValueError('Unsupported proxy protocol {}'.format(self._proxy['protocol']))
|
||||||
|
|
||||||
|
connect_coroutine = aiosocks.open_connection(
|
||||||
|
proxy=proxy,
|
||||||
|
proxy_auth=auth,
|
||||||
|
dst=(self._ip, self._port),
|
||||||
|
remote_resolve=self._proxy.get('remote_resolve', True)
|
||||||
|
)
|
||||||
|
|
||||||
self._reader, self._writer = await asyncio.wait_for(
|
self._reader, self._writer = await asyncio.wait_for(
|
||||||
connect_coroutine,
|
connect_coroutine,
|
||||||
|
|
|
@ -22,7 +22,7 @@ def get_env(name, message, cast=str):
|
||||||
session = os.environ.get('TG_SESSION', 'printer')
|
session = os.environ.get('TG_SESSION', 'printer')
|
||||||
api_id = get_env('TG_API_ID', 'Enter your API ID: ', int)
|
api_id = get_env('TG_API_ID', 'Enter your API ID: ', int)
|
||||||
api_hash = get_env('TG_API_HASH', 'Enter your API hash: ')
|
api_hash = get_env('TG_API_HASH', 'Enter your API hash: ')
|
||||||
proxy = None # https://github.com/qwj/python-proxy#uri-syntax
|
proxy = None # https://docs.telethon.dev/en/latest/basic/signing-in.html#signing-in-behind-a-proxy
|
||||||
|
|
||||||
# Create and start the client so we can make requests (we don't here)
|
# Create and start the client so we can make requests (we don't here)
|
||||||
client = TelegramClient(session, api_id, api_hash, proxy=proxy).start()
|
client = TelegramClient(session, api_id, api_hash, proxy=proxy).start()
|
||||||
|
|
|
@ -27,7 +27,7 @@ def get_env(name, message, cast=str):
|
||||||
session = os.environ.get('TG_SESSION', 'printer')
|
session = os.environ.get('TG_SESSION', 'printer')
|
||||||
api_id = get_env('TG_API_ID', 'Enter your API ID: ', int)
|
api_id = get_env('TG_API_ID', 'Enter your API ID: ', int)
|
||||||
api_hash = get_env('TG_API_HASH', 'Enter your API hash: ')
|
api_hash = get_env('TG_API_HASH', 'Enter your API hash: ')
|
||||||
proxy = None # https://github.com/qwj/python-proxy#uri-syntax
|
proxy = None # https://docs.telethon.dev/en/latest/basic/signing-in.html#signing-in-behind-a-proxy
|
||||||
|
|
||||||
|
|
||||||
# This is our update handler. It is called when a new update arrives.
|
# This is our update handler. It is called when a new update arrives.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user