Skip to content

Commit d5fc12f

Browse files
committed
Added shortcut function proxy_connector for automatically create connector (socks or http proxy)
1 parent 485b906 commit d5fc12f

File tree

6 files changed

+66
-9
lines changed

6 files changed

+66
-9
lines changed

README.rst

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,29 @@ aiohttp usage
114114
import asyncio
115115
import aiohttp
116116
import aiosocks
117-
from aiosocks.connector import SocksConnector
117+
from aiosocks.connector import SocksConnector, proxy_connector
118118
119119
120120
async def load_github_main():
121121
addr = aiosocks.Socks5Addr('127.0.0.1', 1080)
122122
auth = aiosocks.Socks5Auth('proxyuser1', password='pwd')
123123
124124
# remote resolve
125-
# conn = SocksConnector(proxy=addr, proxy_auth=auth, remote_resolve=True)
125+
conn = SocksConnector(proxy=addr, proxy_auth=auth, remote_resolve=True)
126126
127127
# or locale resolve
128128
conn = SocksConnector(proxy=addr, proxy_auth=auth, remote_resolve=False)
129129
130+
# or use shortcut function for automatically create
131+
# SocksConnector/aiohttp.ProxyConnector (socks or http proxy)
132+
conn = proxy_connector(aiosocks.SocksAddr(...),
133+
remote_resolve=True, verify_ssl=False)
134+
# return SocksConnector
135+
136+
conn = proxy_connector(aiosocks.HttpProxyAddr('http://proxy'),
137+
aiosocks.HttpProxyAuth('login', 'pwd'))
138+
# return aiohttp.ProxyConnector (http proxy connector)
139+
130140
try:
131141
with aiohttp.ClientSession(connector=conn) as ses:
132142
async with session.get('http://github.com/') as resp:

aiosocks/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44
SocksConnectionError, InvalidServerReply, InvalidServerVersion
55
)
66
from .helpers import (
7-
SocksAddr, Socks4Addr, Socks5Addr, Socks4Auth, Socks5Auth
7+
SocksAddr, Socks4Addr, Socks5Addr, Socks4Auth,
8+
Socks5Auth, HttpProxyAddr, HttpProxyAuth
89
)
910
from .protocols import Socks4Protocol, Socks5Protocol, DEFAULT_LIMIT
1011

1112
__version__ = '0.1.4'
1213

1314
__all__ = ('Socks4Protocol', 'Socks5Protocol', 'Socks4Auth',
14-
'Socks5Auth', 'Socks4Addr', 'Socks5Addr', 'SocksError',
15-
'NoAcceptableAuthMethods', 'LoginAuthenticationFailed',
16-
'SocksConnectionError', 'InvalidServerVersion',
17-
'InvalidServerReply', 'create_connection', 'open_connection')
15+
'Socks5Auth', 'Socks4Addr', 'Socks5Addr', 'HttpProxyAddr',
16+
'HttpProxyAuth', 'SocksError', 'NoAcceptableAuthMethods',
17+
'LoginAuthenticationFailed', 'SocksConnectionError',
18+
'InvalidServerVersion', 'InvalidServerReply',
19+
'create_connection', 'open_connection')
1820

1921

2022
@asyncio.coroutine

aiosocks/connector.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import ipaddress
55
from aiohttp.errors import ProxyConnectionError
66
from .errors import SocksError, SocksConnectionError
7+
from .helpers import HttpProxyAddr, SocksAddr
78
from . import create_connection
89

910
__all__ = ('SocksConnector',)
@@ -108,3 +109,13 @@ def _create_connection(self, req):
108109
raise aiohttp.ClientOSError(
109110
exc.errno, 'Can not connect to %s:%s [%s]' %
110111
(req.host, req.port, exc.strerror)) from exc
112+
113+
114+
def proxy_connector(proxy, proxy_auth=None, **kwargs):
115+
if isinstance(proxy, HttpProxyAddr):
116+
return aiohttp.ProxyConnector(
117+
proxy.url, proxy_auth=proxy_auth, **kwargs)
118+
elif isinstance(proxy, SocksAddr):
119+
return SocksConnector(proxy, proxy_auth, **kwargs)
120+
else:
121+
raise ValueError('Unsupported `proxy` format')

aiosocks/helpers.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from collections import namedtuple
2+
from aiohttp.helpers import BasicAuth as HttpProxyAuth
23

3-
__all__ = ('Socks4Auth', 'Socks5Auth', 'Socks4Addr', 'Socks5Addr', 'SocksAddr')
4+
__all__ = ('Socks4Auth', 'Socks5Auth', 'Socks4Addr', 'Socks5Addr', 'SocksAddr',
5+
'HttpProxyAddr', 'HttpProxyAuth')
46

57

68
class Socks4Auth(namedtuple('Socks4Auth', ['login', 'encoding'])):
@@ -41,3 +43,10 @@ class Socks4Addr(SocksAddr):
4143

4244
class Socks5Addr(SocksAddr):
4345
pass
46+
47+
48+
class HttpProxyAddr(namedtuple('HttpProxyAddr', ['url'])):
49+
def __new__(cls, url):
50+
if url is None:
51+
raise ValueError('None is not allowed as url value')
52+
return super().__new__(cls, url)

tests/test_connector.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
import asyncio
33
import aiosocks
44
import aiohttp
5+
import pytest
56
from unittest import mock
67
from aiohttp.client_reqrep import ClientRequest
7-
from aiosocks.connector import SocksConnector
8+
from aiosocks.connector import SocksConnector, proxy_connector
89
from .helpers import fake_coroutine
910

1011

@@ -131,3 +132,19 @@ def test_proxy_negotiate_fail(self, cr_conn_mock):
131132

132133
with self.assertRaises(aiosocks.SocksError):
133134
self.loop.run_until_complete(connector.connect(req))
135+
136+
137+
def test_proxy_connector():
138+
socks4_addr = aiosocks.Socks4Addr('h')
139+
socks5_addr = aiosocks.Socks5Addr('h')
140+
http_addr = aiosocks.HttpProxyAddr('http://proxy')
141+
142+
loop = asyncio.new_event_loop()
143+
144+
assert isinstance(proxy_connector(socks4_addr, loop=loop), SocksConnector)
145+
assert isinstance(proxy_connector(socks5_addr, loop=loop), SocksConnector)
146+
assert isinstance(proxy_connector(http_addr, loop=loop),
147+
aiohttp.ProxyConnector)
148+
149+
with pytest.raises(ValueError):
150+
proxy_connector(None)

tests/test_helpers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,11 @@ def test_socks5_addr4():
8383
addr = aiosocks.Socks5Addr('localhost', None)
8484
assert addr.host == 'localhost'
8585
assert addr.port == 1080
86+
87+
88+
def test_http_proxy_addr():
89+
addr = aiosocks.HttpProxyAddr('http://proxy')
90+
assert addr.url == 'http://proxy'
91+
92+
with pytest.raises(ValueError):
93+
aiosocks.HttpProxyAddr(None)

0 commit comments

Comments
 (0)