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
|
|
|
|
|
2021-09-22 06:50:34 +00:00
|
|
|
# Configuration
|
|
|
|
port = 8000
|
|
|
|
|
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
|
|
|
|
|
|
|
class Search():
|
|
|
|
def on_post(self, req, resp):
|
|
|
|
data_post = req.bounded_stream.read()
|
|
|
|
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
|
|
|
|
|
|
|
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()
|
|
|
|
resp.media=cpeGuesser.guessCpe(q['query'])
|
2021-09-17 05:45:01 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-09-18 11:32:31 +00:00
|
|
|
app = falcon.App()
|
|
|
|
app.add_route('/search', Search())
|
|
|
|
|
2021-09-22 06:50:34 +00:00
|
|
|
try:
|
|
|
|
with make_server('', port, app) as httpd:
|
|
|
|
print(f"Serving on port {port}...")
|
|
|
|
httpd.serve_forever()
|
|
|
|
except OSError as e:
|
|
|
|
print (e)
|
|
|
|
sys.exit(1)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
sys.exit(0)
|