|
| 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 logging |
| 12 | +import argparse |
| 13 | +from abc import abstractmethod |
| 14 | +from typing import TYPE_CHECKING, Any |
| 15 | + |
| 16 | + |
| 17 | +try: |
| 18 | + if TYPE_CHECKING: # pragma: no cover |
| 19 | + from paramiko.channel import Channel |
| 20 | + |
| 21 | + from ...common.types import HostPort |
| 22 | +except ImportError: # pragma: no cover |
| 23 | + pass |
| 24 | + |
| 25 | +logger = logging.getLogger(__name__) |
| 26 | + |
| 27 | + |
| 28 | +class BaseSshTunnelHandler: |
| 29 | + |
| 30 | + def __init__(self, flags: argparse.Namespace) -> None: |
| 31 | + self.flags = flags |
| 32 | + |
| 33 | + @abstractmethod |
| 34 | + def on_connection( |
| 35 | + self, |
| 36 | + chan: 'Channel', |
| 37 | + origin: 'HostPort', |
| 38 | + server: 'HostPort', |
| 39 | + ) -> None: |
| 40 | + raise NotImplementedError() |
| 41 | + |
| 42 | + @abstractmethod |
| 43 | + def shutdown(self) -> None: |
| 44 | + raise NotImplementedError() |
| 45 | + |
| 46 | + |
| 47 | +class BaseSshTunnelListener: |
| 48 | + |
| 49 | + def __init__( |
| 50 | + self, |
| 51 | + flags: argparse.Namespace, |
| 52 | + handler: BaseSshTunnelHandler, |
| 53 | + *args: Any, |
| 54 | + **kwargs: Any, |
| 55 | + ) -> None: |
| 56 | + self.flags = flags |
| 57 | + self.handler = handler |
| 58 | + |
| 59 | + def __enter__(self) -> 'BaseSshTunnelListener': |
| 60 | + self.setup() |
| 61 | + return self |
| 62 | + |
| 63 | + def __exit__(self, *args: Any) -> None: |
| 64 | + self.shutdown() |
| 65 | + |
| 66 | + @abstractmethod |
| 67 | + def is_alive(self) -> bool: |
| 68 | + raise NotImplementedError() |
| 69 | + |
| 70 | + @abstractmethod |
| 71 | + def is_active(self) -> bool: |
| 72 | + raise NotImplementedError() |
| 73 | + |
| 74 | + @abstractmethod |
| 75 | + def setup(self) -> None: |
| 76 | + raise NotImplementedError() |
| 77 | + |
| 78 | + @abstractmethod |
| 79 | + def shutdown(self) -> None: |
| 80 | + raise NotImplementedError() |
0 commit comments