2020-07-04 12:47:11 +00:00
|
|
|
# -*- 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.
|
|
|
|
"""
|
|
|
|
import socket
|
2021-11-14 21:47:12 +00:00
|
|
|
import argparse
|
2020-07-04 12:47:11 +00:00
|
|
|
|
2021-11-14 21:47:12 +00:00
|
|
|
from abc import ABC, abstractmethod
|
2022-01-04 19:02:16 +00:00
|
|
|
from typing import List, Union, Optional, TYPE_CHECKING
|
2020-07-04 12:47:11 +00:00
|
|
|
|
|
|
|
from ..core.event import EventQueue
|
|
|
|
from ..core.connection import TcpClientConnection
|
|
|
|
|
2021-12-31 10:28:36 +00:00
|
|
|
from .parser import HttpParser
|
2022-01-04 19:02:16 +00:00
|
|
|
from .descriptors import DescriptorsHandlerMixin
|
2022-01-12 05:49:35 +00:00
|
|
|
from .mixins import TlsInterceptionPropertyMixin
|
2021-12-31 10:28:36 +00:00
|
|
|
|
2021-12-28 08:21:20 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from ..core.connection import UpstreamConnectionPool
|
|
|
|
|
2020-07-04 12:47:11 +00:00
|
|
|
|
2022-01-12 05:49:35 +00:00
|
|
|
class HttpProtocolHandlerPlugin(
|
|
|
|
DescriptorsHandlerMixin,
|
|
|
|
TlsInterceptionPropertyMixin,
|
|
|
|
ABC,
|
|
|
|
):
|
2020-07-04 12:47:11 +00:00
|
|
|
"""Base HttpProtocolHandler Plugin class.
|
|
|
|
|
|
|
|
NOTE: This is an internal plugin and in most cases only useful for core contributors.
|
|
|
|
If you are looking for proxy server plugins see `<proxy.HttpProxyBasePlugin>`.
|
|
|
|
|
|
|
|
Implements various lifecycle events for an accepted client connection.
|
|
|
|
Following events are of interest:
|
|
|
|
|
|
|
|
1. Client Connection Accepted
|
|
|
|
A new plugin instance is created per accepted client connection.
|
|
|
|
Add your logic within __init__ constructor for any per connection setup.
|
|
|
|
2. Client Request Chunk Received
|
|
|
|
on_client_data is called for every chunk of data sent by the client.
|
|
|
|
3. Client Request Complete
|
|
|
|
on_request_complete is called once client request has completed.
|
|
|
|
4. Server Response Chunk Received
|
|
|
|
on_response_chunk is called for every chunk received from the server.
|
|
|
|
5. Client Connection Closed
|
2021-11-16 23:34:29 +00:00
|
|
|
Add your logic within `on_client_connection_close` for any per connection tear-down.
|
2020-07-04 12:47:11 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2021-12-19 16:00:43 +00:00
|
|
|
uid: str,
|
2020-10-14 05:21:56 +00:00
|
|
|
flags: argparse.Namespace,
|
2020-07-04 12:47:11 +00:00
|
|
|
client: TcpClientConnection,
|
|
|
|
request: HttpParser,
|
2022-01-08 17:48:19 +00:00
|
|
|
event_queue: Optional[EventQueue] = None,
|
2021-12-28 08:21:20 +00:00
|
|
|
upstream_conn_pool: Optional['UpstreamConnectionPool'] = None,
|
2021-11-04 11:28:36 +00:00
|
|
|
):
|
2022-01-12 05:49:35 +00:00
|
|
|
super().__init__(uid, flags, client, event_queue, upstream_conn_pool)
|
2021-12-19 16:00:43 +00:00
|
|
|
self.uid: str = uid
|
2020-10-14 05:21:56 +00:00
|
|
|
self.flags: argparse.Namespace = flags
|
2020-07-04 12:47:11 +00:00
|
|
|
self.client: TcpClientConnection = client
|
|
|
|
self.request: HttpParser = request
|
|
|
|
self.event_queue = event_queue
|
2021-12-28 08:21:20 +00:00
|
|
|
self.upstream_conn_pool = upstream_conn_pool
|
2020-07-04 12:47:11 +00:00
|
|
|
|
2021-12-31 10:28:36 +00:00
|
|
|
@staticmethod
|
|
|
|
@abstractmethod
|
|
|
|
def protocols() -> List[int]:
|
|
|
|
raise NotImplementedError()
|
2020-07-04 12:47:11 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def on_client_data(self, raw: memoryview) -> Optional[memoryview]:
|
2021-11-30 23:59:52 +00:00
|
|
|
"""Called only after original request has been completely received."""
|
2020-07-04 12:47:11 +00:00
|
|
|
return raw # pragma: no cover
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def on_request_complete(self) -> Union[socket.socket, bool]:
|
|
|
|
"""Called right after client request parser has reached COMPLETE state."""
|
|
|
|
return False # pragma: no cover
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def on_response_chunk(self, chunk: List[memoryview]) -> List[memoryview]:
|
|
|
|
"""Handle data chunks as received from the server.
|
|
|
|
|
|
|
|
Return optionally modified chunk to return back to client."""
|
|
|
|
return chunk # pragma: no cover
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def on_client_connection_close(self) -> None:
|
2021-10-29 23:02:05 +00:00
|
|
|
"""Client connection shutdown has been received, flush has been called,
|
|
|
|
perform any cleanup work here.
|
|
|
|
"""
|
2020-07-04 12:47:11 +00:00
|
|
|
pass # pragma: no cover
|