cpe-guesser/bin/server.py

83 lines
2.1 KiB
Python
Raw Normal View History

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