2023-06-08 21:24:01 +02:00
|
|
|
import subprocess
|
|
|
|
import json
|
|
|
|
import time
|
2023-06-13 20:55:36 +02:00
|
|
|
from flask import Flask, request, jsonify
|
2023-06-08 21:24:01 +02:00
|
|
|
from I2C_Function import analyze_data, read_data
|
|
|
|
from flask_cors import CORS
|
2023-06-13 20:55:36 +02:00
|
|
|
from threading import Lock
|
2023-06-08 21:24:01 +02:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
CORS(app)
|
|
|
|
|
|
|
|
initialized = False
|
2023-06-13 20:55:36 +02:00
|
|
|
lock = Lock() # Sperre (Lock) für die analyze_data-Methode
|
2023-06-08 21:24:01 +02:00
|
|
|
|
2023-06-12 07:26:18 +02:00
|
|
|
def create_response(status_code, description):
|
|
|
|
return jsonify({
|
|
|
|
'status': status_code,
|
|
|
|
'description': description
|
|
|
|
})
|
|
|
|
|
2023-06-08 21:24:01 +02:00
|
|
|
@app.route('/trng/randomNum/init', methods=['GET'])
|
|
|
|
def initialize_generator():
|
2023-06-12 07:26:18 +02:00
|
|
|
global initialized
|
2023-06-09 15:02:25 +02:00
|
|
|
|
2023-06-13 20:55:36 +02:00
|
|
|
if initialized:
|
|
|
|
return create_response(200, 'System is already initialised, random numbers can be requested')
|
|
|
|
|
2023-06-13 22:46:44 +02:00
|
|
|
start_time = time.time()
|
2023-06-13 20:55:36 +02:00
|
|
|
with lock: # Sperre (Lock) verwenden, um sicherzustellen, dass nur ein Thread die Methode ausführt
|
|
|
|
result = analyze_data(int(1), int(1), startup=True)
|
2023-06-13 22:46:44 +02:00
|
|
|
|
|
|
|
end_time = time.time()
|
|
|
|
duration = end_time - start_time
|
|
|
|
|
|
|
|
if duration > 60:
|
|
|
|
return create_response(555, 'Unable to initialize the random number generator within a timeout of 60 seconds')
|
|
|
|
else:
|
2023-06-13 20:55:36 +02:00
|
|
|
if result is True:
|
|
|
|
initialized = True
|
|
|
|
return create_response(200, 'Successful operation; random number generator is ready and random numbers can be requested')
|
|
|
|
if result == 400:
|
|
|
|
return create_response(543, 'Tests failed, try again')
|
|
|
|
if result is False:
|
|
|
|
return create_response(500, 'Unable to generate random numbers. Restart/Reset System')
|
|
|
|
else:
|
|
|
|
return create_response(555, 'Unable to initialize the random number generator within a timeout of 60 seconds')
|
2023-06-08 21:24:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/trng/randomNum/getRandom', methods=['GET'])
|
|
|
|
def get_random_numbers():
|
2023-06-12 07:26:18 +02:00
|
|
|
global initialized
|
2023-06-13 20:55:36 +02:00
|
|
|
quantity = request.args.get('quantity', default=1)
|
|
|
|
bits = request.args.get('numBits', default=1)
|
|
|
|
|
|
|
|
try:
|
|
|
|
quantity = int(quantity)
|
|
|
|
bits = int(bits)
|
|
|
|
except ValueError:
|
2023-06-12 14:50:34 +02:00
|
|
|
return create_response(400, 'Invalid input. Quantity and bits must be numeric')
|
2023-06-08 21:24:01 +02:00
|
|
|
|
2023-06-13 20:55:36 +02:00
|
|
|
if quantity <= 0 or bits <= 0:
|
2023-06-12 14:50:34 +02:00
|
|
|
return create_response(400, 'Invalid input. Quantity and bits must be positive integers')
|
2023-06-08 21:24:01 +02:00
|
|
|
|
|
|
|
if not initialized:
|
2023-06-12 07:26:18 +02:00
|
|
|
return create_response(432, 'System not ready; try init')
|
2023-06-13 20:55:36 +02:00
|
|
|
|
|
|
|
with lock: # Sperre (Lock) verwenden, um sicherzustellen, dass nur ein Thread die Methode ausführt
|
|
|
|
random_numbers = analyze_data(quantity, bits, startup=False)
|
2023-06-09 15:02:25 +02:00
|
|
|
|
2023-06-13 20:55:36 +02:00
|
|
|
if random_numbers == 400:
|
|
|
|
return create_response(543, 'Tests failed, try again')
|
|
|
|
if random_numbers is False:
|
|
|
|
return create_response(500, 'Unable to generate random numbers. Restart/Reset System')
|
|
|
|
if random_numbers:
|
|
|
|
data = {
|
|
|
|
'status': 200,
|
|
|
|
'description': 'Successful operation; HEX-encoded bit arrays (with leading zeros if required)',
|
|
|
|
'randomNumbers': random_numbers
|
|
|
|
}
|
|
|
|
return jsonify(data)
|
|
|
|
else:
|
|
|
|
return create_response(500, 'Unable to generate random numbers.')
|
2023-06-08 21:24:01 +02:00
|
|
|
|
|
|
|
@app.route('/trng/randomNum/shutdown', methods=['GET'])
|
|
|
|
def shutdown_generator():
|
2023-06-12 07:26:18 +02:00
|
|
|
global initialized
|
2023-06-08 21:24:01 +02:00
|
|
|
|
2023-06-12 07:26:18 +02:00
|
|
|
initialized = False
|
2023-06-08 21:24:01 +02:00
|
|
|
|
2023-06-12 07:26:18 +02:00
|
|
|
return create_response(200, "Successful operation; random number generator has been set to 'standby mode'")
|
2023-06-13 20:55:36 +02:00
|
|
|
|
|
|
|
# Fehlerbehandlungsroutine für nicht übereinstimmende Routen
|
|
|
|
@app.route('/', defaults={'path': ''})
|
|
|
|
@app.route('/<path:path>')
|
|
|
|
def handle_invalid_routes(path):
|
|
|
|
return create_response(404, 'Route not found')
|
2023-06-08 21:24:01 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-06-09 15:02:25 +02:00
|
|
|
|
|
|
|
# Pfade zu den SSL-Zertifikat- und Schlüsseldateien
|
2023-06-08 21:24:01 +02:00
|
|
|
cert_path = '/etc/nginx/ssl/cert-gmtrom.pem'
|
|
|
|
key_path = '/etc/nginx/ssl/cert-gmtrom-key.pem'
|
|
|
|
|
2023-06-09 15:02:25 +02:00
|
|
|
#alte Zertifikate self signed
|
|
|
|
#cert_path = '/etc/nginx/ssl/server.crt'
|
|
|
|
#key_path = '/etc/nginx/ssl/server.key'
|
|
|
|
|
2023-06-08 21:24:01 +02:00
|
|
|
# Starte die Flask-Anwendung mit SSL-Konfiguration
|
|
|
|
app.run(host='0.0.0.0', ssl_context=(cert_path, key_path))
|
2023-06-09 15:02:25 +02:00
|
|
|
#app.run(host='172.16.78.57', port=5000, ssl_context=(cert_path, key_path))
|
2023-06-08 21:24:01 +02:00
|
|
|
#app.run(host='0.0.0.0', ssl_context=adhoc)
|
|
|
|
#app.run(ssl_context=(cert_path, key_path))
|