|
| 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 | +import socket |
| 12 | + |
| 13 | +from abc import ABC, abstractmethod |
| 14 | +from uuid import UUID |
| 15 | +from typing import Tuple, List, Union, Optional |
| 16 | + |
| 17 | +from .parser import HttpParser |
| 18 | + |
| 19 | +from ..common.flags import Flags |
| 20 | +from ..common.types import HasFileno |
| 21 | +from ..core.event import EventQueue |
| 22 | +from ..core.connection import TcpClientConnection |
| 23 | + |
| 24 | + |
| 25 | +class HttpProtocolHandlerPlugin(ABC): |
| 26 | + """Base HttpProtocolHandler Plugin class. |
| 27 | +
|
| 28 | + NOTE: This is an internal plugin and in most cases only useful for core contributors. |
| 29 | + If you are looking for proxy server plugins see `<proxy.HttpProxyBasePlugin>`. |
| 30 | +
|
| 31 | + Implements various lifecycle events for an accepted client connection. |
| 32 | + Following events are of interest: |
| 33 | +
|
| 34 | + 1. Client Connection Accepted |
| 35 | + A new plugin instance is created per accepted client connection. |
| 36 | + Add your logic within __init__ constructor for any per connection setup. |
| 37 | + 2. Client Request Chunk Received |
| 38 | + on_client_data is called for every chunk of data sent by the client. |
| 39 | + 3. Client Request Complete |
| 40 | + on_request_complete is called once client request has completed. |
| 41 | + 4. Server Response Chunk Received |
| 42 | + on_response_chunk is called for every chunk received from the server. |
| 43 | + 5. Client Connection Closed |
| 44 | + Add your logic within `on_client_connection_close` for any per connection teardown. |
| 45 | + """ |
| 46 | + |
| 47 | + def __init__( |
| 48 | + self, |
| 49 | + uid: UUID, |
| 50 | + flags: Flags, |
| 51 | + client: TcpClientConnection, |
| 52 | + request: HttpParser, |
| 53 | + event_queue: EventQueue): |
| 54 | + self.uid: UUID = uid |
| 55 | + self.flags: Flags = flags |
| 56 | + self.client: TcpClientConnection = client |
| 57 | + self.request: HttpParser = request |
| 58 | + self.event_queue = event_queue |
| 59 | + super().__init__() |
| 60 | + |
| 61 | + def name(self) -> str: |
| 62 | + """A unique name for your plugin. |
| 63 | +
|
| 64 | + Defaults to name of the class. This helps plugin developers to directly |
| 65 | + access a specific plugin by its name.""" |
| 66 | + return self.__class__.__name__ |
| 67 | + |
| 68 | + @abstractmethod |
| 69 | + def get_descriptors( |
| 70 | + self) -> Tuple[List[socket.socket], List[socket.socket]]: |
| 71 | + return [], [] # pragma: no cover |
| 72 | + |
| 73 | + @abstractmethod |
| 74 | + def write_to_descriptors(self, w: List[Union[int, HasFileno]]) -> bool: |
| 75 | + return False # pragma: no cover |
| 76 | + |
| 77 | + @abstractmethod |
| 78 | + def read_from_descriptors(self, r: List[Union[int, HasFileno]]) -> bool: |
| 79 | + return False # pragma: no cover |
| 80 | + |
| 81 | + @abstractmethod |
| 82 | + def on_client_data(self, raw: memoryview) -> Optional[memoryview]: |
| 83 | + return raw # pragma: no cover |
| 84 | + |
| 85 | + @abstractmethod |
| 86 | + def on_request_complete(self) -> Union[socket.socket, bool]: |
| 87 | + """Called right after client request parser has reached COMPLETE state.""" |
| 88 | + return False # pragma: no cover |
| 89 | + |
| 90 | + @abstractmethod |
| 91 | + def on_response_chunk(self, chunk: List[memoryview]) -> List[memoryview]: |
| 92 | + """Handle data chunks as received from the server. |
| 93 | +
|
| 94 | + Return optionally modified chunk to return back to client.""" |
| 95 | + return chunk # pragma: no cover |
| 96 | + |
| 97 | + @abstractmethod |
| 98 | + def on_client_connection_close(self) -> None: |
| 99 | + pass # pragma: no cover |
0 commit comments