-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathids_service.py
81 lines (66 loc) · 3.24 KB
/
ids_service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import asyncio
from api_endpoints import *
from tornado.ioloop import IOLoop
from tornado.platform.asyncio import AsyncIOMainLoop
from tornado.web import Application
from tornado.httpclient import AsyncHTTPClient
from WebSocketUtils.websocket_authenticators import *
import json
from time import time
from urllib.parse import urlparse
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
from requests import post
from WebSocketUtils.websocket_authenticators import *
import asyncio
WEBSOCKETS_PORT = 8000
TORNADO_PORT = 5555
TORNADO_DEBUG = True
G = {} # Global Dictionary
class IDSService:
def __init__(self):
global G
import WebSocketUtils.websocket_authenticators
WebSocketUtils.websocket_authenticators.setG(G)
self.start_websocket_server(WEBSOCKETS_PORT)
self.application = self.start_tornado()
def start_websocket_server(self,port):
"""accepts connections from incoming lambda function requests"""
services_authenticator = IDSServiceAuthenticator()
services_message_manager = MessageManagerWebsocketFromServices()
G['lambda_connection_handler'] = ConnectionHandler(authenticator=services_authenticator,
message_manager=services_message_manager)
G['lambda_connection_handler'].accept_connections(port=port)
def start_tornado(self):
"""
Starts a Tornado server with specific endpoints specified in the dictionary endpoints.
Each endpoint should have a respective Handler Class implemented in tge tornado_endpoints module.
To initialize the members of the Handler Classes you pass a dictionary after the Class Argument.
:return: Tornado application that was created
"""
urls = [
("/api/query", AnalyzeQuery, dict(
handlers={"report": MessageManagerWebsocketFromServices.report_to_connections})),
("/api/csrf" , CSRF, dict(
handlers={"report": MessageManagerWebsocketFromServices.report_to_connections})),
('/api/viralurls', ViralUrls,dict(
handlers={"report": MessageManagerWebsocketFromServices.report_to_connections})),
('/api/intrusion' , IntrusionDetection, dict(
handlers={"report": MessageManagerWebsocketFromServices.report_to_connections}))
]
print("Started Tornado")
AsyncIOMainLoop().install() # Allows to use Asyncio main event loop ; https://www.tornadoweb.org/en/branch4.5/asyncio.html
app = Application(urls, debug=TORNADO_DEBUG)
app.listen(TORNADO_PORT)
IOLoop.current().start()
return app
class MessageManagerWebsocketFromServices:
async def process_message(self, connection_and_msg):
'''look at the incoming event (message/command), determine its priority and add it to the eventQ saved
in the global obejct G.'''
@staticmethod
def report_to_connections(event):
global G
for connection in G['lambda_connection_handler'].connections:
asyncio.ensure_future(G['lambda_connection_handler'].connections[connection].send(event))