-
Notifications
You must be signed in to change notification settings - Fork 10
/
web.py
75 lines (65 loc) · 2.53 KB
/
web.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
## fufluns - Copyright 2019-2021 - deroad
import os, json
import tornado.ioloop
import tornado.web
WWW_FOLDER=os.path.join(os.path.dirname(os.path.realpath(__file__)), "www")
class ApiHandler(tornado.web.RequestHandler):
def initialize(self, core):
self.core = core
def set_default_headers(self):
self.set_header('Content-Type', 'application/json')
def get(self, method):
if method == "version":
self.write(json.dumps(self.core.version()))
elif method == "newsession":
self.write(json.dumps({"session": self.core.newsession()}))
elif method.startswith("report/"):
session_id = method[len("report/"):]
if len(session_id) < 32:
self.write(json.dumps({"error": "Session not found."}))
else:
session = self.core.getsession(session_id)
if session is None:
self.write(json.dumps({"error": "Session not found."}))
else:
self.write(session.report())
else:
self.write(json.dumps({"error": "Method Not Allowed"}))
def post(self, method):
if method.startswith("analyze/"):
session_id = method[len("analyze/"):]
if len(session_id) < 32:
self.write(json.dumps({"error": "Session not found."}))
else:
for _, files in self.request.files.items():
for info in files:
session = self.core.analyze(session_id, info["filename"], info["body"])
if session is not None:
self.write(json.dumps({"error": None}))
else:
self.write(message=json.dumps({"error": "Invalid file extension or session."}))
return
else:
self.write(json.dumps({"error": "Method Not Allowed"}))
def make_app(settings, core):
handlers = [
(r"/", tornado.web.RedirectHandler, {"url": "/ui/index.html"}),
(r"/ui", tornado.web.RedirectHandler, {"url": "/ui/index.html"}),
(r"/ui/", tornado.web.RedirectHandler, {"url": "/ui/index.html"}),
(r"/ui/(.*)", tornado.web.StaticFileHandler, {'path': WWW_FOLDER}),
(r"/api", tornado.web.RedirectHandler, {"url": "/api/"}),
(r"/api/(.*)", ApiHandler, core),
(r"/(.*)", tornado.web.RedirectHandler, {"url": "/ui/index.html"}),
]
return tornado.web.Application(handlers, **settings)
class Server():
"""Python http server"""
def __init__(self, core, listen=8080, proto="http", debug=False):
super(Server, self).__init__()
self.listen = listen
self.proto = proto
self.app = make_app({'debug': debug}, dict(core=core))
def run(self):
self.app.listen(self.listen, max_buffer_size=1073741274) # 1GB max
print("Server available at {proto}://localhost:{port}".format(proto=self.proto, port=self.listen))
tornado.ioloop.IOLoop.instance().start()