2019-08-06 15:03:49 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
"""
|
2021-06-02 14:04:52 +00:00
|
|
|
The Tracker_Term Module
|
2019-08-06 15:03:49 +00:00
|
|
|
===================
|
|
|
|
|
|
|
|
"""
|
2021-04-02 07:52:05 +00:00
|
|
|
|
|
|
|
##################################
|
|
|
|
# Import External packages
|
|
|
|
##################################
|
2019-08-06 15:03:49 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
2019-08-09 12:20:13 +00:00
|
|
|
import signal
|
2021-09-28 19:11:20 +00:00
|
|
|
|
2019-08-06 15:03:49 +00:00
|
|
|
|
2021-06-02 14:04:52 +00:00
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
2021-04-02 07:52:05 +00:00
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
2021-06-02 14:04:52 +00:00
|
|
|
from modules.abstract_module import AbstractModule
|
2023-04-13 12:25:02 +00:00
|
|
|
from lib.ConfigLoader import ConfigLoader
|
2023-05-04 14:35:56 +00:00
|
|
|
from lib.objects import ail_objects
|
2020-07-10 13:54:14 +00:00
|
|
|
from lib import Tracker
|
|
|
|
|
2023-03-30 12:58:55 +00:00
|
|
|
from exporter.MailExporter import MailExporterTracker
|
|
|
|
from exporter.WebHookExporter import WebHookExporterTracker
|
|
|
|
|
2019-08-09 12:20:13 +00:00
|
|
|
class TimeoutException(Exception):
|
|
|
|
pass
|
2021-09-28 19:11:20 +00:00
|
|
|
|
|
|
|
|
2019-08-09 12:20:13 +00:00
|
|
|
def timeout_handler(signum, frame):
|
|
|
|
raise TimeoutException
|
2021-09-28 19:11:20 +00:00
|
|
|
|
|
|
|
|
2019-08-09 12:20:13 +00:00
|
|
|
signal.signal(signal.SIGALRM, timeout_handler)
|
|
|
|
|
2019-08-07 10:08:24 +00:00
|
|
|
|
2021-06-02 14:04:52 +00:00
|
|
|
class Tracker_Term(AbstractModule):
|
2021-04-02 07:52:05 +00:00
|
|
|
"""
|
2021-06-02 14:04:52 +00:00
|
|
|
Tracker_Term module for AIL framework
|
2021-04-02 07:52:05 +00:00
|
|
|
"""
|
2021-09-28 19:11:20 +00:00
|
|
|
|
2023-04-13 12:42:57 +00:00
|
|
|
def __init__(self, queue=True):
|
|
|
|
super(Tracker_Term, self).__init__(queue=queue)
|
2019-08-06 15:03:49 +00:00
|
|
|
|
2023-04-13 12:25:02 +00:00
|
|
|
config_loader = ConfigLoader()
|
|
|
|
|
2021-04-02 07:52:05 +00:00
|
|
|
self.pending_seconds = 5
|
2019-08-07 10:08:24 +00:00
|
|
|
|
2023-04-13 12:25:02 +00:00
|
|
|
self.max_execution_time = config_loader.get_config_int('Tracker_Term', "max_execution_time")
|
2019-08-07 10:08:24 +00:00
|
|
|
|
2021-04-02 07:52:05 +00:00
|
|
|
# loads tracked words
|
2023-05-04 14:35:56 +00:00
|
|
|
self.tracked_words = Tracker.get_tracked_words()
|
2021-04-02 07:52:05 +00:00
|
|
|
self.last_refresh_word = time.time()
|
2023-05-04 14:35:56 +00:00
|
|
|
self.tracked_sets = Tracker.get_tracked_sets()
|
2021-04-02 07:52:05 +00:00
|
|
|
self.last_refresh_set = time.time()
|
2019-08-07 10:08:24 +00:00
|
|
|
|
2023-03-30 12:58:55 +00:00
|
|
|
# Exporter
|
|
|
|
self.exporters = {'mail': MailExporterTracker(),
|
|
|
|
'webhook': WebHookExporterTracker()}
|
|
|
|
|
2021-06-02 14:04:52 +00:00
|
|
|
self.redis_logger.info(f"Module: {self.module_name} Launched")
|
2019-08-07 10:08:24 +00:00
|
|
|
|
2023-05-04 14:35:56 +00:00
|
|
|
def compute(self, obj_id, obj_type='item', subtype=''):
|
2021-04-02 14:54:45 +00:00
|
|
|
# refresh Tracked term
|
2023-05-04 14:35:56 +00:00
|
|
|
if self.last_refresh_word < Tracker.get_tracker_last_updated_by_type('word'):
|
|
|
|
self.tracked_words = Tracker.get_tracked_words()
|
2021-04-02 14:54:45 +00:00
|
|
|
self.last_refresh_word = time.time()
|
|
|
|
self.redis_logger.debug('Tracked word refreshed')
|
2021-06-02 14:53:17 +00:00
|
|
|
print('Tracked word refreshed')
|
2021-04-02 14:54:45 +00:00
|
|
|
|
2023-05-04 14:35:56 +00:00
|
|
|
if self.last_refresh_set < Tracker.get_tracker_last_updated_by_type('set'):
|
|
|
|
self.tracked_sets = Tracker.get_tracked_sets()
|
2021-04-02 14:54:45 +00:00
|
|
|
self.last_refresh_set = time.time()
|
|
|
|
self.redis_logger.debug('Tracked set refreshed')
|
2021-06-02 14:53:17 +00:00
|
|
|
print('Tracked set refreshed')
|
2021-04-02 14:54:45 +00:00
|
|
|
|
2023-05-04 14:35:56 +00:00
|
|
|
obj = ail_objects.get_object(obj_type, subtype, obj_id)
|
|
|
|
obj_type = obj.get_type()
|
|
|
|
|
|
|
|
# Object Filter
|
|
|
|
if obj_type not in self.tracked_words and obj_type not in self.tracked_sets:
|
|
|
|
return None
|
|
|
|
|
|
|
|
content = obj.get_content(r_str=True)
|
2019-08-09 12:20:13 +00:00
|
|
|
|
2021-04-02 07:52:05 +00:00
|
|
|
signal.alarm(self.max_execution_time)
|
2019-08-07 10:08:24 +00:00
|
|
|
|
2021-04-02 07:52:05 +00:00
|
|
|
dict_words_freq = None
|
|
|
|
try:
|
2023-05-04 14:35:56 +00:00
|
|
|
dict_words_freq = Tracker.get_text_word_frequency(content)
|
2021-04-02 07:52:05 +00:00
|
|
|
except TimeoutException:
|
2023-05-04 14:35:56 +00:00
|
|
|
self.redis_logger.warning(f"{obj.get_id()} processing timeout")
|
2021-04-02 07:52:05 +00:00
|
|
|
else:
|
|
|
|
signal.alarm(0)
|
2019-08-07 10:08:24 +00:00
|
|
|
|
2021-04-02 07:52:05 +00:00
|
|
|
if dict_words_freq:
|
2019-08-07 10:08:24 +00:00
|
|
|
|
|
|
|
# check solo words
|
2023-05-04 14:35:56 +00:00
|
|
|
for word in self.tracked_words[obj_type]:
|
2019-08-07 10:08:24 +00:00
|
|
|
if word in dict_words_freq:
|
2023-05-04 14:35:56 +00:00
|
|
|
self.new_tracker_found(word, 'word', obj)
|
2021-06-14 15:36:30 +00:00
|
|
|
|
|
|
|
# check words set
|
2023-05-04 14:35:56 +00:00
|
|
|
for tracked_set in self.tracked_sets[obj_type]:
|
2021-06-14 15:36:30 +00:00
|
|
|
nb_uniq_word = 0
|
2023-05-04 14:35:56 +00:00
|
|
|
for word in tracked_set['words']:
|
2021-06-14 15:36:30 +00:00
|
|
|
if word in dict_words_freq:
|
|
|
|
nb_uniq_word += 1
|
2023-05-04 14:35:56 +00:00
|
|
|
if nb_uniq_word >= tracked_set['nb']:
|
|
|
|
self.new_tracker_found(tracked_set['tracked'], 'set', obj)
|
2021-06-14 15:36:30 +00:00
|
|
|
|
2023-05-04 14:35:56 +00:00
|
|
|
def new_tracker_found(self, tracker_name, tracker_type, obj): # TODO FILTER
|
|
|
|
obj_id = obj.get_id()
|
2022-02-16 09:19:53 +00:00
|
|
|
|
2023-05-04 14:35:56 +00:00
|
|
|
for tracker_uuid in Tracker.get_trackers_by_tracked_obj_type(tracker_type, obj.get_type(), tracker_name):
|
2023-03-30 12:58:55 +00:00
|
|
|
tracker = Tracker.Tracker(tracker_uuid)
|
|
|
|
|
2023-05-04 14:35:56 +00:00
|
|
|
# Filter Object
|
|
|
|
filters = tracker.get_filters()
|
|
|
|
if ail_objects.is_filtered(obj, filters):
|
2023-03-30 12:58:55 +00:00
|
|
|
continue
|
|
|
|
|
2023-05-11 14:21:43 +00:00
|
|
|
print(f'new tracked term {tracker_uuid} found: {tracker_name} in {obj_id}')
|
2023-05-04 14:35:56 +00:00
|
|
|
self.redis_logger.warning(f'new tracked term found: {tracker_name} in {obj_id}')
|
|
|
|
|
2023-05-11 14:21:43 +00:00
|
|
|
tracker.add(obj.get_type(), obj.get_subtype(), obj_id)
|
2023-03-30 12:58:55 +00:00
|
|
|
|
|
|
|
# Tags
|
|
|
|
for tag in tracker.get_tags():
|
2023-05-04 14:35:56 +00:00
|
|
|
if obj.get_type() == 'item':
|
|
|
|
msg = f'{tag};{obj_id}'
|
|
|
|
self.add_message_to_queue(msg, 'Tags')
|
|
|
|
else:
|
|
|
|
obj.add_tag(tag)
|
2023-03-30 12:58:55 +00:00
|
|
|
|
|
|
|
# Mail
|
|
|
|
if tracker.mail_export():
|
|
|
|
# TODO add matches + custom subjects
|
2023-05-04 14:35:56 +00:00
|
|
|
self.exporters['mail'].export(tracker, obj)
|
2023-03-30 12:58:55 +00:00
|
|
|
|
|
|
|
# Webhook
|
|
|
|
if tracker.webhook_export():
|
2023-05-04 14:35:56 +00:00
|
|
|
self.exporters['webhook'].export(tracker, obj)
|
2021-10-04 10:55:40 +00:00
|
|
|
|
2021-04-02 07:52:05 +00:00
|
|
|
|
2021-09-28 19:11:20 +00:00
|
|
|
if __name__ == '__main__':
|
2021-06-02 14:04:52 +00:00
|
|
|
module = Tracker_Term()
|
2021-04-02 07:52:05 +00:00
|
|
|
module.run()
|
2023-05-04 14:35:56 +00:00
|
|
|
# module.compute('submitted/2023/05/02/submitted_b1e518f1-703b-40f6-8238-d1c22888197e.gz')
|