From aa20e334b81f7a261f2f6cbf53359d0e64f7088a Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Fri, 5 Nov 2021 01:22:38 +0100 Subject: [PATCH] Enable isort in the pre-commit tool settings --- .flake8 | 1 - .isort.cfg | 25 +++++++++++ .pre-commit-config.yaml | 12 +++--- examples/https_connect_tunnel.py | 8 ++-- examples/pubsub_eventing.py | 8 ++-- examples/ssl_echo_client.py | 3 +- examples/ssl_echo_server.py | 5 +-- examples/tcp_echo_client.py | 3 +- examples/tcp_echo_server.py | 2 +- examples/websocket_client.py | 1 + proxy/__init__.py | 3 +- proxy/__main__.py | 1 + proxy/common/constants.py | 6 +-- proxy/common/flag.py | 3 +- proxy/common/pki.py | 12 +++--- proxy/common/types.py | 4 +- proxy/common/utils.py | 15 +++---- proxy/core/acceptor/__init__.py | 3 +- proxy/core/acceptor/acceptor.py | 15 +++---- proxy/core/acceptor/pool.py | 8 ++-- proxy/core/acceptor/threadless.py | 20 ++++----- proxy/core/acceptor/work.py | 9 ++-- proxy/core/base/__init__.py | 1 + proxy/core/base/tcp_server.py | 9 ++-- proxy/core/base/tcp_tunnel.py | 9 ++-- proxy/core/connection/__init__.py | 3 +- proxy/core/connection/client.py | 4 +- proxy/core/connection/connection.py | 5 ++- proxy/core/connection/server.py | 8 ++-- proxy/core/event/__init__.py | 7 +-- proxy/core/event/dispatcher.py | 9 ++-- proxy/core/event/manager.py | 6 +-- proxy/core/event/names.py | 1 + proxy/core/event/queue.py | 3 +- proxy/core/event/subscriber.py | 9 ++-- proxy/core/ssh/tunnel.py | 2 +- proxy/dashboard/dashboard.py | 12 +++--- proxy/dashboard/inspect_traffic.py | 7 ++- proxy/dashboard/plugin.py | 4 +- proxy/http/chunk_parser.py | 4 +- proxy/http/exception/__init__.py | 1 + proxy/http/exception/http_request_rejected.py | 6 +-- proxy/http/exception/proxy_auth_failed.py | 9 ++-- proxy/http/exception/proxy_conn_failed.py | 9 ++-- proxy/http/handler.py | 24 +++++------ proxy/http/inspector/__init__.py | 1 + proxy/http/inspector/devtools.py | 14 +++--- proxy/http/inspector/transformer.py | 2 +- proxy/http/parser.py | 9 ++-- proxy/http/plugin.py | 8 ++-- proxy/http/proxy/__init__.py | 3 +- proxy/http/proxy/auth.py | 4 +- proxy/http/proxy/plugin.py | 12 +++--- proxy/http/proxy/server.py | 43 +++++++++---------- proxy/http/server/__init__.py | 3 +- proxy/http/server/pac_plugin.py | 12 +++--- proxy/http/server/plugin.py | 8 ++-- proxy/http/server/protocols.py | 1 + proxy/http/server/web.py | 24 +++++------ proxy/http/websocket/__init__.py | 3 +- proxy/http/websocket/client.py | 15 +++---- proxy/http/websocket/frame.py | 11 +++-- proxy/plugin/__init__.py | 17 ++++---- proxy/plugin/cache/__init__.py | 1 + proxy/plugin/cache/base.py | 3 +- proxy/plugin/cache/cache_responses.py | 2 +- proxy/plugin/cache/store/base.py | 1 + proxy/plugin/cache/store/disk.py | 4 +- proxy/plugin/cloudflare_dns.py | 2 + proxy/plugin/custom_dns_resolver.py | 1 - proxy/plugin/filter_by_client_ip.py | 2 +- proxy/plugin/filter_by_upstream.py | 4 +- proxy/plugin/filter_by_url_regex.py | 9 ++-- proxy/plugin/man_in_the_middle.py | 2 +- proxy/plugin/mock_rest_api.py | 4 +- proxy/plugin/modify_chunk_response.py | 4 +- proxy/plugin/modify_post_data.py | 2 +- proxy/plugin/proxy_pool.py | 12 +++--- proxy/plugin/redirect_to_custom_server.py | 6 +-- proxy/plugin/reverse_proxy.py | 14 +++--- proxy/plugin/shortlink.py | 2 +- proxy/plugin/web_server_route.py | 5 ++- proxy/proxy.py | 34 +++++++-------- proxy/testing/test_case.py | 4 +- tests/__init__.py | 1 + tests/common/test_flags.py | 12 +++--- tests/common/test_pki.py | 4 +- tests/common/test_text_bytes.py | 2 +- tests/common/test_utils.py | 4 +- tests/core/test_acceptor.py | 6 +-- tests/core/test_acceptor_pool.py | 4 +- tests/core/test_connection.py | 10 ++--- tests/core/test_event_dispatcher.py | 3 +- tests/core/test_event_queue.py | 2 +- tests/core/test_event_subscriber.py | 8 ++-- .../exceptions/test_http_proxy_auth_failed.py | 6 +-- .../exceptions/test_http_request_rejected.py | 4 +- tests/http/test_chunk_parser.py | 2 +- tests/http/test_http_parser.py | 6 +-- tests/http/test_http_proxy.py | 10 ++--- .../http/test_http_proxy_tls_interception.py | 11 +++-- tests/http/test_protocol_handler.py | 16 +++---- tests/http/test_web_server.py | 8 ++-- tests/http/test_websocket_client.py | 4 +- tests/plugin/test_http_proxy_plugins.py | 21 ++++----- ...ttp_proxy_plugins_with_tls_interception.py | 15 +++---- tests/plugin/utils.py | 6 +-- tests/test_main.py | 26 ++++++----- tests/test_set_open_file_limit.py | 1 + tests/testing/test_embed.py | 6 +-- tests/testing/test_test_case.py | 2 +- version-check.py | 4 +- 112 files changed, 415 insertions(+), 416 deletions(-) create mode 100644 .isort.cfg diff --git a/.flake8 b/.flake8 index cf84c8257f..49ba55bca0 100644 --- a/.flake8 +++ b/.flake8 @@ -28,7 +28,6 @@ extend-exclude = # IMPORTANT: avoid using ignore option, always use extend-ignore instead # Completely and unconditionally ignore the following errors: extend-ignore = - I # flake8-isort is drunk + we have isort integrated into pre-commit B009 # FIXME: `getattr()` called with a constant arg C812 # FIXME: missing trailing comma C819 # FIXME: inline trailing comma diff --git a/.isort.cfg b/.isort.cfg new file mode 100644 index 0000000000..99bbc44fda --- /dev/null +++ b/.isort.cfg @@ -0,0 +1,25 @@ +# https://github.com/timothycrosley/isort/wiki/isort-Settings +[settings] +default_section = THIRDPARTY +# force_to_top=file1.py,file2.py +# forced_separate = django.contrib,django.utils +include_trailing_comma = true +indent = 4 +known_first_party = proxy +# known_future_library = future,pies +# known_standard_library = std,std2 +known_testing_party = pytest,unittest +# length_sort = 1 +# Should be: 80 - 1 +#line_length = 79 +line_length = 127 +lines_after_imports = 2 +# https://pycqa.github.io/isort/docs/configuration/multi_line_output_modes.html +# NOTE: Another mode could be "5" for grouping multiple "import from" under +# NOTE: a single instruction. +multi_line_output = 9 +no_lines_before = LOCALFOLDER +sections=FUTURE,STDLIB,TESTING,THIRDPARTY,FIRSTPARTY,LOCALFOLDER +# skip=file3.py,file4.py +use_parentheses = true +verbose = true diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1770e6656b..fffd107af5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,12 +7,12 @@ repos: args: - --py36-plus -# - repo: https://github.com/timothycrosley/isort.git -# rev: 5.4.2 -# hooks: -# - id: isort -# args: -# - --honor-noqa +- repo: https://github.com/timothycrosley/isort.git + rev: 5.10.0 + hooks: + - id: isort + args: + - --honor-noqa - repo: https://github.com/Lucas-C/pre-commit-hooks.git rev: v1.1.7 diff --git a/examples/https_connect_tunnel.py b/examples/https_connect_tunnel.py index 6bf504da12..6b0de11f41 100644 --- a/examples/https_connect_tunnel.py +++ b/examples/https_connect_tunnel.py @@ -11,13 +11,13 @@ import time from typing import Any, Optional -from proxy.proxy import Proxy from proxy.common.utils import build_http_response -from proxy.http.codes import httpStatusCodes -from proxy.http.parser import httpParserStates -from proxy.http.methods import httpMethods from proxy.core.acceptor import AcceptorPool from proxy.core.base import BaseTcpTunnelHandler +from proxy.http.codes import httpStatusCodes +from proxy.http.methods import httpMethods +from proxy.http.parser import httpParserStates +from proxy.proxy import Proxy class HttpsConnectTunnelHandler(BaseTcpTunnelHandler): diff --git a/examples/pubsub_eventing.py b/examples/pubsub_eventing.py index d8650e5098..11e6e1561a 100644 --- a/examples/pubsub_eventing.py +++ b/examples/pubsub_eventing.py @@ -8,15 +8,15 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import time -import multiprocessing import logging - -from typing import Dict, Any +import multiprocessing +import time +from typing import Any, Dict from proxy.core.event import EventQueue, EventSubscriber, eventNames from proxy.core.event.manager import EventManager + # Enable debug logging to view core event logs logging.basicConfig(level=logging.DEBUG) diff --git a/examples/ssl_echo_client.py b/examples/ssl_echo_client.py index 227b26c94f..a1aee3f544 100644 --- a/examples/ssl_echo_client.py +++ b/examples/ssl_echo_client.py @@ -8,8 +8,9 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -from proxy.core.connection import TcpServerConnection from proxy.common.constants import DEFAULT_BUFFER_SIZE +from proxy.core.connection import TcpServerConnection + if __name__ == '__main__': client = TcpServerConnection('::', 12345) diff --git a/examples/ssl_echo_server.py b/examples/ssl_echo_server.py index b609abd199..466c39c968 100644 --- a/examples/ssl_echo_server.py +++ b/examples/ssl_echo_server.py @@ -11,12 +11,11 @@ import time from typing import Optional -from proxy.proxy import Proxy from proxy.common.utils import wrap_socket from proxy.core.acceptor import AcceptorPool -from proxy.core.connection import TcpClientConnection - from proxy.core.base import BaseTcpServerHandler +from proxy.core.connection import TcpClientConnection +from proxy.proxy import Proxy class EchoSSLServerHandler(BaseTcpServerHandler): diff --git a/examples/tcp_echo_client.py b/examples/tcp_echo_client.py index decabb505f..8596dd153c 100644 --- a/examples/tcp_echo_client.py +++ b/examples/tcp_echo_client.py @@ -8,8 +8,9 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -from proxy.common.utils import socket_connection from proxy.common.constants import DEFAULT_BUFFER_SIZE +from proxy.common.utils import socket_connection + if __name__ == '__main__': with socket_connection(('::', 12345)) as client: diff --git a/examples/tcp_echo_server.py b/examples/tcp_echo_server.py index 16cf3fcb95..974ea47eca 100644 --- a/examples/tcp_echo_server.py +++ b/examples/tcp_echo_server.py @@ -11,9 +11,9 @@ import time from typing import Optional -from proxy.proxy import Proxy from proxy.core.acceptor import AcceptorPool from proxy.core.base import BaseTcpServerHandler +from proxy.proxy import Proxy class EchoServerHandler(BaseTcpServerHandler): diff --git a/examples/websocket_client.py b/examples/websocket_client.py index a382304401..04095bb565 100644 --- a/examples/websocket_client.py +++ b/examples/websocket_client.py @@ -9,6 +9,7 @@ :license: BSD, see LICENSE for more details. """ import time + from proxy.http.websocket import WebsocketClient, WebsocketFrame, websocketOpcodes diff --git a/proxy/__init__.py b/proxy/__init__.py index 16cacc417d..f46acfc616 100755 --- a/proxy/__init__.py +++ b/proxy/__init__.py @@ -8,9 +8,10 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -from .proxy import entry_point, main, Proxy +from .proxy import Proxy, entry_point, main from .testing.test_case import TestCase + __all__ = [ # PyPi package entry_point. See # https://github.com/abhinavsingh/proxy.py#from-command-line-when-installed-using-pip diff --git a/proxy/__main__.py b/proxy/__main__.py index d04d8529d7..844283ec42 100644 --- a/proxy/__main__.py +++ b/proxy/__main__.py @@ -10,5 +10,6 @@ """ from .proxy import entry_point + if __name__ == '__main__': entry_point() diff --git a/proxy/common/constants.py b/proxy/common/constants.py index 641478004d..ea9d9e9a8f 100644 --- a/proxy/common/constants.py +++ b/proxy/common/constants.py @@ -8,15 +8,15 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ +import ipaddress import os -import time import pathlib -import ipaddress - +import time from typing import List from .version import __version__ + PROXY_PY_START_TIME = time.time() # /path/to/proxy.py/proxy folder diff --git a/proxy/common/flag.py b/proxy/common/flag.py index e23789d363..9ceee74644 100644 --- a/proxy/common/flag.py +++ b/proxy/common/flag.py @@ -9,10 +9,11 @@ :license: BSD, see LICENSE for more details. """ import argparse -from typing import Optional, List, Any +from typing import Any, List, Optional from .version import __version__ + __homepage__ = 'https://github.com/abhinavsingh/proxy.py' diff --git a/proxy/common/pki.py b/proxy/common/pki.py index 1c764a9d71..81290bb414 100644 --- a/proxy/common/pki.py +++ b/proxy/common/pki.py @@ -8,19 +8,19 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import time -import sys import argparse import contextlib +import logging import os -import uuid import subprocess +import sys import tempfile -import logging -from typing import List, Generator, Optional, Tuple +import time +import uuid +from typing import Generator, List, Optional, Tuple -from .utils import bytes_ from .constants import COMMA +from .utils import bytes_ from .version import __version__ diff --git a/proxy/common/types.py b/proxy/common/types.py index a95a49e8d2..4b6a204003 100644 --- a/proxy/common/types.py +++ b/proxy/common/types.py @@ -8,11 +8,11 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import queue import ipaddress +import queue import sys +from typing import TYPE_CHECKING, Any, Dict, List, Union -from typing import TYPE_CHECKING, Dict, Any, List, Union # NOTE: Using try/except causes linting problems which is why it's necessary # NOTE: to use this mypy/pylint idiom for py36-py38 compatibility diff --git a/proxy/common/utils.py b/proxy/common/utils.py index 2fb802bf12..7d988a4084 100644 --- a/proxy/common/utils.py +++ b/proxy/common/utils.py @@ -8,18 +8,17 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import ssl -import socket -import logging +import contextlib import functools import ipaddress -import contextlib - +import logging +import socket +import ssl from types import TracebackType -from typing import Optional, Dict, Any, List, Tuple, Type, Callable +from typing import Any, Callable, Dict, List, Optional, Tuple, Type -from .constants import HTTP_1_1, COLON, WHITESPACE, CRLF, DEFAULT_TIMEOUT -from .constants import DEFAULT_LOG_FILE, DEFAULT_LOG_FORMAT, DEFAULT_LOG_LEVEL +from .constants import COLON, CRLF, DEFAULT_LOG_FILE, DEFAULT_LOG_FORMAT, DEFAULT_LOG_LEVEL, DEFAULT_TIMEOUT, HTTP_1_1 +from .constants import WHITESPACE def text_(s: Any, encoding: str = 'utf-8', errors: str = 'strict') -> Any: diff --git a/proxy/core/acceptor/__init__.py b/proxy/core/acceptor/__init__.py index cca3bcdb4d..e590601284 100644 --- a/proxy/core/acceptor/__init__.py +++ b/proxy/core/acceptor/__init__.py @@ -10,8 +10,9 @@ """ from .acceptor import Acceptor from .pool import AcceptorPool -from .work import Work from .threadless import Threadless +from .work import Work + __all__ = [ 'Acceptor', diff --git a/proxy/core/acceptor/acceptor.py b/proxy/core/acceptor/acceptor.py index 78e0f02bd9..33ffaa28d9 100644 --- a/proxy/core/acceptor/acceptor.py +++ b/proxy/core/acceptor/acceptor.py @@ -15,19 +15,18 @@ import selectors import socket import threading - from multiprocessing import connection -from multiprocessing.reduction import send_handle, recv_handle -from typing import Optional, Type, Tuple - -from .work import Work -from .threadless import Threadless +from multiprocessing.reduction import recv_handle, send_handle +from typing import Optional, Tuple, Type -from ..connection import TcpClientConnection -from ..event import EventQueue, eventNames from ...common.constants import DEFAULT_THREADLESS from ...common.flag import flags from ...common.utils import setup_logger +from ..connection import TcpClientConnection +from ..event import EventQueue, eventNames +from .threadless import Threadless +from .work import Work + logger = logging.getLogger(__name__) diff --git a/proxy/core/acceptor/pool.py b/proxy/core/acceptor/pool.py index 0f1a28663e..4acb93f793 100644 --- a/proxy/core/acceptor/pool.py +++ b/proxy/core/acceptor/pool.py @@ -12,18 +12,16 @@ import logging import multiprocessing import socket - from multiprocessing import connection from multiprocessing.reduction import send_handle from typing import List, Optional, Type +from ...common.constants import DEFAULT_BACKLOG, DEFAULT_IPV6_HOSTNAME, DEFAULT_NUM_WORKERS, DEFAULT_PORT +from ...common.flag import flags +from ..event import EventQueue from .acceptor import Acceptor from .work import Work -from ..event import EventQueue - -from ...common.flag import flags -from ...common.constants import DEFAULT_BACKLOG, DEFAULT_IPV6_HOSTNAME, DEFAULT_NUM_WORKERS, DEFAULT_PORT logger = logging.getLogger(__name__) diff --git a/proxy/core/acceptor/threadless.py b/proxy/core/acceptor/threadless.py index 8caf21739b..5061e1a5a6 100644 --- a/proxy/core/acceptor/threadless.py +++ b/proxy/core/acceptor/threadless.py @@ -9,26 +9,24 @@ :license: BSD, see LICENSE for more details. """ import argparse -import os -import socket -import logging import asyncio -import selectors import contextlib +import logging import multiprocessing +import os +import selectors +import socket from multiprocessing import connection from multiprocessing.reduction import recv_handle +from typing import Any, Dict, Generator, List, Optional, Tuple, Type -from typing import Dict, Optional, Tuple, List, Generator, Any, Type - -from .work import Work - +from ...common.constants import DEFAULT_TIMEOUT +from ...common.types import Readables, Writables +from ...common.utils import setup_logger from ..connection import TcpClientConnection from ..event import EventQueue, eventNames +from .work import Work -from ...common.utils import setup_logger -from ...common.types import Readables, Writables -from ...common.constants import DEFAULT_TIMEOUT logger = logging.getLogger(__name__) diff --git a/proxy/core/acceptor/work.py b/proxy/core/acceptor/work.py index 5556a31941..d66c81716b 100644 --- a/proxy/core/acceptor/work.py +++ b/proxy/core/acceptor/work.py @@ -10,14 +10,13 @@ """ import argparse import socket - from abc import ABC, abstractmethod -from uuid import uuid4, UUID -from typing import Optional, Dict, Any +from typing import Any, Dict, Optional +from uuid import UUID, uuid4 -from ..event import eventNames, EventQueue -from ..connection import TcpClientConnection from ...common.types import Readables, Writables +from ..connection import TcpClientConnection +from ..event import EventQueue, eventNames class Work(ABC): diff --git a/proxy/core/base/__init__.py b/proxy/core/base/__init__.py index c60f14778e..3d7e03e207 100644 --- a/proxy/core/base/__init__.py +++ b/proxy/core/base/__init__.py @@ -11,6 +11,7 @@ from .tcp_server import BaseTcpServerHandler from .tcp_tunnel import BaseTcpTunnelHandler + __all__ = [ 'BaseTcpServerHandler', 'BaseTcpTunnelHandler', diff --git a/proxy/core/base/tcp_server.py b/proxy/core/base/tcp_server.py index 0eda82ba21..ca5059b489 100644 --- a/proxy/core/base/tcp_server.py +++ b/proxy/core/base/tcp_server.py @@ -8,14 +8,13 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -from abc import abstractmethod -import socket import selectors +import socket +from abc import abstractmethod +from typing import Any, Dict, Optional -from typing import Dict, Any, Optional - -from proxy.core.acceptor import Work from proxy.common.types import Readables, Writables +from proxy.core.acceptor import Work class BaseTcpServerHandler(Work): diff --git a/proxy/core/base/tcp_tunnel.py b/proxy/core/base/tcp_tunnel.py index 8703247cf2..36af08253e 100644 --- a/proxy/core/base/tcp_tunnel.py +++ b/proxy/core/base/tcp_tunnel.py @@ -8,15 +8,14 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -from abc import abstractmethod -import socket import selectors -from typing import Any, Optional, Dict +import socket +from abc import abstractmethod +from typing import Any, Dict, Optional -from ...http.parser import HttpParser, httpParserTypes from ...common.types import Readables, Writables from ...common.utils import text_ - +from ...http.parser import HttpParser, httpParserTypes from ..connection import TcpServerConnection from .tcp_server import BaseTcpServerHandler diff --git a/proxy/core/connection/__init__.py b/proxy/core/connection/__init__.py index ee44bc14a6..9923ea4338 100644 --- a/proxy/core/connection/__init__.py +++ b/proxy/core/connection/__init__.py @@ -8,10 +8,11 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -from .connection import TcpConnection, TcpConnectionUninitializedException, tcpConnectionTypes from .client import TcpClientConnection +from .connection import TcpConnection, TcpConnectionUninitializedException, tcpConnectionTypes from .server import TcpServerConnection + __all__ = [ 'TcpConnection', 'TcpConnectionUninitializedException', diff --git a/proxy/core/connection/client.py b/proxy/core/connection/client.py index bbc72f207e..dfd56db5aa 100644 --- a/proxy/core/connection/client.py +++ b/proxy/core/connection/client.py @@ -10,9 +10,9 @@ """ import socket import ssl -from typing import Union, Tuple, Optional +from typing import Optional, Tuple, Union -from .connection import TcpConnection, tcpConnectionTypes, TcpConnectionUninitializedException +from .connection import TcpConnection, TcpConnectionUninitializedException, tcpConnectionTypes class TcpClientConnection(TcpConnection): diff --git a/proxy/core/connection/connection.py b/proxy/core/connection/connection.py index daf464337d..6d90558099 100644 --- a/proxy/core/connection/connection.py +++ b/proxy/core/connection/connection.py @@ -8,14 +8,15 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ +import logging import socket import ssl -import logging from abc import ABC, abstractmethod -from typing import NamedTuple, Optional, Union, List +from typing import List, NamedTuple, Optional, Union from ...common.constants import DEFAULT_BUFFER_SIZE, DEFAULT_MAX_SEND_SIZE + logger = logging.getLogger(__name__) diff --git a/proxy/core/connection/server.py b/proxy/core/connection/server.py index fb92937ba8..05e42558db 100644 --- a/proxy/core/connection/server.py +++ b/proxy/core/connection/server.py @@ -8,14 +8,12 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import ssl import socket - -from typing import Optional, Union, Tuple - -from .connection import TcpConnection, tcpConnectionTypes, TcpConnectionUninitializedException +import ssl +from typing import Optional, Tuple, Union from ...common.utils import new_socket_connection +from .connection import TcpConnection, TcpConnectionUninitializedException, tcpConnectionTypes class TcpServerConnection(TcpConnection): diff --git a/proxy/core/event/__init__.py b/proxy/core/event/__init__.py index 17e1074e6e..3c46bd5cce 100644 --- a/proxy/core/event/__init__.py +++ b/proxy/core/event/__init__.py @@ -8,11 +8,12 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -from .queue import EventQueue -from .names import EventNames, eventNames from .dispatcher import EventDispatcher -from .subscriber import EventSubscriber from .manager import EventManager +from .names import EventNames, eventNames +from .queue import EventQueue +from .subscriber import EventSubscriber + __all__ = [ 'eventNames', diff --git a/proxy/core/event/dispatcher.py b/proxy/core/event/dispatcher.py index f65d5bf892..3499358786 100644 --- a/proxy/core/event/dispatcher.py +++ b/proxy/core/event/dispatcher.py @@ -8,16 +8,15 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ +import logging import queue import threading -import logging - -from typing import Dict, Any, List +from typing import Any, Dict, List from ...common.types import DictQueueType - -from .queue import EventQueue from .names import eventNames +from .queue import EventQueue + logger = logging.getLogger(__name__) diff --git a/proxy/core/event/manager.py b/proxy/core/event/manager.py index 9986f82ed6..887d5e8a09 100644 --- a/proxy/core/event/manager.py +++ b/proxy/core/event/manager.py @@ -9,13 +9,13 @@ :license: BSD, see LICENSE for more details. """ import logging -import threading import multiprocessing - +import threading from typing import Optional -from .queue import EventQueue from .dispatcher import EventDispatcher +from .queue import EventQueue + logger = logging.getLogger(__name__) diff --git a/proxy/core/event/names.py b/proxy/core/event/names.py index 980c1ea89b..75a4174f6e 100644 --- a/proxy/core/event/names.py +++ b/proxy/core/event/names.py @@ -10,6 +10,7 @@ """ from typing import NamedTuple + # Name of the events that eventing framework will support # Ideally this must be configurable via command line or # at-least extendable via plugins. diff --git a/proxy/core/event/queue.py b/proxy/core/event/queue.py index 23aaf4e689..1f5a7498a8 100644 --- a/proxy/core/event/queue.py +++ b/proxy/core/event/queue.py @@ -11,10 +11,9 @@ import os import threading import time -from typing import Dict, Optional, Any +from typing import Any, Dict, Optional from ...common.types import DictQueueType - from .names import eventNames diff --git a/proxy/core/event/subscriber.py b/proxy/core/event/subscriber.py index 841fe12f5b..84e6024fc8 100644 --- a/proxy/core/event/subscriber.py +++ b/proxy/core/event/subscriber.py @@ -8,18 +8,17 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ +import logging +import multiprocessing import queue import threading -import multiprocessing -import logging import uuid - -from typing import Dict, Optional, Any, Callable +from typing import Any, Callable, Dict, Optional from ...common.types import DictQueueType - from .queue import EventQueue + logger = logging.getLogger(__name__) diff --git a/proxy/core/ssh/tunnel.py b/proxy/core/ssh/tunnel.py index bb494010f1..a6e683f806 100644 --- a/proxy/core/ssh/tunnel.py +++ b/proxy/core/ssh/tunnel.py @@ -8,7 +8,7 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -from typing import Optional, Tuple, Callable +from typing import Callable, Optional, Tuple import paramiko diff --git a/proxy/dashboard/dashboard.py b/proxy/dashboard/dashboard.py index 57fb5aa81c..0a383e8b1c 100644 --- a/proxy/dashboard/dashboard.py +++ b/proxy/dashboard/dashboard.py @@ -7,18 +7,18 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import os import json import logging -from typing import List, Tuple, Any, Dict - -from .plugin import ProxyDashboardWebsocketPlugin +import os +from typing import Any, Dict, List, Tuple from ..common.utils import build_http_response, bytes_ -from ..http.server import HttpWebServerPlugin, HttpWebServerBasePlugin, httpProtocolTypes +from ..http.codes import httpStatusCodes from ..http.parser import HttpParser +from ..http.server import HttpWebServerBasePlugin, HttpWebServerPlugin, httpProtocolTypes from ..http.websocket import WebsocketFrame -from ..http.codes import httpStatusCodes +from .plugin import ProxyDashboardWebsocketPlugin + logger = logging.getLogger(__name__) diff --git a/proxy/dashboard/inspect_traffic.py b/proxy/dashboard/inspect_traffic.py index 4689d293bb..a3511baaf0 100644 --- a/proxy/dashboard/inspect_traffic.py +++ b/proxy/dashboard/inspect_traffic.py @@ -9,14 +9,13 @@ :license: BSD, see LICENSE for more details. """ import json -from typing import List, Dict, Any - -from .plugin import ProxyDashboardWebsocketPlugin +from typing import Any, Dict, List from ..common.utils import bytes_ -from ..core.event import EventSubscriber from ..core.connection import TcpClientConnection +from ..core.event import EventSubscriber from ..http.websocket import WebsocketFrame +from .plugin import ProxyDashboardWebsocketPlugin class InspectTrafficPlugin(ProxyDashboardWebsocketPlugin): diff --git a/proxy/dashboard/plugin.py b/proxy/dashboard/plugin.py index 8e36a600fc..d5be2b0b96 100644 --- a/proxy/dashboard/plugin.py +++ b/proxy/dashboard/plugin.py @@ -11,12 +11,12 @@ import argparse import json from abc import ABC, abstractmethod -from typing import List, Dict, Any +from typing import Any, Dict, List from ..common.utils import bytes_ -from ..http.websocket import WebsocketFrame from ..core.connection import TcpClientConnection from ..core.event import EventQueue +from ..http.websocket import WebsocketFrame class ProxyDashboardWebsocketPlugin(ABC): diff --git a/proxy/http/chunk_parser.py b/proxy/http/chunk_parser.py index 6a5258097a..d6ad82392b 100644 --- a/proxy/http/chunk_parser.py +++ b/proxy/http/chunk_parser.py @@ -8,10 +8,10 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -from typing import NamedTuple, Tuple, List, Optional +from typing import List, NamedTuple, Optional, Tuple -from ..common.utils import bytes_, find_http_line from ..common.constants import CRLF, DEFAULT_BUFFER_SIZE +from ..common.utils import bytes_, find_http_line ChunkParserStates = NamedTuple( diff --git a/proxy/http/exception/__init__.py b/proxy/http/exception/__init__.py index 513d2bd510..7c8f278db9 100644 --- a/proxy/http/exception/__init__.py +++ b/proxy/http/exception/__init__.py @@ -13,6 +13,7 @@ from .proxy_auth_failed import ProxyAuthenticationFailed from .proxy_conn_failed import ProxyConnectionFailed + __all__ = [ 'HttpProtocolException', 'HttpRequestRejected', diff --git a/proxy/http/exception/http_request_rejected.py b/proxy/http/exception/http_request_rejected.py index a0fa810fc1..8c5866b799 100644 --- a/proxy/http/exception/http_request_rejected.py +++ b/proxy/http/exception/http_request_rejected.py @@ -8,11 +8,11 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -from typing import Optional, Dict +from typing import Dict, Optional -from .base import HttpProtocolException -from ..parser import HttpParser from ...common.utils import build_http_response +from ..parser import HttpParser +from .base import HttpProtocolException class HttpRequestRejected(HttpProtocolException): diff --git a/proxy/http/exception/proxy_auth_failed.py b/proxy/http/exception/proxy_auth_failed.py index 053837e8a8..d5a782bf25 100644 --- a/proxy/http/exception/proxy_auth_failed.py +++ b/proxy/http/exception/proxy_auth_failed.py @@ -8,12 +8,11 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -from .base import HttpProtocolException -from ..parser import HttpParser -from ..codes import httpStatusCodes - -from ...common.constants import PROXY_AGENT_HEADER_VALUE, PROXY_AGENT_HEADER_KEY +from ...common.constants import PROXY_AGENT_HEADER_KEY, PROXY_AGENT_HEADER_VALUE from ...common.utils import build_http_response +from ..codes import httpStatusCodes +from ..parser import HttpParser +from .base import HttpProtocolException class ProxyAuthenticationFailed(HttpProtocolException): diff --git a/proxy/http/exception/proxy_conn_failed.py b/proxy/http/exception/proxy_conn_failed.py index 8cbd73d093..0a4937dcfa 100644 --- a/proxy/http/exception/proxy_conn_failed.py +++ b/proxy/http/exception/proxy_conn_failed.py @@ -8,12 +8,11 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -from .base import HttpProtocolException -from ..parser import HttpParser -from ..codes import httpStatusCodes - -from ...common.constants import PROXY_AGENT_HEADER_VALUE, PROXY_AGENT_HEADER_KEY +from ...common.constants import PROXY_AGENT_HEADER_KEY, PROXY_AGENT_HEADER_VALUE from ...common.utils import build_http_response +from ..codes import httpStatusCodes +from ..parser import HttpParser +from .base import HttpProtocolException class ProxyConnectionFailed(HttpProtocolException): diff --git a/proxy/http/handler.py b/proxy/http/handler.py index 41ec608f86..114413d786 100644 --- a/proxy/http/handler.py +++ b/proxy/http/handler.py @@ -9,28 +9,26 @@ :license: BSD, see LICENSE for more details. """ import argparse -import socket -import selectors -import ssl -import time import contextlib import errno import logging - -from typing import Tuple, List, Union, Optional, Generator, Dict +import selectors +import socket +import ssl +import time +from typing import Dict, Generator, List, Optional, Tuple, Union from uuid import UUID -from .plugin import HttpProtocolHandlerPlugin -from .parser import HttpParser, httpParserStates, httpParserTypes -from .exception import HttpProtocolException - +from ..common.constants import DEFAULT_CLIENT_RECVBUF_SIZE, DEFAULT_KEY_FILE, DEFAULT_TIMEOUT +from ..common.flag import flags from ..common.types import Readables, Writables from ..common.utils import wrap_socket from ..core.acceptor.work import Work -from ..core.event import EventQueue from ..core.connection import TcpClientConnection -from ..common.flag import flags -from ..common.constants import DEFAULT_CLIENT_RECVBUF_SIZE, DEFAULT_KEY_FILE, DEFAULT_TIMEOUT +from ..core.event import EventQueue +from .exception import HttpProtocolException +from .parser import HttpParser, httpParserStates, httpParserTypes +from .plugin import HttpProtocolHandlerPlugin logger = logging.getLogger(__name__) diff --git a/proxy/http/inspector/__init__.py b/proxy/http/inspector/__init__.py index 8e5d4aa017..2db019da6e 100644 --- a/proxy/http/inspector/__init__.py +++ b/proxy/http/inspector/__init__.py @@ -10,6 +10,7 @@ """ from .devtools import DevtoolsProtocolPlugin + __all__ = [ 'DevtoolsProtocolPlugin', ] diff --git a/proxy/http/inspector/devtools.py b/proxy/http/inspector/devtools.py index 800d88024d..b39986560d 100644 --- a/proxy/http/inspector/devtools.py +++ b/proxy/http/inspector/devtools.py @@ -10,17 +10,17 @@ """ import json import logging -from typing import List, Tuple, Any, Dict +from typing import Any, Dict, List, Tuple -from .transformer import CoreEventsToDevtoolsProtocol +from ...common.constants import DEFAULT_DEVTOOLS_WS_PATH +from ...common.flag import flags +from ...common.utils import bytes_, text_ +from ...core.event import EventSubscriber from ..parser import HttpParser -from ..websocket import WebsocketFrame, websocketOpcodes from ..server import HttpWebServerBasePlugin, httpProtocolTypes +from ..websocket import WebsocketFrame, websocketOpcodes +from .transformer import CoreEventsToDevtoolsProtocol -from ...common.utils import bytes_, text_ -from ...core.event import EventSubscriber -from ...common.flag import flags -from ...common.constants import DEFAULT_DEVTOOLS_WS_PATH logger = logging.getLogger(__name__) diff --git a/proxy/http/inspector/transformer.py b/proxy/http/inspector/transformer.py index ebc5ddcede..f1d1e6840d 100644 --- a/proxy/http/inspector/transformer.py +++ b/proxy/http/inspector/transformer.py @@ -13,11 +13,11 @@ import time from typing import Any, Dict -from ..websocket import WebsocketFrame from ...common.constants import PROXY_PY_START_TIME from ...common.utils import bytes_ from ...core.connection import TcpClientConnection from ...core.event import eventNames +from ..websocket import WebsocketFrame class CoreEventsToDevtoolsProtocol: diff --git a/proxy/http/parser.py b/proxy/http/parser.py index 927ca44270..82b86476be 100644 --- a/proxy/http/parser.py +++ b/proxy/http/parser.py @@ -8,14 +8,13 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ +from typing import Dict, List, NamedTuple, Optional, Tuple, Type, TypeVar from urllib import parse as urlparse -from typing import TypeVar, NamedTuple, Optional, Dict, Type, Tuple, List -from .methods import httpMethods -from .chunk_parser import ChunkParser, chunkParserStates - -from ..common.constants import DEFAULT_DISABLE_HEADERS, COLON, SLASH, CRLF, WHITESPACE, HTTP_1_1, DEFAULT_HTTP_PORT +from ..common.constants import COLON, CRLF, DEFAULT_DISABLE_HEADERS, DEFAULT_HTTP_PORT, HTTP_1_1, SLASH, WHITESPACE from ..common.utils import build_http_request, build_http_response, find_http_line, text_ +from .chunk_parser import ChunkParser, chunkParserStates +from .methods import httpMethods HttpParserStates = NamedTuple( diff --git a/proxy/http/plugin.py b/proxy/http/plugin.py index 37566f7266..470e18a3b1 100644 --- a/proxy/http/plugin.py +++ b/proxy/http/plugin.py @@ -10,16 +10,14 @@ """ import argparse import socket - from abc import ABC, abstractmethod +from typing import List, Optional, Tuple, Union from uuid import UUID -from typing import Tuple, List, Union, Optional - -from .parser import HttpParser from ..common.types import Readables, Writables -from ..core.event import EventQueue from ..core.connection import TcpClientConnection +from ..core.event import EventQueue +from .parser import HttpParser class HttpProtocolHandlerPlugin(ABC): diff --git a/proxy/http/proxy/__init__.py b/proxy/http/proxy/__init__.py index 4e18002c0d..233aa91645 100644 --- a/proxy/http/proxy/__init__.py +++ b/proxy/http/proxy/__init__.py @@ -8,9 +8,10 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ +from .auth import AuthPlugin from .plugin import HttpProxyBasePlugin from .server import HttpProxyPlugin -from .auth import AuthPlugin + __all__ = [ 'HttpProxyBasePlugin', diff --git a/proxy/http/proxy/auth.py b/proxy/http/proxy/auth.py index 10e2997b1a..74df58e2e4 100644 --- a/proxy/http/proxy/auth.py +++ b/proxy/http/proxy/auth.py @@ -10,11 +10,11 @@ """ from typing import Optional -from ..exception import ProxyAuthenticationFailed -from ...common.flag import flags from ...common.constants import DEFAULT_BASIC_AUTH +from ...common.flag import flags from ...http.parser import HttpParser from ...http.proxy import HttpProxyBasePlugin +from ..exception import ProxyAuthenticationFailed flags.add_argument( diff --git a/proxy/http/proxy/plugin.py b/proxy/http/proxy/plugin.py index be093f2c4f..a899159134 100644 --- a/proxy/http/proxy/plugin.py +++ b/proxy/http/proxy/plugin.py @@ -8,18 +8,16 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import socket import argparse - -from uuid import UUID -from typing import Any, Dict, List, Optional, Tuple +import socket from abc import ABC, abstractmethod - -from ..parser import HttpParser +from typing import Any, Dict, List, Optional, Tuple +from uuid import UUID from ...common.types import Readables, Writables -from ...core.event import EventQueue from ...core.connection import TcpClientConnection +from ...core.event import EventQueue +from ..parser import HttpParser class HttpProxyBasePlugin(ABC): diff --git a/proxy/http/proxy/server.py b/proxy/http/proxy/server.py index ec0a60676d..5162d42e1a 100644 --- a/proxy/http/proxy/server.py +++ b/proxy/http/proxy/server.py @@ -8,36 +8,33 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import os -import ssl -import time import errno -import socket import logging -import threading +import os +import socket +import ssl import subprocess +import threading +import time +from typing import Any, Dict, List, Optional, Tuple, Union, cast -from typing import Optional, List, Union, Dict, cast, Any, Tuple - -from .plugin import HttpProxyBasePlugin -from ..plugin import HttpProtocolHandlerPlugin -from ..exception import HttpProtocolException, ProxyConnectionFailed -from ..codes import httpStatusCodes -from ..parser import HttpParser, httpParserStates, httpParserTypes -from ..methods import httpMethods - +from ...common.constants import COMMA, DEFAULT_CA_CERT_DIR, DEFAULT_CA_CERT_FILE, DEFAULT_CA_FILE, DEFAULT_CA_KEY_FILE +from ...common.constants import DEFAULT_CA_SIGNING_KEY_FILE, DEFAULT_CERT_FILE, DEFAULT_DISABLE_HEADERS +from ...common.constants import DEFAULT_HTTP_ACCESS_LOG_FORMAT, DEFAULT_HTTPS_ACCESS_LOG_FORMAT, DEFAULT_SERVER_RECVBUF_SIZE +from ...common.constants import PROXY_AGENT_HEADER_VALUE +from ...common.flag import flags +from ...common.pki import gen_csr, gen_public_key, sign_csr from ...common.types import Readables, Writables -from ...common.constants import DEFAULT_CA_CERT_DIR, DEFAULT_CA_CERT_FILE, DEFAULT_CA_FILE -from ...common.constants import DEFAULT_CA_KEY_FILE, DEFAULT_CA_SIGNING_KEY_FILE -from ...common.constants import COMMA, DEFAULT_SERVER_RECVBUF_SIZE, DEFAULT_CERT_FILE -from ...common.constants import PROXY_AGENT_HEADER_VALUE, DEFAULT_DISABLE_HEADERS -from ...common.constants import DEFAULT_HTTP_ACCESS_LOG_FORMAT, DEFAULT_HTTPS_ACCESS_LOG_FORMAT from ...common.utils import build_http_response, text_ -from ...common.pki import gen_public_key, gen_csr, sign_csr - +from ...core.connection import TcpConnectionUninitializedException, TcpServerConnection from ...core.event import eventNames -from ...core.connection import TcpServerConnection, TcpConnectionUninitializedException -from ...common.flag import flags +from ..codes import httpStatusCodes +from ..exception import HttpProtocolException, ProxyConnectionFailed +from ..methods import httpMethods +from ..parser import HttpParser, httpParserStates, httpParserTypes +from ..plugin import HttpProtocolHandlerPlugin +from .plugin import HttpProxyBasePlugin + logger = logging.getLogger(__name__) diff --git a/proxy/http/server/__init__.py b/proxy/http/server/__init__.py index 059c2cc128..bcd241fe88 100644 --- a/proxy/http/server/__init__.py +++ b/proxy/http/server/__init__.py @@ -8,10 +8,11 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -from .web import HttpWebServerPlugin from .pac_plugin import HttpWebServerPacFilePlugin from .plugin import HttpWebServerBasePlugin from .protocols import httpProtocolTypes +from .web import HttpWebServerPlugin + __all__ = [ 'HttpWebServerPlugin', diff --git a/proxy/http/server/pac_plugin.py b/proxy/http/server/pac_plugin.py index 5880137d12..1b77f03211 100644 --- a/proxy/http/server/pac_plugin.py +++ b/proxy/http/server/pac_plugin.py @@ -9,15 +9,15 @@ :license: BSD, see LICENSE for more details. """ import gzip -from typing import List, Tuple, Optional, Any +from typing import Any, List, Optional, Tuple +from ...common.constants import DEFAULT_PAC_FILE, DEFAULT_PAC_FILE_URL_PATH +from ...common.flag import flags +from ...common.utils import build_http_response, bytes_, text_ +from ..parser import HttpParser +from ..websocket import WebsocketFrame from .plugin import HttpWebServerBasePlugin from .protocols import httpProtocolTypes -from ..websocket import WebsocketFrame -from ..parser import HttpParser -from ...common.utils import bytes_, text_, build_http_response -from ...common.flag import flags -from ...common.constants import DEFAULT_PAC_FILE, DEFAULT_PAC_FILE_URL_PATH flags.add_argument( diff --git a/proxy/http/server/plugin.py b/proxy/http/server/plugin.py index 747f15ccb4..508874221e 100644 --- a/proxy/http/server/plugin.py +++ b/proxy/http/server/plugin.py @@ -8,19 +8,17 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import socket import argparse - +import socket from abc import ABC, abstractmethod from typing import List, Tuple from uuid import UUID -from ..websocket import WebsocketFrame -from ..parser import HttpParser - from ...common.types import Readables, Writables from ...core.connection import TcpClientConnection from ...core.event import EventQueue +from ..parser import HttpParser +from ..websocket import WebsocketFrame class HttpWebServerBasePlugin(ABC): diff --git a/proxy/http/server/protocols.py b/proxy/http/server/protocols.py index b0f6202c06..bfa167c658 100644 --- a/proxy/http/server/protocols.py +++ b/proxy/http/server/protocols.py @@ -10,6 +10,7 @@ """ from typing import NamedTuple + HttpProtocolTypes = NamedTuple( 'HttpProtocolTypes', [ ('HTTP', int), diff --git a/proxy/http/server/web.py b/proxy/http/server/web.py index 8a89fc6b4b..d673d17eff 100644 --- a/proxy/http/server/web.py +++ b/proxy/http/server/web.py @@ -9,26 +9,26 @@ :license: BSD, see LICENSE for more details. """ import gzip -import re -import time import logging -import os import mimetypes +import os +import re import socket -from typing import List, Tuple, Optional, Dict, Union, Any, Pattern +import time +from typing import Any, Dict, List, Optional, Pattern, Tuple, Union -from .plugin import HttpWebServerBasePlugin -from .protocols import httpProtocolTypes -from ..exception import HttpProtocolException -from ..websocket import WebsocketFrame, websocketOpcodes +from ...common.constants import DEFAULT_STATIC_SERVER_DIR, PROXY_AGENT_HEADER_VALUE +from ...common.flag import flags +from ...common.types import Readables, Writables +from ...common.utils import build_http_response, build_websocket_handshake_response, bytes_, text_ from ..codes import httpStatusCodes +from ..exception import HttpProtocolException from ..parser import HttpParser, httpParserStates, httpParserTypes from ..plugin import HttpProtocolHandlerPlugin +from ..websocket import WebsocketFrame, websocketOpcodes +from .plugin import HttpWebServerBasePlugin +from .protocols import httpProtocolTypes -from ...common.utils import bytes_, text_, build_http_response, build_websocket_handshake_response -from ...common.constants import DEFAULT_STATIC_SERVER_DIR, PROXY_AGENT_HEADER_VALUE -from ...common.types import Readables, Writables -from ...common.flag import flags logger = logging.getLogger(__name__) diff --git a/proxy/http/websocket/__init__.py b/proxy/http/websocket/__init__.py index 2870e3b26f..1eab0eb2c0 100644 --- a/proxy/http/websocket/__init__.py +++ b/proxy/http/websocket/__init__.py @@ -8,8 +8,9 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -from .frame import WebsocketFrame, websocketOpcodes from .client import WebsocketClient +from .frame import WebsocketFrame, websocketOpcodes + __all__ = [ 'websocketOpcodes', diff --git a/proxy/http/websocket/client.py b/proxy/http/websocket/client.py index 902b891914..5e4f05f351 100644 --- a/proxy/http/websocket/client.py +++ b/proxy/http/websocket/client.py @@ -9,20 +9,17 @@ :license: BSD, see LICENSE for more details. """ import base64 +import secrets import selectors import socket -import secrets import ssl - -from typing import Optional, Union, Callable - -from .frame import WebsocketFrame - -from ..parser import httpParserTypes, HttpParser +from typing import Callable, Optional, Union from ...common.constants import DEFAULT_BUFFER_SIZE -from ...common.utils import new_socket_connection, build_websocket_handshake_request, text_ -from ...core.connection import tcpConnectionTypes, TcpConnection +from ...common.utils import build_websocket_handshake_request, new_socket_connection, text_ +from ...core.connection import TcpConnection, tcpConnectionTypes +from ..parser import HttpParser, httpParserTypes +from .frame import WebsocketFrame class WebsocketClient(TcpConnection): diff --git a/proxy/http/websocket/frame.py b/proxy/http/websocket/frame.py index 6814de02bf..4296c6ab5f 100644 --- a/proxy/http/websocket/frame.py +++ b/proxy/http/websocket/frame.py @@ -8,14 +8,13 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import hashlib import base64 -import struct -import secrets -import logging +import hashlib import io - -from typing import TypeVar, Type, Optional, NamedTuple +import logging +import secrets +import struct +from typing import NamedTuple, Optional, Type, TypeVar WebsocketOpcodes = NamedTuple( diff --git a/proxy/plugin/__init__.py b/proxy/plugin/__init__.py index 6e2ce5e6a6..a3525b61ba 100644 --- a/proxy/plugin/__init__.py +++ b/proxy/plugin/__init__.py @@ -8,21 +8,22 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -from .cache import CacheResponsesPlugin, BaseCacheResponsesPlugin +from .cache import BaseCacheResponsesPlugin, CacheResponsesPlugin +from .cloudflare_dns import CloudflareDnsResolverPlugin +from .custom_dns_resolver import CustomDnsResolverPlugin +from .filter_by_client_ip import FilterByClientIpPlugin from .filter_by_upstream import FilterByUpstreamHostPlugin +from .filter_by_url_regex import FilterByURLRegexPlugin from .man_in_the_middle import ManInTheMiddlePlugin from .mock_rest_api import ProposedRestApiPlugin +from .modify_chunk_response import ModifyChunkResponsePlugin from .modify_post_data import ModifyPostDataPlugin +from .proxy_pool import ProxyPoolPlugin from .redirect_to_custom_server import RedirectToCustomServerPlugin +from .reverse_proxy import ReverseProxyPlugin from .shortlink import ShortLinkPlugin from .web_server_route import WebServerPlugin -from .reverse_proxy import ReverseProxyPlugin -from .proxy_pool import ProxyPoolPlugin -from .filter_by_client_ip import FilterByClientIpPlugin -from .filter_by_url_regex import FilterByURLRegexPlugin -from .modify_chunk_response import ModifyChunkResponsePlugin -from .custom_dns_resolver import CustomDnsResolverPlugin -from .cloudflare_dns import CloudflareDnsResolverPlugin + __all__ = [ 'CacheResponsesPlugin', diff --git a/proxy/plugin/cache/__init__.py b/proxy/plugin/cache/__init__.py index f3bfb84b2c..ce6cfe4291 100644 --- a/proxy/plugin/cache/__init__.py +++ b/proxy/plugin/cache/__init__.py @@ -11,6 +11,7 @@ from .base import BaseCacheResponsesPlugin from .cache_responses import CacheResponsesPlugin + __all__ = [ 'BaseCacheResponsesPlugin', 'CacheResponsesPlugin', diff --git a/proxy/plugin/cache/base.py b/proxy/plugin/cache/base.py index e74d7dd960..d04aad9fc2 100644 --- a/proxy/plugin/cache/base.py +++ b/proxy/plugin/cache/base.py @@ -9,12 +9,13 @@ :license: BSD, see LICENSE for more details. """ import logging -from typing import Optional, Any +from typing import Any, Optional from ...http.parser import HttpParser from ...http.proxy import HttpProxyBasePlugin from .store.base import CacheStore + logger = logging.getLogger(__name__) diff --git a/proxy/plugin/cache/cache_responses.py b/proxy/plugin/cache/cache_responses.py index 200a60cb39..e9ccd126d6 100644 --- a/proxy/plugin/cache/cache_responses.py +++ b/proxy/plugin/cache/cache_responses.py @@ -11,8 +11,8 @@ import multiprocessing from typing import Any -from .store.disk import OnDiskCacheStore from .base import BaseCacheResponsesPlugin +from .store.disk import OnDiskCacheStore class CacheResponsesPlugin(BaseCacheResponsesPlugin): diff --git a/proxy/plugin/cache/store/base.py b/proxy/plugin/cache/store/base.py index eafeaa3c4a..79825af7a8 100644 --- a/proxy/plugin/cache/store/base.py +++ b/proxy/plugin/cache/store/base.py @@ -11,6 +11,7 @@ from abc import ABC, abstractmethod from typing import Optional from uuid import UUID + from ....http.parser import HttpParser diff --git a/proxy/plugin/cache/store/disk.py b/proxy/plugin/cache/store/disk.py index f50565f866..fdad32e8b0 100644 --- a/proxy/plugin/cache/store/disk.py +++ b/proxy/plugin/cache/store/disk.py @@ -11,15 +11,15 @@ import logging import os import tempfile -from typing import Optional, BinaryIO +from typing import BinaryIO, Optional from uuid import UUID from ....common.flag import flags from ....common.utils import text_ from ....http.parser import HttpParser - from .base import CacheStore + logger = logging.getLogger(__name__) diff --git a/proxy/plugin/cloudflare_dns.py b/proxy/plugin/cloudflare_dns.py index eaa45147cb..90aa4d78fc 100644 --- a/proxy/plugin/cloudflare_dns.py +++ b/proxy/plugin/cloudflare_dns.py @@ -10,6 +10,7 @@ """ import logging + try: import httpx except ImportError: @@ -21,6 +22,7 @@ from ..http.parser import HttpParser from ..http.proxy import HttpProxyBasePlugin + logger = logging.getLogger(__name__) diff --git a/proxy/plugin/custom_dns_resolver.py b/proxy/plugin/custom_dns_resolver.py index d714b5c9e8..a61c5b86e0 100644 --- a/proxy/plugin/custom_dns_resolver.py +++ b/proxy/plugin/custom_dns_resolver.py @@ -9,7 +9,6 @@ :license: BSD, see LICENSE for more details. """ import socket - from typing import Optional, Tuple from ..http.parser import HttpParser diff --git a/proxy/plugin/filter_by_client_ip.py b/proxy/plugin/filter_by_client_ip.py index 679679ab70..eb3403ea89 100644 --- a/proxy/plugin/filter_by_client_ip.py +++ b/proxy/plugin/filter_by_client_ip.py @@ -11,9 +11,9 @@ from typing import Optional from ..common.flag import flags +from ..http.codes import httpStatusCodes from ..http.exception import HttpRequestRejected from ..http.parser import HttpParser -from ..http.codes import httpStatusCodes from ..http.proxy import HttpProxyBasePlugin diff --git a/proxy/plugin/filter_by_upstream.py b/proxy/plugin/filter_by_upstream.py index 18c4cb4623..ad8a7c9b00 100644 --- a/proxy/plugin/filter_by_upstream.py +++ b/proxy/plugin/filter_by_upstream.py @@ -10,11 +10,11 @@ """ from typing import Optional -from ..common.utils import text_ from ..common.flag import flags +from ..common.utils import text_ +from ..http.codes import httpStatusCodes from ..http.exception import HttpRequestRejected from ..http.parser import HttpParser -from ..http.codes import httpStatusCodes from ..http.proxy import HttpProxyBasePlugin diff --git a/proxy/plugin/filter_by_url_regex.py b/proxy/plugin/filter_by_url_regex.py index c9e9407111..514a341f81 100644 --- a/proxy/plugin/filter_by_url_regex.py +++ b/proxy/plugin/filter_by_url_regex.py @@ -10,17 +10,16 @@ """ import json import logging - -from typing import Optional, List, Dict, Any +import re +from typing import Any, Dict, List, Optional from ..common.flag import flags +from ..common.utils import text_ +from ..http.codes import httpStatusCodes from ..http.exception import HttpRequestRejected from ..http.parser import HttpParser -from ..http.codes import httpStatusCodes from ..http.proxy import HttpProxyBasePlugin -from ..common.utils import text_ -import re logger = logging.getLogger(__name__) diff --git a/proxy/plugin/man_in_the_middle.py b/proxy/plugin/man_in_the_middle.py index d7723fae94..679c363121 100644 --- a/proxy/plugin/man_in_the_middle.py +++ b/proxy/plugin/man_in_the_middle.py @@ -11,8 +11,8 @@ from typing import Optional from ..common.utils import build_http_response -from ..http.parser import HttpParser from ..http.codes import httpStatusCodes +from ..http.parser import HttpParser from ..http.proxy import HttpProxyBasePlugin diff --git a/proxy/plugin/mock_rest_api.py b/proxy/plugin/mock_rest_api.py index 9768422133..66665ec3b4 100644 --- a/proxy/plugin/mock_rest_api.py +++ b/proxy/plugin/mock_rest_api.py @@ -11,10 +11,10 @@ import json from typing import Optional -from ..common.utils import bytes_, build_http_response, text_ +from ..common.utils import build_http_response, bytes_, text_ +from ..http.codes import httpStatusCodes from ..http.parser import HttpParser from ..http.proxy import HttpProxyBasePlugin -from ..http.codes import httpStatusCodes class ProposedRestApiPlugin(HttpProxyBasePlugin): diff --git a/proxy/plugin/modify_chunk_response.py b/proxy/plugin/modify_chunk_response.py index b3533784d9..d2b95364b7 100644 --- a/proxy/plugin/modify_chunk_response.py +++ b/proxy/plugin/modify_chunk_response.py @@ -8,9 +8,9 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -from typing import Optional, Any +from typing import Any, Optional -from ..http.parser import HttpParser, httpParserTypes, httpParserStates +from ..http.parser import HttpParser, httpParserStates, httpParserTypes from ..http.proxy import HttpProxyBasePlugin diff --git a/proxy/plugin/modify_post_data.py b/proxy/plugin/modify_post_data.py index 5a8db81a0d..c67ad8bf00 100644 --- a/proxy/plugin/modify_post_data.py +++ b/proxy/plugin/modify_post_data.py @@ -11,9 +11,9 @@ from typing import Optional from ..common.utils import bytes_ +from ..http.methods import httpMethods from ..http.parser import HttpParser from ..http.proxy import HttpProxyBasePlugin -from ..http.methods import httpMethods class ModifyPostDataPlugin(HttpProxyBasePlugin): diff --git a/proxy/plugin/proxy_pool.py b/proxy/plugin/proxy_pool.py index 17877487a5..fe2ce3f02d 100644 --- a/proxy/plugin/proxy_pool.py +++ b/proxy/plugin/proxy_pool.py @@ -8,18 +8,18 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ +import logging import random import socket -import logging - -from typing import Dict, List, Optional, Any, Tuple +from typing import Any, Dict, List, Optional, Tuple -from ..core.connection.server import TcpServerConnection from ..common.types import Readables, Writables +from ..core.connection.server import TcpServerConnection from ..http.exception import HttpProtocolException -from ..http.proxy import HttpProxyBasePlugin -from ..http.parser import HttpParser from ..http.methods import httpMethods +from ..http.parser import HttpParser +from ..http.proxy import HttpProxyBasePlugin + logger = logging.getLogger(__name__) diff --git a/proxy/plugin/redirect_to_custom_server.py b/proxy/plugin/redirect_to_custom_server.py index 75d38359eb..58017a4275 100644 --- a/proxy/plugin/redirect_to_custom_server.py +++ b/proxy/plugin/redirect_to_custom_server.py @@ -8,12 +8,12 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -from urllib import parse as urlparse from typing import Optional +from urllib import parse as urlparse -from ..http.proxy import HttpProxyBasePlugin -from ..http.parser import HttpParser from ..http.methods import httpMethods +from ..http.parser import HttpParser +from ..http.proxy import HttpProxyBasePlugin class RedirectToCustomServerPlugin(HttpProxyBasePlugin): diff --git a/proxy/plugin/reverse_proxy.py b/proxy/plugin/reverse_proxy.py index 19cd13e774..7bbb0ac950 100644 --- a/proxy/plugin/reverse_proxy.py +++ b/proxy/plugin/reverse_proxy.py @@ -8,24 +8,24 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import ssl +import logging import random import socket -import logging +import ssl import sysconfig - from pathlib import Path -from typing import List, Optional, Tuple, Any +from typing import Any, List, Optional, Tuple from urllib import parse as urlparse -from ..common.utils import text_ -from ..common.constants import DEFAULT_HTTPS_PORT, DEFAULT_HTTP_PORT +from ..common.constants import DEFAULT_HTTP_PORT, DEFAULT_HTTPS_PORT from ..common.types import Readables, Writables +from ..common.utils import text_ from ..core.connection import TcpServerConnection from ..http.exception import HttpProtocolException from ..http.parser import HttpParser -from ..http.websocket import WebsocketFrame from ..http.server import HttpWebServerBasePlugin, httpProtocolTypes +from ..http.websocket import WebsocketFrame + logger = logging.getLogger(__name__) diff --git a/proxy/plugin/shortlink.py b/proxy/plugin/shortlink.py index 43554dcaaa..eb07824971 100644 --- a/proxy/plugin/shortlink.py +++ b/proxy/plugin/shortlink.py @@ -12,8 +12,8 @@ from ..common.constants import DOT, SLASH from ..common.utils import build_http_response -from ..http.parser import HttpParser from ..http.codes import httpStatusCodes +from ..http.parser import HttpParser from ..http.proxy import HttpProxyBasePlugin diff --git a/proxy/plugin/web_server_route.py b/proxy/plugin/web_server_route.py index 3675db9704..b502fd71a9 100644 --- a/proxy/plugin/web_server_route.py +++ b/proxy/plugin/web_server_route.py @@ -12,10 +12,11 @@ from typing import List, Tuple from ..common.utils import build_http_response -from ..http.parser import HttpParser from ..http.codes import httpStatusCodes -from ..http.websocket import WebsocketFrame +from ..http.parser import HttpParser from ..http.server import HttpWebServerBasePlugin, httpProtocolTypes +from ..http.websocket import WebsocketFrame + logger = logging.getLogger(__name__) diff --git a/proxy/proxy.py b/proxy/proxy.py index 988dec4535..e1014780ea 100644 --- a/proxy/proxy.py +++ b/proxy/proxy.py @@ -12,38 +12,34 @@ import argparse import base64 import collections +import importlib +import inspect import ipaddress +import logging import multiprocessing import os import socket import sys import time -import logging -import importlib -import inspect - from types import TracebackType -from typing import Dict, List, Optional, Any, Type, Union, cast +from typing import Any, Dict, List, Optional, Type, Union, cast from proxy.core.acceptor.work import Work - -from .common.utils import bytes_, text_, setup_logger +from .common.constants import COMMA, DEFAULT_DATA_DIRECTORY_PATH, DEFAULT_DEVTOOLS_WS_PATH, DEFAULT_DISABLE_HEADERS +from .common.constants import DEFAULT_DISABLE_HTTP_PROXY, DEFAULT_ENABLE_DASHBOARD, DEFAULT_ENABLE_DEVTOOLS +from .common.constants import DEFAULT_ENABLE_EVENTS, DEFAULT_ENABLE_STATIC_SERVER, DEFAULT_ENABLE_WEB_SERVER, DEFAULT_LOG_FILE +from .common.constants import DEFAULT_LOG_FORMAT, DEFAULT_LOG_LEVEL, DEFAULT_NUM_WORKERS, DEFAULT_OPEN_FILE_LIMIT +from .common.constants import DEFAULT_PID_FILE, DEFAULT_PLUGINS, DEFAULT_VERSION, DOT, PLUGIN_DASHBOARD +from .common.constants import PLUGIN_DEVTOOLS_PROTOCOL, PLUGIN_HTTP_PROXY, PLUGIN_INSPECT_TRAFFIC, PLUGIN_PAC_FILE +from .common.constants import PLUGIN_PROXY_AUTH, PLUGIN_WEB_SERVER, PY2_DEPRECATION_MESSAGE +from .common.flag import flags from .common.types import IpAddress +from .common.utils import bytes_, setup_logger, text_ from .common.version import __version__ from .core.acceptor import AcceptorPool -from .http.handler import HttpProtocolHandler from .core.event import EventManager -from .common.flag import flags -from .common.constants import COMMA, DEFAULT_DATA_DIRECTORY_PATH, PLUGIN_PROXY_AUTH -from .common.constants import DEFAULT_DEVTOOLS_WS_PATH, DEFAULT_DISABLE_HEADERS -from .common.constants import DEFAULT_DISABLE_HTTP_PROXY, DEFAULT_NUM_WORKERS -from .common.constants import DEFAULT_ENABLE_DASHBOARD, DEFAULT_ENABLE_DEVTOOLS -from .common.constants import DEFAULT_ENABLE_STATIC_SERVER, DEFAULT_ENABLE_WEB_SERVER -from .common.constants import DEFAULT_LOG_FILE, DEFAULT_LOG_FORMAT, DEFAULT_LOG_LEVEL -from .common.constants import DEFAULT_OPEN_FILE_LIMIT, DEFAULT_PID_FILE, DEFAULT_PLUGINS -from .common.constants import DEFAULT_VERSION, DOT, PLUGIN_DASHBOARD, PLUGIN_DEVTOOLS_PROTOCOL -from .common.constants import PLUGIN_HTTP_PROXY, PLUGIN_INSPECT_TRAFFIC, PLUGIN_PAC_FILE -from .common.constants import PLUGIN_WEB_SERVER, PY2_DEPRECATION_MESSAGE, DEFAULT_ENABLE_EVENTS +from .http.handler import HttpProtocolHandler + if os.name != 'nt': import resource diff --git a/proxy/testing/test_case.py b/proxy/testing/test_case.py index 6416f82f37..3c19e3d719 100644 --- a/proxy/testing/test_case.py +++ b/proxy/testing/test_case.py @@ -11,12 +11,12 @@ import contextlib import time import unittest -from typing import Optional, List, Generator, Any +from typing import Any, Generator, List, Optional -from ..proxy import Proxy from ..common.constants import DEFAULT_TIMEOUT from ..common.utils import new_socket_connection from ..plugin import CacheResponsesPlugin +from ..proxy import Proxy class TestCase(unittest.TestCase): diff --git a/tests/__init__.py b/tests/__init__.py index 891fe5fddd..5831815f1f 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -12,4 +12,5 @@ from proxy.common.constants import DEFAULT_LOG_FORMAT + logging.basicConfig(level=logging.DEBUG, format=DEFAULT_LOG_FORMAT) diff --git a/tests/common/test_flags.py b/tests/common/test_flags.py index a11c9ecee3..eb6b98c7db 100644 --- a/tests/common/test_flags.py +++ b/tests/common/test_flags.py @@ -8,16 +8,14 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -from proxy.common.utils import bytes_ -from proxy.common.constants import PLUGIN_HTTP_PROXY import unittest +from typing import Dict, List -from typing import List, Dict - -from proxy.proxy import Proxy +from proxy.common.constants import PLUGIN_HTTP_PROXY +from proxy.common.utils import bytes_ from proxy.http.proxy import HttpProxyPlugin -from proxy.plugin import CacheResponsesPlugin -from proxy.plugin import FilterByUpstreamHostPlugin +from proxy.plugin import CacheResponsesPlugin, FilterByUpstreamHostPlugin +from proxy.proxy import Proxy class TestFlags(unittest.TestCase): diff --git a/tests/common/test_pki.py b/tests/common/test_pki.py index 76dd723837..3d6065b209 100644 --- a/tests/common/test_pki.py +++ b/tests/common/test_pki.py @@ -9,11 +9,11 @@ :license: BSD, see LICENSE for more details. """ import os +import subprocess import tempfile import unittest -import subprocess -from unittest import mock from typing import Tuple +from unittest import mock from proxy.common import pki diff --git a/tests/common/test_text_bytes.py b/tests/common/test_text_bytes.py index c80fa23b5e..0eddb3ee63 100644 --- a/tests/common/test_text_bytes.py +++ b/tests/common/test_text_bytes.py @@ -10,7 +10,7 @@ """ import unittest -from proxy.common.utils import text_, bytes_ +from proxy.common.utils import bytes_, text_ class TestTextBytes(unittest.TestCase): diff --git a/tests/common/test_utils.py b/tests/common/test_utils.py index 6d6217a641..9387da06c8 100644 --- a/tests/common/test_utils.py +++ b/tests/common/test_utils.py @@ -12,8 +12,8 @@ import unittest from unittest import mock -from proxy.common.constants import DEFAULT_IPV6_HOSTNAME, DEFAULT_IPV4_HOSTNAME, DEFAULT_PORT, DEFAULT_TIMEOUT -from proxy.common.constants import DEFAULT_HTTP_PORT +from proxy.common.constants import DEFAULT_HTTP_PORT, DEFAULT_IPV4_HOSTNAME, DEFAULT_IPV6_HOSTNAME, DEFAULT_PORT +from proxy.common.constants import DEFAULT_TIMEOUT from proxy.common.utils import new_socket_connection, socket_connection diff --git a/tests/core/test_acceptor.py b/tests/core/test_acceptor.py index ceb863d2ba..da3b751ff1 100644 --- a/tests/core/test_acceptor.py +++ b/tests/core/test_acceptor.py @@ -8,10 +8,10 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import unittest -import socket -import selectors import multiprocessing +import selectors +import socket +import unittest from unittest import mock from proxy.core.acceptor import Acceptor diff --git a/tests/core/test_acceptor_pool.py b/tests/core/test_acceptor_pool.py index c7f46bf4ac..de7977b1eb 100644 --- a/tests/core/test_acceptor_pool.py +++ b/tests/core/test_acceptor_pool.py @@ -8,12 +8,12 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import unittest import socket +import unittest from unittest import mock -from proxy.proxy import Proxy from proxy.core.acceptor import AcceptorPool +from proxy.proxy import Proxy class TestAcceptorPool(unittest.TestCase): diff --git a/tests/core/test_connection.py b/tests/core/test_connection.py index 905ab56d2b..ca1fb28148 100644 --- a/tests/core/test_connection.py +++ b/tests/core/test_connection.py @@ -8,15 +8,15 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import unittest import socket import ssl -from unittest import mock +import unittest from typing import Optional, Union +from unittest import mock -from proxy.core.connection import tcpConnectionTypes, TcpConnectionUninitializedException -from proxy.core.connection import TcpServerConnection, TcpConnection, TcpClientConnection -from proxy.common.constants import DEFAULT_IPV6_HOSTNAME, DEFAULT_PORT, DEFAULT_IPV4_HOSTNAME +from proxy.common.constants import DEFAULT_IPV4_HOSTNAME, DEFAULT_IPV6_HOSTNAME, DEFAULT_PORT +from proxy.core.connection import TcpClientConnection, TcpConnection, TcpConnectionUninitializedException, TcpServerConnection +from proxy.core.connection import tcpConnectionTypes class TestTcpConnection(unittest.TestCase): diff --git a/tests/core/test_event_dispatcher.py b/tests/core/test_event_dispatcher.py index eb890b3b0c..3663f4da0a 100644 --- a/tests/core/test_event_dispatcher.py +++ b/tests/core/test_event_dispatcher.py @@ -10,10 +10,9 @@ """ import multiprocessing import os +import queue import threading import unittest -import queue - from unittest import mock from proxy.common.types import DictQueueType diff --git a/tests/core/test_event_queue.py b/tests/core/test_event_queue.py index 1955f918e7..4f298c7d12 100644 --- a/tests/core/test_event_queue.py +++ b/tests/core/test_event_queue.py @@ -12,11 +12,11 @@ import os import threading import unittest - from unittest import mock from proxy.core.event import EventQueue, eventNames + MANAGER = multiprocessing.Manager() diff --git a/tests/core/test_event_subscriber.py b/tests/core/test_event_subscriber.py index 8c440a8866..137da17a1d 100644 --- a/tests/core/test_event_subscriber.py +++ b/tests/core/test_event_subscriber.py @@ -8,15 +8,15 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ +import multiprocessing import os import threading import unittest -import multiprocessing -from typing import Dict, Any - +from typing import Any, Dict from unittest import mock -from proxy.core.event import EventQueue, EventDispatcher, EventSubscriber, eventNames +from proxy.core.event import EventDispatcher, EventQueue, EventSubscriber, eventNames + PUBLISHER_ID = threading.get_ident() diff --git a/tests/http/exceptions/test_http_proxy_auth_failed.py b/tests/http/exceptions/test_http_proxy_auth_failed.py index 3faff4d504..1296d5ca0a 100644 --- a/tests/http/exceptions/test_http_proxy_auth_failed.py +++ b/tests/http/exceptions/test_http_proxy_auth_failed.py @@ -12,11 +12,11 @@ import unittest from unittest import mock -from proxy.proxy import Proxy +from proxy.common.utils import build_http_request +from proxy.core.connection import TcpClientConnection from proxy.http.exception.proxy_auth_failed import ProxyAuthenticationFailed from proxy.http.handler import HttpProtocolHandler -from proxy.core.connection import TcpClientConnection -from proxy.common.utils import build_http_request +from proxy.proxy import Proxy class TestHttpProxyAuthFailed(unittest.TestCase): diff --git a/tests/http/exceptions/test_http_request_rejected.py b/tests/http/exceptions/test_http_request_rejected.py index 457b8dc84d..65b53c3feb 100644 --- a/tests/http/exceptions/test_http_request_rejected.py +++ b/tests/http/exceptions/test_http_request_rejected.py @@ -10,11 +10,11 @@ """ import unittest -from proxy.http.parser import HttpParser, httpParserTypes -from proxy.http.exception import HttpRequestRejected from proxy.common.constants import CRLF from proxy.common.utils import build_http_response from proxy.http.codes import httpStatusCodes +from proxy.http.exception import HttpRequestRejected +from proxy.http.parser import HttpParser, httpParserTypes class TestHttpRequestRejected(unittest.TestCase): diff --git a/tests/http/test_chunk_parser.py b/tests/http/test_chunk_parser.py index eb47c264d1..5503b76f43 100644 --- a/tests/http/test_chunk_parser.py +++ b/tests/http/test_chunk_parser.py @@ -10,7 +10,7 @@ """ import unittest -from proxy.http.chunk_parser import chunkParserStates, ChunkParser +from proxy.http.chunk_parser import ChunkParser, chunkParserStates class TestChunkParser(unittest.TestCase): diff --git a/tests/http/test_http_parser.py b/tests/http/test_http_parser.py index 29e0cebdf2..ac3fb21546 100644 --- a/tests/http/test_http_parser.py +++ b/tests/http/test_http_parser.py @@ -11,10 +11,10 @@ import unittest from proxy.common.constants import CRLF -from proxy.common.utils import build_http_request, find_http_line, build_http_response, build_http_header, bytes_ -from proxy.http.methods import httpMethods +from proxy.common.utils import build_http_header, build_http_request, build_http_response, bytes_, find_http_line from proxy.http.codes import httpStatusCodes -from proxy.http.parser import HttpParser, httpParserTypes, httpParserStates +from proxy.http.methods import httpMethods +from proxy.http.parser import HttpParser, httpParserStates, httpParserTypes class TestHttpParser(unittest.TestCase): diff --git a/tests/http/test_http_proxy.py b/tests/http/test_http_proxy.py index 5616b9e75f..746293057f 100644 --- a/tests/http/test_http_proxy.py +++ b/tests/http/test_http_proxy.py @@ -8,17 +8,17 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import unittest import selectors +import unittest from unittest import mock from proxy.common.constants import DEFAULT_HTTP_PORT -from proxy.proxy import Proxy +from proxy.common.utils import build_http_request from proxy.core.connection import TcpClientConnection -from proxy.http.proxy import HttpProxyPlugin -from proxy.http.handler import HttpProtocolHandler from proxy.http.exception import HttpProtocolException -from proxy.common.utils import build_http_request +from proxy.http.handler import HttpProtocolHandler +from proxy.http.proxy import HttpProxyPlugin +from proxy.proxy import Proxy class TestHttpProxyPlugin(unittest.TestCase): diff --git a/tests/http/test_http_proxy_tls_interception.py b/tests/http/test_http_proxy_tls_interception.py index bbbc6d5bb0..9edb6f67b7 100644 --- a/tests/http/test_http_proxy_tls_interception.py +++ b/tests/http/test_http_proxy_tls_interception.py @@ -8,20 +8,19 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import uuid -import unittest +import selectors import socket import ssl -import selectors - +import unittest +import uuid from typing import Any from unittest import mock +from proxy.common.utils import build_http_request, bytes_ from proxy.core.connection import TcpClientConnection, TcpServerConnection from proxy.http.handler import HttpProtocolHandler -from proxy.http.proxy import HttpProxyPlugin from proxy.http.methods import httpMethods -from proxy.common.utils import build_http_request, bytes_ +from proxy.http.proxy import HttpProxyPlugin from proxy.proxy import Proxy diff --git a/tests/http/test_protocol_handler.py b/tests/http/test_protocol_handler.py index fe365e6a95..7268aa1899 100644 --- a/tests/http/test_protocol_handler.py +++ b/tests/http/test_protocol_handler.py @@ -8,23 +8,21 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import unittest -import selectors import base64 - +import selectors +import unittest from typing import cast from unittest import mock -from proxy.proxy import Proxy -from proxy.common.version import __version__ -from proxy.common.utils import bytes_ from proxy.common.constants import CRLF, PLUGIN_HTTP_PROXY, PLUGIN_PROXY_AUTH, PLUGIN_WEB_SERVER +from proxy.common.utils import bytes_ +from proxy.common.version import __version__ from proxy.core.connection import TcpClientConnection -from proxy.http.parser import HttpParser -from proxy.http.proxy import HttpProxyPlugin -from proxy.http.parser import httpParserStates, httpParserTypes from proxy.http.exception import ProxyAuthenticationFailed, ProxyConnectionFailed from proxy.http.handler import HttpProtocolHandler +from proxy.http.parser import HttpParser, httpParserStates, httpParserTypes +from proxy.http.proxy import HttpProxyPlugin +from proxy.proxy import Proxy class TestHttpProtocolHandler(unittest.TestCase): diff --git a/tests/http/test_web_server.py b/tests/http/test_web_server.py index b95bd7c671..3762cb0cff 100644 --- a/tests/http/test_web_server.py +++ b/tests/http/test_web_server.py @@ -10,18 +10,18 @@ """ import gzip import os +import selectors import tempfile import unittest -import selectors from unittest import mock -from proxy.proxy import Proxy +from proxy.common.constants import CRLF, PLUGIN_HTTP_PROXY, PLUGIN_PAC_FILE, PLUGIN_WEB_SERVER, PROXY_PY_DIR +from proxy.common.utils import build_http_request, build_http_response, bytes_, text_ from proxy.core.connection import TcpClientConnection from proxy.http.handler import HttpProtocolHandler from proxy.http.parser import httpParserStates -from proxy.common.utils import build_http_response, build_http_request, bytes_, text_ -from proxy.common.constants import CRLF, PLUGIN_HTTP_PROXY, PLUGIN_PAC_FILE, PLUGIN_WEB_SERVER, PROXY_PY_DIR from proxy.http.server import HttpWebServerPlugin +from proxy.proxy import Proxy class TestWebServerPlugin(unittest.TestCase): diff --git a/tests/http/test_websocket_client.py b/tests/http/test_websocket_client.py index ef500c5b97..b549df9deb 100644 --- a/tests/http/test_websocket_client.py +++ b/tests/http/test_websocket_client.py @@ -11,9 +11,9 @@ import unittest from unittest import mock -from proxy.common.utils import build_websocket_handshake_response, build_websocket_handshake_request -from proxy.http.websocket import WebsocketClient, WebsocketFrame from proxy.common.constants import DEFAULT_PORT +from proxy.common.utils import build_websocket_handshake_request, build_websocket_handshake_response +from proxy.http.websocket import WebsocketClient, WebsocketFrame class TestWebsocketClient(unittest.TestCase): diff --git a/tests/plugin/test_http_proxy_plugins.py b/tests/plugin/test_http_proxy_plugins.py index ab62147a8d..bb64e5be72 100644 --- a/tests/plugin/test_http_proxy_plugins.py +++ b/tests/plugin/test_http_proxy_plugins.py @@ -8,25 +8,22 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import unittest -import selectors import json - -from urllib import parse as urlparse -from unittest import mock -from typing import cast +import selectors +import unittest from pathlib import Path +from typing import cast +from unittest import mock +from urllib import parse as urlparse -from proxy.proxy import Proxy +from proxy.common.constants import DEFAULT_HTTP_PORT, PROXY_AGENT_HEADER_VALUE +from proxy.common.utils import build_http_request, build_http_response, bytes_ from proxy.core.connection import TcpClientConnection +from proxy.http.codes import httpStatusCodes from proxy.http.handler import HttpProtocolHandler from proxy.http.proxy import HttpProxyPlugin -from proxy.common.utils import build_http_request, bytes_, build_http_response -from proxy.common.constants import PROXY_AGENT_HEADER_VALUE, DEFAULT_HTTP_PORT -from proxy.http.codes import httpStatusCodes - from proxy.plugin import ProposedRestApiPlugin, RedirectToCustomServerPlugin - +from proxy.proxy import Proxy from .utils import get_plugin_by_test_name diff --git a/tests/plugin/test_http_proxy_plugins_with_tls_interception.py b/tests/plugin/test_http_proxy_plugins_with_tls_interception.py index 164edcfee9..10ef33b5cb 100644 --- a/tests/plugin/test_http_proxy_plugins_with_tls_interception.py +++ b/tests/plugin/test_http_proxy_plugins_with_tls_interception.py @@ -8,23 +8,20 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import unittest -import socket import selectors +import socket import ssl - -from unittest import mock +import unittest from typing import Any, cast +from unittest import mock -from proxy.proxy import Proxy -from proxy.common.utils import bytes_ -from proxy.common.utils import build_http_request, build_http_response +from proxy.common.utils import build_http_request, build_http_response, bytes_ from proxy.core.connection import TcpClientConnection, TcpServerConnection from proxy.http.codes import httpStatusCodes -from proxy.http.methods import httpMethods from proxy.http.handler import HttpProtocolHandler +from proxy.http.methods import httpMethods from proxy.http.proxy import HttpProxyPlugin - +from proxy.proxy import Proxy from .utils import get_plugin_by_test_name diff --git a/tests/plugin/utils.py b/tests/plugin/utils.py index 3e58ff12f9..c0ef8f1598 100644 --- a/tests/plugin/utils.py +++ b/tests/plugin/utils.py @@ -9,10 +9,10 @@ :license: BSD, see LICENSE for more details. """ from typing import Type -from proxy.http.proxy import HttpProxyBasePlugin -from proxy.plugin import ModifyPostDataPlugin, ProposedRestApiPlugin, RedirectToCustomServerPlugin, \ - FilterByUpstreamHostPlugin, CacheResponsesPlugin, ManInTheMiddlePlugin, FilterByURLRegexPlugin +from proxy.http.proxy import HttpProxyBasePlugin +from proxy.plugin import CacheResponsesPlugin, FilterByUpstreamHostPlugin, FilterByURLRegexPlugin, ManInTheMiddlePlugin +from proxy.plugin import ModifyPostDataPlugin, ProposedRestApiPlugin, RedirectToCustomServerPlugin def get_plugin_by_test_name(test_name: str) -> Type[HttpProxyBasePlugin]: diff --git a/tests/test_main.py b/tests/test_main.py index 8f4ffba2d9..9ab84a1ef1 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -8,25 +8,23 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import unittest -import tempfile import os - +import tempfile +import unittest from unittest import mock -from proxy.proxy import main, Proxy, entry_point +from proxy.common.constants import DEFAULT_BASIC_AUTH, DEFAULT_CA_CERT_FILE, DEFAULT_CA_KEY_FILE, DEFAULT_CA_SIGNING_KEY_FILE +from proxy.common.constants import DEFAULT_CERT_FILE, DEFAULT_CLIENT_RECVBUF_SIZE, DEFAULT_DEVTOOLS_WS_PATH +from proxy.common.constants import DEFAULT_DISABLE_HTTP_PROXY, DEFAULT_ENABLE_DASHBOARD, DEFAULT_ENABLE_DEVTOOLS +from proxy.common.constants import DEFAULT_ENABLE_EVENTS, DEFAULT_ENABLE_STATIC_SERVER, DEFAULT_ENABLE_WEB_SERVER +from proxy.common.constants import DEFAULT_IPV6_HOSTNAME, DEFAULT_KEY_FILE, DEFAULT_LOG_FILE, DEFAULT_LOG_FORMAT +from proxy.common.constants import DEFAULT_LOG_LEVEL, DEFAULT_NUM_WORKERS, DEFAULT_OPEN_FILE_LIMIT, DEFAULT_PAC_FILE +from proxy.common.constants import DEFAULT_PID_FILE, DEFAULT_PLUGINS, DEFAULT_PORT, DEFAULT_SERVER_RECVBUF_SIZE +from proxy.common.constants import DEFAULT_THREADLESS, DEFAULT_TIMEOUT, PY2_DEPRECATION_MESSAGE from proxy.common.utils import bytes_ -from proxy.http.handler import HttpProtocolHandler - -from proxy.common.constants import DEFAULT_ENABLE_DASHBOARD, DEFAULT_LOG_LEVEL, DEFAULT_LOG_FILE, DEFAULT_LOG_FORMAT -from proxy.common.constants import DEFAULT_TIMEOUT, DEFAULT_DEVTOOLS_WS_PATH, DEFAULT_DISABLE_HTTP_PROXY -from proxy.common.constants import DEFAULT_ENABLE_STATIC_SERVER, DEFAULT_ENABLE_EVENTS, DEFAULT_ENABLE_DEVTOOLS -from proxy.common.constants import DEFAULT_ENABLE_WEB_SERVER, DEFAULT_THREADLESS, DEFAULT_CERT_FILE, DEFAULT_KEY_FILE -from proxy.common.constants import DEFAULT_CA_CERT_FILE, DEFAULT_CA_KEY_FILE, DEFAULT_CA_SIGNING_KEY_FILE -from proxy.common.constants import DEFAULT_PAC_FILE, DEFAULT_PLUGINS, DEFAULT_PID_FILE, DEFAULT_PORT, DEFAULT_BASIC_AUTH -from proxy.common.constants import DEFAULT_NUM_WORKERS, DEFAULT_OPEN_FILE_LIMIT, DEFAULT_IPV6_HOSTNAME -from proxy.common.constants import DEFAULT_SERVER_RECVBUF_SIZE, DEFAULT_CLIENT_RECVBUF_SIZE, PY2_DEPRECATION_MESSAGE from proxy.common.version import __version__ +from proxy.http.handler import HttpProtocolHandler +from proxy.proxy import Proxy, entry_point, main def get_temp_file(name: str) -> str: diff --git a/tests/test_set_open_file_limit.py b/tests/test_set_open_file_limit.py index 1163564bed..4b24940c3b 100644 --- a/tests/test_set_open_file_limit.py +++ b/tests/test_set_open_file_limit.py @@ -14,6 +14,7 @@ from proxy.proxy import Proxy + if os.name != 'nt': import resource diff --git a/tests/testing/test_embed.py b/tests/testing/test_embed.py index 2f9f7d0346..c6525dca9e 100644 --- a/tests/testing/test_embed.py +++ b/tests/testing/test_embed.py @@ -8,15 +8,15 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ +import http.client import os import unittest -import http.client -import urllib.request import urllib.error +import urllib.request from proxy import TestCase from proxy.common.constants import DEFAULT_CLIENT_RECVBUF_SIZE, PROXY_AGENT_HEADER_VALUE -from proxy.common.utils import socket_connection, build_http_request, build_http_response +from proxy.common.utils import build_http_request, build_http_response, socket_connection from proxy.http.codes import httpStatusCodes from proxy.http.methods import httpMethods diff --git a/tests/testing/test_test_case.py b/tests/testing/test_test_case.py index 0d4ee869ce..a073c60567 100644 --- a/tests/testing/test_test_case.py +++ b/tests/testing/test_test_case.py @@ -9,8 +9,8 @@ :license: BSD, see LICENSE for more details. """ import unittest -import proxy +import proxy from proxy.common.utils import get_available_port diff --git a/version-check.py b/version-check.py index a957a40e02..2e0eae41dc 100644 --- a/version-check.py +++ b/version-check.py @@ -8,10 +8,12 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import sys import subprocess +import sys + from proxy.common.version import __version__ as lib_version + # This script ensures our versions never run out of sync. # # 1. TODO: Version is hardcoded in homebrew stable package