ok

Mini Shell

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

import asyncio
from logging import getLogger
from pathlib import Path

from defence360agent.contracts.config import Core
from defence360agent.contracts.plugins import MessageSink
from defence360agent.subsys.panels.hosting_panel import HostingPanel
from defence360agent.utils import create_task_and_log_exceptions, retry_on
from imav.malwarelib.subsys.malware import HackerTrapHitsSaver
from im360.subsys.modsec_cache_dir import create_modsec_cache_directory
from im360.subsys.whitelist_rbl import (
    create_rbl_whitelist,
    ensure_rbl_whitelist,
)


logger = getLogger(__name__)


class StartupActions(MessageSink):
    PANEL_NAME_PATH = Path(Core.GLOBAL_CONFDIR) / "panel-name.txt"
    RETRY_TIMEOUT = 5 * 60  # 5 minutes

    def _create_tasks(self):
        async def sleep_on_error(*_):
            await asyncio.sleep(self.RETRY_TIMEOUT)

        tasks = [
            create_task_and_log_exceptions(
                self._loop, HackerTrapHitsSaver.reset_sa_hits
            ),
            create_task_and_log_exceptions(
                self._loop, create_modsec_cache_directory
            ),
            create_task_and_log_exceptions(self._loop, create_rbl_whitelist),
            create_task_and_log_exceptions(self._loop, ensure_rbl_whitelist),
            create_task_and_log_exceptions(
                self._loop,
                retry_on(
                    Exception,
                    on_error=sleep_on_error,
                    max_tries=3,
                )(self.update_panel_name),
            ),
        ]
        return tasks

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

    async def update_panel_name(self):
        self.PANEL_NAME_PATH.write_text(HostingPanel().NAME)

    async def shutdown(self):
        for task in self._tasks:
            task.cancel()
        await asyncio.gather(*self._tasks, return_exceptions=True)

Zerion Mini Shell 1.0