-
-
Notifications
You must be signed in to change notification settings - Fork 111
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
@@ -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)): | ||
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 | ||
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you encountered any issues here? |
||
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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason to remove this? And so does all operation in |
||
[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 | ||
( | ||
|
@@ -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'), | ||
|
@@ -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 | ||
( | ||
|
@@ -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 | ||
], | ||
) | ||
|
@@ -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 | ||
], | ||
) | ||
|
There was a problem hiding this comment.
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.