Skip to content

Commit

Permalink
No abstract method for proxy plugin (#738)
Browse files Browse the repository at this point in the history
* Remove abstractmethod for proxy plugin base class, remove unused methods from bundled plugins

* Move httpStatusCodes, httpMethods and Url within top-level proxy.http package
  • Loading branch information
abhinavsingh authored Nov 14, 2021
1 parent d72ee22 commit 7f1470e
Show file tree
Hide file tree
Showing 49 changed files with 145 additions and 213 deletions.
3 changes: 2 additions & 1 deletion examples/https_connect_tunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
11 changes: 6 additions & 5 deletions proxy/common/pki.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
3 changes: 2 additions & 1 deletion proxy/core/connection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 5 additions & 3 deletions proxy/core/connection/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 5 additions & 11 deletions proxy/core/connection/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions proxy/core/connection/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from typing import Set, Dict, Tuple

from ...common.flag import flags

from .server import TcpServerConnection

logger = logging.getLogger(__name__)
Expand Down
7 changes: 4 additions & 3 deletions proxy/core/connection/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions proxy/core/connection/types.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 4 additions & 2 deletions proxy/dashboard/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down
6 changes: 6 additions & 0 deletions proxy/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]
File renamed without changes.
4 changes: 3 additions & 1 deletion proxy/http/exception/proxy_auth_failed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion proxy/http/exception/proxy_conn_failed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions proxy/http/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions proxy/http/parser/methods.py → proxy/http/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
('PATCH', bytes),
],
)

httpMethods = HttpMethods(
b'GET',
b'HEAD',
Expand Down
6 changes: 0 additions & 6 deletions proxy/http/parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = [
Expand All @@ -22,9 +19,6 @@
'httpParserStates',
'ChunkParser',
'chunkParserStates',
'httpStatusCodes',
'httpMethods',
'Url',
'ProxyProtocol',
'PROXY_PROTOCOL_V2_SIGNATURE',
]
5 changes: 3 additions & 2 deletions proxy/http/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions proxy/http/parser/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions proxy/http/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 1 addition & 11 deletions proxy/http/proxy/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
18 changes: 13 additions & 5 deletions proxy/http/proxy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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]:
Expand All @@ -142,15 +146,19 @@ 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.
For HTTPS connections, chunk will be encrypted unless
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
Expand Down
5 changes: 4 additions & 1 deletion proxy/http/proxy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions proxy/http/server/pac_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion proxy/http/server/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions proxy/http/server/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
('WEBSOCKET', int),
],
)

httpProtocolTypes = HttpProtocolTypes(1, 2, 3)
Loading

0 comments on commit 7f1470e

Please sign in to comment.