Skip to content

Commit 7f1470e

Browse files
authored
No abstract method for proxy plugin (#738)
* Remove abstractmethod for proxy plugin base class, remove unused methods from bundled plugins * Move httpStatusCodes, httpMethods and Url within top-level proxy.http package
1 parent d72ee22 commit 7f1470e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+145
-213
lines changed

examples/https_connect_tunnel.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
from proxy import Proxy
1616
from proxy.common.utils import build_http_response
17-
from proxy.http.parser import httpParserStates, httpStatusCodes
17+
from proxy.http import httpStatusCodes
18+
from proxy.http.parser import httpParserStates
1819
from proxy.core.base import BaseTcpTunnelHandler
1920

2021

proxy/common/pki.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
:copyright: (c) 2013-present by Abhinav Singh and contributors.
99
:license: BSD, see LICENSE for more details.
1010
"""
11-
import time
11+
import os
1212
import sys
13+
import uuid
14+
import time
15+
import logging
16+
import tempfile
1317
import argparse
1418
import contextlib
15-
import os
16-
import uuid
1719
import subprocess
18-
import tempfile
19-
import logging
20+
2021
from typing import List, Generator, Optional, Tuple
2122

2223
from .utils import bytes_

proxy/core/connection/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
:copyright: (c) 2013-present by Abhinav Singh and contributors.
99
:license: BSD, see LICENSE for more details.
1010
"""
11-
from .connection import TcpConnection, TcpConnectionUninitializedException, tcpConnectionTypes
11+
from .connection import TcpConnection, TcpConnectionUninitializedException
1212
from .client import TcpClientConnection
1313
from .server import TcpServerConnection
1414
from .pool import ConnectionPool
15+
from .types import tcpConnectionTypes
1516

1617
__all__ = [
1718
'TcpConnection',

proxy/core/connection/client.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88
:copyright: (c) 2013-present by Abhinav Singh and contributors.
99
:license: BSD, see LICENSE for more details.
1010
"""
11-
import socket
1211
import ssl
12+
import socket
13+
1314
from typing import Union, Tuple, Optional
1415

15-
from .connection import TcpConnection, tcpConnectionTypes, TcpConnectionUninitializedException
16+
from .connection import TcpConnection, TcpConnectionUninitializedException
17+
from .types import tcpConnectionTypes
1618

1719

1820
class TcpClientConnection(TcpConnection):
19-
"""An accepted client connection request."""
21+
"""A buffered client connection object."""
2022

2123
def __init__(
2224
self,

proxy/core/connection/connection.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,18 @@
88
:copyright: (c) 2013-present by Abhinav Singh and contributors.
99
:license: BSD, see LICENSE for more details.
1010
"""
11-
import socket
1211
import ssl
12+
import socket
1313
import logging
14+
1415
from abc import ABC, abstractmethod
15-
from typing import NamedTuple, Optional, Union, List
16+
from typing import Optional, Union, List
1617

1718
from ...common.constants import DEFAULT_BUFFER_SIZE, DEFAULT_MAX_SEND_SIZE
1819

19-
logger = logging.getLogger(__name__)
20-
20+
from .types import tcpConnectionTypes
2121

22-
TcpConnectionTypes = NamedTuple(
23-
'TcpConnectionTypes', [
24-
('SERVER', int),
25-
('CLIENT', int),
26-
],
27-
)
28-
tcpConnectionTypes = TcpConnectionTypes(1, 2)
22+
logger = logging.getLogger(__name__)
2923

3024

3125
class TcpConnectionUninitializedException(Exception):

proxy/core/connection/pool.py

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from typing import Set, Dict, Tuple
1414

1515
from ...common.flag import flags
16+
1617
from .server import TcpServerConnection
1718

1819
logger = logging.getLogger(__name__)

proxy/core/connection/server.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313

1414
from typing import Optional, Union, Tuple
1515

16-
from .connection import TcpConnection, tcpConnectionTypes, TcpConnectionUninitializedException
17-
1816
from ...common.utils import new_socket_connection
1917

18+
from .connection import TcpConnection, TcpConnectionUninitializedException
19+
from .types import tcpConnectionTypes
20+
2021

2122
class TcpServerConnection(TcpConnection):
22-
"""Establishes connection to upstream server."""
23+
"""A buffered server connection object."""
2324

2425
def __init__(self, host: str, port: int) -> None:
2526
super().__init__(tcpConnectionTypes.SERVER)

proxy/core/connection/types.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
proxy.py
4+
~~~~~~~~
5+
⚡⚡⚡ Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on
6+
Network monitoring, controls & Application development, testing, debugging.
7+
8+
:copyright: (c) 2013-present by Abhinav Singh and contributors.
9+
:license: BSD, see LICENSE for more details.
10+
"""
11+
from typing import NamedTuple
12+
13+
14+
TcpConnectionTypes = NamedTuple(
15+
'TcpConnectionTypes', [
16+
('SERVER', int),
17+
('CLIENT', int),
18+
],
19+
)
20+
21+
tcpConnectionTypes = TcpConnectionTypes(1, 2)

proxy/dashboard/dashboard.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
from .plugin import ProxyDashboardWebsocketPlugin
1717

1818
from ..common.utils import build_http_response, bytes_
19-
from ..http.server import HttpWebServerPlugin, HttpWebServerBasePlugin, httpProtocolTypes
20-
from ..http.parser import HttpParser, httpStatusCodes
19+
20+
from ..http import httpStatusCodes
21+
from ..http.parser import HttpParser
2122
from ..http.websocket import WebsocketFrame
23+
from ..http.server import HttpWebServerPlugin, HttpWebServerBasePlugin, httpProtocolTypes
2224

2325
logger = logging.getLogger(__name__)
2426

proxy/http/__init__.py

+6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@
1010
"""
1111
from .handler import HttpProtocolHandler
1212
from .plugin import HttpProtocolHandlerPlugin
13+
from .codes import httpStatusCodes
14+
from .methods import httpMethods
15+
from .url import Url
1316

1417
__all__ = [
1518
'HttpProtocolHandler',
1619
'HttpProtocolHandlerPlugin',
20+
'httpStatusCodes',
21+
'httpMethods',
22+
'Url',
1723
]
File renamed without changes.

proxy/http/exception/proxy_auth_failed.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
:license: BSD, see LICENSE for more details.
1010
"""
1111
from .base import HttpProtocolException
12-
from ..parser import HttpParser, httpStatusCodes
12+
13+
from ..codes import httpStatusCodes
14+
from ..parser import HttpParser
1315

1416
from ...common.constants import PROXY_AGENT_HEADER_VALUE, PROXY_AGENT_HEADER_KEY
1517
from ...common.utils import build_http_response

proxy/http/exception/proxy_conn_failed.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
:license: BSD, see LICENSE for more details.
1010
"""
1111
from .base import HttpProtocolException
12-
from ..parser import HttpParser, httpStatusCodes
12+
13+
from ..codes import httpStatusCodes
14+
from ..parser import HttpParser
1315

1416
from ...common.constants import PROXY_AGENT_HEADER_VALUE, PROXY_AGENT_HEADER_KEY
1517
from ...common.utils import build_http_response

proxy/http/handler.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
:copyright: (c) 2013-present by Abhinav Singh and contributors.
99
:license: BSD, see LICENSE for more details.
1010
"""
11-
import socket
12-
import selectors
1311
import ssl
1412
import time
15-
import contextlib
1613
import errno
14+
import socket
1715
import logging
16+
import selectors
17+
import contextlib
1818

1919
from typing import Tuple, List, Union, Optional, Generator, Dict, Any
2020

proxy/http/parser/methods.py renamed to proxy/http/methods.py

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
('PATCH', bytes),
2525
],
2626
)
27+
2728
httpMethods = HttpMethods(
2829
b'GET',
2930
b'HEAD',

proxy/http/parser/__init__.py

-6
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
"""
1111
from .parser import HttpParser
1212
from .chunk import ChunkParser, chunkParserStates
13-
from .codes import httpStatusCodes
14-
from .methods import httpMethods
1513
from .types import httpParserStates, httpParserTypes
16-
from .url import Url
1714
from .protocol import ProxyProtocol, PROXY_PROTOCOL_V2_SIGNATURE
1815

1916
__all__ = [
@@ -22,9 +19,6 @@
2219
'httpParserStates',
2320
'ChunkParser',
2421
'chunkParserStates',
25-
'httpStatusCodes',
26-
'httpMethods',
27-
'Url',
2822
'ProxyProtocol',
2923
'PROXY_PROTOCOL_V2_SIGNATURE',
3024
]

proxy/http/parser/parser.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
from ...common.utils import build_http_request, build_http_response, find_http_line, text_
1717
from ...common.flag import flags
1818

19-
from .url import Url
20-
from .methods import httpMethods
19+
from ..url import Url
20+
from ..methods import httpMethods
21+
2122
from .protocol import ProxyProtocol
2223
from .chunk import ChunkParser, chunkParserStates
2324
from .types import httpParserTypes, httpParserStates

proxy/http/parser/protocol.py

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
:license: BSD, see LICENSE for more details.
1010
"""
1111
from typing import Optional, Tuple
12+
1213
from ...common.constants import WHITESPACE
1314

1415
PROXY_PROTOCOL_V2_SIGNATURE = b'\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A'

proxy/http/plugin.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
:copyright: (c) 2013-present by Abhinav Singh and contributors.
99
:license: BSD, see LICENSE for more details.
1010
"""
11-
import argparse
1211
import socket
12+
import argparse
1313

14-
from abc import ABC, abstractmethod
1514
from uuid import UUID
15+
from abc import ABC, abstractmethod
1616
from typing import Tuple, List, Union, Optional
1717

1818
from .parser import HttpParser

proxy/http/proxy/auth.py

+1-11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from typing import Optional
1212

1313
from ..exception import ProxyAuthenticationFailed
14+
1415
from ...common.flag import flags
1516
from ...common.constants import DEFAULT_BASIC_AUTH
1617
from ...http.parser import HttpParser
@@ -41,14 +42,3 @@ def before_upstream_connection(
4142
or parts[1] != self.flags.auth_code:
4243
raise ProxyAuthenticationFailed()
4344
return request
44-
45-
def handle_client_request(
46-
self, request: HttpParser,
47-
) -> Optional[HttpParser]:
48-
return request
49-
50-
def handle_upstream_chunk(self, chunk: memoryview) -> memoryview:
51-
return chunk # pragma: no cover
52-
53-
def on_upstream_connection_close(self) -> None:
54-
pass # pragma: no cover

proxy/http/proxy/plugin.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
import socket
1212
import argparse
1313

14+
from abc import ABC
1415
from uuid import UUID
1516
from typing import Any, Dict, List, Optional, Tuple
16-
from abc import ABC, abstractmethod
1717

1818
from ..parser import HttpParser
1919

@@ -93,7 +93,9 @@ def resolve_dns(self, host: str, port: int) -> Tuple[Optional[str], Optional[Tup
9393
"""
9494
return None, None
9595

96-
@abstractmethod
96+
# No longer abstract since 2.4.0
97+
#
98+
# @abstractmethod
9799
def before_upstream_connection(
98100
self, request: HttpParser,
99101
) -> Optional[HttpParser]:
@@ -122,7 +124,9 @@ def handle_client_data(
122124
"""
123125
return raw # pragma: no cover
124126

125-
@abstractmethod
127+
# No longer abstract since 2.4.0
128+
#
129+
# @abstractmethod
126130
def handle_client_request(
127131
self, request: HttpParser,
128132
) -> Optional[HttpParser]:
@@ -142,15 +146,19 @@ def handle_client_request(
142146
"""
143147
return request # pragma: no cover
144148

145-
@abstractmethod
149+
# No longer abstract since 2.4.0
150+
#
151+
# @abstractmethod
146152
def handle_upstream_chunk(self, chunk: memoryview) -> memoryview:
147153
"""Handler called right after receiving raw response from upstream server.
148154
149155
For HTTPS connections, chunk will be encrypted unless
150156
TLS interception is also enabled."""
151157
return chunk # pragma: no cover
152158

153-
@abstractmethod
159+
# No longer abstract since 2.4.0
160+
#
161+
# @abstractmethod
154162
def on_upstream_connection_close(self) -> None:
155163
"""Handler called right after upstream connection has been closed."""
156164
pass # pragma: no cover

proxy/http/proxy/server.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
from typing import Optional, List, Union, Dict, cast, Any, Tuple
2121

2222
from .plugin import HttpProxyBasePlugin
23+
24+
from ..methods import httpMethods
25+
from ..codes import httpStatusCodes
2326
from ..plugin import HttpProtocolHandlerPlugin
2427
from ..exception import HttpProtocolException, ProxyConnectionFailed
25-
from ..parser import HttpParser, httpParserStates, httpParserTypes, httpStatusCodes, httpMethods
28+
from ..parser import HttpParser, httpParserStates, httpParserTypes
2629

2730
from ...common.types import Readables, Writables
2831
from ...common.constants import DEFAULT_CA_CERT_DIR, DEFAULT_CA_CERT_FILE, DEFAULT_CA_FILE

proxy/http/server/pac_plugin.py

+3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
:license: BSD, see LICENSE for more details.
1010
"""
1111
import gzip
12+
1213
from typing import List, Tuple, Optional, Any
1314

1415
from .plugin import HttpWebServerBasePlugin
1516
from .protocols import httpProtocolTypes
17+
1618
from ..websocket import WebsocketFrame
1719
from ..parser import HttpParser
20+
1821
from ...common.utils import bytes_, text_, build_http_response
1922
from ...common.flag import flags
2023
from ...common.constants import DEFAULT_PAC_FILE, DEFAULT_PAC_FILE_URL_PATH

proxy/http/server/plugin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
import socket
1212
import argparse
1313

14+
from uuid import UUID
1415
from abc import ABC, abstractmethod
1516
from typing import Any, Dict, List, Optional, Tuple
16-
from uuid import UUID
1717

1818
from ..websocket import WebsocketFrame
1919
from ..parser import HttpParser

proxy/http/server/protocols.py

+1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717
('WEBSOCKET', int),
1818
],
1919
)
20+
2021
httpProtocolTypes = HttpProtocolTypes(1, 2, 3)

0 commit comments

Comments
 (0)