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.
|
|
|
|
"""
|
|
|
|
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
|
|
|
self.manager: Optional[multiprocessing.managers.SyncManager] = None
|
|
|
|
|
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-10-31 19:49:19 +00:00
|
|
|
self.manager = multiprocessing.Manager()
|
2021-11-08 17:32:36 +00:00
|
|
|
self.queue = EventQueue(self.manager.Queue())
|
|
|
|
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()
|
|
|
|
logger.debug('Thread ID: %d', 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-08 20:04:17 +00:00
|
|
|
assert self.dispatcher_shutdown and self.dispatcher_thread and self.manager
|
2021-11-08 17:32:36 +00:00
|
|
|
self.dispatcher_shutdown.set()
|
|
|
|
self.dispatcher_thread.join()
|
2021-11-08 20:04:17 +00:00
|
|
|
self.manager.shutdown()
|
2021-10-31 19:49:19 +00:00
|
|
|
logger.debug(
|
|
|
|
'Shutdown of global event dispatcher thread %d successful',
|
2021-11-08 17:32:36 +00:00
|
|
|
self.dispatcher_thread.ident,
|
2021-11-04 11:28:36 +00:00
|
|
|
)
|