-
Notifications
You must be signed in to change notification settings - Fork 34
/
simplehttp.py
executable file
·201 lines (172 loc) · 7.64 KB
/
simplehttp.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
import logging
import argparse
from concurrent import futures
import asyncio
from aiohttp import web
import json
import loganalyzer as analyze
loop = asyncio.get_event_loop()
threadPool = futures.ThreadPoolExecutor(thread_name_prefix='loganalyzer: worker thread')
app = web.Application()
with open("templates/index.html", "r") as f: # Grab main HTML page
htmlTemplate = f.read()
with open("templates/detail.html", "r") as f: # Grab details page
htmlDetail = f.read()
def checkUrl(url):
"""Check if the incoming URL can be analyzed"""
return any((analyze.matchGist(url), analyze.matchHaste(url), analyze.matchObs(url), analyze.matchPastebin(url), analyze.matchDiscord(url)))
def getSummaryHTML(messages):
"""Helper func. Generates the summary secion of the HTML page."""
critical = ""
warning = ""
info = ""
for i in messages:
if (i[0] == 3):
critical = critical + """<p><a href="#""" + \
i[1] + """"><button type="button" class="btn btn-danger">""" + \
i[1] + "</button></a></p>\n"
elif (i[0] == 2):
warning = warning + """<p><a href="#""" + \
i[1] + """"><button type="button" class="btn btn-warning">""" + \
i[1] + "</button></a></p>\n"
elif (i[0] == 1):
info = info + """<p><a href="#""" + \
i[1] + """"><button type="button" class="btn btn-info">""" + \
i[1] + "</button></a></p>\n"
if (len(critical) == 0):
critical = "No critical issues."
if (len(warning) == 0):
warning = "No warnings."
if (len(info) == 0):
info = "-"
return critical, warning, info
def getDetailsHTML(messages):
"""Helper func. Generates detailes section of the HTML page."""
res = ""
for i in messages:
if (i[0] == 3):
res = res + htmlDetail.format(anchor=i[1],
sev='danger',
severity='Critical',
title=i[1],
text=i[2])
for i in messages:
if (i[0] == 2):
res = res + htmlDetail.format(anchor=i[1],
sev='warning',
severity='Warning',
title=i[1],
text=i[2])
for i in messages:
if (i[0] == 1):
res = res + htmlDetail.format(anchor=i[1],
sev='info',
severity='Info',
title=i[1],
text=i[2])
return res
def getDescr(messages):
"""Helper func. Gets the desciption created by the analysis."""
res = ""
for i in messages:
if (i[0] == 0):
res = i[2]
return res
def genFullHtmlResponse(url):
"""Runs an analysis and returns a full HTML page with the response."""
msgs = analyze.doAnalysis(url=url)
crit, warn, info = getSummaryHTML(msgs)
details = getDetailsHTML(msgs)
response = htmlTemplate.format(ph=url,
description="""<a href="{}">{}</a>""".format(
url, getDescr(msgs)),
summary_critical=crit,
summary_warning=warn,
summary_info=info,
details=details)
return response
def genEmptyHtmlResponse():
"""Generates a full HTML page with no analysis."""
no_log = "Please analyze a log first."
response_body = htmlTemplate.format(ph="",
description="no log",
summary_critical=no_log,
summary_warning=no_log,
summary_info=no_log,
details="""<p class="text-warning">""" + no_log + """</p>""")
return response_body
def genJsonResponse(url, detailed):
"""Runs an analysis and returns the results as JSON."""
msgs = []
msgs = analyze.doAnalysis(url=url)
critical = []
warning = []
info = []
for i in msgs:
entry = i[1]
if detailed:
entry = {"title": i[1], "details": i[2]}
if (i[0] == 3):
critical.append(entry)
elif (i[0] == 2):
warning.append(entry)
elif (i[0] == 1):
info.append(entry)
return {"critical": critical, "warning": warning, "info": info}
def sync_request_handler(request):
"""Non-async request handler processed within the thread pool. Do not share global resources without a Lock."""
query = request.query # Get HTTP query string as a MultiDict
format = 'html'
if 'format' in query: # Check for requested response format
format = query['format'].lower()
logging.info('New HTTP Request | Remote: {} | Format: {} | Url: {}'.format(request.remote, format, 'url' in query))
if 'url' in query:
url = query['url']
detailed = 'detailed' in query and query['detailed'] == 'true'
if not checkUrl(url): # Return empty data/page if URL is invalid
logging.info('Invalid URL: {}'.format(url))
if format == 'json':
logging.info('Returning empty JSON response.')
return web.json_response({})
else:
logging.info('Returning default HTML response.')
return web.Response(text=genEmptyHtmlResponse(), content_type='text/html')
if format == 'json':
logging.info('Returning JSON response for url: {}'.format(url))
response = genJsonResponse(url, detailed)
return web.json_response(response)
else:
logging.info('Returning HTML response for url: {}'.format(url))
return web.Response(text=genFullHtmlResponse(url), content_type='text/html')
else:
if format == 'json':
logging.info('Returning empty JSON response.')
return web.json_response({})
else:
logging.info('Returning default HTML response.')
return web.Response(text=genEmptyHtmlResponse(), content_type='text/html')
async def request_handler(request):
"""Async request handler. Submits the incoming request to the thread pool to be handled."""
return (await loop.run_in_executor(None, sync_request_handler, request)) # Submits the request to a handler inside the threadpool
def main():
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] [%(funcName)s] %(message)s")
aiohttpLogger = logging.getLogger('aiohttp')
aiohttpLogger.setLevel(logging.WARNING)
parser = argparse.ArgumentParser()
parser.add_argument("--host", default="localhost", type=str, help="address to bind to", dest='host')
parser.add_argument("--port", default="8080", type=int, help="port to bind to", dest='port')
flags = parser.parse_args()
loop.set_default_executor(threadPool) # Set the default executor to our thread pool
app.add_routes([web.get('/', request_handler)])
applicationTask = loop.create_task(web._run_app(app, host=flags.host, port=flags.port, print=logging.info))
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
logging.info('Exiting application.')
applicationTask.cancel() # Shuts down the HTTP server
threadPool.shutdown() # Shuts down the running thread pool
if __name__ == '__main__':
main()