diff --git a/examples/https_connect_tunnel.py b/examples/https_connect_tunnel.py index d830993b8b..e60edaeda0 100644 --- a/examples/https_connect_tunnel.py +++ b/examples/https_connect_tunnel.py @@ -14,7 +14,8 @@ from proxy import Proxy from proxy.common.utils import build_http_response -from proxy.http.parser import httpParserStates, httpStatusCodes +from proxy.http import httpStatusCodes +from proxy.http.parser import httpParserStates from proxy.core.base import BaseTcpTunnelHandler diff --git a/proxy/common/pki.py b/proxy/common/pki.py index ffe69b01cb..30eea2aa78 100644 --- a/proxy/common/pki.py +++ b/proxy/common/pki.py @@ -8,15 +8,16 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import time +import os import sys +import uuid +import time +import logging +import tempfile import argparse import contextlib -import os -import uuid import subprocess -import tempfile -import logging + from typing import List, Generator, Optional, Tuple from .utils import bytes_ diff --git a/proxy/core/connection/__init__.py b/proxy/core/connection/__init__.py index 9da32f6cda..88f5902900 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 .connection import TcpConnection, TcpConnectionUninitializedException from .client import TcpClientConnection from .server import TcpServerConnection from .pool import ConnectionPool +from .types import tcpConnectionTypes __all__ = [ 'TcpConnection', diff --git a/proxy/core/connection/client.py b/proxy/core/connection/client.py index 415b5f24b9..a6d91896cf 100644 --- a/proxy/core/connection/client.py +++ b/proxy/core/connection/client.py @@ -8,15 +8,17 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import socket import ssl +import socket + from typing import Union, Tuple, Optional -from .connection import TcpConnection, tcpConnectionTypes, TcpConnectionUninitializedException +from .connection import TcpConnection, TcpConnectionUninitializedException +from .types import tcpConnectionTypes class TcpClientConnection(TcpConnection): - """An accepted client connection request.""" + """A buffered client connection object.""" def __init__( self, diff --git a/proxy/core/connection/connection.py b/proxy/core/connection/connection.py index 69651b8e9c..65b01769bd 100644 --- a/proxy/core/connection/connection.py +++ b/proxy/core/connection/connection.py @@ -8,24 +8,18 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import socket import ssl +import socket import logging + from abc import ABC, abstractmethod -from typing import NamedTuple, Optional, Union, List +from typing import Optional, Union, List from ...common.constants import DEFAULT_BUFFER_SIZE, DEFAULT_MAX_SEND_SIZE -logger = logging.getLogger(__name__) - +from .types import tcpConnectionTypes -TcpConnectionTypes = NamedTuple( - 'TcpConnectionTypes', [ - ('SERVER', int), - ('CLIENT', int), - ], -) -tcpConnectionTypes = TcpConnectionTypes(1, 2) +logger = logging.getLogger(__name__) class TcpConnectionUninitializedException(Exception): diff --git a/proxy/core/connection/pool.py b/proxy/core/connection/pool.py index 5f5745c93a..13a8f9b2a5 100644 --- a/proxy/core/connection/pool.py +++ b/proxy/core/connection/pool.py @@ -13,6 +13,7 @@ from typing import Set, Dict, Tuple from ...common.flag import flags + from .server import TcpServerConnection logger = logging.getLogger(__name__) diff --git a/proxy/core/connection/server.py b/proxy/core/connection/server.py index ae798e98c9..7aae5371cc 100644 --- a/proxy/core/connection/server.py +++ b/proxy/core/connection/server.py @@ -13,13 +13,14 @@ from typing import Optional, Union, Tuple -from .connection import TcpConnection, tcpConnectionTypes, TcpConnectionUninitializedException - from ...common.utils import new_socket_connection +from .connection import TcpConnection, TcpConnectionUninitializedException +from .types import tcpConnectionTypes + class TcpServerConnection(TcpConnection): - """Establishes connection to upstream server.""" + """A buffered server connection object.""" def __init__(self, host: str, port: int) -> None: super().__init__(tcpConnectionTypes.SERVER) diff --git a/proxy/core/connection/types.py b/proxy/core/connection/types.py new file mode 100644 index 0000000000..44522c81b8 --- /dev/null +++ b/proxy/core/connection/types.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +""" + proxy.py + ~~~~~~~~ + ⚡⚡⚡ Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on + Network monitoring, controls & Application development, testing, debugging. + + :copyright: (c) 2013-present by Abhinav Singh and contributors. + :license: BSD, see LICENSE for more details. +""" +from typing import NamedTuple + + +TcpConnectionTypes = NamedTuple( + 'TcpConnectionTypes', [ + ('SERVER', int), + ('CLIENT', int), + ], +) + +tcpConnectionTypes = TcpConnectionTypes(1, 2) diff --git a/proxy/dashboard/dashboard.py b/proxy/dashboard/dashboard.py index 2a82fa828b..9bb2a3eef3 100644 --- a/proxy/dashboard/dashboard.py +++ b/proxy/dashboard/dashboard.py @@ -16,9 +16,11 @@ from .plugin import ProxyDashboardWebsocketPlugin from ..common.utils import build_http_response, bytes_ -from ..http.server import HttpWebServerPlugin, HttpWebServerBasePlugin, httpProtocolTypes -from ..http.parser import HttpParser, httpStatusCodes + +from ..http import httpStatusCodes +from ..http.parser import HttpParser from ..http.websocket import WebsocketFrame +from ..http.server import HttpWebServerPlugin, HttpWebServerBasePlugin, httpProtocolTypes logger = logging.getLogger(__name__) diff --git a/proxy/http/__init__.py b/proxy/http/__init__.py index e293b97993..f8d2e4fa4f 100644 --- a/proxy/http/__init__.py +++ b/proxy/http/__init__.py @@ -10,8 +10,14 @@ """ from .handler import HttpProtocolHandler from .plugin import HttpProtocolHandlerPlugin +from .codes import httpStatusCodes +from .methods import httpMethods +from .url import Url __all__ = [ 'HttpProtocolHandler', 'HttpProtocolHandlerPlugin', + 'httpStatusCodes', + 'httpMethods', + 'Url', ] diff --git a/proxy/http/parser/codes.py b/proxy/http/codes.py similarity index 100% rename from proxy/http/parser/codes.py rename to proxy/http/codes.py diff --git a/proxy/http/exception/proxy_auth_failed.py b/proxy/http/exception/proxy_auth_failed.py index 83bea76d42..60c6e72219 100644 --- a/proxy/http/exception/proxy_auth_failed.py +++ b/proxy/http/exception/proxy_auth_failed.py @@ -9,7 +9,9 @@ :license: BSD, see LICENSE for more details. """ from .base import HttpProtocolException -from ..parser import HttpParser, httpStatusCodes + +from ..codes import httpStatusCodes +from ..parser import HttpParser from ...common.constants import PROXY_AGENT_HEADER_VALUE, PROXY_AGENT_HEADER_KEY from ...common.utils import build_http_response diff --git a/proxy/http/exception/proxy_conn_failed.py b/proxy/http/exception/proxy_conn_failed.py index 4047787934..daef357647 100644 --- a/proxy/http/exception/proxy_conn_failed.py +++ b/proxy/http/exception/proxy_conn_failed.py @@ -9,7 +9,9 @@ :license: BSD, see LICENSE for more details. """ from .base import HttpProtocolException -from ..parser import HttpParser, httpStatusCodes + +from ..codes import httpStatusCodes +from ..parser import HttpParser from ...common.constants import PROXY_AGENT_HEADER_VALUE, PROXY_AGENT_HEADER_KEY from ...common.utils import build_http_response diff --git a/proxy/http/handler.py b/proxy/http/handler.py index 84cab4dabc..6d6d15116a 100644 --- a/proxy/http/handler.py +++ b/proxy/http/handler.py @@ -8,13 +8,13 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import socket -import selectors import ssl import time -import contextlib import errno +import socket import logging +import selectors +import contextlib from typing import Tuple, List, Union, Optional, Generator, Dict, Any diff --git a/proxy/http/parser/methods.py b/proxy/http/methods.py similarity index 99% rename from proxy/http/parser/methods.py rename to proxy/http/methods.py index 823c9e8e97..791b41c5c5 100644 --- a/proxy/http/parser/methods.py +++ b/proxy/http/methods.py @@ -24,6 +24,7 @@ ('PATCH', bytes), ], ) + httpMethods = HttpMethods( b'GET', b'HEAD', diff --git a/proxy/http/parser/__init__.py b/proxy/http/parser/__init__.py index 62e819e4f7..27326c6233 100644 --- a/proxy/http/parser/__init__.py +++ b/proxy/http/parser/__init__.py @@ -10,10 +10,7 @@ """ from .parser import HttpParser from .chunk import ChunkParser, chunkParserStates -from .codes import httpStatusCodes -from .methods import httpMethods from .types import httpParserStates, httpParserTypes -from .url import Url from .protocol import ProxyProtocol, PROXY_PROTOCOL_V2_SIGNATURE __all__ = [ @@ -22,9 +19,6 @@ 'httpParserStates', 'ChunkParser', 'chunkParserStates', - 'httpStatusCodes', - 'httpMethods', - 'Url', 'ProxyProtocol', 'PROXY_PROTOCOL_V2_SIGNATURE', ] diff --git a/proxy/http/parser/parser.py b/proxy/http/parser/parser.py index 912b0f4bed..d43e0c4fe1 100644 --- a/proxy/http/parser/parser.py +++ b/proxy/http/parser/parser.py @@ -16,8 +16,9 @@ from ...common.utils import build_http_request, build_http_response, find_http_line, text_ from ...common.flag import flags -from .url import Url -from .methods import httpMethods +from ..url import Url +from ..methods import httpMethods + from .protocol import ProxyProtocol from .chunk import ChunkParser, chunkParserStates from .types import httpParserTypes, httpParserStates diff --git a/proxy/http/parser/protocol.py b/proxy/http/parser/protocol.py index 0c3623dd27..d749fdc08a 100644 --- a/proxy/http/parser/protocol.py +++ b/proxy/http/parser/protocol.py @@ -9,6 +9,7 @@ :license: BSD, see LICENSE for more details. """ from typing import Optional, Tuple + from ...common.constants import WHITESPACE PROXY_PROTOCOL_V2_SIGNATURE = b'\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A' diff --git a/proxy/http/plugin.py b/proxy/http/plugin.py index 37566f7266..b9e15b7d56 100644 --- a/proxy/http/plugin.py +++ b/proxy/http/plugin.py @@ -8,11 +8,11 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -import argparse import socket +import argparse -from abc import ABC, abstractmethod from uuid import UUID +from abc import ABC, abstractmethod from typing import Tuple, List, Union, Optional from .parser import HttpParser diff --git a/proxy/http/proxy/auth.py b/proxy/http/proxy/auth.py index 10e2997b1a..103e082061 100644 --- a/proxy/http/proxy/auth.py +++ b/proxy/http/proxy/auth.py @@ -11,6 +11,7 @@ from typing import Optional from ..exception import ProxyAuthenticationFailed + from ...common.flag import flags from ...common.constants import DEFAULT_BASIC_AUTH from ...http.parser import HttpParser @@ -41,14 +42,3 @@ def before_upstream_connection( or parts[1] != self.flags.auth_code: raise ProxyAuthenticationFailed() return request - - def handle_client_request( - self, request: HttpParser, - ) -> Optional[HttpParser]: - return request - - def handle_upstream_chunk(self, chunk: memoryview) -> memoryview: - return chunk # pragma: no cover - - def on_upstream_connection_close(self) -> None: - pass # pragma: no cover diff --git a/proxy/http/proxy/plugin.py b/proxy/http/proxy/plugin.py index be093f2c4f..7ca25d049e 100644 --- a/proxy/http/proxy/plugin.py +++ b/proxy/http/proxy/plugin.py @@ -11,9 +11,9 @@ import socket import argparse +from abc import ABC from uuid import UUID from typing import Any, Dict, List, Optional, Tuple -from abc import ABC, abstractmethod from ..parser import HttpParser @@ -93,7 +93,9 @@ def resolve_dns(self, host: str, port: int) -> Tuple[Optional[str], Optional[Tup """ return None, None - @abstractmethod + # No longer abstract since 2.4.0 + # + # @abstractmethod def before_upstream_connection( self, request: HttpParser, ) -> Optional[HttpParser]: @@ -122,7 +124,9 @@ def handle_client_data( """ return raw # pragma: no cover - @abstractmethod + # No longer abstract since 2.4.0 + # + # @abstractmethod def handle_client_request( self, request: HttpParser, ) -> Optional[HttpParser]: @@ -142,7 +146,9 @@ def handle_client_request( """ return request # pragma: no cover - @abstractmethod + # No longer abstract since 2.4.0 + # + # @abstractmethod def handle_upstream_chunk(self, chunk: memoryview) -> memoryview: """Handler called right after receiving raw response from upstream server. @@ -150,7 +156,9 @@ def handle_upstream_chunk(self, chunk: memoryview) -> memoryview: TLS interception is also enabled.""" return chunk # pragma: no cover - @abstractmethod + # No longer abstract since 2.4.0 + # + # @abstractmethod def on_upstream_connection_close(self) -> None: """Handler called right after upstream connection has been closed.""" pass # pragma: no cover diff --git a/proxy/http/proxy/server.py b/proxy/http/proxy/server.py index 562b94af11..6f76e94eaa 100644 --- a/proxy/http/proxy/server.py +++ b/proxy/http/proxy/server.py @@ -20,9 +20,12 @@ from typing import Optional, List, Union, Dict, cast, Any, Tuple from .plugin import HttpProxyBasePlugin + +from ..methods import httpMethods +from ..codes import httpStatusCodes from ..plugin import HttpProtocolHandlerPlugin from ..exception import HttpProtocolException, ProxyConnectionFailed -from ..parser import HttpParser, httpParserStates, httpParserTypes, httpStatusCodes, httpMethods +from ..parser import HttpParser, httpParserStates, httpParserTypes from ...common.types import Readables, Writables from ...common.constants import DEFAULT_CA_CERT_DIR, DEFAULT_CA_CERT_FILE, DEFAULT_CA_FILE diff --git a/proxy/http/server/pac_plugin.py b/proxy/http/server/pac_plugin.py index 5880137d12..a2e0237a4a 100644 --- a/proxy/http/server/pac_plugin.py +++ b/proxy/http/server/pac_plugin.py @@ -9,12 +9,15 @@ :license: BSD, see LICENSE for more details. """ import gzip + from typing import List, Tuple, Optional, Any 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 diff --git a/proxy/http/server/plugin.py b/proxy/http/server/plugin.py index 9cd5b58513..658ce1a5d2 100644 --- a/proxy/http/server/plugin.py +++ b/proxy/http/server/plugin.py @@ -11,9 +11,9 @@ import socket import argparse +from uuid import UUID from abc import ABC, abstractmethod from typing import Any, Dict, List, Optional, Tuple -from uuid import UUID from ..websocket import WebsocketFrame from ..parser import HttpParser diff --git a/proxy/http/server/protocols.py b/proxy/http/server/protocols.py index b0f6202c06..a3d8434808 100644 --- a/proxy/http/server/protocols.py +++ b/proxy/http/server/protocols.py @@ -17,4 +17,5 @@ ('WEBSOCKET', int), ], ) + httpProtocolTypes = HttpProtocolTypes(1, 2, 3) diff --git a/proxy/http/server/web.py b/proxy/http/server/web.py index b60a21dcaf..b3773bc2ac 100644 --- a/proxy/http/server/web.py +++ b/proxy/http/server/web.py @@ -24,10 +24,11 @@ from ...common.types import Readables, Writables from ...common.flag import flags +from ..codes import httpStatusCodes from ..exception import HttpProtocolException -from ..websocket import WebsocketFrame, websocketOpcodes -from ..parser import HttpParser, httpParserStates, httpParserTypes, httpStatusCodes from ..plugin import HttpProtocolHandlerPlugin +from ..websocket import WebsocketFrame, websocketOpcodes +from ..parser import HttpParser, httpParserStates, httpParserTypes from .plugin import HttpWebServerBasePlugin from .protocols import httpProtocolTypes diff --git a/proxy/http/parser/url.py b/proxy/http/url.py similarity index 98% rename from proxy/http/parser/url.py rename to proxy/http/url.py index 07d113b3ef..05d5fec385 100644 --- a/proxy/http/parser/url.py +++ b/proxy/http/url.py @@ -10,7 +10,7 @@ """ from typing import Optional, Tuple -from ...common.constants import COLON, SLASH +from ..common.constants import COLON, SLASH class Url: diff --git a/proxy/http/websocket/client.py b/proxy/http/websocket/client.py index 528790d12c..648dee0802 100644 --- a/proxy/http/websocket/client.py +++ b/proxy/http/websocket/client.py @@ -8,11 +8,11 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ +import ssl import base64 -import selectors import socket import secrets -import ssl +import selectors from typing import Optional, Union, Callable diff --git a/proxy/http/websocket/frame.py b/proxy/http/websocket/frame.py index 6814de02bf..8207ca9b60 100644 --- a/proxy/http/websocket/frame.py +++ b/proxy/http/websocket/frame.py @@ -8,12 +8,12 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ +import io import hashlib import base64 import struct import secrets import logging -import io from typing import TypeVar, Type, Optional, NamedTuple diff --git a/proxy/plugin/cloudflare_dns.py b/proxy/plugin/cloudflare_dns.py index 6cf1e15688..95ead3eda5 100644 --- a/proxy/plugin/cloudflare_dns.py +++ b/proxy/plugin/cloudflare_dns.py @@ -18,7 +18,6 @@ from typing import Optional, Tuple from ..common.flag import flags -from ..http.parser import HttpParser from ..http.proxy import HttpProxyBasePlugin logger = logging.getLogger(__name__) @@ -80,19 +79,3 @@ def resolve_dns(self, host: str, port: int) -> Tuple[Optional[str], Optional[Tup ), ) return None, None - - def before_upstream_connection( - self, request: HttpParser, - ) -> Optional[HttpParser]: - return request - - def handle_client_request( - self, request: HttpParser, - ) -> Optional[HttpParser]: - return request - - def handle_upstream_chunk(self, chunk: memoryview) -> memoryview: - return chunk - - def on_upstream_connection_close(self) -> None: - pass diff --git a/proxy/plugin/custom_dns_resolver.py b/proxy/plugin/custom_dns_resolver.py index d714b5c9e8..a61480aa2f 100644 --- a/proxy/plugin/custom_dns_resolver.py +++ b/proxy/plugin/custom_dns_resolver.py @@ -12,7 +12,6 @@ from typing import Optional, Tuple -from ..http.parser import HttpParser from ..http.proxy import HttpProxyBasePlugin @@ -34,19 +33,3 @@ def resolve_dns(self, host: str, port: int) -> Tuple[Optional[str], Optional[Tup # Ideally we can also thrown HttpRequestRejected or HttpProtocolException here # Returning None simply fallback to core generated exceptions. return None, None - - def before_upstream_connection( - self, request: HttpParser, - ) -> Optional[HttpParser]: - return request - - def handle_client_request( - self, request: HttpParser, - ) -> Optional[HttpParser]: - return request - - def handle_upstream_chunk(self, chunk: memoryview) -> memoryview: - return chunk - - def on_upstream_connection_close(self) -> None: - pass diff --git a/proxy/plugin/filter_by_client_ip.py b/proxy/plugin/filter_by_client_ip.py index aad88145e9..e946186c5c 100644 --- a/proxy/plugin/filter_by_client_ip.py +++ b/proxy/plugin/filter_by_client_ip.py @@ -11,9 +11,11 @@ from typing import Optional from ..common.flag import flags -from ..http.exception import HttpRequestRejected -from ..http.parser import HttpParser, httpStatusCodes + +from ..http import httpStatusCodes +from ..http.parser import HttpParser from ..http.proxy import HttpProxyBasePlugin +from ..http.exception import HttpRequestRejected flags.add_argument( @@ -39,14 +41,3 @@ def before_upstream_connection( }, ) return request - - def handle_client_request( - self, request: HttpParser, - ) -> Optional[HttpParser]: - return request - - def handle_upstream_chunk(self, chunk: memoryview) -> memoryview: - return chunk - - def on_upstream_connection_close(self) -> None: - pass diff --git a/proxy/plugin/filter_by_upstream.py b/proxy/plugin/filter_by_upstream.py index 59fdcdcceb..0970fa6eac 100644 --- a/proxy/plugin/filter_by_upstream.py +++ b/proxy/plugin/filter_by_upstream.py @@ -12,9 +12,11 @@ from ..common.utils import text_ from ..common.flag import flags -from ..http.exception import HttpRequestRejected -from ..http.parser import HttpParser, httpStatusCodes + +from ..http import httpStatusCodes +from ..http.parser import HttpParser from ..http.proxy import HttpProxyBasePlugin +from ..http.exception import HttpRequestRejected flags.add_argument( @@ -39,14 +41,3 @@ def before_upstream_connection( }, ) return request - - def handle_client_request( - self, request: HttpParser, - ) -> Optional[HttpParser]: - return request - - def handle_upstream_chunk(self, chunk: memoryview) -> memoryview: - return chunk - - def on_upstream_connection_close(self) -> None: - pass diff --git a/proxy/plugin/filter_by_url_regex.py b/proxy/plugin/filter_by_url_regex.py index db25e87873..15d2531452 100644 --- a/proxy/plugin/filter_by_url_regex.py +++ b/proxy/plugin/filter_by_url_regex.py @@ -14,11 +14,13 @@ from typing import Optional, List, Dict, Any from ..common.flag import flags -from ..http.exception import HttpRequestRejected -from ..http.parser import HttpParser, httpStatusCodes -from ..http.proxy import HttpProxyBasePlugin from ..common.utils import text_ +from ..http import httpStatusCodes +from ..http.parser import HttpParser +from ..http.proxy import HttpProxyBasePlugin +from ..http.exception import HttpRequestRejected + import re logger = logging.getLogger(__name__) @@ -46,11 +48,6 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: with open(self.flags.filtered_url_regex_config, 'rb') as f: self.filters = json.load(f) - def before_upstream_connection( - self, request: HttpParser, - ) -> Optional[HttpParser]: - return request - def handle_client_request( self, request: HttpParser, ) -> Optional[HttpParser]: @@ -96,9 +93,3 @@ def handle_client_request( # increment rule number rule_number += 1 return request - - def handle_upstream_chunk(self, chunk: memoryview) -> memoryview: - return chunk - - def on_upstream_connection_close(self) -> None: - pass diff --git a/proxy/plugin/man_in_the_middle.py b/proxy/plugin/man_in_the_middle.py index d2be0fc1af..970547e6f2 100644 --- a/proxy/plugin/man_in_the_middle.py +++ b/proxy/plugin/man_in_the_middle.py @@ -8,26 +8,14 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ -from typing import Optional - from ..common.utils import build_http_response -from ..http.parser import HttpParser, httpStatusCodes +from ..http import httpStatusCodes from ..http.proxy import HttpProxyBasePlugin class ManInTheMiddlePlugin(HttpProxyBasePlugin): """Modifies upstream server responses.""" - def before_upstream_connection( - self, request: HttpParser, - ) -> Optional[HttpParser]: - return request - - def handle_client_request( - self, request: HttpParser, - ) -> Optional[HttpParser]: - return request - def handle_upstream_chunk(self, chunk: memoryview) -> memoryview: return memoryview( build_http_response( @@ -36,6 +24,3 @@ def handle_upstream_chunk(self, chunk: memoryview) -> memoryview: body=b'Hello from man in the middle', ), ) - - def on_upstream_connection_close(self) -> None: - pass diff --git a/proxy/plugin/mock_rest_api.py b/proxy/plugin/mock_rest_api.py index 1309fc6ea6..2643d1e038 100644 --- a/proxy/plugin/mock_rest_api.py +++ b/proxy/plugin/mock_rest_api.py @@ -12,7 +12,9 @@ from typing import Optional from ..common.utils import bytes_, build_http_response, text_ -from ..http.parser import HttpParser, httpStatusCodes + +from ..http import httpStatusCodes +from ..http.parser import HttpParser from ..http.proxy import HttpProxyBasePlugin @@ -92,9 +94,3 @@ def handle_client_request( ), ) return None - - def handle_upstream_chunk(self, chunk: memoryview) -> memoryview: - return chunk - - def on_upstream_connection_close(self) -> None: - pass diff --git a/proxy/plugin/modify_chunk_response.py b/proxy/plugin/modify_chunk_response.py index 440a63d852..ccfba8cb5c 100644 --- a/proxy/plugin/modify_chunk_response.py +++ b/proxy/plugin/modify_chunk_response.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, Any +from typing import Any from ..http.parser import HttpParser, httpParserTypes, httpParserStates from ..http.proxy import HttpProxyBasePlugin @@ -29,16 +29,6 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: # Create a new http protocol parser for response payloads self.response = HttpParser(httpParserTypes.RESPONSE_PARSER) - def before_upstream_connection( - self, request: HttpParser, - ) -> Optional[HttpParser]: - return request - - def handle_client_request( - self, request: HttpParser, - ) -> Optional[HttpParser]: - return request - def handle_upstream_chunk(self, chunk: memoryview) -> memoryview: # Parse the response. # Note that these chunks also include headers @@ -51,6 +41,3 @@ def handle_upstream_chunk(self, chunk: memoryview) -> memoryview: self.response.body = b'\n'.join(self.DEFAULT_CHUNKS) + b'\n' self.client.queue(memoryview(self.response.build_response())) return memoryview(b'') - - def on_upstream_connection_close(self) -> None: - pass diff --git a/proxy/plugin/modify_post_data.py b/proxy/plugin/modify_post_data.py index 9ac0b376d5..97d696261a 100644 --- a/proxy/plugin/modify_post_data.py +++ b/proxy/plugin/modify_post_data.py @@ -11,7 +11,9 @@ from typing import Optional from ..common.utils import bytes_ -from ..http.parser import HttpParser, httpMethods + +from ..http import httpMethods +from ..http.parser import HttpParser from ..http.proxy import HttpProxyBasePlugin @@ -42,9 +44,3 @@ def handle_client_request( request.del_header(b'Content-Type') request.add_header(b'Content-Type', b'application/json') return request - - def handle_upstream_chunk(self, chunk: memoryview) -> memoryview: - return chunk - - def on_upstream_connection_close(self) -> None: - pass diff --git a/proxy/plugin/proxy_pool.py b/proxy/plugin/proxy_pool.py index c6385c447a..3751fd37ad 100644 --- a/proxy/plugin/proxy_pool.py +++ b/proxy/plugin/proxy_pool.py @@ -14,11 +14,14 @@ from typing import Dict, List, Optional, Any, Tuple -from ..common.types import Readables, Writables from ..common.flag import flags +from ..common.types import Readables, Writables + +from ..http import Url, httpMethods +from ..http.parser import HttpParser from ..http.exception import HttpProtocolException from ..http.proxy import HttpProxyBasePlugin -from ..http.parser import HttpParser, Url, httpMethods + from ..core.connection.server import TcpServerConnection logger = logging.getLogger(__name__) diff --git a/proxy/plugin/redirect_to_custom_server.py b/proxy/plugin/redirect_to_custom_server.py index bb500d32ee..d04d7ca089 100644 --- a/proxy/plugin/redirect_to_custom_server.py +++ b/proxy/plugin/redirect_to_custom_server.py @@ -35,14 +35,3 @@ def before_upstream_connection( ).netloc, ) return request - - def handle_client_request( - self, request: HttpParser, - ) -> Optional[HttpParser]: - return request - - def handle_upstream_chunk(self, chunk: memoryview) -> memoryview: - return chunk - - def on_upstream_connection_close(self) -> None: - pass diff --git a/proxy/plugin/shortlink.py b/proxy/plugin/shortlink.py index 88cdfd8f7d..73db81e710 100644 --- a/proxy/plugin/shortlink.py +++ b/proxy/plugin/shortlink.py @@ -12,7 +12,9 @@ from ..common.constants import DOT, SLASH from ..common.utils import build_http_response -from ..http.parser import HttpParser, httpStatusCodes + +from ..http import httpStatusCodes +from ..http.parser import HttpParser from ..http.proxy import HttpProxyBasePlugin @@ -85,9 +87,3 @@ def handle_client_request( ) return None return request - - def handle_upstream_chunk(self, chunk: memoryview) -> memoryview: - return chunk - - def on_upstream_connection_close(self) -> None: - pass diff --git a/proxy/plugin/web_server_route.py b/proxy/plugin/web_server_route.py index b07992b372..52f85deb48 100644 --- a/proxy/plugin/web_server_route.py +++ b/proxy/plugin/web_server_route.py @@ -12,7 +12,9 @@ from typing import List, Tuple from ..common.utils import build_http_response -from ..http.parser import HttpParser, httpStatusCodes + +from ..http import httpStatusCodes +from ..http.parser import HttpParser from ..http.websocket import WebsocketFrame from ..http.server import HttpWebServerBasePlugin, httpProtocolTypes diff --git a/tests/http/exceptions/test_http_request_rejected.py b/tests/http/exceptions/test_http_request_rejected.py index d345afec22..de8f736bc4 100644 --- a/tests/http/exceptions/test_http_request_rejected.py +++ b/tests/http/exceptions/test_http_request_rejected.py @@ -10,7 +10,8 @@ """ import unittest -from proxy.http.parser import HttpParser, httpParserTypes, httpStatusCodes +from proxy.http import httpStatusCodes +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 diff --git a/tests/http/test_http_parser.py b/tests/http/test_http_parser.py index d7c47f73ee..11aa5c47b7 100644 --- a/tests/http/test_http_parser.py +++ b/tests/http/test_http_parser.py @@ -13,7 +13,9 @@ from proxy.common.constants import CRLF, HTTP_1_0 from proxy.common.utils import build_http_request, build_http_response, build_http_header from proxy.common.utils import find_http_line, bytes_ -from proxy.http.parser import HttpParser, httpParserTypes, httpParserStates, httpStatusCodes, httpMethods + +from proxy.http import httpStatusCodes, httpMethods +from proxy.http.parser import HttpParser, httpParserTypes, httpParserStates class TestHttpParser(unittest.TestCase): diff --git a/tests/http/test_http_proxy_tls_interception.py b/tests/http/test_http_proxy_tls_interception.py index 0d9af03965..dda98a925e 100644 --- a/tests/http/test_http_proxy_tls_interception.py +++ b/tests/http/test_http_proxy_tls_interception.py @@ -19,9 +19,8 @@ from proxy.common.constants import DEFAULT_CA_FILE from proxy.core.connection import TcpClientConnection, TcpServerConnection -from proxy.http import HttpProtocolHandler +from proxy.http import HttpProtocolHandler, httpMethods from proxy.http.proxy import HttpProxyPlugin -from proxy.http.parser import httpMethods from proxy.common.utils import build_http_request, bytes_ from proxy.common.flag import FlagParser diff --git a/tests/http/test_url.py b/tests/http/test_url.py index cc8a13240c..11a3c54a45 100644 --- a/tests/http/test_url.py +++ b/tests/http/test_url.py @@ -10,7 +10,7 @@ """ import unittest -from proxy.http.parser import Url +from proxy.http import Url class TestUrl(unittest.TestCase): diff --git a/tests/plugin/test_http_proxy_plugins.py b/tests/plugin/test_http_proxy_plugins.py index 352f484ccc..864f48597a 100644 --- a/tests/plugin/test_http_proxy_plugins.py +++ b/tests/plugin/test_http_proxy_plugins.py @@ -20,7 +20,7 @@ from proxy.common.flag import FlagParser from proxy.core.connection import TcpClientConnection from proxy.http import HttpProtocolHandler -from proxy.http.parser import httpStatusCodes +from proxy.http import httpStatusCodes 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 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 1602af5174..d076f1558a 100644 --- a/tests/plugin/test_http_proxy_plugins_with_tls_interception.py +++ b/tests/plugin/test_http_proxy_plugins_with_tls_interception.py @@ -19,9 +19,9 @@ from proxy.common.flag import FlagParser from proxy.common.utils import bytes_, build_http_request, build_http_response from proxy.core.connection import TcpClientConnection, TcpServerConnection + +from proxy.http import httpMethods, httpStatusCodes, HttpProtocolHandler from proxy.http.proxy import HttpProxyPlugin -from proxy.http.parser import httpStatusCodes, httpMethods -from proxy.http import HttpProtocolHandler from .utils import get_plugin_by_test_name diff --git a/tests/testing/test_embed.py b/tests/testing/test_embed.py index ea664d93f7..a1e2af05e8 100644 --- a/tests/testing/test_embed.py +++ b/tests/testing/test_embed.py @@ -17,8 +17,8 @@ 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 +from proxy.http import httpMethods from proxy.http.server import HttpWebServerPlugin -from proxy.http.parser import httpMethods @unittest.skipIf(os.name == 'nt', 'Disabled for Windows due to weird permission issues.')