コード例 #1
0
    def __init__(self, slicer: Optional[AbstractSlicer],
                 webapp: Optional[AbstractWebApp]):

        self.slicer = slicer
        self.webapp = webapp

        self._logger = Logger(self)
コード例 #2
0
class Master:
    def __init__(self, slicer: Optional[AbstractSlicer],
                 webapp: Optional[AbstractWebApp]):

        self.slicer = slicer
        self.webapp = webapp

        self._logger = Logger(self)

    @classmethod
    def from_configurator(cls, configurator: Configurator):
        return cls(configurator.get_slicer(), configurator.get_webapp())

    def run(self, loop=None):
        if loop is None:
            loop = asyncio.get_event_loop()

        task = asyncio.wait(
            [self._run_webapp(), self._run_slicer()], loop=loop)
        loop.run_until_complete(task)

    async def _run_webapp(self):
        runner = web.AppRunner(self.webapp.app)
        await runner.setup()
        site = web.TCPSite(runner, self.webapp.addr, self.webapp.port)
        await site.start()

        while True:
            if self.webapp.finish == True:
                await runner.cleanup()
                return
            else:
                await asyncio.sleep(60)

    async def _run_slicer(self):

        while True:
            try:
                await self.slicer.slice_pcaps()

            except Exception as e:
                self._logger.error(
                    f'Can\'t properly slice pcaps from slicer-storage: {e}', e)

            await asyncio.sleep(30)
コード例 #3
0
ファイル: sessions_dumper.py プロジェクト: mostobriv/pahan
 def __init__(self):
     self._logger = Logger('SessionsDumper')
コード例 #4
0
 def __init__(self):
     self._logger = Logger('WebApp')
コード例 #5
0
ファイル: pcap_storage.py プロジェクト: mostobriv/pahan
 def __init__(self):
     self._logger = Logger('PcapStorage')
コード例 #6
0
ファイル: slicer.py プロジェクト: mostobriv/pahan
 def __init__(self):
     self._logger = Logger('Slicer')
コード例 #7
0
 def __init__(self):
     self._logger = Logger('DBControl')
コード例 #8
0
class Configurator:
    DEFAULT_SETTINGS_MODULE_NAME = 'settings'

    def __init__(self, settings_module_name=None):
        self._logger = Logger(self)
        self.settings_module_name = self.DEFAULT_SETTINGS_MODULE_NAME if settings_module_name is None else settings_module_name
        self.settings_module = importlib.import_module(
            self.settings_module_name)

    def _get_settings_variable(self,
                               setting_name,
                               default_value=None,
                               may_missing=False):
        if hasattr(self.settings_module, setting_name):
            return getattr(self.settings_module, setting_name)
        if may_missing:
            return default_value
        raise ValueError('Specify variable %s in settings file (%s)' %
                         (setting_name, self.settings_module_name))

    # TODO: add support for recursive parsing of settings objects
    def _get_settings_object(self,
                             setting_name,
                             may_missing=False,
                             recursive=False):
        object_spec = self._get_settings_variable(setting_name,
                                                  may_missing=may_missing)
        if object_spec is None:
            return None

        # If you don't need args and kwargs,
        # you can use just 'module.name.class_name' instead of { 'type': 'module.name.class_name' }
        if type(object_spec) is str:
            object_spec = {'type': object_spec}

        if type(object_spec) is not dict:
            raise ValueError(
                'Variable %s in settings file (%s) should be dict or str, not %s'
                % (setting_name, self.settings_module_name, type(object_spec)))

        object_type_name = self._get_dict_value(object_spec, 'type',
                                                setting_name)
        object_args = object_spec.get('args', ())
        object_kwargs = object_spec.get('kwargs', {})

        try:
            object_type = utils.import_type(object_type_name)
        except ValueError as e:
            raise ValueError('Can not find type %s for initializing %s: %s' %
                             (object_type_name, setting_name, e))

        self._logger.info('Creating %s with arguments %s and kwarguments %s' %
                          (object_type.__name__, object_args, object_kwargs))
        return object_type(*object_args, **object_kwargs)

    def _get_dict_value(self, dict_object, param, setting_name):
        try:
            return dict_object[param]
        except KeyError:
            raise ValueError(
                'Variable %s in settings file (%s) should has key "%s"' %
                (setting_name, self.settings_module_name, param))

    def get_flag_format(self):
        return self._get_settings_variable('FLAG_FORMAT')

    def get_database(self):
        return self._get_settings_object('DATABASE')

    def get_slicer(self):
        return self._get_settings_object('SLICER')

    def get_webapp(self):
        return self._get_settings_object('WEBAPP')
コード例 #9
0
 def __init__(self, settings_module_name=None):
     self._logger = Logger(self)
     self.settings_module_name = self.DEFAULT_SETTINGS_MODULE_NAME if settings_module_name is None else settings_module_name
     self.settings_module = importlib.import_module(
         self.settings_module_name)