mirror of
https://github.com/migatu/conjurer.git
synced 2026-07-14 13:34:40 +00:00
77 lines
2.0 KiB
Python
77 lines
2.0 KiB
Python
import logging
|
|
import threading
|
|
import json
|
|
from queue import Queue, Empty
|
|
from flask import Flask, jsonify, request
|
|
from waitress import serve
|
|
|
|
HOST_ADDRESS = "192.168.1.191"
|
|
PORT_ADDRESS = 5000
|
|
OUT_COMM_Q = Queue()
|
|
IN_COMM_Q = Queue()
|
|
internal_q = Queue()
|
|
app = Flask(__name__)
|
|
|
|
@app.route("/conjurer", methods=["POST"])
|
|
def answer_external_command():
|
|
record = json.loads(request.data)
|
|
app.logger.info(record)
|
|
app.logger.info("DATA RECEIVED")
|
|
|
|
|
|
while True:
|
|
try:
|
|
q_data = internal_q.get(block=False)
|
|
except Empty:
|
|
break
|
|
if q_data.uuid in record:
|
|
pass
|
|
|
|
return jsonify("SUCCESS")
|
|
|
|
@app.route("/conjurer", methods=["GET"])
|
|
def check_alive():
|
|
return jsonify("ALIVE")
|
|
|
|
def flask_debug(_logger):
|
|
"""
|
|
The `flask_debug` function starts a Flask application in debug mode without using the reloader.
|
|
Do not use for production for fucks sake.
|
|
"""
|
|
_logger.info("Attempt debug")
|
|
# trunk-ignore(bandit/B201)
|
|
app.run(debug=True, use_reloader=False, host=HOST_ADDRESS, port=PORT_ADDRESS)
|
|
|
|
|
|
def waitress_run(_logger):
|
|
"""
|
|
The `waitress_run` function serves the `app` on host "0.0.0.0"
|
|
and port 5000 using the Waitress WSGI server.
|
|
"""
|
|
_logger.info("Attempt waitress")
|
|
serve(app, host=HOST_ADDRESS, port=PORT_ADDRESS)
|
|
|
|
def scan_queue(_logger):
|
|
while True:
|
|
data = OUT_COMM_Q.get()
|
|
_logger.info(data)
|
|
internal_q.put(data)
|
|
query_list.append(data)
|
|
|
|
def comm_subroutine(logger):
|
|
#logger.setLevel(logging.DEBUG)
|
|
logger.info("Started")
|
|
threads = []
|
|
threads.append(threading.Thread(target=flask_debug, args=(logger,)))
|
|
#threads.append(threading.Thread(target=waitress_run, args=(logger,)))
|
|
threads.append(threading.Thread(target=scan_queue,args=(logger,)))
|
|
|
|
for worker in threads:
|
|
worker.start()
|
|
for worker in threads:
|
|
worker.join()
|
|
|
|
if __name__ == "__main__":
|
|
logging_client = logging.getLogger(__name__)
|
|
comm_subroutine(logging_client)
|