Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tests and styles #74

Merged
merged 1 commit into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/find_and_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def get_pages(urls, proxy_pool, timeout=10, loop=None):
def main():
loop = asyncio.get_event_loop()

proxies = asyncio.Queue(loop=loop)
proxies = asyncio.Queue()
proxy_pool = ProxyPool(proxies)

judges = [
Expand Down
4 changes: 2 additions & 2 deletions proxybroker/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(
**kwargs,
):
self._loop = loop or asyncio.get_event_loop()
self._proxies = queue or asyncio.Queue(loop=self._loop)
self._proxies = queue or asyncio.Queue()
self._resolver = Resolver(loop=self._loop)
self._timeout = timeout
self._verify_ssl = verify_ssl
Expand Down Expand Up @@ -97,7 +97,7 @@ def __init__(
max_tries = attempts_conn

# The maximum number of concurrent checking proxies
self._on_check = asyncio.Queue(maxsize=max_conn, loop=self._loop)
self._on_check = asyncio.Queue(maxsize=max_conn)
self._max_tries = max_tries
self._judges = judges
self._providers = [
Expand Down
3 changes: 2 additions & 1 deletion proxybroker/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ def _get_anonymity_lvl(real_ext_ip, proxy, judge, content):
class ProxyChecker(Checker):
def __init__(self, *args, **kwargs):
warnings.warn(
'`ProxyChecker` is deprecated, use `Checker` instead.', DeprecationWarning,
'`ProxyChecker` is deprecated, use `Checker` instead.',
DeprecationWarning,
)
super().__init__(*args, **kwargs)
7 changes: 5 additions & 2 deletions proxybroker/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ def add_grab_args(group):

def add_serve_args(group):
group.add_argument(
'--host', type=str, default='127.0.0.1', help='Host of local proxy server',
'--host',
type=str,
default='127.0.0.1',
help='Host of local proxy server',
)
group.add_argument(
'--port', type=int, default=8888, help='Port of local proxy server'
Expand Down Expand Up @@ -380,7 +383,7 @@ def cli(args=sys.argv[1:]):
ns.types.append(('HTTP', ns.anon_lvl))

loop = asyncio.get_event_loop()
proxies = asyncio.Queue(loop=loop)
proxies = asyncio.Queue()
broker = Broker(
proxies,
max_conn=ns.max_conn,
Expand Down
3 changes: 1 addition & 2 deletions proxybroker/judge.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ def __init__(self, url, timeout=8, verify_ssl=False, loop=None):
self._resolver = Resolver(loop=self._loop)

def __repr__(self):
"""Class representation
"""
"""Class representation"""
return '<Judge [%s] %s>' % (self.scheme, self.host)

@classmethod
Expand Down
6 changes: 6 additions & 0 deletions proxybroker/negotiators.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import struct
from abc import ABC, abstractmethod
from socket import inet_aton
Expand Down Expand Up @@ -61,6 +62,8 @@ async def negotiate(self, **kwargs):
await self._proxy.send(struct.pack('3B', 5, 1, 0))
resp = await self._proxy.recv(2)

if not isinstance(resp, (bytes, str)):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you encountered any issues here?
Because the response comes from the python build-in asyncio.StreamReader, it should return the bytes object.

raise TypeError(f"{type(resp).__name__} is not supported")
if resp[0] == 0x05 and resp[1] == 0xFF:
self._proxy.log('Failed (auth is required)', err=BadResponseError)
raise BadResponseError
Expand Down Expand Up @@ -92,6 +95,9 @@ async def negotiate(self, **kwargs):

await self._proxy.send(struct.pack('>2BH5B', 4, 1, port, *bip, 0))
resp = await self._proxy.recv(8)
if isinstance(resp, asyncio.Future):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you encountered any issues here?
Because the response comes from asyncio and it should only possibly be the Future object.

resp = await resp
assert not isinstance(resp, asyncio.Future)

if resp[0] != 0x00 or resp[1] != 0x5A:
self._proxy.log('Failed (invalid data)', err=BadResponseError)
Expand Down
3 changes: 2 additions & 1 deletion proxybroker/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,8 @@ def __init__(self, *args, **kwargs):
proto=('HTTP', 'CONNECT:80', 'HTTPS', 'CONNECT:25'),
), # 200
Provider(
url='http://cn-proxy.com/', proto=('HTTP', 'CONNECT:80', 'HTTPS', 'CONNECT:25'),
url='http://cn-proxy.com/',
proto=('HTTP', 'CONNECT:80', 'HTTPS', 'CONNECT:25'),
), # 70
Provider(
url='https://hugeproxies.com/home/',
Expand Down
2 changes: 2 additions & 0 deletions proxybroker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def get_all_ip(page):

def get_status_code(resp, start=9, stop=12):
try:
if not isinstance(resp, (bytes, str)):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the viewpoint to check the external argument is acceptable, but the resp in this module should be all bytes object, and what I would like to know is there any edge case happened?

raise TypeError(f'{type(resp).__name__} is not supported')
code = int(resp[start:stop])
except ValueError:
return 400 # Bad Request
Expand Down
33 changes: 17 additions & 16 deletions tests/test_negotiators.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ def test_base_attrs(proxy, ngtr, check_anon_lvl, use_full_path):
(
'SOCKS5',
80,
future_iter(b'\x05\x00', b'\x05\x00\x00\x01\xc0\xa8\x00\x18\xce\xdf'),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to remove this? And so does all operation in tests folder

[b'\x05\x00', b'\x05\x00\x00\x01\xc0\xa8\x00\x18\xce\xdf'],
[call(b'\x05\x01\x00'), call(b'\x05\x01\x00\x01\x7f\x00\x00\x01\x00P')],
),
(
'SOCKS5',
443,
future_iter(b'\x05\x00', b'\x05\x00'),
[b'\x05\x00', b'\x05\x00'],
[call(b'\x05\x01\x00'), call(b'\x05\x01\x00\x01\x7f\x00\x00\x01\x01\xbb')],
), # noqa
(
Expand Down Expand Up @@ -85,7 +85,7 @@ async def test_socks_negotiate(proxy, ngtr, port, recv, expected):
'ngtr,recv,expected',
[
# wrong response:
('SOCKS5', future_iter(b'\x05\xff'), [call(b'\x05\x01\x00')]),
('SOCKS5', [b'\x05\xff'], [call(b'\x05\x01\x00')]),
(
'SOCKS4',
future_iter(b'HTTP/1.1 400 Bad Request'),
Expand All @@ -94,7 +94,7 @@ async def test_socks_negotiate(proxy, ngtr, port, recv, expected):
# failed to connect:
(
'SOCKS5',
future_iter(b'\x05\x00', b'\x05\x05'),
(b'\x05\x00', b'\x05\x05'),
[call(b'\x05\x01\x00'), call(b'\x05\x01\x00\x01\x7f\x00\x00\x01\x00P')],
), # noqa
(
Expand Down Expand Up @@ -122,19 +122,20 @@ async def test_socks_negotiate_error(proxy, ngtr, recv, expected):
(
'CONNECT:80',
80,
future_iter(b'HTTP/1.1 200 Connection established\r\n\r\n'),
[b'HTTP/1.1 200 Connection established\r\n\r\n'],
), # noqa
(
'CONNECT:25',
25,
future_iter(
b'HTTP/1.1 200 Connection established\r\n\r\n', b'220 smtp2.test.com',
),
[
b'HTTP/1.1 200 Connection established\r\n\r\n',
b'220 smtp2.test.com',
],
), # noqa
(
'HTTPS',
443,
future_iter(b'HTTP/1.1 200 Connection established\r\n\r\n'),
[b'HTTP/1.1 200 Connection established\r\n\r\n'],
), # noqa
],
)
Expand All @@ -155,21 +156,21 @@ async def test_connect_negotiate(proxy, ngtr, port, recv):
@pytest.mark.parametrize(
'ngtr,recv',
[
('CONNECT:80', future_iter(b'HTTP/1.1 400 Bad Request\r\n\r\n')),
('CONNECT:80', [b'HTTP/1.1 400 Bad Request\r\n\r\n']),
(
'CONNECT:80',
future_iter(b'<html>\r\n<head><title>400 Bad Request</title></head>\r\n'),
[b'<html>\r\n<head><title>400 Bad Request</title></head>\r\n'],
), # noqa
('CONNECT:25', future_iter(b'HTTP/1.1 400 Bad Request\r\n\r\n')),
('CONNECT:25', [b'HTTP/1.1 400 Bad Request\r\n\r\n']),
(
'CONNECT:25',
future_iter(b'<html>\r\n<head><title>400 Bad Request</title></head>\r\n'),
[b'<html>\r\n<head><title>400 Bad Request</title></head>\r\n'],
), # noqa
('CONNECT:25', future_iter(b'HTTP/1.1 200 OK\r\n\r\n', b'')),
('HTTPS', future_iter(b'HTTP/1.1 400 Bad Request\r\n\r\n')),
('CONNECT:25', [b'HTTP/1.1 200 OK\r\n\r\n', b'']),
('HTTPS', [b'HTTP/1.1 400 Bad Request\r\n\r\n']),
(
'HTTPS',
future_iter(b'<html>\r\n<head><title>400 Bad Request</title></head>\r\n'),
[b'<html>\r\n<head><title>400 Bad Request</title></head>\r\n'],
), # noqa
],
)
Expand Down
4 changes: 3 additions & 1 deletion tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ async def test_resolve_cache(event_loop, mocker, resolver):

resolver._cached_hosts.clear()
f = future_iter(
[ResolveResult('127.0.0.1', 0)], [ResolveResult('127.0.0.2', 0)], [Exception],
[ResolveResult('127.0.0.1', 0)],
[ResolveResult('127.0.0.2', 0)],
[Exception],
)
with mocker.patch('aiodns.DNSResolver.query', side_effect=f):
await resolver.resolve('test.com')
Expand Down