ok

Mini Shell

Direktori : /opt/imunify360/venv/lib/python3.11/site-packages/im360/plugins/protector/
Upload File :
Current File : //opt/imunify360/venv/lib/python3.11/site-packages/im360/plugins/protector/synclist.py

from logging import getLogger

from defence360agent import utils
from defence360agent.contracts.messages import MessageType
from defence360agent.contracts.plugins import (
    MessageSink,
    MessageSource,
    expect,
)
from defence360agent.model.simplification import run_in_executor
from im360.contracts.config import Webshield
from im360.model.firewall import LastSynclist

logger = getLogger(__name__)


class SynclistPlugin(MessageSink, MessageSource):
    """
    This plugin is merely to initiate synclist request on server
    connected, reconnected events
    """

    PROCESSING_ORDER = MessageSink.ProcessingOrder.SYNCLIST_UPDATE
    AVAILABLE_ON_FREEMIUM = False

    async def create_sink(self, loop):
        self._loop = loop

    async def create_source(self, loop, sink):
        self._loop = loop
        self._sink = sink
        self._previous_webshield_status = None

    async def send_synclist_request(self):
        timestamp = await run_in_executor(
            self._loop, lambda: LastSynclist.get_timestamp(LastSynclist.IP)
        )
        await self._sink.process_message(
            MessageType.SynclistRequest(timestamp=timestamp)
        )

    @expect(MessageType.SynclistResponse)
    async def process_ip_synclist_response(self, _):
        await run_in_executor(
            self._loop, lambda: LastSynclist.update_timestamp(LastSynclist.IP)
        )

    @expect(MessageType.ConfigUpdate)
    @utils.log_error_and_ignore()
    async def on_config_update(self, message):
        is_webshield_enabled = Webshield.ENABLE
        if is_webshield_enabled and (
            self._previous_webshield_status != is_webshield_enabled
        ):
            self._previous_webshield_status = is_webshield_enabled
            await self.send_synclist_request()

    @expect(MessageType.ServerConnected, MessageType.ServerReconnected)
    async def connection_made(self, _):
        await self.send_synclist_request()

Zerion Mini Shell 1.0