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
|
|
|
|
|
|
|
|
@app.route('/trng/randomNum/init', methods=['GET'])
|
|
|
|
def initialize_generator():
|
|
|
|
global initialized # Zugriff auf die globale Variable
|
2023-06-09 15:02:25 +02:00
|
|
|
|
|
|
|
#parameter 1 und 2 sind default values für Tests
|
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-09 15:02:25 +02:00
|
|
|
return jsonify({'message': 'successful operation; random number generator is ready and random numbers can be requested'}), 200
|
2023-06-08 21:24:01 +02:00
|
|
|
else:
|
|
|
|
return jsonify({'error': 'Unable to initialize the random number generator within a timeout of 60 seconds.'}), 555
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/trng/randomNum/getRandom', methods=['GET'])
|
|
|
|
def get_random_numbers():
|
|
|
|
global initialized # Zugriff auf die globale Variable
|
|
|
|
quantity = request.args.get('quantity')
|
|
|
|
bits = request.args.get('numBits')
|
|
|
|
|
|
|
|
if not quantity.isdigit() or not bits.isdigit():
|
|
|
|
return jsonify({'error': 'Invalid input. Quantity and bits must be numeric.'}), 400
|
|
|
|
|
|
|
|
if int(quantity) <= 0 or int(bits) <= 0:
|
|
|
|
return jsonify({'error': 'Invalid input. Quantity and bits must be positive integers.'}), 400
|
|
|
|
|
|
|
|
# Überprüfe den Initialisierungsstatus
|
|
|
|
if not initialized:
|
2023-06-09 15:02:25 +02:00
|
|
|
return jsonify({'error': 'system not ready; try init'}), 432
|
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)
|
|
|
|
|
|
|
|
if random_numbers is False:
|
2023-06-08 21:24:01 +02:00
|
|
|
return jsonify({'error': 'Unable to generate random numbers.'}), 500
|
2023-06-09 15:02:25 +02:00
|
|
|
if random_numbers:
|
|
|
|
data = {'randomNumbers': random_numbers} # Erstellen des Datenobjekts mit dem JSON-String
|
|
|
|
return jsonify(data), 200
|
2023-06-08 21:24:01 +02:00
|
|
|
else:
|
|
|
|
return jsonify({'error': 'Unable to generate random numbers.'}), 500
|
|
|
|
|
|
|
|
@app.route('/trng/randomNum/shutdown', methods=['GET'])
|
|
|
|
def shutdown_generator():
|
|
|
|
global initialized # Zugriff auf die globale Variable
|
|
|
|
|
|
|
|
initialized = False # Setze den Initialisierungsstatus zurück
|
|
|
|
|
|
|
|
# Beispielantwort
|
2023-06-09 15:02:25 +02:00
|
|
|
return jsonify({'message': "successful operation; random number generator has been set to 'standby mode'"}), 200
|
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))
|