2023-06-08 21:24:01 +02:00
|
|
|
import subprocess
|
|
|
|
import json
|
|
|
|
import time
|
|
|
|
from flask import Flask, request, jsonify, session
|
|
|
|
from I2C_Function import analyze_data, read_data
|
|
|
|
from flask_cors import CORS
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
CORS(app)
|
|
|
|
|
|
|
|
initialized = False
|
|
|
|
|
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-08 21:24:01 +02:00
|
|
|
result = analyze_data(int(1), int(1), startup=True)
|
|
|
|
|
|
|
|
if result is True:
|
|
|
|
initialized = True
|
2023-06-12 07:26:18 +02:00
|
|
|
return create_response(200, 'Successful operation; random number generator is ready and random numbers can be requested')
|
|
|
|
if random_numbers == 400:
|
|
|
|
return create_response(500, 'Tests failed, try again')
|
|
|
|
if random_numbers is False:
|
|
|
|
return create_response(500, 'Unable to generate random numbers. Restart/Reset System')
|
2023-06-08 21:24:01 +02:00
|
|
|
else:
|
2023-06-12 07:26:18 +02:00
|
|
|
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-08 21:24:01 +02:00
|
|
|
quantity = request.args.get('quantity')
|
|
|
|
bits = request.args.get('numBits')
|
|
|
|
|
|
|
|
if not quantity.isdigit() or not bits.isdigit():
|
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
|
|
|
|
|
|
|
if int(quantity) <= 0 or int(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-08 21:24:01 +02:00
|
|
|
|
2023-06-09 15:02:25 +02:00
|
|
|
random_numbers = analyze_data(int(quantity), int(bits), startup=False)
|
|
|
|
|
2023-06-12 07:26:18 +02:00
|
|
|
if random_numbers == 400:
|
|
|
|
return create_response(500, 'Tests failed, try again')
|
2023-06-09 15:02:25 +02:00
|
|
|
if random_numbers is False:
|
2023-06-12 07:26:18 +02:00
|
|
|
return create_response(500, 'Unable to generate random numbers. Restart/Reset System')
|
2023-06-09 15:02:25 +02:00
|
|
|
if random_numbers:
|
2023-06-12 07:26:18 +02:00
|
|
|
data = {
|
|
|
|
'status': 200,
|
|
|
|
'description': 'Successful operation; HEX-encoded bit arrays (with leading zeros if required)',
|
|
|
|
'randomNumbers': random_numbers
|
|
|
|
}
|
|
|
|
return jsonify(data)
|
2023-06-08 21:24:01 +02:00
|
|
|
else:
|
2023-06-12 07:26:18 +02:00
|
|
|
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-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))
|