2019-12-02 06:46:00 +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.
|
|
|
|
"""
|
2020-10-14 05:21:56 +00:00
|
|
|
import argparse
|
2021-10-29 23:02:05 +00:00
|
|
|
|
|
|
|
from abc import ABC, abstractmethod
|
2022-01-08 17:48:19 +00:00
|
|
|
from typing import Any, Dict, List, Optional, Tuple, TYPE_CHECKING
|
2021-10-29 23:02:05 +00:00
|
|
|
|
2019-12-02 06:46:00 +00:00
|
|
|
from ..websocket import WebsocketFrame
|
|
|
|
from ..parser import HttpParser
|
2022-01-04 19:02:16 +00:00
|
|
|
from ..descriptors import DescriptorsHandlerMixin
|
2019-12-02 06:46:00 +00:00
|
|
|
|
|
|
|
from ...core.connection import TcpClientConnection
|
|
|
|
from ...core.event import EventQueue
|
|
|
|
|
2022-01-08 17:48:19 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from ...core.connection import UpstreamConnectionPool
|
|
|
|
|
2019-12-02 06:46:00 +00:00
|
|
|
|
2022-01-04 19:02:16 +00:00
|
|
|
class HttpWebServerBasePlugin(DescriptorsHandlerMixin, ABC):
|
2019-12-02 06:46:00 +00:00
|
|
|
"""Web Server Plugin for routing of requests."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2021-12-19 16:00:43 +00:00
|
|
|
uid: str,
|
2020-10-14 05:21:56 +00:00
|
|
|
flags: argparse.Namespace,
|
2019-12-02 06:46:00 +00:00
|
|
|
client: TcpClientConnection,
|
2021-11-04 11:28:36 +00:00
|
|
|
event_queue: EventQueue,
|
2022-01-08 17:48:19 +00:00
|
|
|
upstream_conn_pool: Optional['UpstreamConnectionPool'] = None,
|
2021-11-04 11:28:36 +00:00
|
|
|
):
|
2019-12-02 06:46:00 +00:00
|
|
|
self.uid = uid
|
|
|
|
self.flags = flags
|
|
|
|
self.client = client
|
|
|
|
self.event_queue = event_queue
|
2022-01-08 17:48:19 +00:00
|
|
|
self.upstream_conn_pool = upstream_conn_pool
|
2019-12-02 06:46:00 +00:00
|
|
|
|
2021-11-05 10:25:56 +00:00
|
|
|
def name(self) -> str:
|
|
|
|
"""A unique name for your plugin.
|
|
|
|
|
|
|
|
Defaults to name of the class. This helps plugin developers to directly
|
|
|
|
access a specific plugin by its name."""
|
|
|
|
return self.__class__.__name__ # pragma: no cover
|
|
|
|
|
2019-12-02 06:46:00 +00:00
|
|
|
@abstractmethod
|
|
|
|
def routes(self) -> List[Tuple[int, str]]:
|
|
|
|
"""Return List(protocol, path) that this plugin handles."""
|
|
|
|
raise NotImplementedError() # pragma: no cover
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def handle_request(self, request: HttpParser) -> None:
|
|
|
|
"""Handle the request and serve response."""
|
|
|
|
raise NotImplementedError() # pragma: no cover
|
|
|
|
|
2021-11-05 10:25:56 +00:00
|
|
|
def on_client_connection_close(self) -> None:
|
|
|
|
"""Client has closed the connection, do any clean up task now."""
|
|
|
|
pass
|
|
|
|
|
2021-11-19 18:35:24 +00:00
|
|
|
# No longer abstract since v2.4.0
|
|
|
|
#
|
|
|
|
# @abstractmethod
|
2019-12-02 06:46:00 +00:00
|
|
|
def on_websocket_open(self) -> None:
|
|
|
|
"""Called when websocket handshake has finished."""
|
2021-11-19 18:35:24 +00:00
|
|
|
pass # pragma: no cover
|
2019-12-02 06:46:00 +00:00
|
|
|
|
2021-11-19 18:35:24 +00:00
|
|
|
# No longer abstract since v2.4.0
|
|
|
|
#
|
|
|
|
# @abstractmethod
|
2019-12-02 06:46:00 +00:00
|
|
|
def on_websocket_message(self, frame: WebsocketFrame) -> None:
|
|
|
|
"""Handle websocket frame."""
|
2021-11-19 18:35:24 +00:00
|
|
|
return None # pragma: no cover
|
2019-12-02 06:46:00 +00:00
|
|
|
|
2021-11-05 10:25:56 +00:00
|
|
|
# Deprecated since v2.4.0
|
|
|
|
#
|
|
|
|
# Instead use on_client_connection_close.
|
|
|
|
#
|
|
|
|
# This callback is no longer invoked. Kindly
|
|
|
|
# update your plugin before upgrading to v2.4.0.
|
|
|
|
#
|
|
|
|
# @abstractmethod
|
|
|
|
# def on_websocket_close(self) -> None:
|
|
|
|
# """Called when websocket connection has been closed."""
|
|
|
|
# raise NotImplementedError() # pragma: no cover
|
2021-11-12 21:29:43 +00:00
|
|
|
|
|
|
|
def on_access_log(self, context: Dict[str, Any]) -> Optional[Dict[str, Any]]:
|
|
|
|
"""Use this method to override default access log format (see
|
|
|
|
DEFAULT_WEB_ACCESS_LOG_FORMAT) or to add/update/modify passed context
|
|
|
|
for usage by default access logger.
|
|
|
|
|
|
|
|
Return updated log context to use for default logging format, OR
|
|
|
|
Return None if plugin has logged the request.
|
|
|
|
"""
|
|
|
|
return context
|