2023-02-15 11:02:47 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
"""
|
|
|
|
Importer Class
|
|
|
|
================
|
|
|
|
|
|
|
|
Import Content
|
|
|
|
|
|
|
|
"""
|
2024-07-10 15:10:07 +02:00
|
|
|
import json
|
|
|
|
import logging
|
2023-02-15 11:02:47 +01:00
|
|
|
import os
|
|
|
|
import requests
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from abc import ABC
|
|
|
|
|
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
#################################
|
|
|
|
# Import Project packages
|
|
|
|
#################################
|
|
|
|
from exporter.abstract_exporter import AbstractExporter
|
2024-07-10 15:10:07 +02:00
|
|
|
from lib.ail_core import get_ail_uuid
|
2023-02-15 11:02:47 +01:00
|
|
|
|
2024-07-10 15:10:07 +02:00
|
|
|
logger = logging.getLogger()
|
2023-02-15 11:02:47 +01:00
|
|
|
|
|
|
|
class WebHookExporter(AbstractExporter, ABC):
|
|
|
|
def __init__(self, url=''):
|
|
|
|
super().__init__()
|
|
|
|
self.url = url
|
|
|
|
|
|
|
|
def set_url(self, url):
|
|
|
|
self.url = url
|
|
|
|
|
|
|
|
def _export(self, data):
|
|
|
|
try:
|
|
|
|
response = requests.post(self.url, json=data)
|
|
|
|
if response.status_code >= 400:
|
2024-07-10 15:10:07 +02:00
|
|
|
logger.error(f"Webhook request failed for {self.url}\nReason: {response.reason}")
|
2023-02-15 11:02:47 +01:00
|
|
|
except Exception as e:
|
2024-07-10 15:10:07 +02:00
|
|
|
logger.error(f"Webhook request failed for {self.url}\nReason: Something went wrong {e}")
|
2023-02-15 11:02:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
class WebHookExporterTracker(WebHookExporter):
|
|
|
|
|
|
|
|
def __init__(self, url=''):
|
|
|
|
super().__init__(url=url)
|
|
|
|
|
|
|
|
# TODO Change exported keys
|
2024-07-10 15:10:07 +02:00
|
|
|
def export(self, tracker, obj, matches=[]):
|
2023-02-15 11:02:47 +01:00
|
|
|
self.set_url(tracker.get_webhook())
|
2024-07-10 15:10:07 +02:00
|
|
|
data = {'version': 0,
|
|
|
|
'type': 'tracker:match',
|
|
|
|
'ail_uuid': get_ail_uuid(),
|
|
|
|
'tracker': {
|
|
|
|
'uuid': tracker.get_uuid(),
|
|
|
|
'type': tracker.get_type(),
|
|
|
|
'tags': list(tracker.get_tags()),
|
|
|
|
'tracker': tracker.get_tracked(),
|
|
|
|
},
|
|
|
|
'obj': {'type': obj.get_type(),
|
|
|
|
'subtype': obj.get_subtype(r_str=True),
|
|
|
|
'id': obj.get_id(),
|
|
|
|
'tags': list(obj.get_tags()),
|
|
|
|
'url': obj.get_link()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if matches:
|
|
|
|
data['matches'] = matches
|
2023-02-15 11:02:47 +01:00
|
|
|
|
2024-07-10 15:10:07 +02:00
|
|
|
# data = json.dumps(data)
|
2023-02-15 11:02:47 +01:00
|
|
|
self._export(data)
|