@@ -9,12 +9,12 @@ python 3.5+ | |||
``` | |||
import asyncio | |||
from aiosocks import ( | |||
Socks4Server, Socks5Server, Socks4Auth, Socks5Auth, create_connection | |||
Socks4Addr, Socks5Addr, Socks4Auth, Socks5Auth, create_connection | |||
) | |||
async def connect(): | |||
socks5_serv = Socks5Server('127.0.0.1', 1080) | |||
socks4_serv = Socks4Server('127.0.0.1', 1080) | |||
socks5_serv = Socks5Addr('127.0.0.1', 1080) | |||
socks4_serv = Socks4Addr('127.0.0.1', 1080) | |||
socks5_auth = Socks5Auth('login', 'pwd') | |||
socks4_auth = Socks4Auth('ident') | |||
@@ -42,11 +42,11 @@ if __name__ == '__main__': | |||
``` | |||
import asyncio | |||
import aiohttp | |||
from aiosocks import Socks5Server, Socks5Auth | |||
from aiosocks import Socks5Addr, Socks5Auth | |||
from aiosocks.connector import SocksConnector | |||
async def load_github_main(): | |||
serv = Socks5Server('127.0.0.1', 1080) | |||
serv = Socks5Addr('127.0.0.1', 1080) | |||
auth = Socks5Auth('proxyuser1', password='pwd') | |||
conn = SocksConnector(proxy=serv, proxy_auth=auth, remote_resolve=False) | |||
@@ -6,7 +6,7 @@ from .protocols import Socks4Protocol, Socks5Protocol | |||
__version__ = '0.1a' | |||
__all__ = ('Socks4Protocol', 'Socks5Protocol', 'Socks4Auth', | |||
'Socks5Auth', 'Socks4Server', 'Socks5Server', 'SocksError', | |||
'Socks5Auth', 'Socks4Addr', 'Socks5Addr', 'SocksError', | |||
'NoAcceptableAuthMethods', 'LoginAuthenticationFailed', | |||
'InvalidServerVersion', 'InvalidServerReply', 'create_connection') | |||
@@ -15,8 +15,8 @@ async def create_connection(protocol_factory, proxy, proxy_auth, dst, *, remote_ | |||
loop=None, ssl=None, family=0, proto=0, flags=0, sock=None, | |||
local_addr=None, server_hostname=None): | |||
assert isinstance(proxy, SocksServer), ( | |||
'proxy must be Socks4Server() or Socks5Server() tuple' | |||
assert isinstance(proxy, SocksAddr), ( | |||
'proxy must be Socks4Addr() or Socks5Addr() tuple' | |||
) | |||
assert proxy_auth is None or isinstance(proxy_auth, (Socks4Auth, Socks5Auth)), ( | |||
@@ -26,18 +26,18 @@ async def create_connection(protocol_factory, proxy, proxy_auth, dst, *, remote_ | |||
'invalid dst format, tuple("dst_host", dst_port))' | |||
) | |||
if (isinstance(proxy, Socks4Server) and not | |||
if (isinstance(proxy, Socks4Addr) and not | |||
(proxy_auth is None or isinstance(proxy_auth, Socks4Auth))): | |||
raise ValueError("proxy is Socks4Server but proxy_auth is not Socks4Auth") | |||
raise ValueError("proxy is Socks4Addr but proxy_auth is not Socks4Auth") | |||
if (isinstance(proxy, Socks5Server) and not | |||
if (isinstance(proxy, Socks5Addr) and not | |||
(proxy_auth is None or isinstance(proxy_auth, Socks5Auth))): | |||
raise ValueError("proxy is Socks5Server but proxy_auth is not Socks5Auth") | |||
raise ValueError("proxy is Socks5Addr but proxy_auth is not Socks5Auth") | |||
loop = loop or asyncio.get_event_loop() | |||
def socks_factory(): | |||
if isinstance(proxy, Socks4Server): | |||
if isinstance(proxy, Socks4Addr): | |||
socks_proto = Socks4Protocol | |||
else: | |||
socks_proto = Socks5Protocol | |||
@@ -1,6 +1,6 @@ | |||
from collections import namedtuple | |||
__all__ = ('Socks4Auth', 'Socks5Auth', 'Socks4Server', 'Socks5Server', 'SocksServer') | |||
__all__ = ('Socks4Auth', 'Socks5Auth', 'Socks4Addr', 'Socks5Addr', 'SocksAddr') | |||
class Socks4Auth(namedtuple('Socks4Auth', ['login', 'encoding'])): | |||
@@ -24,7 +24,7 @@ class Socks5Auth(namedtuple('Socks5Auth', ['login', 'password', 'encoding'])): | |||
password.encode(encoding), encoding) | |||
class SocksServer(namedtuple('SocksServer', ['host', 'port'])): | |||
class SocksAddr(namedtuple('SocksServer', ['host', 'port'])): | |||
def __new__(cls, host, port): | |||
if host is None: | |||
raise ValueError('None is not allowed as host value') | |||
@@ -35,9 +35,9 @@ class SocksServer(namedtuple('SocksServer', ['host', 'port'])): | |||
return super().__new__(cls, host, port) | |||
class Socks4Server(SocksServer): | |||
class Socks4Addr(SocksAddr): | |||
pass | |||
class Socks5Server(SocksServer): | |||
class Socks5Addr(SocksAddr): | |||
pass |
@@ -3,7 +3,7 @@ import socket | |||
import struct | |||
from . import constants as c | |||
from .helpers import ( | |||
Socks4Server, Socks5Server, Socks5Auth, Socks4Auth | |||
Socks4Addr, Socks5Addr, Socks5Auth, Socks4Auth | |||
) | |||
from .errors import * | |||
@@ -67,7 +67,7 @@ class SocksProtocol(asyncio.StreamReaderProtocol): | |||
class Socks4Protocol(SocksProtocol): | |||
def __init__(self, proxy, proxy_auth, dst, remote_resolve=True, loop=None): | |||
if not isinstance(proxy, Socks4Server): | |||
if not isinstance(proxy, Socks4Addr): | |||
raise ValueError('Invalid proxy format') | |||
if proxy_auth is not None and not isinstance(proxy_auth, Socks4Auth): | |||
@@ -111,7 +111,7 @@ class Socks4Protocol(SocksProtocol): | |||
class Socks5Protocol(SocksProtocol): | |||
def __init__(self, proxy, proxy_auth, dst, remote_resolve=True, loop=None): | |||
if not isinstance(proxy, Socks5Server): | |||
if not isinstance(proxy, Socks5Addr): | |||
raise ValueError('Invalid proxy format') | |||
if proxy_auth is not None and not isinstance(proxy_auth, Socks5Auth): | |||