2021-09-18 11:32:31 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2021-09-17 05:45:01 +00:00
|
|
|
import falcon
|
|
|
|
from wsgiref.simple_server import make_server
|
|
|
|
import json
|
2023-07-08 18:50:49 +00:00
|
|
|
from dynaconf import Dynaconf
|
2021-09-17 05:45:01 +00:00
|
|
|
|
2021-09-22 06:50:34 +00:00
|
|
|
# Configuration
|
2024-04-05 14:03:23 +00:00
|
|
|
settings = Dynaconf(settings_files=["../config/settings.yaml"])
|
2023-07-08 18:50:49 +00:00
|
|
|
port = settings.server.port
|
2021-09-22 06:50:34 +00:00
|
|
|
|
2021-09-18 11:32:31 +00:00
|
|
|
runPath = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
sys.path.append(os.path.join(runPath, ".."))
|
|
|
|
from lib.cpeguesser import CPEGuesser
|
2021-09-17 05:45:01 +00:00
|
|
|
|
2021-10-19 16:30:29 +00:00
|
|
|
|
|
|
|
class Search:
|
2021-09-17 05:45:01 +00:00
|
|
|
def on_post(self, req, resp):
|
|
|
|
data_post = req.bounded_stream.read()
|
2024-04-05 14:03:23 +00:00
|
|
|
js = data_post.decode("utf-8")
|
2021-09-18 11:32:31 +00:00
|
|
|
try:
|
|
|
|
q = json.loads(js)
|
|
|
|
except ValueError:
|
|
|
|
resp.status = falcon.HTTP_400
|
|
|
|
resp.media = "Missing query array or incorrect JSON format"
|
2021-09-22 05:34:28 +00:00
|
|
|
return
|
2021-09-17 05:45:01 +00:00
|
|
|
|
2024-04-05 14:03:23 +00:00
|
|
|
if "query" in q:
|
2021-09-22 05:34:28 +00:00
|
|
|
pass
|
2021-09-17 05:45:01 +00:00
|
|
|
else:
|
2021-09-18 11:32:31 +00:00
|
|
|
resp.status = falcon.HTTP_400
|
2021-09-17 05:45:01 +00:00
|
|
|
resp.media = "Missing query array or incorrect JSON format"
|
|
|
|
return
|
|
|
|
|
2021-09-18 11:32:31 +00:00
|
|
|
cpeGuesser = CPEGuesser()
|
2024-04-05 14:03:23 +00:00
|
|
|
resp.media = cpeGuesser.guessCpe(q["query"])
|
2021-10-19 16:30:29 +00:00
|
|
|
|
2021-09-17 05:45:01 +00:00
|
|
|
|
2024-04-05 14:03:23 +00:00
|
|
|
if __name__ == "__main__":
|
2021-09-18 11:32:31 +00:00
|
|
|
app = falcon.App()
|
2024-04-05 14:03:23 +00:00
|
|
|
app.add_route("/search", Search())
|
2021-09-18 11:32:31 +00:00
|
|
|
|
2021-09-22 06:50:34 +00:00
|
|
|
try:
|
2024-04-05 14:03:23 +00:00
|
|
|
with make_server("", port, app) as httpd:
|
2021-09-22 06:50:34 +00:00
|
|
|
print(f"Serving on port {port}...")
|
|
|
|
httpd.serve_forever()
|
|
|
|
except OSError as e:
|
2021-10-19 16:30:29 +00:00
|
|
|
print(e)
|
2021-09-22 06:50:34 +00:00
|
|
|
sys.exit(1)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
sys.exit(0)
|