2021-10-31 19:49:19 +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.
|
2021-11-16 23:34:29 +00:00
|
|
|
|
|
|
|
.. spelling::
|
|
|
|
|
|
|
|
eventing
|
2021-10-31 19:49:19 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
import threading
|
|
|
|
import multiprocessing
|
|
|
|
|
2021-11-09 22:57:40 +00:00
|
|
|
from typing import Any, Optional
|
2021-10-31 19:49:19 +00:00
|
|
|
|
|
|
|
from .queue import EventQueue
|
|
|
|
from .dispatcher import EventDispatcher
|
|
|
|
|
2021-11-07 21:51:42 +00:00
|
|
|
from ...common.flag import flags
|
|
|
|
from ...common.constants import DEFAULT_ENABLE_EVENTS
|
|
|
|
|
2021-10-31 19:49:19 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-11-07 21:51:42 +00:00
|
|
|
flags.add_argument(
|
|
|
|
'--enable-events',
|
|
|
|
action='store_true',
|
|
|
|
default=DEFAULT_ENABLE_EVENTS,
|
|
|
|
help='Default: False. Enables core to dispatch lifecycle events. '
|
|
|
|
'Plugins can be used to subscribe for core events.',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-10-31 19:49:19 +00:00
|
|
|
class EventManager:
|
2021-11-08 17:32:36 +00:00
|
|
|
"""Event manager is a context manager which provides
|
|
|
|
encapsulation around various setup and shutdown steps
|
|
|
|
to start the eventing core.
|
2021-10-31 19:49:19 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
2021-11-08 17:32:36 +00:00
|
|
|
self.queue: Optional[EventQueue] = None
|
|
|
|
self.dispatcher: Optional[EventDispatcher] = None
|
|
|
|
self.dispatcher_thread: Optional[threading.Thread] = None
|
|
|
|
self.dispatcher_shutdown: Optional[threading.Event] = None
|
2021-10-31 19:49:19 +00:00
|
|
|
|
2021-11-08 17:32:36 +00:00
|
|
|
def __enter__(self) -> 'EventManager':
|
|
|
|
self.setup()
|
|
|
|
return self
|
|
|
|
|
2021-11-09 22:57:40 +00:00
|
|
|
def __exit__(self, *args: Any) -> None:
|
2021-11-08 17:32:36 +00:00
|
|
|
self.shutdown()
|
|
|
|
|
|
|
|
def setup(self) -> None:
|
2021-11-10 15:17:28 +00:00
|
|
|
self.queue = EventQueue(multiprocessing.Queue())
|
2021-11-08 17:32:36 +00:00
|
|
|
self.dispatcher_shutdown = threading.Event()
|
|
|
|
assert self.dispatcher_shutdown
|
|
|
|
assert self.queue
|
|
|
|
self.dispatcher = EventDispatcher(
|
|
|
|
shutdown=self.dispatcher_shutdown,
|
|
|
|
event_queue=self.queue,
|
2021-10-31 19:49:19 +00:00
|
|
|
)
|
2021-11-08 17:32:36 +00:00
|
|
|
self.dispatcher_thread = threading.Thread(
|
|
|
|
target=self.dispatcher.run,
|
2021-10-31 19:49:19 +00:00
|
|
|
)
|
2021-11-08 17:32:36 +00:00
|
|
|
self.dispatcher_thread.start()
|
2021-11-10 15:17:28 +00:00
|
|
|
logger.debug('Dispatcher#%d started', self.dispatcher_thread.ident)
|
2021-10-31 19:49:19 +00:00
|
|
|
|
2021-11-08 17:32:36 +00:00
|
|
|
def shutdown(self) -> None:
|
2021-11-10 15:17:28 +00:00
|
|
|
assert self.dispatcher_shutdown and self.dispatcher_thread
|
2021-11-08 17:32:36 +00:00
|
|
|
self.dispatcher_shutdown.set()
|
|
|
|
self.dispatcher_thread.join()
|
2021-10-31 19:49:19 +00:00
|
|
|
logger.debug(
|
2021-11-10 15:17:28 +00:00
|
|
|
'Dispatcher#%d shutdown',
|
2021-11-08 17:32:36 +00:00
|
|
|
self.dispatcher_thread.ident,
|
2021-11-04 11:28:36 +00:00
|
|
|
)
|