Change signature of _parse_proxy() to maintain

backwards compatibility with PySocks format.
This commit is contained in:
Serhii Dylda 2020-11-08 16:12:07 +01:00
parent e4eaecb1cd
commit 795e5c7854
2 changed files with 24 additions and 16 deletions

View File

@ -136,23 +136,31 @@ consisting of parameters described `in aiosocks usage`__.
Example:
* .. code-block:: python
* All of these are equal:
proxy = ('socks5', '1.1.1.1', 5555, 'foo', 'bar', True)
.. code-block:: python
* .. code-block:: python
proxy = ('socks5', '1.1.1.1', 5555, True, 'foo', 'bar')
proxy = (socks.SOCKS5, '1.1.1.1', 5555, True, 'foo', 'bar')
proxy = (2, '1.1.1.1', 5555, True, 'foo', 'bar')
proxy = ['socks5', '1.1.1.1', 5555, 'foo', 'bar', True]
* All of these are equal:
.. code-block:: python
proxy = ['socks5', '1.1.1.1', 5555, True, 'foo', 'bar']
proxy = [socks.SOCKS5, '1.1.1.1', 5555, True, 'foo', 'bar']
proxy = [2, '1.1.1.1', 5555, True, 'foo', 'bar']
* .. code-block:: python
proxy = {
'protocol': 'socks5', # (mandatory) protocol to use, default socks5, allowed values: 'socks5' (or 2), 'socks4' (or 1)
'host': '1.1.1.1', # (mandatory) proxy IP address
'proxy_type': 'socks5', # (mandatory) protocol to use, allowed values: 'socks5' (or 2), 'socks4' (or 1)
'addr': '1.1.1.1', # (mandatory) proxy IP address
'port': 5555, # (mandatory) proxy port number
'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
'rdns': True # (optional) whether to use remote or local resolve, default remote
}
.. __: https://github.com/nibrag/aiosocks#installation

View File

@ -104,30 +104,30 @@ class Connection(abc.ABC):
await self._writer.drain()
@staticmethod
def _parse_proxy(protocol, host, port, username=None, password=None, remote_resolve=True):
def _parse_proxy(proxy_type, addr, port, rdns=True, username=None, password=None):
proxy, auth = None, None
if isinstance(protocol, str):
protocol = protocol.lower()
if isinstance(proxy_type, str):
proxy_type = proxy_type.lower()
# We do the check for numerical values here
# to be backwards compatible with PySocks proxy format,
# (since socks.SOCKS5 = 2 and socks.SOCKS4 = 1)
if protocol == 'socks5' or protocol == 2:
proxy = aiosocks.Socks5Addr(host, port)
if proxy_type == 'socks5' or proxy_type == 2:
proxy = aiosocks.Socks5Addr(addr, port)
if username and password:
auth = aiosocks.Socks5Auth(username, password)
elif protocol == 'socks4' or protocol == 1:
proxy = aiosocks.Socks4Addr(host, port)
elif proxy_type == 'socks4' or proxy_type == 1:
proxy = aiosocks.Socks4Addr(addr, port)
if username:
auth = aiosocks.Socks4Auth(username)
else:
raise ValueError('Unsupported proxy protocol {}'.format(protocol))
raise ValueError('Unsupported proxy protocol {}'.format(proxy_type))
return proxy, auth, remote_resolve
return proxy, auth, rdns
async def connect(self, timeout=None, ssl=None):
"""